/
magefile.go
136 lines (118 loc) · 3.3 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
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/magefile/mage/sh"
"github.com/yulog/mi-diary/app"
"github.com/yulog/mi-diary/infra"
"github.com/yulog/mi-diary/migrate"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
var (
BIN string = "mi-diary"
VERSION string = getVersion()
CURRENT_REVISION, _ = sh.Output("git", "rev-parse", "--short", "HEAD")
BUILD_LDFLAGS string = "-s -w -X main.revision=" + CURRENT_REVISION
)
// func init() {
// VERSION = getVersion()
// CURRENT_REVISION, _ = sh.Output("git", "rev-parse", "--short", "HEAD")
// }
func getVersion() string {
_, err := exec.LookPath("gobump")
if err != nil {
fmt.Println("installing gobump")
sh.Run("go", "install", "github.com/x-motemen/gobump/cmd/gobump@latest")
}
v, _ := sh.Output("gobump", "show", "-r", ".")
return v
}
// A build step that requires additional params, or platform specific steps for example
func Build() error {
// mg.Deps(InstallDeps)
fmt.Println("Building...")
bin := BIN
if runtime.GOOS == "windows" {
bin += ".exe"
}
cmd := exec.Command("go", "build", "-trimpath", "-ldflags="+BUILD_LDFLAGS, "-o", bin, ".")
return cmd.Run()
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
cmd := exec.Command("go", "install", "-ldflags="+BUILD_LDFLAGS, ".")
return cmd.Run()
}
// Manage your deps, or running package managers.
// func InstallDeps() error {
// fmt.Println("Installing Deps...")
// cmd := exec.Command("go", "get", "github.com/stretchr/piglatin")
// return cmd.Run()
// }
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("goxz")
bin := BIN
if runtime.GOOS == "windows" {
bin += ".exe"
}
os.RemoveAll(bin)
}
func ShowVersion() {
fmt.Println(getVersion())
}
func Credits() {
_, err := exec.LookPath("gocredits")
if err != nil {
fmt.Println("installing gocredits")
sh.Run("go", "install", "github.com/Songmu/gocredits")
}
s, _ := sh.Output("gocredits", ".")
f, _ := os.Create("CREDITS")
f.WriteString(s)
defer f.Close()
}
func Cross() {
_, err := exec.LookPath("goxz")
if err != nil {
fmt.Println("installing goxz")
sh.Run("go", "install", "github.com/Songmu/goxz/cmd/goxz@latest")
}
sh.Run("goxz", "-n", BIN, "-pv=v"+VERSION, ".")
}
func Bump() {
_, err := exec.LookPath("gobump")
if err != nil {
fmt.Println("installing gobump")
sh.Run("go", "install", "github.com/x-motemen/gobump/cmd/gobump@latest")
}
sh.Run("gobump", "up", "-w", ".")
}
func Upload() {
_, err := exec.LookPath("ghr")
if err != nil {
fmt.Println("installing ghr")
sh.Run("go", "install", "github.com/tcnksm/ghr@latest")
}
sh.Run("ghr", "-draft", "v"+VERSION, "goxz")
}
func GenMigrate() {
fmt.Println("Generate schema...")
migrate.GenerateSchema()
fmt.Println("Generate diff...")
sh.Run("atlas", "migrate", "diff", "migration", "--dir", "file://migrate/migrations?format=golang-migrate", "--dev-url", "sqlite://file?mode=memory", "--to", "file://migrate/schema.sql")
}
func Migrate() {
fmt.Println("Migration...")
app := app.New()
infra := infra.New(app)
migrate.Do(infra)
}