Skip to content

Commit 86442a2

Browse files
committed
feat(build): inject build-time version info into binary
1 parent d12eff3 commit 86442a2

6 files changed

Lines changed: 76 additions & 22 deletions

File tree

Makefile

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
SHELL := /bin/bash
22

33
BINARY_NAME=matecommit
4+
VERSION_PKG=github.com/thomas-vilte/matecommit/internal/version
45

5-
.PHONY: all build test test-race test-grep clean lint help
6+
# Build variables
7+
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "dev")
8+
BUILD_DATE=$(shell date +%F)
9+
10+
# LDFLAGS to inject version information
11+
LDFLAGS=-ldflags "\
12+
-X $(VERSION_PKG).GitCommit=$(GIT_COMMIT) \
13+
-X $(VERSION_PKG).BuildDate=$(BUILD_DATE)"
14+
15+
.PHONY: all build build-release test test-race test-grep clean lint help
616

717
all: build
818

919
build:
1020
@echo "Building $(BINARY_NAME)..."
11-
@go build -o $(BINARY_NAME) cmd/main.go
21+
@echo " Git Commit: $(GIT_COMMIT)"
22+
@echo " Build Date: $(BUILD_DATE)"
23+
@go build $(LDFLAGS) -o $(BINARY_NAME) ./cmd/matecommit/main.go
1224
@echo "Build complete: $(BINARY_NAME)"
1325

26+
build-release:
27+
@echo "Building $(BINARY_NAME) for release..."
28+
@echo " Git Commit: $(GIT_COMMIT)"
29+
@echo " Build Date: $(BUILD_DATE)"
30+
@go build $(LDFLAGS) -trimpath -o $(BINARY_NAME) ./cmd/matecommit/main.go
31+
@echo "Release build complete: $(BINARY_NAME)"
32+
1433
test:
1534
@echo "Running tests..."
1635
@go test -v ./...
@@ -35,7 +54,8 @@ lint:
3554

3655
help:
3756
@echo "Available commands:"
38-
@echo " make build - Build the binary"
57+
@echo " make build - Build the binary with version info"
58+
@echo " make build-release - Build optimized binary for release"
3959
@echo " make test - Run standard tests"
4060
@echo " make test-race - Run tests with race detection (filtered output)"
4161
@echo " make test-grep - Run tests and grep for failures"

cmd/matecommit/main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ import (
3232
"github.com/thomas-vilte/matecommit/internal/ui"
3333
"github.com/thomas-vilte/matecommit/internal/vcs"
3434
"github.com/thomas-vilte/matecommit/internal/vcs/github"
35-
"github.com/thomas-vilte/matecommit/internal/version"
35+
versionpkg "github.com/thomas-vilte/matecommit/internal/version"
3636
"github.com/urfave/cli/v3"
3737
)
3838

