-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.ps1
106 lines (79 loc) · 3.52 KB
/
publish.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
<#
.SYNOPSIS
Build a Go application to various platforms
.DESCRIPTION
.EXAMPLE
.NOTES
Don't move this script, is must be in the same folder as main.go.
.LINK
https://github.com/dhcgn/GoTemplate
#>
if ((Get-Command Go -ErrorAction Ignore) -eq $null) {
Write-Error "Couldn't find Go, is PATH to Go missing?"
return
}
$appName = "GoHomeMaticMqtt"
$version = Get-Content -Path .\packaging\VERSION
$publishFolder = "publish"
$debugFolder = "debug"
$compressPublish = $false
$rootFolder = Split-Path -parent $PSCommandPath
$upx = [System.IO.Path]::Combine($rootFolder, "build", "tools", "upx.exe" )
# Just uncomment the platfoms you don't need
$platforms = @()
$platforms += @{GOOS = "windows"; GOARCH = "amd64"; }
#$platforms += @{GOOS = "windows"; GOARCH = "386"; }
#$platforms += @{GOOS = "linux"; GOARCH = "amd64"; }
#$platforms += @{GOOS = "linux"; GOARCH = "386"; }
$platforms += @{GOOS = "linux"; GOARCH = "arm"; }
# $platforms += @{GOOS = "linux"; GOARCH = "arm64"; }
# $platforms += @{GOOS = "darwin"; GOARCH = "amd64"; }
# Clean Up
Remove-Item -Path ([System.IO.Path]::Combine($rootFolder, "build", $publishFolder)) -Recurse -ErrorAction Ignore
Remove-Item -Path ([System.IO.Path]::Combine($rootFolder, "build", $debugFolder)) -Recurse -ErrorAction Ignore
# Build
$count = 0
$maxCount = $platforms.Count * 2
if($compressPublish)
{
$maxCount += $platforms.Count
}
foreach ($item in $platforms ) {
# Write-Host "Build" $item.GOOS $item.GOARCH -ForegroundColor Green
Write-Progress -Activity ("Build $($item.GOOS) $($item.GOARCH)") -PercentComplete ([Double]$count / $maxCount * 100)
$env:GOOS = $item.GOOS
$env:GOARCH = $item.GOARCH
if ($item.GOOS -eq "windows") {
$extension = ".exe"
}
else {
$extension = $null
}
$buildCode = (Join-Path -Path $rootFolder "cmd\hm2mqtt\")
$count += 1
Write-Progress -Activity ("Build $($item.GOOS) $($item.GOARCH)") -Status "Build publish" -PercentComplete ([Double]$count / $maxCount * 100)
$buildOutput = ([System.IO.Path]::Combine( $rootFolder, "build", $publishFolder, ("{0}_{1}_{2}{3}" -f $appName, $item.GOOS, $item.GOARCH, $extension)))
$executeExpression = "go build -ldflags ""-s -w -X main.version={0}"" -trimpath -o {1} {2}" -f $version, $buildOutput, $buildCode
Write-Host "Execute", $executeExpression -ForegroundColor Green
Invoke-Expression $executeExpression
if ($LASTEXITCODE -ne 0){
Write-Host "ERROR" -ForegroundColor Red
}
if ($compressPublish) {
$count += 1
Write-Progress -Activity ("Build $($item.GOOS) $($item.GOARCH)") -Status "Compress publish" -PercentComplete ([Double]$count / $maxCount * 100)
$executeExpression = "$upx --lzma $buildOutput -q"
Write-Host "Execute", $executeExpression -ForegroundColor Green
Invoke-Expression -Command $executeExpression >> $null
}
$count += 1
Write-Progress -Activity ("Build $($item.GOOS) $($item.GOARCH)") -Status "Build debug" -PercentComplete ([Double]$count / $maxCount * 100)
$buildOutput = ([System.IO.Path]::Combine( $rootFolder, "build", $debugFolder, ("{0}_{1}_{2}{3}" -f $appName, $item.GOOS, $item.GOARCH, $extension)))
$executeExpression = "go build -ldflags ""-X main.version={0}"" -o {1} {2}" -f $version, $buildOutput, $buildCode
Write-Host "Execute", $executeExpression -ForegroundColor Green
Invoke-Expression $executeExpression
if ($LASTEXITCODE -ne 0){
Write-Host "ERROR" -ForegroundColor Red
}
}
Write-Host "Done!" -ForegroundColor Green