0

at the moment my project has a license file. No extension attached to the file. The file itself only contains a key.

I have 3 build configuration

Dev
Stage
Prod

At the moment I have 4 license file.

GLicense 

GLicense_dev
GLicense_stage
GLicense_prod

I tried to use #c preprocessor directive but the third party dll requires me to have the license name exactly as GLicense. The next approach I was thinking of taking was overriding the content of GLicense when building. I was wondering how abouts do I do that?

T.S.
  • 18,195
  • 11
  • 58
  • 78
Master
  • 2,038
  • 2
  • 27
  • 77
  • Quick answer: You should be able to solve this via a pre-build task that copies the respective file for each configuration to the desired GLincense file name in your build dir. – Filburt Mar 31 '20 at 18:50
  • Thanks! That makes alot of sense. @Filburt – Master Mar 31 '20 at 19:19

2 Answers2

2

I believe the true answer is to use $(TargetDir) vs $(ProjectDir)$(OutDir) in "copy To". You don't want to have a mention of $(ProjectDir) on right side. What if your output is outside of your project folder, may be even different computer. Plus, instead of changing project file in project XML, you can just open properties and in "build events" add this single event

if $(ConfigurationName) == dev copy /y "$(ProjectDir)GLicense_dev" "$(TargetDir)GLicense"
if $(ConfigurationName) == stage copy /y "$(ProjectDir)GLicense_stage" "$(TargetDir)GLicense"
if $(ConfigurationName) == prod copy /y "$(ProjectDir)GLicense_prod" "$(TargetDir)GLicense"

Another way (and this is true Visual Studio solution because it eliminates CMD/Batch coding. The downside in your case, you have 3 different files with the same name in 3 different folders), add your 3 files to a project as content in 3 different folders but file name - same (GLicense). And pick "Copy Always" in build action. Then in project file XML add Condition=" '$(Configuration)' == 'dev' on the file itself rather than property group

<None Include="dev\GLicense" Condition=" '$(Configuration)' == 'dev'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="stage\GLicense" Condition=" '$(Configuration)' == 'stage'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="prod\GLicense" Condition=" '$(Configuration)' == 'prod'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • @Master you have to check my answer above – T.S. Apr 01 '20 at 03:51
  • I included `$(ProjectDir)` because from the problem description, I figured the OP needed to put the `GLicense` into the project directory to be picked up by build rather than copy to `$(TargetDir)`. In any case your answer adds more valuable information. – Filburt Apr 01 '20 at 06:40
  • 1
    @Filburt Thanks. You're right, It is not clear if the license needed during the build or post build. I would be surprised if it is needed during the build. Usually licenses are loaded at runtime. Well, these 2 answers will cover all the scenarios – T.S. Apr 01 '20 at 14:10
1

Modify your Projects pre-build command line to include the desired source file for each configuration

copy /y "$(ProjectDir)GLicense_dev" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_stage" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_prod" "$(ProjectDir)$(OutDir)GLicense"

Your Project file will look something like this

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'dev|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_dev.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'stage|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_stage.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_prod.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
T.S.
  • 18,195
  • 11
  • 58
  • 78
Filburt
  • 17,626
  • 12
  • 64
  • 115