Coder Social home page Coder Social logo

nil4 / dotnet-transform-xdt Goto Github PK

View Code? Open in Web Editor NEW
118.0 11.0 12.0 193 KB

Modern .NET tools and library for XDT (Xml Document Transformation)

License: Apache License 2.0

C# 99.06% PowerShell 0.94%
dotnet-cli transformation xdt xml cli global-tool netstandard20

dotnet-transform-xdt's People

Contributors

ivanfranco502 avatar jstallm avatar karelkral avatar nil4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dotnet-transform-xdt's Issues

Publish as global tool

In #18 it was suggested to make dotnet-transform-xdt available as a global tool.

This issue tracks making that happen once .NET Core 2.1 is released and the global tool requirements stabilize and are documented (interim docs available here and here).

Duplicate <rule> in the <rewrite><rules> section.

I've been using this tool for a while now with no issues. But out of the blue it has started adding duplicate <rule> nodes to the <rewrite><rules> section. I haven't even updated the xdt package version.

My web.config in the project looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44350" />
      </environmentVariables>
    </aspNetCore>
    <rewrite>
      <rules></rules>
    </rewrite>
  </system.webServer>
</configuration>

And my web.Staging.config in the project looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
	<system.webServer>

		<aspNetCore>
			<environmentVariables>
				<environmentVariable name="ASPNETCORE_ENVIRONMENT"
									 value="Staging"
									 xdt:Transform="SetAttributes"
									 xdt:Locator="Match(name)"/>
			</environmentVariables>
		</aspNetCore>

		<rewrite>
			<rules>
				<rule name="HTTP to HTTPS redirect -- Staging"
					  stopProcessing="true"
					  xdt:Transform="Insert">
					<match url="(.*)" />
					<conditions>
						<add input="{HTTPS}"
							 pattern="off"
							 ignoreCase="true" />
					</conditions>
					<action type="Redirect"
							redirectType="Permanent"
							url="https://{HTTP_HOST}/{R:1}" />
				</rule>
			</rules>
		</rewrite>

	</system.webServer>
</configuration>

And the deployed web.config that get published to our staging server via AzDevOps pipeline looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\MyCompany.MyProject.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
        <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44350" />
      </environmentVariables>
    </aspNetCore>
    <rewrite>
      <rules>

        <rule name="HTTP to HTTPS redirect -- Staging" stopProcessing="true">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}"/>
        </rule>

        <rule name="HTTP to HTTPS redirect -- Staging" stopProcessing="true">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}"/>
        </rule>

      </rules>
    </rewrite>
  </system.webServer>
</configuration>
<!--ProjectGuid: 546334BB-0661-42B6-844E-31DC3677C6C1-->

The MyCompany.MyProject.csproj file has this:

	<ItemGroup>
		<DotNetCliToolReference Include="Microsoft.DotNet.Xdt.Tools" Version="2.0.0" />
	</ItemGroup>

	<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>

Is there anything that stands out about this setup?

Does not accept specified project file

My solution file (.sln) and project file (.csproj) is in the same folder location. If I simply run dotnet transform-xdt I get:

Specify which project file to use because this 'D:\Projects\Foo' contains more than one project file.

Expected behavior
To specify the project file using normal syntax dotnet transform-xdt .\Foo.csproj which would result in normal operation.

Observed behavior
It still displays the same error message regardless of specifying the project or solution file.

Additional
This same problem persists when attempting to run the transform as part of the publish step. e.g.

 dotnet publish .\Foo.csproj -c production -o D:\webpublish\foo\
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Foo -> D:\Projects\Foo\bin\production\netcoreapp1.1\Web.dll
  Foo -> D:\webpublish\foo\
  Specify which project file to use because this 'D:\Projects\Foo' contains more than one project file.
D:\Projects\Foo\Foo.csproj(80,5): error MSB3073: The command "dotnet transform-xdt --xml "D:\Projects\Foo\Web.config" --transform "D:\Projects\Foo\Web.production.config" --output "D:\webpublish\Foo\Web.config"" exited with code 1.

Option --quiet and -q are not recognized

v2.2.0

According to the help there is a quiet option that will only print error messages. However, it is not recognized as an option.

Options:
--help|-h|-? Print usage information
--quiet|-q Print only error messages
--verbose|-v Print verbose messages while transforming

dotnet-xdt.exe --source "source.config" --transform "transform.config" --output "output.config" --quiet
[dotnet-xdt] Invalid argument: '--quiet'

dotnet-xdt.exe -s source.config -t transform.config -o output.config -q
[dotnet-xdt] Invalid argument: '-q'

transform-xdt hangs intermittently

Hi,

I'm using Microsoft.DotNet.Xdt.Tools version 2.0.0 .Net Core 2.0 and added the following command to run after publish but it hangs intermittently.

