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

34 lines
1.2 KiB
PowerShell
Raw Normal View History

2020-10-13 17:10:22 +07:00
$ErrorActionPreference = "Stop"
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 {
$shebangLine = Get-Content -Path $($_.FullName) | Select-Object -First 1
if ($shebangLine -eq $ExpectedShebang) {
2020-10-13 17:55:00 +07:00
Write-Host "Pattern '$ExpectedShebang' found in '$($_.FullName)'"
2020-10-13 18:06:14 +07:00
}
else {
2020-10-13 17:55:00 +07:00
Write-Host "Pattern '$ExpectedShebang' not found in '$($_.FullName)'"
$ScriptWithoutShebangLine += $($_.FullName)
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 17:55:00 +07:00
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathUbuntu -Pattern "#!/bin/bash -e"
$ScriptsWithBrokenShebang += Validate-Scripts -Path $PathMacOS -Pattern "#!/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
}