Skip to content

Commit

Permalink
chore: Make generation of versioninfo.json work without tags or git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 11, 2022
1 parent 55b72c6 commit b6039e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
43 changes: 22 additions & 21 deletions assets/templates/versioninfo.json.tmpl
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
{{- $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 -}}
{{- $comments := "" -}}
{{- if exists ".git" -}}
{{- $commitHash := output "git" "rev-parse" "HEAD" | trim -}}
{{- if ne "" (output "git" "diff" "--stat" | trim) -}}
{{- $commitHash = printf "%s-dirty" $commitHash -}}
{{- end -}}
{{- $comments = printf "commit: %s" $commitHash -}}
{{- end -}}

{{- $version := semver (output "git" "describe" "--abbrev=0" "--tags" | trim) -}}
{{- $versionDict := (dict
"Major" $version.Major
"Minor" $version.Minor
"Patch" $version.Patch
"Build" 0)
-}}
{{- $versionStr := "v0.0.0" -}}
{{- if and (exists ".git") (output "git" "tag" "--list" | trim) -}}
{{- $versionStr = output "git" "describe" "--abbrev=0" "--tags" | trim -}}
{{- end -}}
{{- $versionDict := dict -}}
{{- with semver $versionStr -}}
{{- $versionDict = (dict "Major" .Major "Minor" .Minor "Patch" .Patch "Build" 0) -}}
{{- end -}}

{{- dict
"FixedFileInfo" (dict
"FixedFileInfo" (dict
"FileVersion" $versionDict
"ProductVersion" $versionDict)
"StringFileInfo" (dict
"Comments" (printf "commit: %s" $commitHash)
"StringFileInfo" (dict
"Comments" $comments
"FileDescription" "Manage your dotfiles across multiple diverse machines, securely."
"FileVersion" $version.Original
"FileVersion" $versionStr
"InternalName" $filename
"LegalCopyright" (printf "Copyright (c) 2018-%s Tom Payne" $currentYear)
"LegalCopyright" (printf "Copyright (c) 2018-%s Tom Payne" (now | date "2006"))
"OriginalFilename" $filename
"ProductName" $name
"ProductVersion" $version.Original)
| toPrettyJson
}}
"ProductVersion" $versionStr)
| 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 @@ -3,8 +3,10 @@ package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -98,6 +100,16 @@ func run() error {
buffer := &bytes.Buffer{}
funcMap := sprig.TxtFuncMap()
gitHubClient := newGitHubClient(context.Background())
funcMap["exists"] = func(name string) bool {
switch _, err := os.Stat(name); {
case err == nil:
return true
case errors.Is(err, fs.ErrNotExist):
return false
default:
panic(err)
}
}
funcMap["gitHubLatestRelease"] = gitHubClient.gitHubLatestRelease
funcMap["gitHubListReleases"] = gitHubClient.gitHubListReleases
funcMap["gitHubTimestampFormat"] = func(layout string, timestamp github.Timestamp) string {
Expand Down

0 comments on commit b6039e7

Please sign in to comment.