From 831c80a6e7f3cba360c2aefdd9ed5d4752d2b877 Mon Sep 17 00:00:00 2001 From: Christopher Costa Date: Fri, 24 May 2019 10:50:27 -0700 Subject: [PATCH 1/5] Add Azure DevOps script logging for Windows --- eng/common/LoggingCommandFunctions.ps1 | 601 +++++++++++++++++++++++++ eng/common/build.ps1 | 3 +- eng/common/tools.ps1 | 102 ++++- 3 files changed, 682 insertions(+), 24 deletions(-) create mode 100644 eng/common/LoggingCommandFunctions.ps1 diff --git a/eng/common/LoggingCommandFunctions.ps1 b/eng/common/LoggingCommandFunctions.ps1 new file mode 100644 index 00000000000..027c034da68 --- /dev/null +++ b/eng/common/LoggingCommandFunctions.ps1 @@ -0,0 +1,601 @@ +$script:loggingCommandPrefix = '##vso[' +$script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"? + New-Object psobject -Property @{ Token = ';' ; Replacement = '%3B' } + New-Object psobject -Property @{ Token = "`r" ; Replacement = '%0D' } + New-Object psobject -Property @{ Token = "`n" ; Replacement = '%0A' } + New-Object psobject -Property @{ Token = "]" ; Replacement = '%5D' } +) +# TODO: BUG: Escape % ??? +# TODO: Add test to verify don't need to escape "=". + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-AddAttachment { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Type, + [Parameter(Mandatory = $true)] + [string]$Name, + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'addattachment' -Data $Path -Properties @{ + 'type' = $Type + 'name' = $Name + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UploadSummary { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'uploadsummary' -Data $Path -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-SetEndpoint { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Id, + [Parameter(Mandatory = $true)] + [string]$Field, + [Parameter(Mandatory = $true)] + [string]$Key, + [Parameter(Mandatory = $true)] + [string]$Value, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'setendpoint' -Data $Value -Properties @{ + 'id' = $Id + 'field' = $Field + 'key' = $Key + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-AddBuildTag { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Value, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'build' -Event 'addbuildtag' -Data $Value -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-AssociateArtifact { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Name, + [Parameter(Mandatory = $true)] + [string]$Path, + [Parameter(Mandatory = $true)] + [string]$Type, + [hashtable]$Properties, + [switch]$AsOutput) + + $p = @{ } + if ($Properties) { + foreach ($key in $Properties.Keys) { + $p[$key] = $Properties[$key] + } + } + + $p['artifactname'] = $Name + $p['artifacttype'] = $Type + Write-LoggingCommand -Area 'artifact' -Event 'associate' -Data $Path -Properties $p -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-LogDetail { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [guid]$Id, + $ParentId, + [string]$Type, + [string]$Name, + $Order, + $StartTime, + $FinishTime, + $Progress, + [ValidateSet('Unknown', 'Initialized', 'InProgress', 'Completed')] + [Parameter()] + $State, + [ValidateSet('Succeeded', 'SucceededWithIssues', 'Failed', 'Cancelled', 'Skipped')] + [Parameter()] + $Result, + [string]$Message, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'logdetail' -Data $Message -Properties @{ + 'id' = $Id + 'parentid' = $ParentId + 'type' = $Type + 'name' = $Name + 'order' = $Order + 'starttime' = $StartTime + 'finishtime' = $FinishTime + 'progress' = $Progress + 'state' = $State + 'result' = $Result + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-SetProgress { + [CmdletBinding()] + param( + [ValidateRange(0, 100)] + [Parameter(Mandatory = $true)] + [int]$Percent, + [string]$CurrentOperation, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'setprogress' -Data $CurrentOperation -Properties @{ + 'value' = $Percent + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-SetResult { + [CmdletBinding(DefaultParameterSetName = 'AsOutput')] + param( + [ValidateSet("Succeeded", "SucceededWithIssues", "Failed", "Cancelled", "Skipped")] + [Parameter(Mandatory = $true)] + [string]$Result, + [string]$Message, + [Parameter(ParameterSetName = 'AsOutput')] + [switch]$AsOutput, + [Parameter(ParameterSetName = 'DoNotThrow')] + [switch]$DoNotThrow) + + Write-LoggingCommand -Area 'task' -Event 'complete' -Data $Message -Properties @{ + 'result' = $Result + } -AsOutput:$AsOutput + if ($Result -eq 'Failed' -and !$AsOutput -and !$DoNotThrow) { + # Special internal exception type to control the flow. Not currently intended + # for public usage and subject to change. + throw (New-Object VstsTaskSdk.TerminationException($Message)) + } +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-SetSecret { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Value, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'setsecret' -Data $Value -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-SetVariable { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Name, + [string]$Value, + [switch]$Secret, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{ + 'variable' = $Name + 'issecret' = $Secret + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-TaskDebug { + [CmdletBinding()] + param( + [string]$Message, + [switch]$AsOutput) + + Write-TaskDebug_Internal @PSBoundParameters +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-TaskError { + [CmdletBinding()] + param( + [string]$Message, + [string]$ErrCode, + [string]$SourcePath, + [string]$LineNumber, + [string]$ColumnNumber, + [switch]$AsOutput) + + Write-LogIssue -Type error @PSBoundParameters +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-TaskVerbose { + [CmdletBinding()] + param( + [string]$Message, + [switch]$AsOutput) + + Write-TaskDebug_Internal @PSBoundParameters -AsVerbose +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-TaskWarning { + [CmdletBinding()] + param( + [string]$Message, + [string]$ErrCode, + [string]$SourcePath, + [string]$LineNumber, + [string]$ColumnNumber, + [switch]$AsOutput) + + Write-LogIssue -Type warning @PSBoundParameters +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UploadFile { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'uploadfile' -Data $Path -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-PrependPath { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UpdateBuildNumber { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Value, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'build' -Event 'updatebuildnumber' -Data $Value -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UploadArtifact { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$ContainerFolder, + [Parameter(Mandatory = $true)] + [string]$Name, + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'artifact' -Event 'upload' -Data $Path -Properties @{ + 'containerfolder' = $ContainerFolder + 'artifactname' = $Name + } -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UploadBuildLog { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'build' -Event 'uploadlog' -Data $Path -AsOutput:$AsOutput +} + +<# +.SYNOPSIS +See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md + +.PARAMETER AsOutput +Indicates whether to write the logging command directly to the host or to the output pipeline. +#> +function Write-UpdateReleaseName { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Name, + [switch]$AsOutput) + + Write-LoggingCommand -Area 'release' -Event 'updatereleasename' -Data $Name -AsOutput:$AsOutput +} + +######################################## +# Private functions. +######################################## +function Format-LoggingCommandData { + [CmdletBinding()] + param([string]$Value, [switch]$Reverse) + + if (!$Value) { + return '' + } + + if (!$Reverse) { + foreach ($mapping in $script:loggingCommandEscapeMappings) { + $Value = $Value.Replace($mapping.Token, $mapping.Replacement) + } + } else { + for ($i = $script:loggingCommandEscapeMappings.Length - 1 ; $i -ge 0 ; $i--) { + $mapping = $script:loggingCommandEscapeMappings[$i] + $Value = $Value.Replace($mapping.Replacement, $mapping.Token) + } + } + + return $Value +} + +function Format-LoggingCommand { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Area, + [Parameter(Mandatory = $true)] + [string]$Event, + [string]$Data, + [hashtable]$Properties) + + # Append the preamble. + [System.Text.StringBuilder]$sb = New-Object -TypeName System.Text.StringBuilder + $null = $sb.Append($script:loggingCommandPrefix).Append($Area).Append('.').Append($Event) + + # Append the properties. + if ($Properties) { + $first = $true + foreach ($key in $Properties.Keys) { + [string]$value = Format-LoggingCommandData $Properties[$key] + if ($value) { + if ($first) { + $null = $sb.Append(' ') + $first = $false + } else { + $null = $sb.Append(';') + } + + $null = $sb.Append("$key=$value") + } + } + } + + # Append the tail and output the value. + $Data = Format-LoggingCommandData $Data + $sb.Append(']').Append($Data).ToString() +} + +function Write-LoggingCommand { + [CmdletBinding(DefaultParameterSetName = 'Parameters')] + param( + [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')] + [string]$Area, + [Parameter(Mandatory = $true, ParameterSetName = 'Parameters')] + [string]$Event, + [Parameter(ParameterSetName = 'Parameters')] + [string]$Data, + [Parameter(ParameterSetName = 'Parameters')] + [hashtable]$Properties, + [Parameter(Mandatory = $true, ParameterSetName = 'Object')] + $Command, + [switch]$AsOutput) + + if ($PSCmdlet.ParameterSetName -eq 'Object') { + Write-LoggingCommand -Area $Command.Area -Event $Command.Event -Data $Command.Data -Properties $Command.Properties -AsOutput:$AsOutput + return + } + + $command = Format-LoggingCommand -Area $Area -Event $Event -Data $Data -Properties $Properties + if ($AsOutput) { + $command + } else { + Write-Host $command + } +} + +function Write-LogIssue { + [CmdletBinding()] + param( + [ValidateSet('warning', 'error')] + [Parameter(Mandatory = $true)] + [string]$Type, + [string]$Message, + [string]$ErrCode, + [string]$SourcePath, + [string]$LineNumber, + [string]$ColumnNumber, + [switch]$AsOutput) + + $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{ + 'type' = $Type + 'code' = $ErrCode + 'sourcepath' = $SourcePath + 'linenumber' = $LineNumber + 'columnnumber' = $ColumnNumber + } + if ($AsOutput) { + return $command + } + + if ($Type -eq 'error') { + $foregroundColor = $host.PrivateData.ErrorForegroundColor + $backgroundColor = $host.PrivateData.ErrorBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::Red + $backgroundColor = [System.ConsoleColor]::Black + } + } else { + $foregroundColor = $host.PrivateData.WarningForegroundColor + $backgroundColor = $host.PrivateData.WarningBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::Yellow + $backgroundColor = [System.ConsoleColor]::Black + } + } + + Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor +} + +function Write-TaskDebug_Internal { + [CmdletBinding()] + param( + [string]$Message, + [switch]$AsVerbose, + [switch]$AsOutput) + + $command = Format-LoggingCommand -Area 'task' -Event 'debug' -Data $Message + if ($AsOutput) { + return $command + } + + if ($AsVerbose) { + $foregroundColor = $host.PrivateData.VerboseForegroundColor + $backgroundColor = $host.PrivateData.VerboseBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::Cyan + $backgroundColor = [System.ConsoleColor]::Black + } + } else { + $foregroundColor = $host.PrivateData.DebugForegroundColor + $backgroundColor = $host.PrivateData.DebugBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::DarkGray + $backgroundColor = [System.ConsoleColor]::Black + } + } + + Write-Host -Object $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor +} \ No newline at end of file diff --git a/eng/common/build.ps1 b/eng/common/build.ps1 index 67046a43f8c..4cb2ce489b6 100644 --- a/eng/common/build.ps1 +++ b/eng/common/build.ps1 @@ -133,9 +133,8 @@ try { Build } catch { - Write-Host $_ - Write-Host $_.Exception Write-Host $_.ScriptStackTrace + Write-PipelineTaskError -Message $_ ExitWithExitCode 1 } diff --git a/eng/common/tools.ps1 b/eng/common/tools.ps1 index 9cea610a27f..d119e9611bf 100644 --- a/eng/common/tools.ps1 +++ b/eng/common/tools.ps1 @@ -49,6 +49,8 @@ set-strictmode -version 2.0 $ErrorActionPreference = "Stop" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +. $PSScriptRoot\LoggingCommandFunctions.ps1 + function Create-Directory([string[]] $path) { if (!(Test-Path $path)) { New-Item -path $path -force -itemType "Directory" | Out-Null @@ -92,6 +94,67 @@ function Exec-Process([string]$command, [string]$commandArgs) { } } +function Write-PipelineTaskError { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Message, + [string]$Type = 'error', + [string]$ErrCode, + [string]$SourcePath, + [string]$LineNumber, + [string]$ColumnNumber, + [switch]$AsOutput) + + if(!$ci) { + if($Type -eq 'error') { + Write-Error $Message + return + } + elseif ($Type -eq 'warning') { + Write-Warning $Message + return + } + } + + if(($Type -ne 'error') -and ($Type -ne 'warning')) { + Write-Host $Message + return + } + + if($Type -eq 'error') { + Write-TaskError -Message:$Message -ErrCode:$ErrCode -SourcePath:$SourcePath -LineNumber:$LineNumber -ColumnNumber:$ColumnNumber -AsOutput:$AsOutput + } + else { + Write-TaskWarning -Message:$Message -ErrCode:$ErrCode -SourcePath:$SourcePath -LineNumber:$LineNumber -ColumnNumber:$ColumnNumber -AsOutput:$AsOutput + } +} + +function Write-PipelineSetVariable { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Name, + [string]$Value, + [switch]$Secret, + [switch]$AsOutput) + + if($ci) { + Write-SetVariable -Name:$Name -Value:$Value -Secret:$Secret -AsOutput:$AsOutput + } +} + +function Write-PipelinePrependPath { + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string]$Path, + [switch]$AsOutput) + if($ci) { + Write-PrependPath -Path:$Path -AsOutput:$AsOutput + } +} + function InitializeDotNetCli([bool]$install) { if (Test-Path variable:global:_DotNetInstallDir) { return $global:_DotNetInstallDir @@ -134,7 +197,7 @@ function InitializeDotNetCli([bool]$install) { if ($install) { InstallDotNetSdk $dotnetRoot $dotnetSdkVersion } else { - Write-Host "Unable to find dotnet with SDK version '$dotnetSdkVersion'" -ForegroundColor Red + Write-PipelineTaskError "Unable to find dotnet with SDK version '$dotnetSdkVersion'" ExitWithExitCode 1 } } @@ -147,12 +210,10 @@ function InitializeDotNetCli([bool]$install) { # It also ensures that VS msbuild will use the downloaded sdk targets. $env:PATH = "$dotnetRoot;$env:PATH" - if ($ci) { - # Make Sure that our bootstrapped dotnet cli is avaliable in future steps of the Azure Pipelines build - Write-Host "##vso[task.prependpath]$dotnetRoot" - Write-Host "##vso[task.setvariable variable=DOTNET_MULTILEVEL_LOOKUP]0" - Write-Host "##vso[task.setvariable variable=DOTNET_SKIP_FIRST_TIME_EXPERIENCE]1" - } + # Make Sure that our bootstrapped dotnet cli is avaliable in future steps of the Azure Pipelines build + Write-PipelinePrependPath -Path $dotnetRoot + Write-PipelineSetVariable -Name 'DOTNET_MULTILEVEL_LOOKUP' -Value '0' + Write-PipelineSetVariable -Name 'DOTNET_SKIP_FIRST_TIME_EXPERIENCE' -Value '1' return $global:_DotNetInstallDir = $dotnetRoot } @@ -184,7 +245,7 @@ function InstallDotNet([string] $dotnetRoot, [string] $version, [string] $archit & $installScript @installParameters if ($lastExitCode -ne 0) { - Write-Host "Failed to install dotnet cli (exit code '$lastExitCode')." -ForegroundColor Red + Write-PipelineTaskError -Message "Failed to install dotnet cli (exit code '$lastExitCode')." ExitWithExitCode $lastExitCode } } @@ -358,7 +419,7 @@ function InitializeBuildTool() { if ($msbuildEngine -eq "dotnet") { if (!$dotnetRoot) { - Write-Host "/global.json must specify 'tools.dotnet'." -ForegroundColor Red + Write-PipelineTaskError "/global.json must specify 'tools.dotnet'." ExitWithExitCode 1 } @@ -367,13 +428,13 @@ function InitializeBuildTool() { try { $msbuildPath = InitializeVisualStudioMSBuild -install:$restore } catch { - Write-Host $_ -ForegroundColor Red + Write-PipelineTaskError $_ ExitWithExitCode 1 } $buildTool = @{ Path = $msbuildPath; Command = ""; Tool = "vs"; Framework = "net472" } } else { - Write-Host "Unexpected value of -msbuildEngine: '$msbuildEngine'." -ForegroundColor Red + Write-PipelineTaskError "Unexpected value of -msbuildEngine: '$msbuildEngine'." ExitWithExitCode 1 } @@ -390,7 +451,7 @@ function GetDefaultMSBuildEngine() { return "dotnet" } - Write-Host "-msbuildEngine must be specified, or /global.json must specify 'tools.dotnet' or 'tools.vs'." -ForegroundColor Red + Write-PipelineTaskError "-msbuildEngine must be specified, or /global.json must specify 'tools.dotnet' or 'tools.vs'." ExitWithExitCode 1 } @@ -441,7 +502,7 @@ function InitializeToolset() { } if (-not $restore) { - Write-Host "Toolset version $toolsetVersion has not been restored." -ForegroundColor Red + Write-PipelineTaskError "Toolset version $toolsetVersion has not been restored." ExitWithExitCode 1 } @@ -526,7 +587,7 @@ function MSBuild-Core() { $exitCode = Exec-Process $buildTool.Path $cmdArgs if ($exitCode -ne 0) { - Write-Host "Build failed." -ForegroundColor Red + Write-PipelineTaskError "Build failed." $buildLog = GetMSBuildBinaryLogCommandLineArgument $args if ($buildLog -ne $null) { @@ -569,11 +630,8 @@ Create-Directory $ToolsetDir Create-Directory $TempDir Create-Directory $LogDir -if ($ci) { - Write-Host "##vso[task.setvariable variable=Artifacts]$ArtifactsDir" - Write-Host "##vso[task.setvariable variable=Artifacts.Toolset]$ToolsetDir" - Write-Host "##vso[task.setvariable variable=Artifacts.Log]$LogDir" - - $env:TEMP = $TempDir - $env:TMP = $TempDir -} +Write-PipelineSetVariable -Name 'Artifacts' -Value $ArtifactsDir +Write-PipelineSetVariable -Name 'Artifacts.Toolset' -Value $ToolsetDir +Write-PipelineSetVariable -Name 'Artifacts.Log' -Value $LogDir +Write-PipelineSetVariable -Name 'TEMP' -Value $TempDir +Write-PipelineSetVariable -Name 'TMP' -Value $TempDir From 1b7406938892d4c952743f04b2624be76cd35d04 Mon Sep 17 00:00:00 2001 From: Christopher Costa Date: Fri, 24 May 2019 10:53:09 -0700 Subject: [PATCH 2/5] add comment --- eng/common/LoggingCommandFunctions.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/eng/common/LoggingCommandFunctions.ps1 b/eng/common/LoggingCommandFunctions.ps1 index 027c034da68..4a5b729f84e 100644 --- a/eng/common/LoggingCommandFunctions.ps1 +++ b/eng/common/LoggingCommandFunctions.ps1 @@ -1,3 +1,5 @@ +# Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 + $script:loggingCommandPrefix = '##vso[' $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"? New-Object psobject -Property @{ Token = ';' ; Replacement = '%3B' } From 85aa98dafe9afcd4bb2b9f771113c8cfae7885a1 Mon Sep 17 00:00:00 2001 From: Christopher Costa Date: Tue, 28 May 2019 11:54:50 -0700 Subject: [PATCH 3/5] PR feedback: trim logging file --- eng/common/LoggingCommandFunctions.ps1 | 449 +------------------------ eng/common/tools.ps1 | 4 +- 2 files changed, 6 insertions(+), 447 deletions(-) diff --git a/eng/common/LoggingCommandFunctions.ps1 b/eng/common/LoggingCommandFunctions.ps1 index 4a5b729f84e..c69e67c7106 100644 --- a/eng/common/LoggingCommandFunctions.ps1 +++ b/eng/common/LoggingCommandFunctions.ps1 @@ -1,5 +1,7 @@ # Source for this file was taken from https://github.com/microsoft/azure-pipelines-task-lib/blob/11c9439d4af17e6475d9fe058e6b2e03914d17e6/powershell/VstsTaskSdk/LoggingCommandFunctions.ps1 +# NOTE: You should not be calling these method directly as they are likely to change. Instead you should be calling the Write-Pipeline* functions defined in tools.ps1 + $script:loggingCommandPrefix = '##vso[' $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"? New-Object psobject -Property @{ Token = ';' ; Replacement = '%3B' } @@ -14,231 +16,6 @@ $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%" .SYNOPSIS See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-AddAttachment { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Type, - [Parameter(Mandatory = $true)] - [string]$Name, - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'addattachment' -Data $Path -Properties @{ - 'type' = $Type - 'name' = $Name - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UploadSummary { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'uploadsummary' -Data $Path -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-SetEndpoint { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Id, - [Parameter(Mandatory = $true)] - [string]$Field, - [Parameter(Mandatory = $true)] - [string]$Key, - [Parameter(Mandatory = $true)] - [string]$Value, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'setendpoint' -Data $Value -Properties @{ - 'id' = $Id - 'field' = $Field - 'key' = $Key - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-AddBuildTag { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Value, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'build' -Event 'addbuildtag' -Data $Value -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-AssociateArtifact { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Name, - [Parameter(Mandatory = $true)] - [string]$Path, - [Parameter(Mandatory = $true)] - [string]$Type, - [hashtable]$Properties, - [switch]$AsOutput) - - $p = @{ } - if ($Properties) { - foreach ($key in $Properties.Keys) { - $p[$key] = $Properties[$key] - } - } - - $p['artifactname'] = $Name - $p['artifacttype'] = $Type - Write-LoggingCommand -Area 'artifact' -Event 'associate' -Data $Path -Properties $p -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-LogDetail { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [guid]$Id, - $ParentId, - [string]$Type, - [string]$Name, - $Order, - $StartTime, - $FinishTime, - $Progress, - [ValidateSet('Unknown', 'Initialized', 'InProgress', 'Completed')] - [Parameter()] - $State, - [ValidateSet('Succeeded', 'SucceededWithIssues', 'Failed', 'Cancelled', 'Skipped')] - [Parameter()] - $Result, - [string]$Message, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'logdetail' -Data $Message -Properties @{ - 'id' = $Id - 'parentid' = $ParentId - 'type' = $Type - 'name' = $Name - 'order' = $Order - 'starttime' = $StartTime - 'finishtime' = $FinishTime - 'progress' = $Progress - 'state' = $State - 'result' = $Result - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-SetProgress { - [CmdletBinding()] - param( - [ValidateRange(0, 100)] - [Parameter(Mandatory = $true)] - [int]$Percent, - [string]$CurrentOperation, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'setprogress' -Data $CurrentOperation -Properties @{ - 'value' = $Percent - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-SetResult { - [CmdletBinding(DefaultParameterSetName = 'AsOutput')] - param( - [ValidateSet("Succeeded", "SucceededWithIssues", "Failed", "Cancelled", "Skipped")] - [Parameter(Mandatory = $true)] - [string]$Result, - [string]$Message, - [Parameter(ParameterSetName = 'AsOutput')] - [switch]$AsOutput, - [Parameter(ParameterSetName = 'DoNotThrow')] - [switch]$DoNotThrow) - - Write-LoggingCommand -Area 'task' -Event 'complete' -Data $Message -Properties @{ - 'result' = $Result - } -AsOutput:$AsOutput - if ($Result -eq 'Failed' -and !$AsOutput -and !$DoNotThrow) { - # Special internal exception type to control the flow. Not currently intended - # for public usage and subject to change. - throw (New-Object VstsTaskSdk.TerminationException($Message)) - } -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-SetSecret { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Value, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'setsecret' -Data $Value -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - .PARAMETER AsOutput Indicates whether to write the logging command directly to the host or to the output pipeline. #> @@ -261,22 +38,6 @@ function Write-SetVariable { .SYNOPSIS See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-TaskDebug { - [CmdletBinding()] - param( - [string]$Message, - [switch]$AsOutput) - - Write-TaskDebug_Internal @PSBoundParameters -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - .PARAMETER AsOutput Indicates whether to write the logging command directly to the host or to the output pipeline. #> @@ -297,59 +58,6 @@ function Write-TaskError { .SYNOPSIS See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-TaskVerbose { - [CmdletBinding()] - param( - [string]$Message, - [switch]$AsOutput) - - Write-TaskDebug_Internal @PSBoundParameters -AsVerbose -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-TaskWarning { - [CmdletBinding()] - param( - [string]$Message, - [string]$ErrCode, - [string]$SourcePath, - [string]$LineNumber, - [string]$ColumnNumber, - [switch]$AsOutput) - - Write-LogIssue -Type warning @PSBoundParameters -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UploadFile { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'uploadfile' -Data $Path -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - .PARAMETER AsOutput Indicates whether to write the logging command directly to the host or to the output pipeline. #> @@ -363,84 +71,9 @@ function Write-PrependPath { Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput } -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UpdateBuildNumber { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Value, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'build' -Event 'updatebuildnumber' -Data $Value -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UploadArtifact { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$ContainerFolder, - [Parameter(Mandatory = $true)] - [string]$Name, - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'artifact' -Event 'upload' -Data $Path -Properties @{ - 'containerfolder' = $ContainerFolder - 'artifactname' = $Name - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UploadBuildLog { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'build' -Event 'uploadlog' -Data $Path -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-UpdateReleaseName { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Name, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'release' -Event 'updatereleasename' -Data $Name -AsOutput:$AsOutput -} - -######################################## +<######################################## # Private functions. -######################################## +########################################> function Format-LoggingCommandData { [CmdletBinding()] param([string]$Value, [switch]$Reverse) @@ -526,78 +159,4 @@ function Write-LoggingCommand { } else { Write-Host $command } -} - -function Write-LogIssue { - [CmdletBinding()] - param( - [ValidateSet('warning', 'error')] - [Parameter(Mandatory = $true)] - [string]$Type, - [string]$Message, - [string]$ErrCode, - [string]$SourcePath, - [string]$LineNumber, - [string]$ColumnNumber, - [switch]$AsOutput) - - $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{ - 'type' = $Type - 'code' = $ErrCode - 'sourcepath' = $SourcePath - 'linenumber' = $LineNumber - 'columnnumber' = $ColumnNumber - } - if ($AsOutput) { - return $command - } - - if ($Type -eq 'error') { - $foregroundColor = $host.PrivateData.ErrorForegroundColor - $backgroundColor = $host.PrivateData.ErrorBackgroundColor - if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { - $foregroundColor = [System.ConsoleColor]::Red - $backgroundColor = [System.ConsoleColor]::Black - } - } else { - $foregroundColor = $host.PrivateData.WarningForegroundColor - $backgroundColor = $host.PrivateData.WarningBackgroundColor - if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { - $foregroundColor = [System.ConsoleColor]::Yellow - $backgroundColor = [System.ConsoleColor]::Black - } - } - - Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor -} - -function Write-TaskDebug_Internal { - [CmdletBinding()] - param( - [string]$Message, - [switch]$AsVerbose, - [switch]$AsOutput) - - $command = Format-LoggingCommand -Area 'task' -Event 'debug' -Data $Message - if ($AsOutput) { - return $command - } - - if ($AsVerbose) { - $foregroundColor = $host.PrivateData.VerboseForegroundColor - $backgroundColor = $host.PrivateData.VerboseBackgroundColor - if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { - $foregroundColor = [System.ConsoleColor]::Cyan - $backgroundColor = [System.ConsoleColor]::Black - } - } else { - $foregroundColor = $host.PrivateData.DebugForegroundColor - $backgroundColor = $host.PrivateData.DebugBackgroundColor - if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { - $foregroundColor = [System.ConsoleColor]::DarkGray - $backgroundColor = [System.ConsoleColor]::Black - } - } - - Write-Host -Object $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor } \ No newline at end of file diff --git a/eng/common/tools.ps1 b/eng/common/tools.ps1 index d119e9611bf..e191b0b5f86 100644 --- a/eng/common/tools.ps1 +++ b/eng/common/tools.ps1 @@ -49,8 +49,6 @@ set-strictmode -version 2.0 $ErrorActionPreference = "Stop" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -. $PSScriptRoot\LoggingCommandFunctions.ps1 - function Create-Directory([string[]] $path) { if (!(Test-Path $path)) { New-Item -path $path -force -itemType "Directory" | Out-Null @@ -615,6 +613,8 @@ function GetMSBuildBinaryLogCommandLineArgument($arguments) { return $null } +. $PSScriptRoot\LoggingCommandFunctions.ps1 + $RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..") $EngRoot = Resolve-Path (Join-Path $PSScriptRoot "..") $ArtifactsDir = Join-Path $RepoRoot "artifacts" From c7497934ff9258573965be1ead9a20686b5a7412 Mon Sep 17 00:00:00 2001 From: Christopher Costa Date: Tue, 28 May 2019 14:50:36 -0700 Subject: [PATCH 4/5] Add missing function --- eng/common/LoggingCommandFunctions.ps1 | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/eng/common/LoggingCommandFunctions.ps1 b/eng/common/LoggingCommandFunctions.ps1 index c69e67c7106..4e838f08064 100644 --- a/eng/common/LoggingCommandFunctions.ps1 +++ b/eng/common/LoggingCommandFunctions.ps1 @@ -159,4 +159,47 @@ function Write-LoggingCommand { } else { Write-Host $command } +} + +function Write-LogIssue { + [CmdletBinding()] + param( + [ValidateSet('warning', 'error')] + [Parameter(Mandatory = $true)] + [string]$Type, + [string]$Message, + [string]$ErrCode, + [string]$SourcePath, + [string]$LineNumber, + [string]$ColumnNumber, + [switch]$AsOutput) + + $command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{ + 'type' = $Type + 'code' = $ErrCode + 'sourcepath' = $SourcePath + 'linenumber' = $LineNumber + 'columnnumber' = $ColumnNumber + } + if ($AsOutput) { + return $command + } + + if ($Type -eq 'error') { + $foregroundColor = $host.PrivateData.ErrorForegroundColor + $backgroundColor = $host.PrivateData.ErrorBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::Red + $backgroundColor = [System.ConsoleColor]::Black + } + } else { + $foregroundColor = $host.PrivateData.WarningForegroundColor + $backgroundColor = $host.PrivateData.WarningBackgroundColor + if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) { + $foregroundColor = [System.ConsoleColor]::Yellow + $backgroundColor = [System.ConsoleColor]::Black + } + } + + Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor } \ No newline at end of file From 4d11908a7057e9ad5b6c293223c5f9329538547e Mon Sep 17 00:00:00 2001 From: Christopher Costa Date: Wed, 29 May 2019 10:21:10 -0700 Subject: [PATCH 5/5] Remove more functions to simplify cleanup later --- eng/common/LoggingCommandFunctions.ps1 | 59 -------------------------- eng/common/tools.ps1 | 17 ++++---- 2 files changed, 9 insertions(+), 67 deletions(-) diff --git a/eng/common/LoggingCommandFunctions.ps1 b/eng/common/LoggingCommandFunctions.ps1 index 4e838f08064..c225eaecbf2 100644 --- a/eng/common/LoggingCommandFunctions.ps1 +++ b/eng/common/LoggingCommandFunctions.ps1 @@ -12,65 +12,6 @@ $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%" # TODO: BUG: Escape % ??? # TODO: Add test to verify don't need to escape "=". -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-SetVariable { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Name, - [string]$Value, - [switch]$Secret, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{ - 'variable' = $Name - 'issecret' = $Secret - } -AsOutput:$AsOutput -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-TaskError { - [CmdletBinding()] - param( - [string]$Message, - [string]$ErrCode, - [string]$SourcePath, - [string]$LineNumber, - [string]$ColumnNumber, - [switch]$AsOutput) - - Write-LogIssue -Type error @PSBoundParameters -} - -<# -.SYNOPSIS -See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md - -.PARAMETER AsOutput -Indicates whether to write the logging command directly to the host or to the output pipeline. -#> -function Write-PrependPath { - [CmdletBinding()] - param( - [Parameter(Mandatory = $true)] - [string]$Path, - [switch]$AsOutput) - - Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput -} - <######################################## # Private functions. ########################################> diff --git a/eng/common/tools.ps1 b/eng/common/tools.ps1 index e191b0b5f86..3983d719be3 100644 --- a/eng/common/tools.ps1 +++ b/eng/common/tools.ps1 @@ -97,6 +97,7 @@ function Write-PipelineTaskError { param( [Parameter(Mandatory = $true)] [string]$Message, + [Parameter(Mandatory = $false)] [string]$Type = 'error', [string]$ErrCode, [string]$SourcePath, @@ -119,13 +120,10 @@ function Write-PipelineTaskError { Write-Host $Message return } - - if($Type -eq 'error') { - Write-TaskError -Message:$Message -ErrCode:$ErrCode -SourcePath:$SourcePath -LineNumber:$LineNumber -ColumnNumber:$ColumnNumber -AsOutput:$AsOutput - } - else { - Write-TaskWarning -Message:$Message -ErrCode:$ErrCode -SourcePath:$SourcePath -LineNumber:$LineNumber -ColumnNumber:$ColumnNumber -AsOutput:$AsOutput + if(-not $PSBoundParameters.ContainsKey('Type')) { + $PSBoundParameters.Add('Type', 'error') } + Write-LogIssue @PSBoundParameters } function Write-PipelineSetVariable { @@ -138,7 +136,10 @@ function Write-PipelineSetVariable { [switch]$AsOutput) if($ci) { - Write-SetVariable -Name:$Name -Value:$Value -Secret:$Secret -AsOutput:$AsOutput + Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $Value -Properties @{ + 'variable' = $Name + 'issecret' = $Secret + } -AsOutput:$AsOutput } } @@ -149,7 +150,7 @@ function Write-PipelinePrependPath { [string]$Path, [switch]$AsOutput) if($ci) { - Write-PrependPath -Path:$Path -AsOutput:$AsOutput + Write-LoggingCommand -Area 'task' -Event 'prependpath' -Data $Path -AsOutput:$AsOutput } }