-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
139 lines (116 loc) · 4.15 KB
/
build.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
package commands
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/quikdev/go/context"
"github.com/quikdev/go/util"
goupx "github.com/alegrey91/go-upx"
fs "github.com/coreybutler/go-fsutil"
"github.com/dustin/go-humanize"
)
type Build struct {
Bundle []string `name:"bundle" short:"b" type:"string" help:"Bundle the application into a tarball/zipball" enum:"zip,tar"`
OS []string `name:"os" type:"string" help:"The operating system(s) to build for (any options from 'go tool dist list' is valid)"`
WASM bool `name:"wasm" type:"bool" help:"Output a web assembly (OS is ignored when this option is true)"`
Output string `name:"output" short:"o" type:"string" help:"Output file name"`
Tips bool `name:"tips" short:"t" type:"bool" help:"Display tips in the generated commands"`
Minify bool `name:"minify" short:"m" type:"bool" help:"Set ldflags to strip debugging symbols and remove DWARF generations"`
Shrink bool `name:"shrink" short:"s" type:"bool" help:"Set gccgoflags to strip debugging symbols and remove DWARF generations"`
Compress bool `name:"compress" short:"c" type:"bool" help:"Compress with UPX"`
Tiny bool `name:"tiny" type:"bool" help:"Use tinygo (if available) instead of go to build."`
DryRun bool `name:"dry-run" short:"d" type:"bool" help:"Display the command without executing it."`
NoWork bool `name:"nowork" type:"bool" help:"Set GOWORK=off when building"`
Update bool `name:"update" short:"u" type:"bool" help:"Update (go mod tidy) before building."`
IgnoreCache bool `name:"no-cache" type:"bool" help:"Ignore the cache and rebuild, even if no Go files have changed."`
Profile []string `name:"profile" optional:"" help:"Name of the manifest.json profile attribute to apply."`
File string `arg:"source" optional:"" help:"Go source file (ex: main.go)"`
// Container string `name:"container" default:"docker" type:"string" enum:"docker,podman" help:"The containerization technology to build with"`
}
func (b *Build) Run(c *Context) error {
ctx := context.New(b.Profile...)
ctx.Configure()
if len(strings.TrimSpace(ctx.InputFile())) == 0 {
_, err := util.FindMainFileInDirectory("./")
if err != nil {
util.Stderr(err)
util.SubtleHighlight("Only apps with a 'package main' can be built (not modules).")
os.Exit(1)
}
}
if b.IgnoreCache {
ctx.IgnoreCache = b.IgnoreCache
}
if b.Update {
ctx.Tidy = true
}
if b.Tiny {
ctx.Tiny = b.Tiny
}
if b.WASM {
ctx.OS = []string{"js"}
} else {
ctx.OS = b.OS
}
// Autorecognize UPX support
if b.Compress == util.EmptyBool {
b.Compress = ctx.UPX
}
if b.Minify {
ctx.StripSymbols = true
ctx.StripDebugging = true
}
if b.Shrink {
ctx.GCCGoFlags.Add("-s")
ctx.GCCGoFlags.Add("-w")
}
// Run this before go mod tidy to determine whether the output is cached
cmd := ctx.BuildCommand()
if c.Debug {
b.Tips = true
}
if !b.DryRun {
if ctx.Tidy {
if !ctx.Cached {
util.BailOnError(util.StreamNoStdErr("go mod tidy"))
// util.Run("go mod tidy"))
fmt.Println("")
}
}
}
// Display command
fmt.Println(cmd.Display(b.Tips))
parentdir := filepath.Dir(ctx.Output())
if !fs.Exists(parentdir) {
fs.Mkdirp(parentdir)
}
// Run command
if !b.DryRun {
if b.NoWork {
os.Setenv("GOWORK", "off")
}
if ctx.BuildFast {
current := os.Getenv("GOGC")
if len(current) == 0 {
current = "2000"
}
os.Setenv("GOGC", current)
}
cmd.Run(ctx.CWD)
if b.Compress {
util.Stdout("\n# compressing executable\n")
upx := goupx.NewUPX()
options := goupx.Options{
CompressionTuningOpt: goupx.CompressionTuningOptions{
Brute: 1,
},
}
_, err := upx.Compress(ctx.Output(), 9, options)
util.BailOnError(err)
util.HighlightCommand("upx", upx.GetArgs()...)
util.Stdout(fmt.Sprintf(" ↳ Name: %v\n ↳ Format: %v\n ↳ Original: %v\n ↳ Compressed: %v\n ↳ Ratio: %v%% of original\n", upx.CmdExecution.GetName(), upx.CmdExecution.GetFormat(), humanize.Bytes(upx.CmdExecution.GetOriginalFileSize()), humanize.Bytes(upx.CmdExecution.GetCompressedFileSize()), upx.CmdExecution.GetRatio()))
}
}
return nil
}