Skip to content

Commit 043c50e

Browse files
committedDec 1, 2023
Introduce winmake
Introduce winmake for making binaries and tests. Signed-off-by: Ashley Cui <[email protected]>
1 parent c5c9990 commit 043c50e

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
 

‎win-lib.ps1

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env powershell
2+
3+
# This powershell script is intended to be "dot-sourced" by other scripts.
4+
# It's purpose is identical to that of the `lib.sh` script for Linux environments.
5+
6+
# Behave similar to `set -e` in bash, but ONLY for powershell commandlets!
7+
# For all legacy, program, and script calls use Run-Command() or Check-Exit()
8+
$ErrorActionPreference = 'Stop'
9+
10+
# Any golang compilation needs to know what it's building for.
11+
$Env:GOOS = "windows"
12+
$Env:GOARCH = "amd64"
13+
14+
# Items only relevant in a CI environment.
15+
if ($Env:CI -eq "true") {
16+
# Unnecessary and intrusive. They claim parameter/variable
17+
# values aren't collected, but there could be a bug leading
18+
# to a concern over leaking of some sensitive-value. Stop this.
19+
$Env:POWERSHELL_TELEMETRY_OPTOUT = "true"
20+
21+
# Unnecessary and potentially disruptive. Powershell will
22+
# never ever be updated during automation execution. Stop this.
23+
$Env:POWERSHELL_UPDATECHECK = "off"
24+
25+
# Color in output may confuse tooling and makes logs hard to read.
26+
# TODO: There are probably other places where color needs to be disabled
27+
# in a slightly different way :(
28+
$Env:NO_COLOR = "true"
29+
30+
# Defined by .cirrus.yml for use by all the linux tasks.
31+
# Drop all global envs which have unix paths, defaults are fine.
32+
Remove-Item Env:\GOPATH -ErrorAction:Ignore
33+
Remove-Item Env:\GOSRC -ErrorAction:Ignore
34+
Remove-Item Env:\GOCACHE -ErrorAction:Ignore
35+
36+
# Defined by Cirrus-CI
37+
# Drop large known env variables (an env > 32k will break MSI/ICE validation)
38+
Remove-Item Env:\CIRRUS_COMMIT_MESSAGE -ErrorAction:Ignore
39+
Remove-Item Env:\CIRRUS_CHANGE_MESSAGE -ErrorAction:Ignore
40+
Remove-Item Env:\CIRRUS_PR_BODY -ErrorAction:Ignore
41+
}
42+
43+
# Non-powershell commands do not halt execution on error! This helper
44+
# should be called after every critical operation to check and halt on a
45+
# non-zero exit code. Be careful not to use this for powershell commandlets
46+
# (builtins)! They set '$?' to "True" (failed) or "False" success so calling
47+
# this would mask failures. Rely on $ErrorActionPreference = 'Stop' instead.
48+
function Check-Exit {
49+
$result = $LASTEXITCODE # WARNING: might not be a number!
50+
if ( ($result -ne $null) -and ($result -ne 0) ) {
51+
# https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.callstackframe
52+
$caller = (Get-PSCallStack)[1]
53+
Write-Host "Exit code = '$result' from $($caller.ScriptName):$($caller.ScriptLineNumber)"
54+
Exit $result
55+
}
56+
}
57+
58+
# Small helper to avoid needing to write 'Check-Exit' after every
59+
# non-powershell instruction. It simply prints then executes the _QUOTED_
60+
# argument followed by Check-Exit.
61+
# N/B: Escape any nested quotes with back-tick ("`") characters.
62+
# WARNING: DO NOT use this with powershell builtins! It will not do what you expect!
63+
function Run-Command {
64+
param (
65+
[string] $command
66+
)
67+
68+
Write-Host $command
69+
70+
Invoke-Expression $command
71+
Check-Exit
72+
}

‎winmake.ps1

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
. ./win-lib.ps1
3+
4+
# Targets
5+
function Binaries{
6+
New-Item -ItemType Directory -Force -Path "./bin"
7+
8+
Run-Command "go build -o bin ./cmd/kvpctl"
9+
Run-Command "go build -o bin ./cmd/dumpvms"
10+
Run-Command "go build -o bin ./cmd/createvm"
11+
Run-Command "go build -o bin ./cmd/updatevm"
12+
}
13+
14+
function Make-Clean{
15+
Remove-Item ./bin -Recurse -Force -ErrorAction Ignore -Confirm:$false
16+
}
17+
18+
function Local-Test {
19+
param (
20+
[string]$files
21+
);
22+
Build-Ginkgo
23+
if ($files) {
24+
$files = " --focus-file $files "
25+
}
26+
27+
Run-Command "./test/tools/build/ginkgo.exe $files ./test/e2e/. "
28+
}
29+
30+
# Helpers
31+
function Build-Ginkgo{
32+
if (Test-Path -Path ./test/tools/build/ginkgo.exe -PathType Leaf) {
33+
return
34+
}
35+
Write-Host "Building Ginkgo"
36+
Push-Location ./test/tools
37+
Run-Command "go build -o build/ginkgo.exe ./vendor/github.com/onsi/ginkgo/v2/ginkgo"
38+
Pop-Location
39+
}
40+
41+
42+
# Init script
43+
$target = $args[0]
44+
45+
# TODO: add validate target
46+
switch ($target) {
47+
{$_ -in '', 'binaries'} {
48+
Binaries
49+
}
50+
'localtest' {
51+
if ($args.Count -gt 1) {
52+
$files = $args[1]
53+
}
54+
Local-Test -files $files
55+
}
56+
'clean' {
57+
Make-Clean
58+
}
59+
default {
60+
Write-Host "Usage: " $MyInvocation.MyCommand.Name "<target> [options]"
61+
Write-Host
62+
Write-Host "Example: Build binaries "
63+
Write-Host " .\winmake binaries"
64+
Write-Host
65+
Write-Host "Example: Run all tests "
66+
Write-Host " .\winmake localtest"
67+
Write-Host
68+
}
69+
}

0 commit comments

Comments
 (0)
Please sign in to comment.