-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmagefile.go
141 lines (116 loc) · 2.72 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
// Copyright (c) 2019 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
// +build mage
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/deanishe/awgo/util"
"github.com/deanishe/awgo/util/build"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
var (
info *build.Info
buildDir = "./build"
distDir = "./dist"
env map[string]string
)
func init() {
var err error
if info, err = build.NewInfo(); err != nil {
panic(err)
}
env = info.Env()
env["TAGS"] = "private"
}
func mod(args ...string) error {
argv := append([]string{"mod"}, args...)
return sh.RunWith(info.Env(), "go", argv...)
}
// Aliases are mage command aliases.
var Aliases = map[string]interface{}{
"b": Build,
"c": Clean,
"d": Dist,
"l": Link,
}
// Build build workflow in ./build
func Build() error {
mg.Deps(cleanBuild)
// mg.Deps(Deps)
fmt.Println("building ...")
if err := sh.RunWith(env, "go", "build", "-tags", "$TAGS", "-o", buildDir+"/gcal", "."); err != nil {
return err
}
globs := build.Globs(
"*.png",
"info.plist",
"*.html",
"README.md",
"LICENCE.txt",
"icons/*.png",
)
return build.SymlinkGlobs(buildDir, globs...)
}
// run workflow
func Run() error {
mg.Deps(Build)
fmt.Println("running ...")
return sh.RunWith(info.Env(), buildDir+"/gcal", "-h")
}
// build .alfredworkflow file in ./dist
func Dist() error {
mg.SerialDeps(Clean, Build)
p, err := build.Export(buildDir, distDir)
if err != nil {
return err
}
fmt.Printf("exported %q\n", p)
return nil
}
// symlink ./build directory to Alfred's workflow directory
func Link() error {
mg.Deps(Build)
dir := filepath.Join(info.AlfredWorkflowDir, info.BundleID)
fmt.Printf("linking %q to %q ...\n", buildDir, dir)
if err := sh.Rm(dir); err != nil {
return err
}
return build.Symlink(dir, buildDir, true)
}
// clean & download dependencies
func Deps() error {
mg.Deps(cleanDeps)
fmt.Println("downloading deps ...")
return mod("download")
}
// remove build files
func Clean() {
fmt.Println("cleaning ...")
mg.Deps(cleanBuild, cleanMage)
}
func cleanDeps() error { return mod("tidy", "-v") }
func cleanDir(name string) error {
if !util.PathExists(name) {
return nil
}
infos, err := ioutil.ReadDir(name)
if err != nil {
return err
}
for _, fi := range infos {
if err := sh.Rm(filepath.Join(name, fi.Name())); err != nil {
return err
}
}
return nil
}
// remove generated icons
func CleanIcons() error { return cleanDir("./icons") }
func cleanBuild() error { return cleanDir("./build") }
func cleanMage() error { return sh.Run("mage", "-clean") }