-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
95 lines (85 loc) · 2.29 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
//go:build mage
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"runtime"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Install mg.Namespace
// Runs `go mod download` and then builds the `raytracer` binary.
func Build() error {
if err := sh.Run("go", "mod", "download"); err != nil {
return err
}
return sh.Run("go", "build", "-o", "raytracer", "./cmd")
}
// Runs the `raytracer` binary, building it first if necessary.
func Run() error {
mg.Deps(Build)
output, err := sh.Output("./raytracer")
fmt.Printf("%s\n", output)
return err
}
// Displays the generated image, generating it first if necessary.
func View() error {
pngImage := "image.png"
cwd, err := os.Getwd()
if err != nil {
return err
}
if _, err := os.Stat(path.Join(cwd, pngImage)); errors.Is(err, os.ErrNotExist) {
fmt.Printf("%s not found, running raytracer ✨\n", pngImage)
mg.Deps(Build, Run)
}
return sh.Run("open", "-a", "Preview", pngImage)
}
// Removes the generated PNG image from disk.
func Clean() error {
pngImage := "image.png"
cwd, err := os.Getwd()
if err != nil {
return err
}
fullImagePath := path.Join(cwd, pngImage)
if _, err := os.Stat(fullImagePath); err == nil {
fmt.Printf("%s found, removing from disk ✨\n", pngImage)
return os.Remove(fullImagePath)
}
fmt.Printf("%s not found, nothing to remove ✨\n", pngImage)
return nil
}
// Installs all system and Go dependencies.
func (Install) Deps() error {
if runtime.GOOS == "linux" {
if err := sh.Run("sudo", "apt-get", "update"); err != nil {
return err
}
if err := sh.Run("sudo", "apt-get", "install", "-y", "libsdl2-image-dev", "libsdl2-mixer-dev", "libsdl2-ttf-dev", "libsdl2-gfx-dev"); err != nil {
return err
}
} else if runtime.GOOS == "darwin" {
if err := sh.Run("brew", "install", "sdl2_image", "sdl2_mixer", "sdl2_ttf", "sdl2_gfx", "pkg-config"); err != nil {
return err
}
} else {
return errors.New("unknown OS")
}
return sh.Run("go", "mod", "download")
}
// Runs the unit tests.
func Test() error {
mg.Deps(Install.Deps)
if _, err := exec.LookPath("gotestsum"); err == nil {
output, err := sh.Output("gotestsum", "--no-color=false")
fmt.Printf("%s\n", output)
return err
}
output, err := sh.Output("go", "test", "-v", "./...")
fmt.Printf("%s\n", output)
return err
}