Skip to content

Instantly share code, notes, and snippets.

@efim-a-efim
Last active April 26, 2016 10:49
Show Gist options
  • Save efim-a-efim/817a4f67b54abea1df21410611286ce5 to your computer and use it in GitHub Desktop.
Save efim-a-efim/817a4f67b54abea1df21410611286ce5 to your computer and use it in GitHub Desktop.
MSBuild transforms
<Target Name="BuildJS" BeforeTargets="TransformAllFiles" >
<Exec Command="npm install" WorkingDirectory="$(MSBuildProjectDirectory)\Content" />
<Exec Command=".\node_modules\.bin\mimosa build --errorout -P dist-config" WorkingDirectory="$(MSBuildProjectDirectory)\Content" />
<ItemGroup>
<_JSContent Include="Content\public\**\*.*" />
</ItemGroup>
<ItemGroup>
<_JSContent Condition="$([System.String]::Copy('%(Filename)').EndsWith('.$(Configuration)'))">
<DependentUpon>%(RelativeDir)$([System.String]::Copy('%(Filename)').Replace('.$(Configuration)',''))%(Extension)</DependentUpon>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</_JSContent>
</ItemGroup>
<Message Importance="High" Text="%(Identity) @(Content->'%(DependentUpon)') %(RelativeDir)$([System.String]::Copy('%(Filename)').Replace('.$(Configuration)',''))%(Extension)" />
<ItemGroup>
<Content Include="@(_JSContent)" />
</ItemGroup>
</Target>
<Target Name="CleanJS">
<Exec Command=".\node_modules\.bin\mimosa clean --force" WorkingDirectory="$(MSBuildProjectDirectory)\Content" />
</Target>
<PropertyGroup>
<BuildDependsOn>
BuildJS;
$(BuildDependsOn);
</BuildDependsOn>
<CleanDependsOn>
$(CleanDependsOn);
CleanJS
</CleanDependsOn>
</PropertyGroup>
<Content Include="Web.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="Web.CI.config">
<DependentUpon>Web.config</DependentUpon>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<SubType>Designer</SubType>
</None>
<None Include="Web.Production.config">
<DependentUpon>Web.config</DependentUpon>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<SubType>Designer</SubType>
</None>
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<ItemDefinitionGroup>
<_FilesToTransform>
<IsAppConfig>false</IsAppConfig>
</_FilesToTransform>
</ItemDefinitionGroup>
<Target Name="CopyTransformedConfigs" DependsOnTargets="DiscoverFilesToTransform;TransformAllFiles;" AfterTargets="TransformAllFiles;PipelinePreDeployCopyAllFilesToOneFolder" BeforeTargets="OctoPack">
<Copy
SourceFiles="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')"
DestinationFiles="@(_FilesToTransformNotAppConfig->'$(PackageTempRootDir)\PackageTmp\%(RelativeDir)%(Filename)%(Extension)')"
/>
<Copy
SourceFiles="@(_FilesToReplace->'$(OutDir)%(DependentUpon)')"
DestinationFiles="@(_FilesToReplace->'$(PackageTempRootDir)\PackageTmp\%(DependentUpon)')"
/>
</Target>
<Target Name="TransformAllFiles" DependsOnTargets="DiscoverFilesToTransform;" AfterTargets="Build;_CopyAppConfigFile" BeforeTargets="OctoPack">
<!-- Now we have the item list _FilesToTransformNotAppConfig and _AppConfigToTransform item lists -->
<!-- Transform the app.config file -->
<ItemGroup>
<_AppConfigTarget Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
</ItemGroup>
<PropertyGroup>
<_AppConfigDest>@(_AppConfigTarget->'%(FullPath)')</_AppConfigDest>
</PropertyGroup>
<MakeDir Directories="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)')"
Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')"/>
<MakeDir Directories="@(_FilesToReplace->'$(OutDir)%(RelativeDir)')"
Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')"/>
<TransformXml Source="@(_AppConfigToTransform->'%(FullPath)')"
Transform="%(_Transform)"
Destination="$(_AppConfigDest)"
Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " />
<TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')"
Transform="%(_Transform)"
Destination="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')"
Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " />
<Copy
SourceFiles="@(_FilesToReplace)"
DestinationFiles="@(_FilesToReplace->'$(OutDir)%(DependentUpon)')" />
</Target>
<Target Name="DiscoverFilesToTransform">
<!--
This will look through items list: None & Content for those
with Metadata <TransformOnBuild>True</TransformOnBuild>
-->
<ItemGroup>
<_Transforms Include="@(None);@(Content);@(Resource);@(EmbeddedResource)"
Condition="$([System.String]::Copy('%(Filename)%(Extension)').EndsWith('.$(Configuration)%(Extension)')) AND ( '%(Extension)'=='.xml' OR '%(Extension)'=='.config' )"/>
<_FilesToTransform Include="@(_Transforms->'%(DependentUpon)')">
<_Transform>%(_Transforms.Identity)</_Transform>
</_FilesToTransform>
<_FilesToReplace Exclude="@(_Transforms)" Include="@(None);@(Content);@(Resource);@(EmbeddedResource)" Condition="$([System.String]::Copy('%(Filename)%(Extension)').EndsWith('.$(Configuration)%(Extension)'))" />
</ItemGroup>
<PropertyGroup>
<_AppConfigFullPath>@(AppConfigWithTargetPath->'%(RootDir)%(Directory)%(Filename)%(Extension)')</_AppConfigFullPath>
</PropertyGroup>
<!-- Now look to see if any of these are the app.config file -->
<ItemGroup>
<_FilesToTransform Condition=" '%(FullPath)'=='$(_AppConfigFullPath)'">
<IsAppConfig>true</IsAppConfig>
</_FilesToTransform>
</ItemGroup>
<ItemGroup>
<_FilesToTransformNotAppConfig Include="@(_FilesToTransform)"
Condition=" '%(IsAppConfig)'!='true'"/>
<_AppConfigToTransform Include="@(_FilesToTransform)"
Condition=" '%(IsAppConfig)'=='true'"/>
</ItemGroup>
<Message Text="FilesToTransform: %(_FilesToTransform.Identity)" Importance="High"/>
<Message Text="FilesToReplace: %(_FilesToReplace.Identity) %(_FilesTmp.DependentUpon)" Importance="High"/>
</Target>
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
TransformAllFiles
</BuildDependsOn>
</PropertyGroup>
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="TransformAllFiles" BeforeTargets="OctoPack;">
<ItemGroup>
<_Transforms Include="@(None);@(Content);@(Resource);@(EmbeddedResource)" Condition="$([System.String]::Copy('%(Filename)%(Extension)').EndsWith('.$(Configuration)%(Extension)')) AND ( '%(Extension)'=='.xml' OR '%(Extension)'=='.config' )" />
<_FilesToTransform Include="@(_Transforms->'%(DependentUpon)')">
<_Transform>%(_Transforms.Identity)</_Transform>
</_FilesToTransform>
<_FilesToReplace Exclude="@(_Transforms)" Include="@(None);@(Content);@(Resource);@(EmbeddedResource)" Condition="$([System.String]::Copy('%(Filename)%(Extension)').EndsWith('.$(Configuration)%(Extension)'))" />
</ItemGroup>
<Message Text="FilesToTransform: %(_FilesToTransform.Identity)" Importance="High" />
<Message Text="FilesToReplace: %(_FilesToReplace.Identity) %(_FilesTmp.DependentUpon)" Importance="High" />
<Copy SourceFiles="@(_FilesToTransform)" DestinationFiles="@(_FilesToTransform->'%(RelativeDir)%(Filename).Base%(Extension)')" Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')" />
<TransformXml Source="@(_FilesToTransform->'%(RelativeDir)%(Filename).Base%(Extension)')" Transform="%(_Transform)" Destination="@(_FilesToTransform)" Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " />
<Copy SourceFiles="@(_FilesToReplace)" DestinationFiles="@(_FilesToReplace->'%(DependentUpon)')" />
</Target>
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
TransformAllFiles
</BuildDependsOn>
</PropertyGroup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment