diff options
Diffstat (limited to 'Magefile.go')
| -rw-r--r-- | Magefile.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Magefile.go b/Magefile.go new file mode 100644 index 0000000..015f0f1 --- /dev/null +++ b/Magefile.go @@ -0,0 +1,73 @@ +//go:build mage + +// Package main provides build targets for anelephantinachinashop. +// Targets follow the same style as other projects: Default builds, Test runs tests, +// Bench runs benchmarks, Install copies binary to GOPATH/bin. +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +const binaryName = "anelephantinachinashop" + +// Default builds the project. +func Default() { + mg.Deps(Build) +} + +// Build compiles the binary. +func Build() error { + return sh.RunV("go", "build", "-o", binaryName, "./cmd/anelephantinachinashop") +} + +// Test runs all unit tests. +func Test() error { + return sh.RunV("go", "test", "./...") +} + +// Bench runs all benchmarks in the cpu package. +func Bench() error { + return sh.RunV("go", "test", "-bench=.", "-benchmem", "./internal/cpu/...") +} + +// Install builds and copies the binary to GOPATH/bin. +func Install() error { + mg.Deps(Build) + + gopath := os.Getenv("GOPATH") + if gopath == "" { + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("get home dir: %w", err) + } + gopath = filepath.Join(home, "go") + } + + binDir := filepath.Join(gopath, "bin") + if err := os.MkdirAll(binDir, 0755); err != nil { + return fmt.Errorf("create bin dir: %w", err) + } + + return sh.RunV("cp", "-v", binaryName, binDir) +} + +// Uninstall removes the binary from GOPATH/bin. +func Uninstall() error { + gopath := os.Getenv("GOPATH") + if gopath == "" { + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("get home dir: %w", err) + } + gopath = filepath.Join(home, "go") + } + + binPath := filepath.Join(gopath, "bin", binaryName) + return os.Remove(binPath) +} |
