Skip to content

Commit

Permalink
feat: Populate VERSIONINFO on Windows builds
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Oct 26, 2022
1 parent 6b82c37 commit b5fe4e2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ env:
GO_VERSION: 1.19.2
GOFUMPT_VERSION: 0.4.0
GOLANGCI_LINT_VERSION: 1.50.1
GOVERSIONINFO_VERSION: 1.4.0
TPARSE_VERSION: 0.11.1
jobs:
changes:
Expand Down Expand Up @@ -187,6 +188,8 @@ jobs:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
with:
fetch-depth: 0
- uses: actions/setup-go@268d8c0ca0432bb2cf416faae41297df9d262d7f
with:
cache: true
Expand All @@ -196,6 +199,9 @@ jobs:
run: |
sudo apt-get --quiet update
sudo apt-get --no-install-suggests --no-install-recommends --quiet --yes install musl-tools snapcraft
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@v${{ env.GOVERSIONINFO_VERSION }}
go run ./internal/cmds/execute-template -output ./versioninfo.json ./assets/templates/versioninfo.json.tmpl
goversioninfo -platform-specific
- name: build-release
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
uses: goreleaser/goreleaser-action@ff11ca24a9b39f2d36796d1fbd7a4e39c182630a
Expand Down Expand Up @@ -392,6 +398,9 @@ jobs:
sudo apt-get --no-install-suggests --no-install-recommends --quiet --yes install musl-tools snapcraft
sudo snap install --classic snapcraft
sudo apt-get --quiet remove snapcraft
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@v${{ env.GOVERSIONINFO_VERSION }}
go run ./internal/cmds/execute-template -output ./versioninfo.json ./assets/templates/versioninfo.json.tmpl
goversioninfo -platform-specific
- name: check-snapcraft-credentials
run: snapcraft whoami
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
/bin/chezmoi
/bin/gofumpt
/bin/golangci-lint
/bin/goversioninfo
/chezmoi
/chezmoi.exe
/coverage.out
/dist
/resource_windows_*.syso
/versioninfo.json
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GO?=go
GOFUMPT_VERSION=$(shell awk '/GOFUMPT_VERSION:/ { print $$2 }' .github/workflows/main.yml)
GOLANGCI_LINT_VERSION=$(shell awk '/GOLANGCI_LINT_VERSION:/ { print $$2 }' .github/workflows/main.yml)
GOVERSIONINFO_VERSION=$(shell awk '/GOVERSIONINFO_VERSION:/ { print $$2 }' .github/workflows/main.yml)
ifdef VERSION
GO_LDFLAGS+=-X main.version=${VERSION}
endif
Expand Down Expand Up @@ -66,7 +67,7 @@ build-linux:
GOOS=linux GOARCH=amd64 ${GO} build -tags=noupgrade -o /dev/null .

.PHONY: build-windows
build-windows:
build-windows: create-syso
GOOS=windows GOARCH=amd64 ${GO} build -o /dev/null .

.PHONY: run
Expand Down Expand Up @@ -116,8 +117,13 @@ format: ensure-gofumpt
find . -name \*.go | xargs ./bin/gofumpt -extra -w
find . -name \*.txtar | xargs ${GO} run ./internal/cmds/lint-txtar -w

.PHONY: create-syso
create-syso: ensure-goversioninfo
${GO} run ./internal/cmds/execute-template -output ./versioninfo.json ./assets/templates/versioninfo.json.tmpl
goversioninfo -platform-specific

.PHONY: ensure-tools
ensure-tools: ensure-gofumpt ensure-golangci-lint
ensure-tools: ensure-gofumpt ensure-golangci-lint ensure-goversioninfo

.PHONY: ensure-gofumpt
ensure-gofumpt:
Expand All @@ -132,6 +138,12 @@ ensure-golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- v${GOLANGCI_LINT_VERSION} ; \
fi

.PHONY: ensure-goversioninfo
ensure-goversioninfo:
if [ ! -x bin/goversioninfo ] ; then \
GOBIN=$(shell pwd)/bin ${GO} install "github.com/josephspurrier/goversioninfo/cmd/goversioninfo@v${GOVERSIONINFO_VERSION}" ; \
fi

.PHONY: release
release:
goreleaser release \
Expand Down
34 changes: 34 additions & 0 deletions assets/templates/versioninfo.json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- $name := "chezmoi" -}}
{{- $filename := printf "%s.exe" $name -}}

{{- /* Get the current year using the Magical Reference Date format */ -}}
{{- $currentYear := now | date "2006" -}}

{{- $commitHash := output "git" "rev-parse" "HEAD" | trim -}}
{{- if ne "" (output "git" "diff" "--stat" | trim) -}}
{{- $commitHash = printf "%s-dirty" $commitHash -}}
{{- end -}}

{{- $version := semver (output "git" "describe" "--abbrev=0" "--tags" | trim) -}}
{{- $versionDict := (dict
"Major" $version.Major
"Minor" $version.Minor
"Patch" $version.Patch
"Build" 0)
-}}

{{- dict
"FixedFileInfo" (dict
"FileVersion" $versionDict
"ProductVersion" $versionDict)
"StringFileInfo" (dict
"Comments" (printf "commit: %s" $commitHash)
"FileDescription" "Manage your dotfiles across multiple diverse machines, securely."
"FileVersion" $version.Original
"InternalName" $filename
"LegalCopyright" (printf "Copyright (c) 2018-%s Tom Payne" $currentYear)
"OriginalFilename" $filename
"ProductName" $name
"ProductVersion" $version.Original)
| toPrettyJson
-}}
12 changes: 12 additions & 0 deletions internal/cmds/execute-template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -102,6 +103,17 @@ func run() error {
funcMap["gitHubTimestampFormat"] = func(layout string, timestamp github.Timestamp) string {
return timestamp.Format(layout)
}
// FIXME we should use chezmoi's template functions if/when needed,
// for now we use a bespoke output function
funcMap["output"] = func(name string, args ...string) string {
out, err := exec.Command(name, args...).Output()
if err != nil {
panic(err)
}
// FIXME we should be able to return output directly, but
// github.com/Masterminds/sprig's trim function only accepts strings
return string(out)
}
funcMap["replaceAllRegex"] = func(expr, repl, s string) string {
return regexp.MustCompile(expr).ReplaceAllString(s, repl)
}
Expand Down

0 comments on commit b5fe4e2

Please sign in to comment.