-
Notifications
You must be signed in to change notification settings - Fork 35
/
common-helpers.psm1
108 lines (97 loc) · 2.99 KB
/
common-helpers.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
.SYNOPSIS
The execute command and print all output to the logs
#>
function Execute-Command {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][string] $Command
)
Write-Debug "Execute $Command"
try {
Invoke-Expression $Command | ForEach-Object { Write-Host $_ }
if ($LASTEXITCODE -ne 0) { throw "Exit code: $LASTEXITCODE"}
}
catch {
$errorMessage = "Error happened during command execution: $Command `n $_"
Write-Host $errorMessage
if ($ErrorActionPreference -ne "Continue") {
# avoid logging Azure DevOps issues in case of $ErrorActionPreference -eq Continue
Write-Host "##vso[task.logissue type=error;] $errorMessage"
}
}
}
<#
.SYNOPSIS
Download file from url and return local path to file
#>
function Download-File {
param(
[Parameter(Mandatory=$true)]
[Uri]$Uri,
[Parameter(Mandatory=$true)]
[String]$OutputFolder
)
$targetFilename = [IO.Path]::GetFileName($Uri)
$targetFilepath = Join-Path $OutputFolder $targetFilename
Write-Debug "Download source from $Uri to $OutFile"
try {
(New-Object System.Net.WebClient).DownloadFile($Uri, $targetFilepath)
return $targetFilepath
} catch {
Write-Host "Error during downloading file from '$Uri'"
"$_"
exit 1
}
}
<#
.SYNOPSIS
Generate file that contains the list of all files in particular directory
#>
function New-ToolStructureDump {
param(
[Parameter(Mandatory=$true)]
[String]$ToolPath,
[Parameter(Mandatory=$true)]
[String]$OutputFolder
)
$outputFile = Join-Path $OutputFolder "tools_structure.txt"
$folderContent = Get-ChildItem -Path $ToolPath -Recurse | Sort-Object | Select-Object -Property FullName, Length
$folderContent | ForEach-Object {
$relativePath = $_.FullName.Replace($ToolPath, "");
return "${relativePath}"
} | Out-File -FilePath $outputFile
}
<#
.SYNOPSIS
Check if it is macOS / Ubuntu platform
#>
function IsNixPlatform {
param(
[Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
[String]$Platform
)
return ($Platform -match "macos") -or ($Platform -match "darwin") -or ($Platform -match "ubuntu") -or ($Platform -match "linux")
}
<#
.SYNOPSIS
Get root directory of selected tool
#>
function GetToolDirectory {
param(
[Parameter(Mandatory=$true)]
[String]$ToolName,
[Parameter(Mandatory=$true)]
[String]$Version,
[Parameter(Mandatory=$true)]
[String]$Architecture
)
$targetPath = $env:AGENT_TOOLSDIRECTORY
if ([string]::IsNullOrEmpty($targetPath)) {
# GitHub Windows images don't have `AGENT_TOOLSDIRECTORY` variable
$targetPath = $env:RUNNER_TOOL_CACHE
}
$ToolcachePath = Join-Path -Path $targetPath -ChildPath $ToolName
$ToolcacheVersionPath = Join-Path -Path $ToolcachePath -ChildPath $Version
return Join-Path $ToolcacheVersionPath $Architecture
}