From 6c6591517fcb235b683d04984cb79195db4f273e Mon Sep 17 00:00:00 2001 From: Shawn Sesna Date: Wed, 7 Feb 2024 11:55:02 -0800 Subject: [PATCH] Fixing issue where extra lines were being added at the end of the file. (#1482) * Fixing issue where extra lines were being added at the end of the file. * Forgot to update metadata --- step-templates/variables-substitute-in-files.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/step-templates/variables-substitute-in-files.json b/step-templates/variables-substitute-in-files.json index 170daa546..ada3ac09e 100644 --- a/step-templates/variables-substitute-in-files.json +++ b/step-templates/variables-substitute-in-files.json @@ -3,11 +3,11 @@ "Name": "Variables - Substitute in Files", "Description": "This step template attempts to replace Octopus variable placeholders in one or more file(s) with their values from the `$OctopusParameters` Dictionary.\n\n---\n\n### Version 18+:\n\nVersion 18 and higher pins the version of Octostache and Sprache (an Octostache dependency) to the following versions:\n- Octostache: [version 3.2.1](https://www.nuget.org/packages/Octostache/3.2.1)\n- Sprache: [version 2.3.1](https://www.nuget.org/packages/Sprache/2.3.1)\n\nThis is to resolve issues when running the step template on Windows, where the .NET 4.0 (`net40`) version is no longer present in newer versions of these libraries.\n\n**As a result, no new functionality available in Octostache, such as new commands/functions, will be present**\n\nIf you need later functionality, use the built-in Octopus [variable substitution feature](https://octopus.com/docs/projects/variables/variable-substitutions).\n\n### Breaking Change - version 15+:\n\nPrevious versions of this step template would attempt to find the Octostache library bundled with [Calamari](https://github.com/OctopusDeploy/Calamari) as part of a deployment or runbook. Due to compatibility issues running the `netstandard` version of Octostache bundled with newer versions of Calamari on Windows PowerShell, **this functionality has now been removed**.\n\nAs a result, **version 15** and higher of this step template now look for Octostache on the deployment target or worker in the NuGet cache. If it's not found, it will attempt to download two required binaries:\n\n1. Octostache from [nuget.org](https://www.nuget.org/packages/Octostache)\n1. Sprache (an Octostache dependency) from [nuget.org](https://www.nuget.org/packages/Sprache)\n\nIf access to [nuget.org](https://www.nuget.org) is restricted on your deployment target or worker, this step template may fail to execute successfully.\n\nIn that case, it's recommended to use the built-in Octopus [variable substitution feature](https://octopus.com/docs/projects/variables/variable-substitutions).\n\n---\n\n### Notes:\n\n- Tested on Windows PowerShell only running Octopus **2023.2.4954**.\n", "ActionType": "Octopus.Script", - "Version": 18, + "Version": 19, "CommunityActionTemplateId": null, "Packages": [], "Properties": { - "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\";\nfunction Get-Exceptions {\n param ($ExceptionObject)\n \n if ($null -ne $ExceptionObject.InnerException) {\n Get-Exceptions -ExceptionObject $ExceptionObject.InnerException\n }\n \n Write-Warning \"Exception is: $($ExceptionObject.Message)\"\n}\n\nfunction Resolve-OctopusVariablesInTemplate {\n <#\n.SYNOPSIS\n\tResolves Octopus variables in files with their values from a OctopusParameters\n\n.DESCRIPTION\n\tLooks for files using Get-ChildItem and in each of the files replaces ${Variable} with the value from $OctopusParameters.\n\tFiles are written back using UTF-8.\n\tRequires PowerShell 3.0 or higher.\n\n.PARAMETER Path\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Filter\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Include\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Exclude\n\tPassed to Get-ChildItem to find the files you want to process\n\t\n.PARAMETER Recurse\n\tPassed to Get-ChildItem to find the files you want to process\n\n#>\n Param(\n [string]$Path,\n [string]$Filter = \"*.config\",\n [string[]]$Include,\n [string[]]$Exclude,\n [switch]$Recurse,\n [string]$OctostacheLocation\n )\n\t\n if (-not $OctopusParameters) { throw \"No OctopusParameters found\" }\n\t\n Write-Output \"Tentacle Version: $env:TentacleVersion\"\n Write-Output \"PowerShell version...\"\n Write-Output $PSVersionTable\n Write-Output \"Path = $Path\"\n\t\n Write-Output \"Getting target files...\"\n $TargetFiles = Get-ChildItem -File -Path $Path -Filter $Filter -Include $Include -Exclude $Exclude -Recurse:$Recurse\n if ($TargetFiles.Count -eq 0) {\n Write-Warning \"`tDid not find any files to process!\"\n return\n }\n else {\n Write-Output \"`tFound $($TargetFiles.Count) file(s)\"\n }\n\t\n Import-Octostache -OctostacheLocation $OctostacheLocation\n\n foreach ($File in $TargetFiles) {\n Resolve-VariablesUsingOctostache $File.FullName\n } \n}\n\n\nfunction Import-Octostache {\n Param(\n [string]$OctostacheLocation\n )\n \n $OctostachePath = $null\n $SprachePath = $null\n \n Write-Output \"Searching for installed version of Octostache.\"\n if (-not [string]::IsNullOrWhiteSpace($OctostacheLocation)) {\n if (-not (Test-Path $OctostacheLocation)) {\n Write-Error \"Octostache path: $OctostacheLocation doesnt exist.\"\n Exit 1\n }\n Write-Verbose \"Searching in $OctostacheLocation for Octostache.dll\"\n $OctostacheLibraryLocations = Get-ChildItem -File -Path $OctostacheLocation -Filter \"OctoStache.dll\" -Recurse\n $OctostachePath = ($OctostacheLibraryLocations | Select-Object -First 1).FullName\n Write-Verbose \"Searching in $OctostacheLocation for Sprache.dll\"\n $SpracheLibraryLocations = Get-ChildItem -File -Path $OctostacheLocation -Filter \"Sprache.dll\" -Recurse\n $SprachePath = ($SpracheLibraryLocations | Select-Object -First 1).FullName\n }\n else {\n try {\n $OctostachePackage = (Get-Package Octostache -ErrorAction Stop) | Select-Object -First 1\n } \n catch {\n $OctostachePackage = $null\n }\n\n if ($null -eq $OctostachePackage) {\n Write-Output \"Downloading Octostache (v3.2.1) from nuget.org.\"\n Install-Package Octostache -MaximumVersion \"3.2.1\" -source https://www.nuget.org/api/v2 -Force -SkipDependencies\n $OctostachePackage = (Get-Package Octostache) | Select-Object -First 1\n }\n \n $OctostachePath = Join-Path (Get-Item $OctostachePackage.source).Directory.FullName \"lib/net40/Octostache.dll\"\n\n try {\n $SprachePackage = (Get-Package Sprache -ErrorAction Stop) | Select-Object -First 1\n } \n catch {\n $SprachePackage = $null\n }\n\n if ($null -eq $SprachePackage) {\n Write-Output \"Downloading Sprache (v2.3.1) from nuget.org.\"\n Install-Package Sprache -MaximumVersion \"2.3.1\" -source https://www.nuget.org/api/v2 -Force -SkipDependencies\n $SprachePackage = @(Get-Package Sprache) | Select-Object -First 1\n }\n\n $SprachePath = Join-Path (Get-Item $SprachePackage.source).Directory.FullName \"lib/net40/Sprache.dll\"\n }\n\n Write-Verbose \"Octostache path: $OctostachePath\"\n Write-Verbose \"Sprache path: $SprachePath\"\n\n if ([string]::IsNullOrWhiteSpace($OctostachePath) -or [string]::IsNullOrWhiteSpace($SprachePath)) {\n Write-Error \"Couldnt locate either the Octostache or Sprache library.\"\n Exit 1\n }\n\n Write-Output \"Adding type $SprachePath\"\n Add-Type -Path $SprachePath\n\n Write-Output \"Adding type $OctostachePath\"\n Add-Type -Path $OctostachePath\n}\n\nfunction Resolve-VariablesUsingOctostache {\n Param(\n [string]$TemplateFile\n )\n\t\t\n Write-Output \"Loading template file $TemplateFile...\"\n $TemplateContent = Get-Content -Raw $TemplateFile\n Write-Output \"`tRead $($TemplateContent.Length) bytes\"\n\t\n $Dictionary = New-Object -TypeName Octostache.VariableDictionary\n\t\n # Load the hastable into the dictionary\n Write-Output \"Loading `$OctopusParameters...\"\n foreach ($Variable in $OctopusParameters.GetEnumerator()) {\n Write-Verbose \"#{$($Variable.Key)} = $($Variable.Value)\"\n $Dictionary.Set($Variable.Key, $Variable.Value)\n }\n\t\n Write-Output \"Resolving variables...\"\n \n try {\n $EvaluatedTemplate = $Dictionary.Evaluate($TemplateContent)\n }\n catch {\n Get-Exceptions -ExceptionObject $Error.Exception\n throw\n }\n\t\n Write-Output \"Writing the resolved template to $($TemplateFile) (UTF8 encoding)\"\n $EvaluatedTemplate | Out-File $TemplateFile -Force\t-Encoding UTF8\n Write-Output \"Done!\"\n}\n\n$FunctionParameters = @{}\n\nif ($null -ne $OctopusParameters['Path']) { $FunctionParameters.Add('Path', $OctopusParameters['Path']) }\nif ($null -ne $OctopusParameters['Filter']) { $FunctionParameters.Add('Filter', $OctopusParameters['Filter']) }\nif ($null -ne $OctopusParameters['Include']) { $FunctionParameters.Add('Include', $($OctopusParameters['Include'] -split \"`n\")) }\nif ($null -ne $OctopusParameters['Exclude']) { $FunctionParameters.Add('Exclude', $($OctopusParameters['Exclude'] -split \"`n\")) }\nif ($null -ne $OctopusParameters['Recurse']) { $FunctionParameters.Add('Recurse', [System.Convert]::ToBoolean($OctopusParameters['Recurse'])) }\nif (-not [string]::IsNullOrWhiteSpace($OctopusParameters['Variables.SubstituteInFiles.OctostacheLocation'])) { $FunctionParameters.Add('OctostacheLocation', $OctopusParameters['Variables.SubstituteInFiles.OctostacheLocation']) }\n\nResolve-OctopusVariablesInTemplate @FunctionParameters", + "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\";\nfunction Get-Exceptions {\n param ($ExceptionObject)\n \n if ($null -ne $ExceptionObject.InnerException) {\n Get-Exceptions -ExceptionObject $ExceptionObject.InnerException\n }\n \n Write-Warning \"Exception is: $($ExceptionObject.Message)\"\n}\n\nfunction Resolve-OctopusVariablesInTemplate {\n <#\n.SYNOPSIS\n\tResolves Octopus variables in files with their values from a OctopusParameters\n\n.DESCRIPTION\n\tLooks for files using Get-ChildItem and in each of the files replaces ${Variable} with the value from $OctopusParameters.\n\tFiles are written back using UTF-8.\n\tRequires PowerShell 3.0 or higher.\n\n.PARAMETER Path\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Filter\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Include\n\tPassed to Get-ChildItem to find the files you want to process\n\n.PARAMETER Exclude\n\tPassed to Get-ChildItem to find the files you want to process\n\t\n.PARAMETER Recurse\n\tPassed to Get-ChildItem to find the files you want to process\n\n#>\n Param(\n [string]$Path,\n [string]$Filter = \"*.config\",\n [string[]]$Include,\n [string[]]$Exclude,\n [switch]$Recurse,\n [string]$OctostacheLocation\n )\n\t\n if (-not $OctopusParameters) { throw \"No OctopusParameters found\" }\n\t\n Write-Output \"Tentacle Version: $env:TentacleVersion\"\n Write-Output \"PowerShell version...\"\n Write-Output $PSVersionTable\n Write-Output \"Path = $Path\"\n\t\n Write-Output \"Getting target files...\"\n $TargetFiles = Get-ChildItem -File -Path $Path -Filter $Filter -Include $Include -Exclude $Exclude -Recurse:$Recurse\n if ($TargetFiles.Count -eq 0) {\n Write-Warning \"`tDid not find any files to process!\"\n return\n }\n else {\n Write-Output \"`tFound $($TargetFiles.Count) file(s)\"\n }\n\t\n Import-Octostache -OctostacheLocation $OctostacheLocation\n\n foreach ($File in $TargetFiles) {\n Resolve-VariablesUsingOctostache $File.FullName\n } \n}\n\n\nfunction Import-Octostache {\n Param(\n [string]$OctostacheLocation\n )\n \n $OctostachePath = $null\n $SprachePath = $null\n \n Write-Output \"Searching for installed version of Octostache.\"\n if (-not [string]::IsNullOrWhiteSpace($OctostacheLocation)) {\n if (-not (Test-Path $OctostacheLocation)) {\n Write-Error \"Octostache path: $OctostacheLocation doesnt exist.\"\n Exit 1\n }\n Write-Verbose \"Searching in $OctostacheLocation for Octostache.dll\"\n $OctostacheLibraryLocations = Get-ChildItem -File -Path $OctostacheLocation -Filter \"OctoStache.dll\" -Recurse\n $OctostachePath = ($OctostacheLibraryLocations | Select-Object -First 1).FullName\n Write-Verbose \"Searching in $OctostacheLocation for Sprache.dll\"\n $SpracheLibraryLocations = Get-ChildItem -File -Path $OctostacheLocation -Filter \"Sprache.dll\" -Recurse\n $SprachePath = ($SpracheLibraryLocations | Select-Object -First 1).FullName\n }\n else {\n try {\n $OctostachePackage = (Get-Package Octostache -ErrorAction Stop) | Select-Object -First 1\n } \n catch {\n $OctostachePackage = $null\n }\n\n if ($null -eq $OctostachePackage) {\n Write-Output \"Downloading Octostache (v3.2.1) from nuget.org.\"\n Install-Package Octostache -MaximumVersion \"3.2.1\" -source https://www.nuget.org/api/v2 -Force -SkipDependencies\n $OctostachePackage = (Get-Package Octostache) | Select-Object -First 1\n }\n \n $OctostachePath = Join-Path (Get-Item $OctostachePackage.source).Directory.FullName \"lib/net40/Octostache.dll\"\n\n try {\n $SprachePackage = (Get-Package Sprache -ErrorAction Stop) | Select-Object -First 1\n } \n catch {\n $SprachePackage = $null\n }\n\n if ($null -eq $SprachePackage) {\n Write-Output \"Downloading Sprache (v2.3.1) from nuget.org.\"\n Install-Package Sprache -MaximumVersion \"2.3.1\" -source https://www.nuget.org/api/v2 -Force -SkipDependencies\n $SprachePackage = @(Get-Package Sprache) | Select-Object -First 1\n }\n\n $SprachePath = Join-Path (Get-Item $SprachePackage.source).Directory.FullName \"lib/net40/Sprache.dll\"\n }\n\n Write-Verbose \"Octostache path: $OctostachePath\"\n Write-Verbose \"Sprache path: $SprachePath\"\n\n if ([string]::IsNullOrWhiteSpace($OctostachePath) -or [string]::IsNullOrWhiteSpace($SprachePath)) {\n Write-Error \"Couldnt locate either the Octostache or Sprache library.\"\n Exit 1\n }\n\n Write-Output \"Adding type $SprachePath\"\n Add-Type -Path $SprachePath\n\n Write-Output \"Adding type $OctostachePath\"\n Add-Type -Path $OctostachePath\n}\n\nfunction Resolve-VariablesUsingOctostache {\n Param(\n [string]$TemplateFile\n )\n\t\t\n Write-Output \"Loading template file $TemplateFile...\"\n $TemplateContent = Get-Content -Raw $TemplateFile\n Write-Output \"`tRead $($TemplateContent.Length) bytes\"\n\t\n $Dictionary = New-Object -TypeName Octostache.VariableDictionary\n\t\n # Load the hastable into the dictionary\n Write-Output \"Loading `$OctopusParameters...\"\n foreach ($Variable in $OctopusParameters.GetEnumerator()) {\n Write-Verbose \"#{$($Variable.Key)} = $($Variable.Value)\"\n $Dictionary.Set($Variable.Key, $Variable.Value)\n }\n\t\n Write-Output \"Resolving variables...\"\n \n try {\n $EvaluatedTemplate = $Dictionary.Evaluate($TemplateContent)\n }\n catch {\n Get-Exceptions -ExceptionObject $Error.Exception\n throw\n }\n\t\n Write-Output \"Writing the resolved template to $($TemplateFile) (UTF8 encoding)\"\n #$EvaluatedTemplate | Out-File $TemplateFile -Force\t-Encoding UTF8\n $EvaluatedTemplate -join \"rn\" | Set-Content $TemplateFile -NoNewLine -Encoding UTF8 -Force\n Write-Output \"Done!\"\n}\n\n$FunctionParameters = @{}\n\nif ($null -ne $OctopusParameters['Path']) { $FunctionParameters.Add('Path', $OctopusParameters['Path']) }\nif ($null -ne $OctopusParameters['Filter']) { $FunctionParameters.Add('Filter', $OctopusParameters['Filter']) }\nif ($null -ne $OctopusParameters['Include']) { $FunctionParameters.Add('Include', $($OctopusParameters['Include'] -split \"`n\")) }\nif ($null -ne $OctopusParameters['Exclude']) { $FunctionParameters.Add('Exclude', $($OctopusParameters['Exclude'] -split \"`n\")) }\nif ($null -ne $OctopusParameters['Recurse']) { $FunctionParameters.Add('Recurse', [System.Convert]::ToBoolean($OctopusParameters['Recurse'])) }\nif (-not [string]::IsNullOrWhiteSpace($OctopusParameters['Variables.SubstituteInFiles.OctostacheLocation'])) { $FunctionParameters.Add('OctostacheLocation', $OctopusParameters['Variables.SubstituteInFiles.OctostacheLocation']) }\n\nResolve-OctopusVariablesInTemplate @FunctionParameters", "Octopus.Action.Script.Syntax": "PowerShell", "Octopus.Action.Script.ScriptSource": "Inline" }, @@ -68,10 +68,10 @@ } ], "LastModifiedAt": "2023-04-04T10:37:47.270Z", - "LastModifiedBy": "harrisonmeister", + "LastModifiedBy": "twerthi", "$Meta": { - "ExportedAt": "2023-04-04T10:37:47.270Z", - "OctopusVersion": "2023.2.4954", + "ExportedAt": "2024-02-05T21:47:52.063Z", + "OctopusVersion": "2023.4.8290", "Type": "ActionTemplate" }, "Category": "octopus"