forked from getfider/fider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
151 lines (122 loc) · 3.17 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
142
143
144
145
146
147
148
149
150
151
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// warning shown when at least one dependency is not installed
var missingDepsWarning = `Dependencies %v are missing. Please install them and try again.
To learn how, visit our contributors guide: https://github.com/getfider/fider/blob/master/CONTRIBUTING.md.
`
// required dependencies for building fider
var requiredDeps = []string{
"air",
"godotenv",
"docker",
"npm",
"node",
"mage",
"golangci-lint",
}
var buildTime = time.Now().Format("2006.01.02.150405")
var buildNumber = os.Getenv("CIRCLE_BUILD_NUM")
var exeName = "fider"
var Aliases = map[string]interface{}{
"build": Build.All,
"test": Test.All,
"watch": Watch.All,
"lint": Lint.All,
}
func init() {
os.Setenv("MAGEFILE_VERBOSE", "true")
if runtime.GOOS == "windows" {
exeName = "fider.exe"
}
missingDeps := missingDependencies()
if len(missingDeps) > 0 {
fmt.Printf(missingDepsWarning, missingDeps)
os.Exit(1)
}
}
func Run() error {
return sh.Run("godotenv", "-f", ".env", "./"+exeName)
}
func Migrate() error {
return sh.Run("godotenv", "-f", ".env", "./"+exeName, "migrate")
}
func Clean() error {
os.RemoveAll("./dist")
return os.Mkdir("./dist", 0777)
}
type Watch mg.Namespace
func (Watch) All() {
mg.SerialDeps(Clean, Build.Server, Migrate)
mg.Deps(Watch.Server, Watch.UI)
}
func (Watch) UI() error {
return sh.Run("npx", "webpack", "-w")
}
func (Watch) Server() error {
return sh.Run("air", "-c", "air.conf")
}
type Build mg.Namespace
func (Build) All() {
mg.Deps(Build.Server, Build.UI)
}
func (Build) Server() error {
env := map[string]string{
"GOOS": runtime.GOOS,
"GOARCH": runtime.GOARCH,
}
ldflags := "-s -w -X main.buildtime=" + buildTime + " -X main.buildnumber=" + buildNumber
return sh.RunWith(env, "go", "build", "-ldflags", ldflags, "-o", exeName, ".")
}
func (Build) UI() error {
mg.Deps(Clean)
env := map[string]string{"NODE_ENV": "production"}
return sh.RunWith(env, "npx", "webpack", "-p")
}
type Test mg.Namespace
func (Test) All() {
mg.Deps(Test.Server, Test.UI)
}
func (Test) Coverage() error {
mg.Deps(Build.Server)
sh.Run("godotenv", "-f", ".test.env", "./"+exeName, "migrate")
return sh.Run("godotenv", "-f", ".test.env", "go", "test", "./...", "-coverprofile=cover.out", "-coverpkg=all", "-p=8", "-race")
}
func (Test) Server() error {
mg.Deps(Build.Server)
sh.Run("godotenv", "-f", ".test.env", "./"+exeName, "migrate")
return sh.Run("godotenv", "-f", ".test.env", "go", "test", "./...", "-race")
}
func (Test) UI() error {
env := map[string]string{"TZ": "GMT"}
return sh.RunWith(env, "npx", "jest", "./public")
}
type Lint mg.Namespace
func (Lint) All() {
mg.Deps(Lint.Server, Lint.UI)
}
func (Lint) UI() error {
return sh.Run("npx", "tslint", "-c", "tslint.json", "'public/**/*.{ts,tsx}'", "'tests/**/*.{ts,tsx}'")
}
func (Lint) Server() error {
return sh.Run("golangci-lint", "run")
}
// Utils
func missingDependencies() []string {
var missingDeps []string
for _, dep := range requiredDeps {
_, err := exec.LookPath(dep)
if err != nil {
missingDeps = append(missingDeps, dep)
}
}
return missingDeps
}