39+
var (
40+
version = versionpkg.Version
41+
commit = &versionpkg.GitCommit
42+
date = &versionpkg.BuildDate
43+
)
44+
3945
func main() {
4046
app, err := initializeApp()
4147
if err != nil {
@@ -82,7 +88,7 @@ func initializeApp() (*cli.Command, error) {
8288
return &cli.Command{
8389
Name: "mate-commit",
8490
Usage: translations.GetMessage("app_usage", 0, nil),
85-
Version: version.Version,
91+
Version: versionpkg.Info(),
8692
Description: translations.GetMessage("app_description", 0, nil),
8793
Commands: commands,
8894
EnableShellCompletion: true,
@@ -274,7 +280,7 @@ func setupCommands(t *i18n.Translations, cfgApp *cfg.Config, gitService *git.Git
274280
func startBackgroundVersionCheck() {
275281
go func() {
276282
checker := services.NewVersionUpdater(
277-
services.WithCurrentVersion(version.FullVersion()),
283+
services.WithCurrentVersion(versionpkg.FullVersion()),
278284
)
279285
_, _ = checker.CheckForUpdates(context.Background())
280286
}()
@@ -286,7 +292,7 @@ func handleVersionNotification(t *i18n.Translations) {
286292
cmdName := args[1]
287293
if cmdName != "update" && cmdName != "completion" {
288294
checker := services.NewVersionUpdater(
289-
services.WithCurrentVersion(version.FullVersion()),
295+
services.WithCurrentVersion(versionpkg.FullVersion()),
290296
)
291297

292298
updateInfo, err := checker.GetUpdateInfo()

internal/builder/binary_builder.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ type BuildTarget struct {
2727
}
2828

2929
type BinaryBuilder struct {
30-
mainPath string
31-
binaryName string
32-
version string
33-
commit string
34-
date string
35-
buildDir string
30+
mainPath string
31+
binaryName string
32+
version string
33+
commit string
34+
date string
35+
buildDir string
36+
versionPackage string
3637
}
3738

3839
type Option func(*BinaryBuilder)
@@ -61,6 +62,12 @@ func WithDate(date string) Option {
6162
}
6263
}
6364

65+
func WithVersionPackage(pkg string) Option {
66+
return func(b *BinaryBuilder) {
67+
b.versionPackage = pkg
68+
}
69+
}
70+
6471
func NewBinaryBuilder(mainPath, binaryName string, opts ...Option) *BinaryBuilder {
6572
b := &BinaryBuilder{
6673
mainPath: mainPath,
@@ -116,12 +123,23 @@ func (b *BinaryBuilder) BuildBinary(ctx context.Context, target BuildTarget) (st
116123
outputPath += ".exe"
117124
}
118125

119-
ldflags := fmt.Sprintf(
120-
"-s -w -X main.version=%s -X main.commit=%s -X main.date=%s",
121-
b.version,
122-
b.commit,
123-
b.date,
124-
)
126+
var ldflags string
127+
if b.versionPackage != "" {
128+
ldflags = fmt.Sprintf(
129+
"-s -w -X %s.GitCommit=%s -X %s.BuildDate=%s",
130+
b.versionPackage,
131+
b.commit,
132+
b.versionPackage,
133+
b.date,
134+
)
135+
} else {
136+
ldflags = fmt.Sprintf(
137+
"-s -w -X main.version=%s -X main.commit=%s -X main.date=%s",
138+
b.version,
139+
b.commit,
140+
b.date,
141+
)
142+
}
125143

126144
cmd := exec.CommandContext(ctx, "go", "build",
127145
"-o", outputPath,

internal/vcs/github/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ func (ghc *GitHubClient) uploadBinaries(ctx context.Context, releaseID int64, ve
10431043

10441044
builderBinary := ghc.binaryBuilderFactory.NewBuilder(
10451045
ghc.mainPath,
1046-
"matecommit",
1046+
ghc.repo,
10471047
builder.WithVersion(version),
10481048
builder.WithCommit(commit),
10491049
builder.WithDate(date),
@@ -1054,7 +1054,6 @@ func (ghc *GitHubClient) uploadBinaries(ctx context.Context, releaseID int64, ve
10541054
"version", version,
10551055
"build_dir", tempDir)
10561056

1057-
// Compilar binarios (el builder enviará eventos de progreso)
10581057
archives, err := builderBinary.BuildAndPackageAll(ctx, progressCh)
10591058
if err != nil {
10601059
return fmt.Errorf("failed to build binaries: %w", err)

internal/vcs/github/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ func TestGitHubClient_CreateRelease(t *testing.T) {
409409
)
410410
client.binaryBuilderFactory = mockFactory
411411

412-
mockFactory.On("NewBuilder", mock.Anything, "matecommit", mock.Anything).
412+
mockFactory.On("NewBuilder", mock.Anything, mock.Anything, mock.Anything).
413413
Return(mockPackager)
414414

415415
tmpFile, err := os.CreateTemp("", "dummy-archive-*.tar.gz")

internal/version/version.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ package version
44
// This version should be updated in each release
55
const Version = "1.7.0"
66

7+
// GitCommit is the git commit hash (injected at build time)
8+
var GitCommit = "dev"
9+
10+
// BuildDate is the build date (injected at build time)
11+
var BuildDate = "unknown"
12+
713
// FullVersion returns the version with the v prefix
814
func FullVersion() string {
915
return "v" + Version
1016
}
17+
18+
// Info returns complete version information
19+
func Info() string {
20+
return "v" + Version + " (commit: " + GitCommit + ", built: " + BuildDate + ")"
21+
}

0 commit comments

Comments
 (0)