-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
141 lines (110 loc) · 3.36 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/magefile/mage/sh"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Build
// A build step that requires additional params, or platform specific steps for example
func Build() error {
fmt.Println("Building...")
// Clear the bin folder
if err := os.RemoveAll("bin"); err != nil {
return fmt.Errorf("failed to clear bin folder: %w", err)
}
// Get the current commit hash
hash, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
if err != nil {
return fmt.Errorf("failed to get commit hash: %w", err)
}
date := time.Now().Format(time.RFC3339)
fmt.Println("Commit hash:", strings.TrimSpace(string(hash)))
fmt.Println("Build date:", date)
buildFlags := fmt.Sprintf("-X main.Commit=%s -X main.Date=%s", hash, date)
cmd := exec.Command("go", "build", "-o", "bin/goschema", "-ldflags", buildFlags, ".")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func Tools() error {
fmt.Println("Building tools...")
platforms := []string{"linux", "darwin", "windows"}
archs := []string{"amd64"}
const (
output = "bin/vaultdb"
source = "./tools/vaultdb"
)
for _, platform := range platforms {
for _, arch := range archs {
if err := buildBinary(platform, arch, output, source, platform, arch); err != nil {
return fmt.Errorf("failed to build for %s/%s: %w", platform, arch, err)
}
}
}
return nil
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename("bin/goschema", "/usr/local/bin/goschema")
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("bin")
}
// Build the release binaries
func Release() error {
mg.Deps(Clean)
Tools()
fmt.Println("Building...")
platforms := []string{"linux", "darwin", "windows"}
archs := []string{"amd64"}
const (
output = "bin/goschema"
source = "."
)
for _, platform := range platforms {
for _, arch := range archs {
if err := buildBinary(platform, arch, output, source, platform, arch); err != nil {
return fmt.Errorf("failed to build for %s/%s: %w", platform, arch, err)
}
}
}
return nil
}
func buildBinary(platform, arch, output, source string, suffixes ...string) error {
fmt.Println("Building for", platform)
// Get the current commit hash
hash, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
if err != nil {
return fmt.Errorf("failed to get commit hash: %w", err)
}
date := time.Now().Format(time.RFC3339)
fmt.Println("Commit hash:", strings.TrimSpace(string(hash)))
fmt.Println("Build date:", date)
buildFlags := fmt.Sprintf("-X main.Commit=%s -X main.Date=%s", hash, date)
// Store the current GOOS and GOARCH
defer func() {
fmt.Println("Restoring GOOS and GOARCH to", runtime.GOOS, runtime.GOARCH)
os.Setenv("GOOS", runtime.GOOS)
os.Setenv("GOARCH", runtime.GOARCH)
}()
// Set the GOOS and GOARCH to the desired platform
os.Setenv("GOOS", platform)
os.Setenv("GOARCH", arch)
if len(suffixes) > 0 {
output += "-" + strings.Join(suffixes, "-")
}
return sh.Run("go", "build", "-o", output, "-ldflags", buildFlags, source)
}