dotnet transform-xdt -x web.config -t web.$(ConfigurationName).config -o $(PublishDir)web.config

I test the command as below when I encounter publish is hanging and it hangs the same way.

dotnet transform-xdt -x web.config -t web.Staging.config -o web.test.config

I wasn't able to see it returning an error message to help investigating the issue further, don't know if the tool stores logs either. Problem disappears as it appeared after a period of time that I'm not sure if it's a fixed amount.

Can I prevent the hangs or find out the reason?

Version for package `Microsoft.Dotnet.Xdt.Tools` could not be resolved.

Hi

Since adding the dotnet 2.2 SDK, I am unable to run the dotnet transform-xdt commands. All that I am getting is
Version for package Microsoft.Dotnet.Xdt.Tools could not be resolved.

I do have the following:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.Dotnet.Xdt.Tools" Version="2.0.0" /> </ItemGroup>
And have the following as a build requirement:

<PropertyGroup>
<_SourceConfig>$(MSBuildThisFileDirectory)nlogbase.config</_SourceConfig>
<_XdtTransform>$(MSBuildThisFileDirectory)nlogbase.$(Configuration).config</_XdtTransform>
<_TargetConfig>$(TargetDir)nlog.config</_TargetConfig>
</PropertyGroup>
<Exec Command="dotnet transform-xdt --verbose --xml &quot;$(_SourceConfig)&quot; --transform &quot;$(_XdtTransform)&quot; --output &quot;$(_TargetConfig)&quot;" />
</Target>

It works 100% when I uninstall 2.2 SDK

Apart from the web.*.config that has to be transformed, we create nlog.config files per environment using the transformation tool.

Any corrective measures that can be done?

Task remove new lines which break config files

Let's say I have a App.config with this section:

      <setting name="InstanceName" serializeAs="String">
        <value>[[InstanceName]]</value>
      </setting>

And a App.Debug.Config with this section:

      <setting name="InstanceName" serializeAs="String" xdt:Transform="Replace" xdt:Locator="Match(name)">
        <value>Debug</value>
      </setting>

After dotnet-transform-xdt does it work, the resulting file will become:

      <setting name="InstanceName" serializeAs="String"><value>Debug</value></setting>

At first glance this shouldn't be a problem because it's still a valid XML file, but somehow on execution the .NET System.Configuration class don't like this, and throw a "System.Configuration.ConfigurationErrorsException" exception:

System.Configuration.ConfigurationErrorsException: Unrecognized element 'setting'. (Project\bin\Debug\net5.0\ProjectName.exe.config line 28)

A quick Google search lead me to this and this which are VERY old posts, but it still seems to be the case. I tried with project targeting net472 AND net5.0 and it throw the same error.

Create output folder when doesn't exist

Current behaviour

When a user specifies a non-existent output folder, the transformation process failed with the following error:

> dotnet xdt --source "locationOriginalFile" --transform "locationDeltaFile" --output "inexistentLocationOutputFile"
[dotnet-xdt] Transforming 'locationOriginalFile' using 'locationDeltaFile' into 'inexistentLocationOutputFile'
[dotnet-xdt] Unexpected error: System.IO.DirectoryNotFoundException: Could not find a part of the path 'inexistentLocationOutputFile'.
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at DotNet.Xdt.Program.Main(String[] args) in d:\a\1\s\dotnet-xdt\Program.cs:line 62

Expected behaviour

The process should create the non-existent folder, so the transformation would succeed.

Outdated Microsoft.DotNet.Xdt.Tools project level tool in nuget

Thanks for all the work on this project!

I was attempting to use the project level Microsoft.DotNet.Xdt.Tools tool in my project and noticed it still seems to have the issue whitespace preservation issues described in #4.

The two other nuget packages appear to have been published more recently. Is the recommendation to use the global tool over the project level tool?

Add info about publishing console apps

Thank for your project!

This config works for me:

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Xdt.Tools" Version="2.0.0" />
  </ItemGroup>
  <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>

Would you like to fix a bug about removing xmlns?

1.the target file

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings>
    <add key="test20180523" value="1" />
  </appSettings>
</configuration>

2.the transform file

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" xdt:Transform="RemoveAttributes(xmlns)" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" />

when I try to apply the transform to the target,nothing happened.

Does not work with latest msbuild tooling

I'm running dotnet version 1.0.0-rc4-004771

Using

  <Target Name="ApplyXdtConfigTransform" BeforeTargets="_WebConfigTransform">
    <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;" />
  </Target>
  

Transformations do not run on publish.

Trying to sub BeforeTargets="_WebConfigTransform" for AfterTargets="Build" and running dotnet build does not seem to actually do anything. I've tried doing the recommended process also on the sample projects and they do not work either.

