-
Notifications
You must be signed in to change notification settings - Fork 1
/
magefile.go
132 lines (113 loc) · 2.57 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
//go:build mage
// +build mage
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/magefile/mage/sh"
)
const runPath = "./cmd/trace"
// build ray-tracer executable under ./build/
func Build() error {
if err := sh.Run("go", "mod", "download"); err != nil {
return err
}
return sh.Run("go", "build", "-o", "./build/trace", runPath)
}
func goRun(cmd, output string, extra ...string) error {
args := []string{"run", cmd, "-o", output}
return sh.Run("go", append(args, extra...)...)
}
// run the ray-tracer and outputs image to out.ppm
func Run() error {
return goRun(runPath, "out.ppm")
}
// Profiling
// run the ray-tracer with cpu profiling
func Cpuprofile() error {
return goRun(runPath, "out.ppm", "-cpuprofile", "cpu.prof")
}
// run the ray-tracer with memory profiling
func Memprofile() error {
return goRun(runPath, "out.ppm", "-memprofile", "mem.prof")
}
// Testing
var goTest = sh.OutCmd("go", "test")
// run unit tests
func Test() error {
out, err := goTest("./pkg/...")
fmt.Println(out)
return err
}
// run unit tests with test coverage
func Cov() error {
_, err := goTest("-coverprofile", "cover.out", "./pkg/...")
if err != nil {
return err
}
return sh.Run("go", "tool", "cover", "-html", "./cover.out")
}
// run smoke-tests and output images to ./output/
func Smoke() error {
err := os.Mkdir("./output", os.ModeDir)
if err != nil && !os.IsExist(err) {
return err
}
for _, t := range []struct{ cmd, output string }{
{"./cmd/ppm", "output/test.ppm"},
{"./cmd/clock", "output/clock.ppm"},
{"./cmd/projectile", "output/projectile.ppm"},
} {
err = goRun(t.cmd, t.output)
if err != nil {
return err
}
}
return nil
}
// Misc.
// remove build artifacts and output images
func Clean() error {
if err := os.RemoveAll("build"); err != nil {
return err
}
if err := os.RemoveAll("output"); err != nil {
return err
}
if err := removeGlob("*.prof"); err != nil {
return err
}
if err := removeGlob("*.ppm"); err != nil {
return err
}
return removeGlob("*.out")
}
func removeGlob(glob string) error {
fs, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, f := range fs {
if err := os.Remove(f); err != nil {
return err
}
}
return nil
}
// open documentation
func Docs() error {
url := "https://pkg.go.dev/github.com/noahssarcastic/gort"
switch runtime.GOOS {
case "linux":
return sh.Run("xdg-open", url)
case "windows":
return sh.Run("rundll32", "url.dll,FileProtocolHandler", url)
case "darwin":
return sh.Run("open", url)
default:
return errors.New("unsupported platform")
}
}