slow-cheetah icon indicating copy to clipboard operation
slow-cheetah copied to clipboard

Support for publishing on .NET Core

Open davilimap opened this issue 7 years ago • 16 comments

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.

davilimap avatar Apr 25 '17 17:04 davilimap

By the moment I am using dotnet-transform-xdt

andresmoschini avatar Aug 09 '17 16:08 andresmoschini

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 avatar Sep 10 '17 18:09 HansKindberg

@HansKindberg , thanks for posting. My xml transformations in asp.net core now works!

PedroNunoSantos avatar Oct 18 '17 13:10 PedroNunoSantos

Are there any plans on supporting this feature?

xumix avatar Dec 11 '18 09:12 xumix

Any update on this?

wouterroos avatar Mar 06 '19 14:03 wouterroos

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

gbergamo avatar Jul 16 '19 14:07 gbergamo

Hi @gbergamo

I suppose you want to transform when publishing a .NET Core console application. Is that correct?

HansKindberg avatar Jul 29 '19 08:07 HansKindberg

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

d668 avatar Aug 02 '19 06:08 d668

any other solution that doesn't involve 250 lines of gibberish code?

d668 avatar Aug 02 '19 10:08 d668

@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 &quot;$(_SourceWebConfig)&quot; --transform &quot;$(_XdtTransform)&quot; --output &quot;$(_TargetWebConfig)&quot;" 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 &quot;$(_SourceWebConfig)&quot; --transform &quot;$(_XdtTransform)&quot; --output &quot;$(_TargetWebConfig)&quot;" 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 &quot;$(_SourceWebConfig)&quot; --transform &quot;$(_XdtTransform)&quot; --output &quot;$(_TargetWebConfig)&quot;" Condition="Exists('$(_XdtTransform)')" />
  </Target>

xumix avatar Aug 02 '19 10:08 xumix

@xumix in .NET 4 I could debug any environment config. Is it impossible now?

d668 avatar Aug 02 '19 10:08 d668

@d668 try and tell us. I'm not the author nor obligated to help you.

xumix avatar Aug 02 '19 10:08 xumix

Here is a demo https://github.com/d668/NetCoreConfigTransform/ .NET Core 2.2 Console app App.config transforms using slow-cheetah

d668 avatar Aug 02 '19 12:08 d668

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)

codystott avatar Aug 14 '19 18:08 codystott

What worked for me:

<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 avatar Aug 27 '19 15:08 tkirill

@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

Bjego avatar Sep 26 '19 06:09 Bjego