Skip to content

Commit

Permalink
chore(cmd): Clean up versioning with functional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 13, 2024
1 parent 9789574 commit 4560fb0
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 25 deletions.
2 changes: 1 addition & 1 deletion assets/darwin/cask/gones.rb.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cask "gones" do
desc "An NES emulator written in Go."
homepage "https://github.com/gabe565/gones"
{{- if eq .Version "latest" }}
{{- if eq .Version "beta" }}
version :latest
{{- else }}
version "{{ trimPrefix "v" .Version }}"
Expand Down
2 changes: 2 additions & 0 deletions assets/darwin/info/info.plist.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
<key>CFBundleGetInfoString</key>
<string>GoNES by gabe565</string>
{{- with .Version }}
{{- if ne . "beta" }}
<key>CFBundleVersion</key>
<string>{{ trimPrefix "v" . }}</string>
<key>CFBundleShortVersionString</key>
<string>{{ trimPrefix "v" . }}</string>
{{- end }}
{{- end }}
<key>CFBundleIconFile</key>
<string>GoNES.icns</string>
<key>LSUIElement</key>
Expand Down
7 changes: 6 additions & 1 deletion cmd/gones/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/signal"
"runtime"

"github.com/gabe565/gones/cmd/options"
"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/console"
"github.com/gabe565/gones/internal/ppu"
Expand All @@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

func New() *cobra.Command {
func New(opts ...options.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "gones ROM",
Short: "NES emulator written in Go",
Expand All @@ -28,6 +29,10 @@ func New() *cobra.Command {
}
config.Flags(cmd)

for _, opt := range opts {
opt(cmd)
}

return cmd
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/gonesutil/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"os"

"github.com/gabe565/gones/cmd/gonesutil/root"
"github.com/gabe565/gones/cmd/options"
)

var version = ""

func main() {
rootCmd := root.New()
rootCmd := root.New(options.WithVersion(version))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/gonesutil/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package root

import (
"github.com/gabe565/gones/cmd/gonesutil/ls"
"github.com/gabe565/gones/cmd/options"
"github.com/spf13/cobra"
)

func New() *cobra.Command {
func New(opts ...options.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "gonesutil",
Short: "GoNES command-line utilities",

DisableAutoGenTag: true,
}

cmd.AddCommand(ls.New())

for _, opt := range opts {
opt(cmd)
}

return cmd
}
12 changes: 12 additions & 0 deletions cmd/options/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package options

import "github.com/spf13/cobra"

type Option func(cmd *cobra.Command)

func WithVersion(version string) Option {
return func(cmd *cobra.Command) {
cmd.Version = buildVersion(version)
cmd.InitDefaultVersionFlag()
}
}
35 changes: 35 additions & 0 deletions cmd/options/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package options

import "runtime/debug"

func buildVersion(version string) string {
var commit string
var modified bool
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
}

if commit != "" {
if len(commit) > 8 {
commit = commit[:8]
}
if modified {
commit = "*" + commit
}
if version == "" {
version = commit
} else {
version += " (" + commit + ")"
}
}
return version
}
4 changes: 2 additions & 2 deletions hack/build-darwin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BINARY_NAME='gones'
APP_NAME='GoNES'
ICONSET=darwin/GoNES.iconset
ICNS=darwin/GoNES.icns
VERSION="${VERSION:-}"
VERSION="${VERSION:-beta}"

set -euo pipefail

Expand All @@ -29,7 +29,7 @@ go generate
export GOOS=darwin CGO_ENABLED=1
for ARCH in amd64 arm64; do
echo Build "$BINARY_NAME-$ARCH"
GOARCH="$ARCH" go build -ldflags='-w -s' -trimpath -tags gzip,ebitenginesinglethread -o "dist/$BINARY_NAME-$ARCH" .
GOARCH="$ARCH" go build -ldflags="-w -s -X main.version=$VERSION" -trimpath -tags gzip,ebitenginesinglethread -o "dist/$BINARY_NAME-$ARCH" .
done

# Merge binaries
Expand Down
10 changes: 6 additions & 4 deletions hack/build-windows.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

BINARY_NAME='GoNES'
VERSION="${VERSION:-}"
VERSION="${VERSION:-beta}"

set -euo pipefail

Expand All @@ -19,16 +19,18 @@ cp -a assets/{png/icon_48x48,windows/icon48}.png
cp -a assets/{png/icon_64x64,windows/icon64}.png
cp -a assets/{png/icon_128x128,windows/icon128}.png
cp -a assets/{png/icon_256x256,windows/icon256}.png
if [[ -n "${VERSION:-}" && "$VERSION" == v* ]]; then
WINRES_FLAGS=( --product-version="${VERSION#v}.0" --file-version="${VERSION#v}.0" )
if [[ "${VERSION:-}" == v* ]]; then
WINRES_FLAGS+=( --product-version="${VERSION#v}.0" --file-version="${VERSION#v}.0" )
fi
set +u
go-winres make --arch=amd64,arm64 --in=assets/windows/winres.json "${WINRES_FLAGS[@]}"
set -u

go generate

# Build binary
export GOOS=windows CGO_ENABLED=1
for ARCH in amd64 arm64; do
echo Build "$BINARY_NAME-$ARCH.exe"
GOARCH="$ARCH" go build -ldflags='-w -s -H=windowsgui' -trimpath -tags gzip,ebitenginesinglethread -o "dist/$BINARY_NAME-$ARCH.exe" .
GOARCH="$ARCH" go build -ldflags="-w -s -H=windowsgui -X main.version=$VERSION" -trimpath -tags gzip,ebitenginesinglethread -o "dist/$BINARY_NAME-$ARCH.exe" .
done
17 changes: 3 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/gabe565/gones/cmd/gones"
"github.com/gabe565/gones/cmd/options"
_ "github.com/gabe565/gones/internal/pprof"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -10,24 +11,12 @@ import (
//go:generate cp $GOROOT/misc/wasm/wasm_exec.js web/src/scripts
//go:generate sh -c "gzip -c internal/database/database.csv > internal/database/database.csv.gz"

const (
Version = "next"
Commit = ""
)
var version = "beta"

func main() {
cobra.MousetrapHelpText = ""
rootCmd := gones.New()
rootCmd.Version = buildVersion()
rootCmd := gones.New(options.WithVersion(version))
if err := rootCmd.Execute(); err != nil {
log.Fatal().Err(err).Msg("Exiting due to an error")
}
}

func buildVersion() string {
result := Version
if Commit != "" {
result += " (" + Commit + ")"
}
return result
}

0 comments on commit 4560fb0

Please sign in to comment.