Doesn't work for netcoreapp1.1 ?

I could be doing something wrong I admit, but after following the instructions and updating my csproj file, I get the following error in the output on publish:

Version for package Microsoft.DotNet.Xdt.Tools could not be resolved.

I'm targeting the netcoreapp1.1 framework and using Microsoft.AspNetCore version 1.1.1

Nice but

I suggest creating an installer (with framework 462 for an exe) instead of a nuget package.

Two reasons:

  1. Install once, and allow all applications to use it is easier
  2. if the application is aspnet core with framework, it is not compatible

Simplifications for samples

It seems to work ok without &quot; paths and with a _TargetWebConfig aimed at the $(PublishDir), which seems like it would simplify samples a bit (unless it's gonna ๐Ÿ’ฅ otherwise).

<Target Name="RemoveHandlersFromWebConfig" AfterTargets="_WebConfigTransform">
  <PropertyGroup>
    <_SourceWebConfig>$(PublishDir)web.config</_SourceWebConfig>
    <_XdtTransform>$(MSBuildThisFileDirectory)web.RemoveHandlers.config</_XdtTransform>
    <_TargetWebConfig>$(PublishDir)web.config</_TargetWebConfig>
  </PropertyGroup>
  <Exec Command="dotnet transform-xdt --xml $(_SourceWebConfig) --transform $(_XdtTransform) --output $(_TargetWebConfig)" Condition="Exists('$(_XdtTransform)')" />
</Target>

... you may want the capitalized "W" tho in web.config. I keep mine lowercase, but I am aware of the tradition of using Web.config in projects out there. [Kind'a strange sometimes which conventions the .NET Core team kept and which ones they changed, since IIS.net is full of upper-case "W" Web.config mentions.]

[EDIT] Is the &quot; path thing to cover spaces in paths?

Error

I get

Severity	Code	Description	Project	File	Line	Suppression State
Error		The command "dotnet transform-xdt --xml "C:\Users\Kevin\Projects\PocketNurse\PocketNurse\Web.config" --transform "C:\Users\Kevin\Projects\PocketNurse\PocketNurse\Web.Omnicell.config" --output "C:\Users\Kevin\Projects\PocketNurse\PocketNurse\obj\Omnicell\netcoreapp2.0\PubTmp\Out\Web.config"" exited with code 1.	PocketNurse	C:\Users\Kevin\Projects\PocketNurse\PocketNurse\PocketNurse.csproj	34

from this project

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <UserSecretsId>aspnet-PocketNurse-36604171-3424-4347-80E3-30D24138EC43</UserSecretsId>
    <Configurations>Debug;Release;Omnicell;Docker</Configurations>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="EPPlus" Version="4.5.1" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" PrivateAssets="All" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Xdt.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.1" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Models\UploadViewModel\" />
  </ItemGroup>
  
  <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>

</Project>

Adding --verbose I get something like

[XDT] Failed: 'xdt' is an undeclared prefix. Line 18, position 29.

Cannot Run with multiple *proj in same directory

Hi,
I have tested this in a new solution and it works fine. Thanks and great job. However when i tried to incorporate this in my actual solution i am getting error "Specify which project file to use because this c:\pathtofolder\project contains more than one project file."

I looked in the source folder and i have
project.csproj
project.csproj.user
project.csproj.vspscc

This appears to be a known issue with CLI Tools
Is there a way to get around this without deleting other files ?

Package type 'DotnetCliTool' is not supported by project

I have a ASP.NET Core 1.1 MVC project targeting net452 and I'm trying to use Microsoft.DotNet.Xdt.Tools with it. I've tried to install it using Nuget but I'm getting this error. Is it not usable in this .NET Core version?

Package 'Microsoft.DotNet.Xdt.Tools 1.2.0' has a package type 'DotnetCliTool' that is not supported by project 'MyProject'.

csproj:

<PropertyGroup>
    <TargetFramework>net452</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

Run with only .NET Core 3.x installed

Now that .NET Core 3.1 is out it would be nice if the tool could be run without requiring .NET Core 2.x to be installed.

With 3.0 a new project property RollForward was introduced, and setting it to Major allows running to tool on a newer major version than what it was built for. See here for more details.

Run this *after* publish?

Unfortunately this can only run with the SDK installed. I want to run this standalone on a server with just the runtime available.

Would you accept a PR to

  1. Factor out all functionality short of command line args into a new netstandard project
    This would allow a scenario where a deployed app using this can simply accept a switch to do a transform, no additional tools needed to carry around.
  2. Add another project which is a plain old command line app and which can be downloaded e.g. from the GitHub Releases page and ran directly.

Also add some TargetFrameworks and pack the stuff for .NET Core 2.0 and 2.1 in the same package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.