Files

44 lines
1.6 KiB
PowerShell
Raw Permalink Normal View History

2020-10-13 17:10:22 +07:00
$ErrorActionPreference = "Stop"
2020-10-13 20:49:07 +07:00
2020-10-13 17:10:22 +07:00
function Validate-Scripts {
Param (
[Parameter(Mandatory=$true)]
[string[]]$Path,
[Parameter(Mandatory=$true)]
2020-10-13 17:55:00 +07:00
[string]$ExpectedShebang
2020-10-13 17:10:22 +07:00
)
2020-10-13 18:06:14 +07:00
$ScriptWithoutShebangLine = @()
Get-ChildItem $path -Recurse -File -Filter "*.sh" | ForEach-Object {
2020-10-13 21:32:58 +07:00
$relativePath = Resolve-Path $_.FullName -Relative
2020-10-13 21:39:12 +07:00
$shebangLine = Get-Content -Path $_.FullName | Select-Object -First 1
2020-10-13 18:06:14 +07:00
if ($shebangLine -eq $ExpectedShebang) {
2020-10-13 21:32:58 +07:00
Write-Host "[+] '$relativePath'"
2020-10-13 18:06:14 +07:00
}
else {
2020-10-13 21:39:12 +07:00
Write-Host "[-] '$relativePath'"
2020-10-13 21:32:58 +07:00
$ScriptWithoutShebangLine += $relativePath
2020-10-13 18:06:14 +07:00
}
2020-10-13 17:10:22 +07:00
}
2020-10-14 02:04:32 +07:00
return $ScriptWithoutShebangLine
2020-10-13 21:36:03 +07:00
}
2020-10-13 17:10:22 +07:00
$PathUbuntu = "./images/ubuntu/scripts"
$PathMacOS = "./images/macos"
2020-10-13 21:21:37 +07:00
$PatternUbuntu = "#!/bin/bash -e"
$PatternMacOS = "#!/bin/bash -e -o pipefail"
2020-10-13 17:24:06 +07:00
$ScriptsWithBrokenShebang = @()
2020-10-13 21:21:37 +07:00
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathUbuntu -ExpectedShebang $PatternUbuntu
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathMacOS -ExpectedShebang $PatternMacOS
2020-10-13 17:10:22 +07:00
if ($ScriptsWithBrokenShebang.Length -gt 0) {
2020-10-13 21:32:58 +07:00
Write-Host "`n`n`n##[error] The following scripts have incorrect shebang:"
2020-10-13 17:10:22 +07:00
$ScriptsWithBrokenShebang | ForEach-Object {
2020-10-13 21:32:58 +07:00
Write-Host "##[error] '$_'"
2020-10-13 18:06:14 +07:00
}
Write-Host "`n`n##[error] Expected shebang for scripts in 'images/ubuntu' folder is '$PatternUbuntu'"
2020-10-13 21:26:48 +07:00
Write-Host "##[error] Expected shebang for scripts in 'images/macos' folder is '$PatternMacOS'"
2020-10-13 17:55:00 +07:00
exit 1
2020-10-13 21:21:37 +07:00
else {
Write-Host "All scripts have correct shebang."
}
}