|
| 1 | +// Build tasks for the GitHub CLI project. |
| 2 | +// |
| 3 | +// Usage: go run script/build.go [<task>] |
| 4 | +// |
| 5 | +// Known tasks are: |
| 6 | +// |
| 7 | +// bin/gh: |
| 8 | +// Builds the main executable. |
| 9 | +// Supported environment variables: |
| 10 | +// - GH_VERSION: determined from source by default |
| 11 | +// - GH_OAUTH_CLIENT_ID |
| 12 | +// - GH_OAUTH_CLIENT_SECRET |
| 13 | +// - SOURCE_DATE_EPOCH: enables reproducible builds |
| 14 | +// - GO_LDFLAGS |
| 15 | +// |
| 16 | +// manpages: |
| 17 | +// Builds the man pages under `share/man/man1/`. |
| 18 | +// |
| 19 | +// clean: |
| 20 | +// Deletes all built files. |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "io/ioutil" |
| 28 | + "os" |
| 29 | + "os/exec" |
| 30 | + "path/filepath" |
| 31 | + "runtime" |
| 32 | + "strconv" |
| 33 | + "strings" |
| 34 | + "time" |
| 35 | + |
| 36 | + "github.com/cli/safeexec" |
| 37 | +) |
| 38 | + |
| 39 | +var tasks = map[string]func(string) error{ |
| 40 | + "bin/gh": func(exe string) error { |
| 41 | + info, err := os.Stat(exe) |
| 42 | + if err == nil && !sourceFilesLaterThan(info.ModTime()) { |
| 43 | + fmt.Printf("%s: `%s` is up to date.\n", self, exe) |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + ldflags := os.Getenv("GO_LDFLAGS") |
| 48 | + ldflags = fmt.Sprintf("-X github.com/cli/cli/internal/build.Version=%s %s", version(), ldflags) |
| 49 | + ldflags = fmt.Sprintf("-X github.com/cli/cli/internal/build.Date=%s %s", date(), ldflags) |
| 50 | + if oauthSecret := os.Getenv("GH_OAUTH_CLIENT_SECRET"); oauthSecret != "" { |
| 51 | + ldflags = fmt.Sprintf("-X github.com/cli/cli/internal/authflow.oauthClientSecret=%s %s", oauthSecret, ldflags) |
| 52 | + ldflags = fmt.Sprintf("-X github.com/cli/cli/internal/authflow.oauthClientID=%s %s", os.Getenv("GH_OAUTH_CLIENT_ID"), ldflags) |
| 53 | + } |
| 54 | + |
| 55 | + return run("go", "build", "-trimpath", "-ldflags", ldflags, "-o", exe, "./cmd/gh") |
| 56 | + }, |
| 57 | + "manpages": func(_ string) error { |
| 58 | + return run("go", "run", "./cmd/gen-docs", "--man-page", "--doc-path", "./share/man/man1/") |
| 59 | + }, |
| 60 | + "clean": func(_ string) error { |
| 61 | + return rmrf("bin", "share") |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +var self string |
| 66 | + |
| 67 | +func main() { |
| 68 | + task := "bin/gh" |
| 69 | + if runtime.GOOS == "windows" { |
| 70 | + task = "bin\\gh.exe" |
| 71 | + } |
| 72 | + |
| 73 | + if len(os.Args) > 1 { |
| 74 | + task = os.Args[1] |
| 75 | + } |
| 76 | + |
| 77 | + self = filepath.Base(os.Args[0]) |
| 78 | + if self == "build" { |
| 79 | + self = "build.go" |
| 80 | + } |
| 81 | + |
| 82 | + t := tasks[normalizeTask(task)] |
| 83 | + if t == nil { |
| 84 | + fmt.Fprintf(os.Stderr, "Don't know how to build task `%s`.\n", task) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + err := t(task) |
| 89 | + if err != nil { |
| 90 | + fmt.Fprintln(os.Stderr, err) |
| 91 | + fmt.Fprintf(os.Stderr, "%s: building task `%s` failed.\n", self, task) |
| 92 | + os.Exit(1) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func version() string { |
| 97 | + if versionEnv := os.Getenv("GH_VERSION"); versionEnv != "" { |
| 98 | + return versionEnv |
| 99 | + } |
| 100 | + if desc, err := cmdOutput("git", "describe", "--tags"); err == nil { |
| 101 | + return desc |
| 102 | + } |
| 103 | + rev, _ := cmdOutput("git", "rev-parse", "--short", "HEAD") |
| 104 | + return rev |
| 105 | +} |
| 106 | + |
| 107 | +func date() string { |
| 108 | + t := time.Now() |
| 109 | + if sourceDate := os.Getenv("SOURCE_DATE_EPOCH"); sourceDate != "" { |
| 110 | + if sec, err := strconv.ParseInt(sourceDate, 10, 64); err == nil { |
| 111 | + t = time.Unix(sec, 0) |
| 112 | + } |
| 113 | + } |
| 114 | + return t.Format("2006-01-02") |
| 115 | +} |
| 116 | + |
| 117 | +func sourceFilesLaterThan(t time.Time) bool { |
| 118 | + foundLater := false |
| 119 | + _ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + if foundLater { |
| 124 | + return filepath.SkipDir |
| 125 | + } |
| 126 | + if len(path) > 1 && (path[0] == '.' || path[0] == '_') { |
| 127 | + if info.IsDir() { |
| 128 | + return filepath.SkipDir |
| 129 | + } else { |
| 130 | + return nil |
| 131 | + } |
| 132 | + } |
| 133 | + if info.IsDir() { |
| 134 | + return nil |
| 135 | + } |
| 136 | + if path == "go.mod" || path == "go.sum" || (strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go")) { |
| 137 | + if info.ModTime().After(t) { |
| 138 | + foundLater = true |
| 139 | + } |
| 140 | + } |
| 141 | + return nil |
| 142 | + }) |
| 143 | + return foundLater |
| 144 | +} |
| 145 | + |
| 146 | +func rmrf(targets ...string) error { |
| 147 | + args := append([]string{"rm", "-rf"}, targets...) |
| 148 | + announce(args...) |
| 149 | + for _, target := range targets { |
| 150 | + if err := os.RemoveAll(target); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func announce(args ...string) { |
| 158 | + fmt.Println(shellInspect(args)) |
| 159 | +} |
| 160 | + |
| 161 | +func run(args ...string) error { |
| 162 | + exe, err := safeexec.LookPath(args[0]) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + announce(args...) |
| 167 | + cmd := exec.Command(exe, args[1:]...) |
| 168 | + return cmd.Run() |
| 169 | +} |
| 170 | + |
| 171 | +func cmdOutput(args ...string) (string, error) { |
| 172 | + exe, err := safeexec.LookPath(args[0]) |
| 173 | + if err != nil { |
| 174 | + return "", err |
| 175 | + } |
| 176 | + cmd := exec.Command(exe, args[1:]...) |
| 177 | + cmd.Stderr = ioutil.Discard |
| 178 | + out, err := cmd.Output() |
| 179 | + return strings.TrimSuffix(string(out), "\n"), err |
| 180 | +} |
| 181 | + |
| 182 | +func shellInspect(args []string) string { |
| 183 | + fmtArgs := make([]string, len(args)) |
| 184 | + for i, arg := range args { |
| 185 | + if strings.ContainsAny(arg, " \t'\"") { |
| 186 | + fmtArgs[i] = fmt.Sprintf("%q", arg) |
| 187 | + } else { |
| 188 | + fmtArgs[i] = arg |
| 189 | + } |
| 190 | + } |
| 191 | + return strings.Join(fmtArgs, " ") |
| 192 | +} |
| 193 | + |
| 194 | +func normalizeTask(t string) string { |
| 195 | + return filepath.ToSlash(strings.TrimSuffix(t, ".exe")) |
| 196 | +} |
0 commit comments