Skip to content

Commit 9a80805

Browse files
committed
feat: ⚡ add magefile setup
add magefile setup
1 parent 5e6952e commit 9a80805

File tree

8 files changed

+166
-3
lines changed

8 files changed

+166
-3
lines changed

.github/workflows/go.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ jobs:
2525
- name: Task Build
2626
run: task build
2727
- name: Task Test
28-
run: task test
28+
run: task test
29+
- name: Task Build for mage
30+
run: task build-gg
31+
- name: Test with gg
32+
run: gg test

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ go.work.sum
2323

2424
# env file
2525
.env
26-
bin
26+
bin
27+
gg
28+
mage

README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,92 @@ func Test_DeleteNewsByID(t *testing.T) {
266266
})
267267
}
268268
}
269-
```
269+
```
270+
271+
## Add Mage Tools
272+
273+
[Mage](https://magefile.org/) is a tool for setup build tool with golang
274+
275+
### how to use
276+
277+
1. install dependency
278+
279+
```shell
280+
go get github.com/magefile/mage/mage
281+
```
282+
283+
2. create a execute file
284+
285+
```golang
286+
//go:build ignore
287+
// +build ignore
288+
289+
package main
290+
291+
import (
292+
"os"
293+
294+
"github.com/magefile/mage/mage"
295+
)
296+
297+
func main() { os.Exit(mage.Main()) }
298+
```
299+
300+
**Notice**: 檔案前面的 // +build ignore 是避免 mage 編譯這隻檔案
301+
302+
3. create a task file for run task
303+
304+
```golang
305+
//go:build mage
306+
// +build mage
307+
308+
package main
309+
310+
import (
311+
"github.com/magefile/mage/mg"
312+
"github.com/magefile/mage/sh"
313+
)
314+
315+
var Default = Build
316+
317+
// clean the build binary
318+
func Clean() error {
319+
return sh.Rm("bin")
320+
}
321+
322+
// Creates the binary in the current directory.
323+
func Build() error {
324+
mg.Deps(Clean)
325+
if err := sh.Run("go", "mod", "download"); err != nil {
326+
return err
327+
}
328+
return sh.Run("go", "build", "-o", "./bin/server", "./cmd/main.go")
329+
}
330+
331+
// start the server
332+
func Launch() error {
333+
mg.Deps(Build)
334+
err := sh.RunV("./bin/server")
335+
if err != nil {
336+
return err
337+
}
338+
return nil
339+
}
340+
341+
// run the test
342+
func Test() error {
343+
err := sh.RunV("go", "test", "-v", "./...")
344+
if err != nil {
345+
return err
346+
}
347+
return nil
348+
}
349+
```
350+
351+
task 形式分成兩種 1. 有 error return 值 2. 沒有 error return
352+
353+
而參數的部份可以帶入 context 來作而外的處理
354+
355+
相依性則透過 mg.Deps 來設定
356+
357+
預設不會列印執行結果及過程。如果需要有列印執行結果,需要使用 sh.RunV 來作處理

Taskfile.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ tasks:
1616
deps:
1717
- build
1818
silent: true
19+
build-mage:
20+
cmds:
21+
- CGO_ENABLED=0 GOOS=linux go build -o mage mage-tools/mage.go
22+
silent: true
23+
build-gg:
24+
cmds:
25+
- mage -d mage-tools -compile ../gg
26+
deps:
27+
- build-mage
28+
silent: true
1929
coverage:
2030
cmds:
2131
- go test -v -cover ./...

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/json-iterator/go v1.1.12 // indirect
2424
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
2525
github.com/leodido/go-urn v1.4.0 // indirect
26+
github.com/magefile/mage v1.15.0 // indirect
2627
github.com/mattn/go-isatty v0.0.20 // indirect
2728
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2829
github.com/modern-go/reflect2 v1.0.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4949
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
5050
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
5151
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
52+
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
53+
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5254
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
5355
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
5456
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=

mage-tools/mage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import (
7+
"os"
8+
9+
"github.com/magefile/mage/mage"
10+
)
11+
12+
func main() { os.Exit(mage.Main()) }

mage-tools/magefile.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build mage
2+
// +build mage
3+
4+
package main
5+
6+
import (
7+
"github.com/magefile/mage/mg"
8+
"github.com/magefile/mage/sh"
9+
)
10+
11+
var Default = Build
12+
13+
// clean the build binary
14+
func Clean() error {
15+
return sh.Rm("bin")
16+
}
17+
18+
// Creates the binary in the current directory.
19+
func Build() error {
20+
mg.Deps(Clean)
21+
if err := sh.Run("go", "mod", "download"); err != nil {
22+
return err
23+
}
24+
return sh.Run("go", "build", "-o", "./bin/server", "./cmd/main.go")
25+
}
26+
27+
// start the server
28+
func Launch() error {
29+
mg.Deps(Build)
30+
err := sh.RunV("./bin/server")
31+
if err != nil {
32+
return err
33+
}
34+
return nil
35+
}
36+
37+
// run the test
38+
func Test() error {
39+
err := sh.RunV("go", "test", "-v", "./...")
40+
if err != nil {
41+
return err
42+
}
43+
return nil
44+
}

0 commit comments

Comments
 (0)