-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.ps1
executable file
·165 lines (143 loc) · 3.99 KB
/
Makefile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env pwsh
$env:NAME = "tf"
## Read .env
if (Test-Path -Path ".env" -PathType Leaf) {
Get-Content -Path ".env" | ForEach-Object {
$key, $value = $_ -split '=', 2
Set-Item -Path "env:$key" -Value $value
}
}
# Parse command line arguments
$targets = @()
$currentTarget = ""
foreach ($arg in $args) {
if ($arg -match '=') {
## Set variable
$key, $value = $arg -split '=', 2
Set-Item -Path "env:$key" -Value $value
}
else {
## Remember the target
$targets += $arg
}
}
if (-not $env:GO) { $env:GO = "go" }
if (-not $env:GORELEASER) { $env:GORELEASER = "goreleaser" }
if ($env:GOOS) {
$GOOS = $env:GOOS
} else {
$GOOS = & $env:GO env GOOS
}
if ($GOOS -eq "windows") {
$env:EXE = ".exe"
}
else {
$env:EXE = ""
}
if (-not $env:BIN) { $env:BIN = "$env:NAME$env:EXE" }
if ($env:OS -eq "Windows_NT") {
if ($env:LOCALAPPDATA) {
if (-not $env:BINDIR) { $env:BINDIR = "$env:LOCALAPPDATA\Microsoft\WindowsApps" }
}
else {
if (-not $env:BINDIR) { $env:BINDIR = "C:\Windows\System32" }
}
}
else {
if (Test-Path "$env:HOME\.local\bin") {
if (-not $env:BINDIR) { $env:BINDIR = "$env:HOME\.local\bin" }
}
elseif (Test-Path "$env:HOME\bin") {
if (-not $env:BINDIR) { $env:BINDIR = "$env:HOME\bin" }
}
else {
if (-not $env:BINDIR) { $env:BINDIR = "/usr/local/bin" }
}
}
function Invoke-CommandWithEcho {
param (
[string]$Command,
[string[]]$Arguments
)
Write-Host $Command $Arguments
$processInfo = Start-Process -FilePath $Command -ArgumentList $Arguments -NoNewWindow -PassThru
$processInfo.WaitForExit()
if ($processInfo.ExitCode -and $processInfo.ExitCode -ne 0) {
Write-Host "make: *** [$currentTarget] Error $($processInfo.ExitCode)"
break
}
}
function Invoke-ExpressionWithEcho {
param (
[string]$Command
)
Write-Host $Command
Invoke-Expression -Command $Command
}
function Write-Target {
param (
[string]$Target
)
Write-Host "Executing target: $Target"
}
## TARGET build Build app binary for single target
function Invoke-Target-Build {
Write-Target "build"
# $(GORELEASER) build --clean --snapshot --single-target --output $(BIN)
Invoke-CommandWithEcho $env:GORELEASER -Arguments "build", "--clean", "--snapshot", "--single-target", "--output", $env:BIN
}
## TARGET install Build and install app binary
function Invoke-Target-Install {
if (-not (Test-Path -Path "$env:BIN" -PathType Leaf)) {
Invoke-Target-Build
}
Write-Target "install"
Invoke-ExpressionWithEcho "Copy-Item -Path '$env:BIN' -Destination $env:BINDIR -Force"
}
## TARGET uninstall Uninstall app binary
function Invoke-Target-Uninstall {
Write-Target "uninstall"
$path = Join-Path $env:BINDIR "$env:BIN"
Invoke-ExpressionWithEcho -Command "Remove-Item $path -Force -ErrorAction SilentlyContinue"
}
## TARGET download Download Go modules
function Invoke-Target-Download {
Write-Target "download"
Invoke-CommandWithEcho $env:GO -Arguments "mod", "download"
}
## TARGET tidy Tidy Go modules
function Invoke-Target-Tidy {
Write-Target "tidy"
Invoke-CommandWithEcho $env:GO -Arguments "mod", "tidy"
}
## TARGET upgrade Upgrade Go modules
function Invoke-Target-Upgrade {
Write-Target "upgrade"
Invoke-CommandWithEcho $env:GO -Arguments "get", "-u"
}
## TARGET clean Clean working directory
function Invoke-Target-Clean {
Write-Target "clean"
Invoke-ExpressionWithEcho -Command "Remove-Item '$env:BIN' -Force -ErrorAction SilentlyContinue"
Invoke-ExpressionWithEcho -Command "Remove-Item dist -Recurse -Force -ErrorAction SilentlyContinue"
}
function Invoke-Target-Help {
Write-Host "Targets:"
Get-Content $PSCommandPath |
Select-String '^## TARGET ' |
Sort-Object |
ForEach-Object {
$target, $description = $_ -split ' ', 4 | Select-Object -Skip 2
Write-Output (" {0,-20} {1}" -f $target, $description)
}
}
## Run target
if ($targets.Count -eq 0) {
& Invoke-Target-Help
}
else {
foreach ($target in $targets) {
$currentTarget = $target
Invoke-Expression ("Invoke-Target-" + $target)
}
}