forked from andreaskoch/togglcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.go
351 lines (272 loc) · 9.4 KB
/
make.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// +build ignore
// Copyright 2016 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The make.go script builds the Toggl⥃CSV command-line utility.
//
// $ go run make.go -install
//
// View the README.md for further details.
//
// The output binaries go into the ./bin/ directory
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// ProjectName contains the name of the project
const ProjectName = "togglcsv"
// PackagePath contains the package name of the project
const ProjectNamespace = "github.com/andreaskoch/togglcsv"
// GOPATH environment variable name.
const GOPATH_ENVIRONMENT_VARIABLE = "GOPATH"
// GOBIN environment variable name
const GOBIN_ENVIRONMENT_VARIBALE = "GOBIN"
// GOOS environment variable name
const GOOS_ENVIRONMENT_VARIBALE = "GOOS"
// GOARCH environment variable name
const GOARCH_ENVIRONMENT_VARIBALE = "GOARCH"
// GO15VENDOREXPERIMENT environment variable name
const GO15VENDOREXPERIMENT_ENVIRONMENT_VARIBALE = "GO15VENDOREXPERIMENT"
var (
// packages
packages = []string{
"",
"toggl",
}
// command line flags
verboseFlagIsSet = flag.Bool("v", false, "Verbose mode")
installFlagIsSet = flag.Bool("install", false, fmt.Sprintf("Compiles the %s binaries", ProjectName))
crossCompileFlagIsSet = flag.Bool("crosscompile", false, fmt.Sprintf("Compile %s binaries for all platforms and architectures", ProjectName))
testFlagIsSet = flag.Bool("test", false, "Run the tests")
coverageFlagIsSet = flag.Bool("coverage", false, "Run the test and create a code coverage report")
// The GOPATH for the current project
goPath = getWorkingDirectory()
// The GOBIN for the current project
goBin = filepath.Join(goPath, "bin")
// A list of all supported compilation targets (e.g. "windows/amd64")
compilationTargets = []compilationTarget{
compilationTarget{"darwin", "amd64", []string{}},
compilationTarget{"linux", "amd64", []string{}},
compilationTarget{"linux", "arm", []string{}},
compilationTarget{"linux", "arm", []string{"GOARM=5"}},
compilationTarget{"linux", "arm", []string{"GOARM=6"}},
compilationTarget{"linux", "arm", []string{"GOARM=7"}},
compilationTarget{"windows", "amd64", []string{}},
}
)
// Compilation Target Definition
type compilationTarget struct {
OperatingSystem string
Architecture string
OtherVariables []string
}
func (target *compilationTarget) String() string {
return fmt.Sprintf("%s/%s", target.OperatingSystem, target.Architecture)
}
func init() {
executableName := "make.go"
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s provides functions for compiling %s.\n", executableName, ProjectName)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, " go run %s [options]\n", executableName)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, "\n")
flag.PrintDefaults()
}
}
func main() {
log.SetFlags(0)
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
return
}
if *verboseFlagIsSet {
fmt.Printf("%s: %s\n", GOPATH_ENVIRONMENT_VARIABLE, goPath)
fmt.Printf("%s: %s\n", GOBIN_ENVIRONMENT_VARIBALE, goBin)
}
if *installFlagIsSet {
if err := install(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
return
}
if *crossCompileFlagIsSet {
if err := crossCompile(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
return
}
if *testFlagIsSet {
if err := test(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
return
}
if *coverageFlagIsSet {
if err := codecoverage(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
return
}
flag.Usage()
}
// Install all parts of allmark using go install.
func install() error {
// prepare the environment variables
environmentVariables := cleanGoEnv()
environmentVariables = setEnv(environmentVariables, GOBIN_ENVIRONMENT_VARIBALE, goBin)
environmentVariables = setEnv(environmentVariables, GO15VENDOREXPERIMENT_ENVIRONMENT_VARIBALE, "1")
return runCommand(os.Stdout, os.Stderr, goPath, environmentVariables, "go", "install", fmt.Sprintf("%s/...", ProjectNamespace))
}
// test runs all tests
func test() error {
// prepare the environment variables
environmentVariables := cleanGoEnv()
environmentVariables = setEnv(environmentVariables, GOBIN_ENVIRONMENT_VARIBALE, goBin)
environmentVariables = setEnv(environmentVariables, GO15VENDOREXPERIMENT_ENVIRONMENT_VARIBALE, "1")
testError := runCommand(os.Stdout, os.Stderr, goPath, environmentVariables, "go", "test", fmt.Sprintf("%s/...", ProjectNamespace))
if testError != nil {
return testError
}
return nil
}
// Cross-compile all parts of allmark for all supported platforms.
func crossCompile() error {
// prepare the environment variables
environmentVariables := cleanGoEnv()
environmentVariables = setEnv(environmentVariables, GOBIN_ENVIRONMENT_VARIBALE, goBin)
environmentVariables = setEnv(environmentVariables, GO15VENDOREXPERIMENT_ENVIRONMENT_VARIBALE, "1")
// iterate over all supported compilation targets
for _, target := range compilationTargets {
// assemble the target path
targetFile := filepath.Join(goBin, fmt.Sprintf("%s_%s_%s", ProjectName, target.OperatingSystem, target.Architecture))
// prepare environment variables for cross-compilation
crossCompileEnvironemntVariables := environmentVariables
crossCompileEnvironemntVariables = setEnv(crossCompileEnvironemntVariables, GOOS_ENVIRONMENT_VARIBALE, target.OperatingSystem)
crossCompileEnvironemntVariables = setEnv(crossCompileEnvironemntVariables, GOARCH_ENVIRONMENT_VARIBALE, target.Architecture)
// add additional environment variables
for _, additionalEnvVariable := range target.OtherVariables {
components := strings.Split(additionalEnvVariable, "=")
name := components[0]
value := components[1]
// append additional environment variables to target file name
targetFile += fmt.Sprintf("_%s_%s", strings.ToLower(name), strings.ToLower(value))
crossCompileEnvironemntVariables = setEnv(crossCompileEnvironemntVariables, name, value)
}
// build the package for the specified os and arch
if *verboseFlagIsSet {
fmt.Printf("Compiling %s for %s\n", ProjectName, target.String())
}
// add .exe extension for windows
if target.OperatingSystem == "windows" {
targetFile += ".exe"
}
err := runCommand(os.Stdout,
os.Stderr,
goPath,
crossCompileEnvironemntVariables,
"go",
"build",
"-o",
targetFile,
fmt.Sprintf("%s", ProjectNamespace))
if err != nil {
return err
}
}
return nil
}
// codecoverage runs all tests and creates a code coverage report
func codecoverage() error {
// prepare the environment variables
environmentVariables := cleanGoEnv()
environmentVariables = setEnv(environmentVariables, GOBIN_ENVIRONMENT_VARIBALE, goBin)
environmentVariables = setEnv(environmentVariables, GO15VENDOREXPERIMENT_ENVIRONMENT_VARIBALE, "1")
for _, packageName := range packages {
packageTitle := packageName
if packageTitle == "" {
packageTitle = "cli"
}
packagePath := "./" + packageName
coverageError := runCommand(os.Stdout, os.Stderr, goPath, environmentVariables, "go", "test", fmt.Sprintf("-coverprofile=coverage-%s.out", packageTitle), packagePath)
if coverageError != nil {
return coverageError
}
reportError := runCommand(os.Stdout, os.Stderr, goPath, environmentVariables, "go", "tool", "cover", fmt.Sprintf("-html=coverage-%s.out", packageTitle), "-o", fmt.Sprintf("coverage-%s.html", packageTitle))
if reportError != nil {
return reportError
}
}
return nil
}
// getWorkingDirectory returns the current working directory path or fails.
func getWorkingDirectory() string {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current directory: %v", err)
}
return wd
}
// Execute go in the specified go path with the supplied command arguments.
func runCommand(stdout, stderr io.Writer, workingDirectory string, environmentVariables []string, command string, args ...string) error {
// Create the command
cmdName := fmt.Sprintf("%s %s", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
// Set the working directory
cmd.Dir = workingDirectory
// set environment variables
cmd.Env = environmentVariables
// Capture the output
cmd.Stdout = stdout
cmd.Stderr = stderr
if *verboseFlagIsSet {
log.Printf("Running %s", cmdName)
}
// execute the command
err := cmd.Run()
if err != nil {
return fmt.Errorf("Error running %s: %v", cmdName, err.Error())
}
return nil
}
// cleanGoEnv returns a copy of the current environment with GOPATH_ENVIRONMENT_VARIABLE and GOBIN_ENVIRONMENT_VARIBALE removed.
func cleanGoEnv() (clean []string) {
for _, env := range os.Environ() {
if strings.HasPrefix(env, GOBIN_ENVIRONMENT_VARIBALE+"=") {
continue
}
clean = append(clean, env)
}
return
}
// setEnv sets the given key & value in the provided environment.
// Each value in the env list should be of the form key=value.
func setEnv(env []string, key, value string) []string {
for i, s := range env {
if strings.HasPrefix(s, fmt.Sprintf("%s=", key)) {
env[i] = envPair(key, value)
return env
}
}
env = append(env, envPair(key, value))
return env
}
// Create an environment variable of the form key=value.
func envPair(key, value string) string {
return fmt.Sprintf("%s=%s", key, value)
}