slow-cheetah
slow-cheetah copied to clipboard
Support for publishing on .NET Core
While the build pipeline in .NET Core is similar to the one in the previous .NET framework, the publishing pipeline is very different. Currently SlowCheetah does not insert itself into .NET Core publishing process, causing transform files to not be properly copied to the publish location.
By the moment I am using dotnet-transform-xdt
I have temporary solved it by adding the following target to the project-file. Helped by @andresmoschini comment: https://gist.githubusercontent.com/HansKindberg/7ec0d4779f810675cc51dd5398669726/raw/cfea987bfc9807190ea9c2e4a36dbb40ce1b6be4/ASP.NET-Core-SlowCheetah-temporary-publish-fix.targets
@HansKindberg , thanks for posting. My xml transformations in asp.net core now works!
Are there any plans on supporting this feature?
Any update on this?
Hey @HansKindberg thanks for your Target property... Just a question, you are using it for Web.Config and AppSettings.json, right? Do you have any idea how I can do it for AppSettings.json and App.Config? Thanks
Hi @gbergamo
I suppose you want to transform when publishing a .NET Core console application. Is that correct?
In .Net 4 you could debug and build for an environment. I am a bit confused what is the proper way for .Net Core. The link that everyone is posting is about ASP, not console app
any other solution that doesn't involve 250 lines of gibberish code?
@d668
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Xdt.Tools" Version="2.0.0" />
</ItemGroup>
<!-- For web projects -->
<Target Name="ApplyXdtConfigTransform" BeforeTargets="_TransformWebConfig">
<PropertyGroup>
<_SourceWebConfig>$(MSBuildThisFileDirectory)web.config</_SourceWebConfig>
<_XdtTransform>$(MSBuildThisFileDirectory)web.$(Configuration).config</_XdtTransform>
<_TargetWebConfig>$(PublishDir)web.config</_TargetWebConfig>
</PropertyGroup>
<Exec Command="dotnet transform-xdt --xml "$(_SourceWebConfig)" --transform "$(_XdtTransform)" --output "$(_TargetWebConfig)"" Condition="Exists('$(_XdtTransform)')" />
</Target>
<!-- For console projects -->
<Target Name="ApplyXdtConfigTransform" BeforeTargets="_CopyAppConfigFile">
<PropertyGroup>
<_SourceWebConfig>@(AppConfigWithTargetPath)</_SourceWebConfig>
<_XdtTransform>$(MSBuildThisFileDirectory)App.$(Configuration).config</_XdtTransform>
<_TargetWebConfig>@(AppConfigWithTargetPath)</_TargetWebConfig>
</PropertyGroup>
<Exec Command="dotnet transform-xdt --xml "$(_SourceWebConfig)" --transform "$(_XdtTransform)" --output "$(_TargetWebConfig)"" Condition="Exists('$(_XdtTransform)')" />
</Target>
<!-- For other files -->
<Target Name="ApplyXdtConfigTransform2" AfterTargets="_CopyOutOfDateSourceItemsToOutputDirectory">
<PropertyGroup>
<_SourceWebConfig>$(OutDir)Nlog.config</_SourceWebConfig>
<_XdtTransform>$(MSBuildThisFileDirectory)Nlog.$(Configuration).config</_XdtTransform>
<_TargetWebConfig>$(OutDir)Nlog.config</_TargetWebConfig>
</PropertyGroup>
<Exec Command="dotnet transform-xdt --xml "$(_SourceWebConfig)" --transform "$(_XdtTransform)" --output "$(_TargetWebConfig)"" Condition="Exists('$(_XdtTransform)')" />
</Target>
@xumix in .NET 4 I could debug any environment config. Is it impossible now?
@d668 try and tell us. I'm not the author nor obligated to help you.
Here is a demo https://github.com/d668/NetCoreConfigTransform/ .NET Core 2.2 Console app App.config transforms using slow-cheetah
I got it working by adding a new Project Build Configuration (i.e. "MYHOST1") to the project I want to publish, then adding a new Solution Configuration (same name, "MYHOST1") which uses the new Project Build Configuration for that project, then on the Publish Profile specifying "MYHOST1" as the Configuration.
Not very clean, but seems to work. (.NET Core 2.1 console app; SlowCheetah 3.2.20)
What worked for me:
- Use latest Microsoft.VisualStudio.SlowCheetah 3.2.20 instead of old SlowCheetah 2.5.48
- Set
TransformOnBuild
for app.config:
<ItemGroup>
<None Include="app.config">
<TransformOnBuild>true</TransformOnBuild>
</None>
</ItemGroup>
- Import SlowCheetah at the end of csproj:
<Import Project="$(RestorePackagesPath)microsoft.visualstudio.slowcheetah\3.2.20\build\Microsoft.VisualStudio.SlowCheetah.targets"
Condition="Exists('$(RestorePackagesPath)microsoft.visualstudio.slowcheetah\3.2.20\build\Microsoft.VisualStudio.SlowCheetah.targets')" />
Could someone verify is this workaround is safe?
BTW if you can't upgrade SlowCheetah for some reason it is possible to make old SlowCheetah 2.5 work too. It requires editing its .targets file in build
folder in package:
<!-- insert this line -->
<__SC_IntermediateAppConfig>$(IntermediateOutputPath)$(MSBuildProjectFile)-sc.App.config</__SC_IntermediateAppConfig>
<!-- before this line -->
<_Sc_HasAppConfigTransform>false</_Sc_HasAppConfigTransform>
Another option is to use simple transform task which works only for App.config from this discussion. It was reviewed by MSBuild maintainer @rainersigwald so it should work good.
@tkirill Hey your hint importing SlowCheetah at the end of the csproj file works for me. I didn't need this fix for some of my projects, but one special .net core service publishing just the subpath got fixed by this:
File to transform: nlog.config
Building: TFS 2017 - running 4 cmd commands :
- dotnet restore
- dotnet build -c Release
- dotnet test -c Release -l trx
- dotnet publish .\src\ServiceName\ServiceName.csproj -c Release -o $(build.artifactstagingdirectory)
Package version:
<PackageReference Include="Microsoft.VisualStudio.SlowCheetah" Version="3.2.26">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Extended CSProj:
<Import Project="$(RestorePackagesPath)microsoft.visualstudio.slowcheetah\3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets"
Condition="Exists('$(RestorePackagesPath)microsoft.visualstudio.slowcheetah\3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets')" />
This referrs to: https://github.com/NLog/NLog/wiki/WebService-target---Workaround-for-url-variables