-
Notifications
You must be signed in to change notification settings - Fork 48
/
build.go
154 lines (121 loc) · 3.86 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const (
// GOLANGCI_VERSION to be used for linting.
GOLANGCI_VERSION = "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.31.0"
// GO_ANALYZER_VERSION to be used for go-analyzer.
GO_ANALYZER_VERSION = "github.com/dusk-network/go-analyzer/cmd/go-analyzer"
)
// GOBIN environment variable.
func GOBIN() string {
if os.Getenv("GOBIN") == "" {
log.Fatal("GOBIN not set")
}
return os.Getenv("GOBIN")
}
func main() {
log.SetFlags(log.Lshortfile)
if _, err := os.Stat(filepath.Join("scripts", "build.go")); os.IsNotExist(err) {
log.Fatal("should run build from root dir")
}
if len(os.Args) < 2 {
log.Fatal("cmd required, eg: install")
}
switch os.Args[1] {
case "install":
install(os.Args[2:])
case "lint":
lint()
case "go-analyzer":
goAnalyzer()
default:
log.Fatal("cmd not found: ", os.Args[1])
}
}
//nolint:gosec
func install(cmdline []string) {
var (
race = flag.Bool("race", false, "build Dusk exec with Race enabled")
debug = flag.Bool("debug", false, "build Dusk exec with Debug enabled")
)
_ = flag.CommandLine.Parse(cmdline)
argsList := append([]string{"list"}, []string{"./..."}...)
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), argsList...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not list packages: %v\n%s", err, string(out))
}
var packages []string
for _, line := range strings.Split(string(out), "\n") {
if strings.Contains(line, "/dusk-blockchain/") && !strings.Contains(line, "/harness") {
packages = append(packages, strings.TrimSpace(line))
}
}
argsInstall := []string{"install"}
if *race {
argsInstall = append(argsInstall, "-race")
}
if *debug {
argsInstall = append(argsInstall, "-gcflags=\"all=-N -l\"")
}
cmd = exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), argsInstall...)
cmd.Args = append(cmd.Args, "-v")
cmd.Args = append(cmd.Args, packages...)
fmt.Println("Building Dusk ...", strings.Join(cmd.Args, " \\\n"))
cmd.Stderr, cmd.Stdout = os.Stderr, os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal("Error: Could not build Dusk. ", "error: ", err, ", cmd: ", cmd)
}
}
//nolint:gosec
func lint() {
v := flag.Bool("v", false, "log verbosely")
// Make sure GOLANGCI is downloaded and available.
argsGet := []string{"get", GOLANGCI_VERSION}
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), argsGet...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not list pkgs: %v\n%s", err, string(out))
}
cmd = exec.Command(filepath.Join(GOBIN(), "golangci-lint"))
cmd.Args = append(cmd.Args, "run", "--config", ".golangci.yml")
if *v {
cmd.Args = append(cmd.Args, "-v")
}
fmt.Println("Linting Dusk ...", strings.Join(cmd.Args, " \\\n"))
cmd.Stderr, cmd.Stdout = os.Stderr, os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal("Error: Could not Lint Dusk ... ", "error: ", err, ", cmd: ", cmd)
}
}
//nolint:gosec
func goAnalyzer() {
// Make sure go-analyzer is downloaded and available.
argsGet := []string{"get", GO_ANALYZER_VERSION}
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), argsGet...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("could not list pkgs: %v\n%s", err, string(out))
}
cmd = exec.Command(filepath.Join(GOBIN(), "go-analyzer"))
cmd.Args = append(cmd.Args, "-a")
fmt.Println("Analyzing Dusk ...", strings.Join(cmd.Args, " \\\n"))
cmd.Stderr, cmd.Stdout = os.Stderr, os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal("Error: Could not Analyze Dusk ... ", "error: ", err, ", cmd: ", cmd)
}
}