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

38 lines
1.2 KiB
PowerShell
Raw Normal View History

2020-10-13 17:10:22 +07:00
param(
[Parameter(Mandatory)] [string] $path,
[Parameter(Mandatory)] [string] $pattern
)
$ErrorActionPreference = "Stop"
function Validate-Scripts {
Param (
[Parameter(Mandatory=$true)]
[string[]]$Path,
[Parameter(Mandatory=$true)]
[string]$Pattern
)
2020-10-13 17:24:06 +07:00
$ScriptWithoutShebangLine = @()
2020-10-13 17:10:22 +07:00
Get-ChildItem $path | ForEach-Object {
if (Get-Content -Path $($_.FullName) | Select-Object -First 1 | Select-String -Pattern $Pattern -Quiet) {
Write-Host "Pattern '$Pattern' found in '$($_.FullName)'"
}
else {
Write-Host "Pattern '$Pattern' not found in '$($_.FullName)'"
2020-10-13 17:24:06 +07:00
$ScriptWithoutShebangLine += $($_.FullName)
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:24:06 +07:00
$ScriptsWithBrokenShebang = @()
2020-10-13 17:25:30 +07:00
# Check Ubuntu contains required shebang string
2020-10-13 17:24:06 +07:00
$ScriptsWithBrokenShebang += Validate-Scripts -Path $path1 -Pattern "#!/bin/bash -e"
2020-10-13 17:25:30 +07:00
# Check MacOS contains required shebang string
$ScriptsWithBrokenShebang += Validate-Scripts -Path $path2 -Pattern "#!/bin/bash -e -o pipefail"
2020-10-13 17:10:22 +07:00
if ($ScriptsWithBrokenShebang.Length -gt 0) {
$ScriptsWithBrokenShebang | ForEach-Object {
Write-Warning "The following script does not contain shebang: '$_'"
}
exit 1
}