feat: improve build scripts

This commit is contained in:
Toby 2022-11-02 23:33:30 +00:00
parent 7cc5e9e41a
commit 904a197af9
2 changed files with 50 additions and 10 deletions

View File

@ -1,13 +1,32 @@
# Hysteria local build script for Windows (PowerShell)
# Hysteria build script for Windows (PowerShell)
$platforms = @("windows/amd64", "linux/amd64", "darwin/amd64")
$ldflags = "-s -w"
# Environment variable options:
# - HY_APP_VERSION: App version
# - HY_APP_COMMIT: App commit hash
# - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64")
if (!(Get-Command go -ErrorAction SilentlyContinue)) {
Write-Host "Error: go is not installed." -ForegroundColor Red
exit 1
}
$ldflags = "-s -w -X 'main.appDate=$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")'"
if ($env:HY_APP_VERSION) {
$ldflags += " -X 'main.appVersion=$($env:HY_APP_VERSION)'"
}
if ($env:HY_APP_COMMIT) {
$ldflags += " -X 'main.appCommit=$($env:HY_APP_COMMIT)'"
}
if ($env:HY_APP_PLATFORMS) {
$platforms = $env:HY_APP_PLATFORMS.Split(",")
}
else {
$goos = go env GOOS
$goarch = go env GOARCH
$platforms = @("$goos/$goarch")
}
if (Test-Path build) {
Remove-Item -Recurse -Force build
}
@ -23,7 +42,11 @@ foreach ($platform in $platforms) {
if ($env:GOOS -eq "windows") {
$output = "$output.exe"
}
go build -o $output -ldflags $ldflags ./cmd/
go build -o $output -tags=gpl -ldflags $ldflags -trimpath ./cmd/
if ($LastExitCode -ne 0) {
Write-Host "Error: failed to build $env:GOOS/$env:GOARCH" -ForegroundColor Red
exit 1
}
}
Write-Host "Build complete." -ForegroundColor Green

View File

@ -1,16 +1,29 @@
#!/bin/bash
# Hysteria local build script for Linux
# Change these to whatever you want
platforms=("windows/amd64" "linux/amd64" "darwin/amd64")
ldflags="-s -w"
# Hysteria build script for Linux
# Environment variable options:
# - HY_APP_VERSION: App version
# - HY_APP_COMMIT: App commit hash
# - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64")
if ! [ -x "$(command -v go)" ]; then
echo 'Error: go is not installed.' >&2
exit 1
fi
ldflags="-s -w -X 'main.appDate=$(date -u '+%F %T')'"
if [ -n "$HY_APP_VERSION" ]; then
ldflags="$ldflags -X 'main.appVersion=$HY_APP_VERSION'"
fi
if [ -n "$HY_APP_COMMIT" ]; then
ldflags="$ldflags -X 'main.appCommit=$HY_APP_COMMIT'"
fi
if [ -z "$HY_APP_PLATFORMS" ]; then
HY_APP_PLATFORMS="$(go env GOOS)/$(go env GOARCH)"
fi
platforms=(${HY_APP_PLATFORMS//,/ })
mkdir -p build
rm -rf build/*
@ -24,7 +37,11 @@ for platform in "${platforms[@]}"; do
if [ $GOOS = "windows" ]; then
output="$output.exe"
fi
env GOOS=$GOOS GOARCH=$GOARCH go build -o $output -ldflags "$ldflags" ./cmd/
env GOOS=$GOOS GOARCH=$GOARCH go build -o $output -tags=gpl -ldflags "$ldflags" -trimpath ./cmd/
if [ $? -ne 0 ]; then
echo "Error: failed to build $GOOS/$GOARCH"
exit 1
fi
done
echo "Build complete."