Files
runner-images-temp/images.CI/shebang-linter.ps1
T

35 lines
1.3 KiB
PowerShell
Raw Normal View History

2020-10-13 17:10:22 +07:00
$ErrorActionPreference = "Stop"
2020-10-13 20:47:35 +07:00
# Comment here
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 20:02:18 +07:00
$RelativePath = Join-Path $Path $($_.Name)
$shebangLine = Get-Content -Path $RelativePath | Select-Object -First 1
2020-10-13 18:06:14 +07:00
if ($shebangLine -eq $ExpectedShebang) {
2020-10-13 20:02:18 +07:00
Write-Host "Pattern '$ExpectedShebang' found in '$RelativePath'"
2020-10-13 18:06:14 +07:00
}
else {
2020-10-13 20:02:18 +07:00
Write-Host "Pattern '$ExpectedShebang' not found in '$RelativePath'"
$ScriptWithoutShebangLine += $RelativePath
2020-10-13 18:06:14 +07:00
}
2020-10-13 17:10:22 +07:00
}
2020-10-13 17:24:06 +07:00
return $ScriptWithoutShebangLine
2020-10-13 17:10:22 +07:00
}
2020-10-13 17:55:00 +07:00
$PathUbuntu = "./images/linux/scripts"
$PathMacOS = "./images/macos/provision"
2020-10-13 17:24:06 +07:00
$ScriptsWithBrokenShebang = @()
2020-10-13 18:31:14 +07:00
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathUbuntu -ExpectedShebang "#!/bin/bash -e"
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathMacOS -ExpectedShebang "#!/bin/bash -e -o pipefail"
2020-10-13 17:10:22 +07:00
if ($ScriptsWithBrokenShebang.Length -gt 0) {
$ScriptsWithBrokenShebang | ForEach-Object {
2020-10-13 17:55:00 +07:00
Write-Warning "The following script does not contain shebang: '$_'"
2020-10-13 18:06:14 +07:00
}
2020-10-13 17:55:00 +07:00
exit 1
2020-10-13 17:10:22 +07:00
}