microsoft/typespec

Public

mirrored fromhttps://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efeedc42a9fb908c64751d97fea2ae3d7dfeb4ee

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

eng/emitters/scripts/CommandInvocation-Helpers.ps1

73lines · modecode

1#Requires -Version 7.0
2Set-StrictMode -Version 3.0
3
4function Set-ConsoleEncoding
5{
6 [CmdletBinding()]
7 param
8 (
9 [string] $Encoding = 'utf-8'
10 )
11
12 $outputEncoding = [System.Text.Encoding]::GetEncoding($Encoding)
13 [Console]::OutputEncoding = $outputEncoding
14 [Console]::InputEncoding = $outputEncoding
15}
16
17function Invoke-LoggedCommand {
18 [CmdletBinding()]
19 param(
20 [Parameter(Mandatory = $true)]
21 [string] $Command,
22
23 [ValidateScript({ Test-Path $_ -PathType Container })]
24 [string] $ExecutePath,
25
26 [switch] $GroupOutput,
27
28 [switch] $IgnoreExitCode
29 )
30
31 $pipelineBuild = !!$env:TF_BUILD
32 $startTime = Get-Date
33
34 if($pipelineBuild -and $GroupOutput) {
35 Write-Host "##[group]$Command"
36 } else {
37 Write-Host "> $Command"
38 }
39
40 if($ExecutePath) {
41 Push-Location $ExecutePath
42 }
43
44 try {
45 Invoke-Expression $Command
46
47 $duration = (Get-Date) - $startTime
48
49 if($pipelineBuild -and $GroupOutput) {
50 Write-Host "##[endgroup]"
51 }
52
53 if($IgnoreExitCode) {
54 Write-Host "Command completed ($duration)`n"
55 }
56 elseif($LastExitCode -ne 0)
57 {
58 if($pipelineBuild) {
59 Write-Error "##[error]Command completed with exit code $LastExitCode ($duration): $Command`n"
60 } else {
61 Write-Error "Command completed with exit code $LastExitCode ($duration): $Command`n"
62 }
63 }
64 else {
65 Write-Host "Command completed ($duration)`n"
66 }
67 }
68 finally {
69 if($ExecutePath) {
70 Pop-Location
71 }
72 }
73}
74