Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlowCheetah failing quietly. #29

Open
default-account-name opened this issue Jul 27, 2021 · 0 comments
Open

SlowCheetah failing quietly. #29

default-account-name opened this issue Jul 27, 2021 · 0 comments

Comments

@default-account-name
Copy link

default-account-name commented Jul 27, 2021

Hello all. First, my issue could very well be solved if I could find the correct and complete documentation for slow cheetah usage, specifically in PowerShell.

I'm trying to add the ability to transform json config files via powershell for use in certain cases (but not ALL environments/situations, so adding this to source/project files is not an option).

I'm able to do this exact same thing using XDT for web.config files in other builds, so I know its possible with the correct usage.

So far, I'm able to get all the way to the point where the transform should occur, but then get an error that is completely silent other than 'false'.

I was able to find someones example on stack exchange (https://stackoverflow.com/questions/62944828/can-you-run-microsoft-visualstudio-slowcheetah-from-powershell), which has been modified/simplified to eliminate issues. I'm left with this:

function GetNuget(){
  process{
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    'Downloading nuget.exe' | Write-Verbose
    $webclient = New-Object System.Net.WebClient
    $webclient.DownloadFile('https://dist.nuget.org/win-x86-commandline/latest/nuget.exe', "$env:LOCALAPPDATA\NuGet\BuildTools\nuget.exe")

    return "$env:LOCALAPPDATA\NuGet\BuildTools\nuget.exe"
  }
}

function GetNugetPackage(){

  if(!(Test-Path "$env:LOCALAPPDATA\NuGet\BuildTools\")){ 
      New-Item -Path "$env:LOCALAPPDATA\NuGet\BuildTools\" -ItemType Directory | Out-Null
  }
  
  $cmdArgs = @(
    'install',
    'Microsoft.VisualStudio.SlowCheetah',
    '-OutputDirectory',
    (Resolve-Path "$env:LOCALAPPDATA\NuGet\BuildTools\").ToString()
  )

  &(GetNuget `
      -toolsDir "$env:LOCALAPPDATA\NuGet\BuildTools\" `
      -nugetDownloadUrl 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe') `
      $cmdArgs | Out-Null

  $toolPath = (
    Get-ChildItem `
    -Path "$env:LOCALAPPDATA\NuGet\BuildTools\" `
    -Include 'Microsoft.VisualStudio.SlowCheetah.dll' `
    -Recurse
    ) | Select-Object -First 1

  return $toolPath
}


function TransformConfig{
    [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 = GetNugetPackage `
                        -packageName 'Microsoft.VisualStudio.SlowCheetah' `
                        -toolFileName 'Microsoft.VisualStudio.SlowCheetah.dll' `
                        -toolsDir "$env:LOCALAPPDATA\NuGet\BuildTools\"

        [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);
        $transformer.Transform($sourcePath, $transformPath, $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) { }
        }
    }
"@

TransformConfig -sourceFile $sourceFile -transformFile $transformFile -destFile $destFile

I would appreciate, above all else, links to docs explaining proper usage. Understanding and being able to implement this more properly is ideal, but any help is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant