Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active September 11, 2015 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/4d905512734374864112 to your computer and use it in GitHub Desktop.
Save guitarrapc/4d905512734374864112 to your computer and use it in GitHub Desktop.
<!-- ****************************************** -->
<!-- Begin App.config Transform Settings -->
<!-- VSなど、BuildTargetを渡さずビルドした時専用 -->
<!-- ****************************************** -->
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config') And '$(BuildTarget)'==''">
<!--Generate transformed app config in the intermediate directory-->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!--Force build process to use the transformed configuration file from now on.-->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.-->
<Target Name="AfterPublish">
<PropertyGroup>
<DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
</PropertyGroup>
<!--Publish copies the untransformed App.config to deployment directory so overwrite it-->
<Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
</Target>
<!-- ****************************************** -->
<!-- Begin App.config Custom Transform Settings -->
<!-- MSBuild で /p:BuildTarget=Xxx を渡した時専用 -->
<!-- ****************************************** -->
<Target Name="AfterCompile" Condition="Exists('app.$(BuildTarget).config') And '$(BuildTarget)'!=''">
<!--Generate transformed app config in the intermediate directory-->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(BuildTarget).config" />
<!--Force build process to use the transformed configuration file from now on.-->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<!-- ここで↑で生成した $(IntermediateOutputPath) (つまりobj/$(Configuration)) にあるコピー元となる -sc.App.Config を差し替え-->
<!-- -sc.App.Config が元となって、bin/$(Configuration) の exe.config が差し変わる -->
<!-- -sc.App.Config が .exe.config より新しいかも判定して MSBuild はコピーしてるので、両方差し替えるの雑だけど安定 -->
<Target Name="AfterBuild" Condition="Exists('app.$(BuildTarget).config') And '$(BuildTarget)'!=''">
<Delete Files="$(IntermediateOutputPath)$(ProjectFileName)-sc.App.config" />
<Copy SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config"
DestinationFiles="$(OutDir)$(TargetFileName).config" />
<Copy SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config"
DestinationFiles="$(IntermediateOutputPath)$(ProjectFileName)-sc.App.config" />
</Target>
<!-- ****************************************** -->
<!-- End App.config Custom Transform Settings -->
<!-- ****************************************** -->
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild" ConsoleApplication2\ConsoleApplication2.csproj /p:Configuration=Release,BuildTarget=Hoge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment