你可以从 powershell 运行 Microsoft.VisualStudio.SlowCheetah 吗?

Can you run Microsoft.VisualStudio.SlowCheetah from powershell?

我想 运行 一个 powershell 构建脚本,其中我有一些配置文件 (xml/json) 不是 app.config、appsettings.json 也不是 web.config 我想根据构建配置转换的文件。完美的工具似乎是 VisualStudio.SlowCheetah,因为它同时支持 xml 和 json,并且它使用与 web.config 转换相同的底层技术(也在我的项目中) .有什么方法可以从 powershell 中 运行 这个工具吗?如果能有一个在解决方案中进行转换的工具也可以对我的辅助文件进行转换,那就太好了?

所以这是我的概念证明:

我的文件夹包含 4 个文件:

  1. PerformTransform。ps1 - 将启动转换的构建脚本的替代
  2. Transform-Config.ps1 - 使用 SlowCheetah 执行转换的脚本
  3. Sample.config - 示例配置文件
  4. Sample.Prod.config - 示例 xml 转换文件

PerformTransform。ps1 看起来像:

cls
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

# Temporarily adds the script folder to the path
# so that the Transform-Config command is available
if(($env:Path -split ';') -notcontains $scriptPath) {
    $env:Path += ';' + $scriptPath
}

Transform-Config "$scriptPath\Sample.config" "$scriptPath\Sample.Prod.config" "$scriptPath\Sample.Transformed.config"

这是我的转换配置。ps1:

#!/usr/bin/env powershell
<#
.SYNOPSIS
    You can use this script to easly transform any XML file using XDT or JSON file using JDT.
    To use this script you can just save it locally and execute it. The script
    will download its dependencies automatically.
#>
[cmdletbinding()]
param(
    [Parameter(
        Mandatory=$true,
        Position=0)]
    $sourceFile,

    [Parameter(
        Mandatory=$true,
        Position=1)]
    $transformFile,

    [Parameter(
        Mandatory=$true,
        Position=2)]
    $destFile
)

$loggingStubSource = @"
    using System;

    namespace Microsoft.VisualStudio.SlowCheetah
    {
        public class LoggingStub : ITransformationLogger
        {
            public void LogError(string message, params object[] messageArgs) { }
            public void LogError(string file, int lineNumber, int linePosition, string message, params object[] messageArgs) { }
            public void LogErrorFromException(Exception ex) { }
            public void LogErrorFromException(Exception ex, string file, int lineNumber, int linePosition) { }
            public void LogMessage(LogMessageImportance importance, string message, params object[] messageArgs) { }
            public void LogWarning(string message, params object[] messageArgs) { }
            public void LogWarning(string file, int lineNumber, int linePosition, string message, params object[] messageArgs) { }
        }
    }
"@    # this here-string terminator needs to be at column zero

<#
.SYNOPSIS
    If nuget is not in the tools
    folder then it will be downloaded there.
#>
function Get-Nuget(){
    [cmdletbinding()]
    param(
        $toolsDir = "$env:LOCALAPPDATA\NuGet\BuildTools\",
        $nugetDownloadUrl = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
    )
    process{
        $nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
        
        if(!(Test-Path $nugetDestPath)){
            'Downloading nuget.exe' | Write-Verbose
            # download nuget
            $webclient = New-Object System.Net.WebClient
            $webclient.DownloadFile($nugetDownloadUrl, $nugetDestPath)

            # double check that is was written to disk
            if(!(Test-Path $nugetDestPath)){
                throw 'unable to download nuget'
            }
        }

        # return the path of the file
        $nugetDestPath
    }
}

function Get-Nuget-Package(){
    [cmdletbinding()]
    param(
        [Parameter(
         Mandatory=$true,
         Position=0)]
        $packageName,
        [Parameter(
         Mandatory=$true,
         Position=1)]
        $toolFileName,
        $toolsDir = "$env:LOCALAPPDATA\NuGet\BuildTools\",
        $nugetDownloadUrl = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
    )
    process{
        if(!(Test-Path $toolsDir)){ 
            New-Item -Path $toolsDir -ItemType Directory | Out-Null
        }

        $toolPath = (Get-ChildItem -Path $toolsDir -Include $toolFileName -Recurse) | Select-Object -First 1

        if($toolPath){
            return $toolPath
        }

        "Downloading package [$packageName] since it was not found in the tools folder [$toolsDir]" | Write-Verbose
        
        $cmdArgs = @('install',$packageName,'-OutputDirectory',(Resolve-Path $toolsDir).ToString())
        "Calling nuget.exe to download [$packageName] with the following args: [{0} {1}]" -f (Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl), ($cmdArgs -join ' ') | Write-Verbose
        &(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null

        $toolPath = (Get-ChildItem -Path $toolsDir -Include $toolFileName -Recurse) | Select-Object -First 1
        return $toolPath
    }
}


function Transform-Config{
    [cmdletbinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0)]
        $sourceFile,

        [Parameter(
            Mandatory=$true,
            Position=1)]
        $transformFile,

        [Parameter(
            Mandatory=$true,
            Position=2)]
        $destFile,

        $toolsDir = "$env:LOCALAPPDATA\NuGet\BuildTools\"
    )
    process{
        $sourcePath    = (Resolve-Path $sourceFile).ToString()
        $transformPath = (Resolve-Path $transformFile).ToString()

        $cheetahPath = Get-Nuget-Package -packageName 'Microsoft.VisualStudio.SlowCheetah' -toolFileName 'Microsoft.VisualStudio.SlowCheetah.dll' -toolsDir $toolsDir

        if(!$cheetahPath){
            throw ('Failed to download Slow Cheetah package')
        }

        if (-not ([System.Management.Automation.PSTypeName]'Microsoft.VisualStudio.SlowCheetah.LoggingStub').Type)
        {
            [Reflection.Assembly]::LoadFrom($cheetahPath.FullName) | Out-Null       
            Add-Type -TypeDefinition $loggingStubSource -Language CSharp -ReferencedAssemblies $cheetahPath.FullName
        }
        $logStub = New-Object Microsoft.VisualStudio.SlowCheetah.LoggingStub

        $transformer = [Microsoft.VisualStudio.SlowCheetah.TransformerFactory]::GetTransformer($sourcePath, $logStub);
        $success = $transformer.Transform($sourcePath, $transformPath, $destFile);
        if(!$success){
            throw ("Transform of file [] failed!!!!")
        }
        Write-Host "Transform successful."
    }
}

Transform-Config -sourceFile $sourceFile -transformFile $transformFile -destFile $destFile

配置文件并不重要,您应该可以使用现有的 app.config 和 app.ENV.config 转换文件来玩这个。

如果有更简单的方法,请告诉我!