spruce/magefiles/mage.go
Dan Anglin 649f074d25
All checks were successful
/ test (pull_request) Successful in 26s
/ lint (pull_request) Successful in 26s
Update Project
Changes:

- ci: Add a workflow for Forgejo Actions
- docs: Update README.asciidoc
- tests: add a mage target for running tests
2023-12-06 14:42:18 +00:00

133 lines
2.8 KiB
Go

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
Default = Build
binary = "spruce"
defaultInstallPrefix = "/usr/local"
)
// Test run the go tests.
// To enable verbose mode set SPRUCE_TEST_VERBOSE=1.
// To enable coverage mode set SPRUCE_TEST_COVER=1.
func Test() error {
goTest := sh.RunCmd("go", "test")
args := []string{"./..."}
if os.Getenv("SPRUCE_TEST_VERBOSE") == "1" {
args = append(args, "-v")
}
if os.Getenv("SPRUCE_TEST_COVER") == "1" {
args = append(args, "-cover")
}
return goTest(args...)
}
// Lint runs golangci-lint against the code.
func Lint() error {
return sh.RunV("golangci-lint", "run", "--color", "always")
}
// Build builds the binary.
func Build() error {
flags := ldflags()
return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/spruce")
}
// Install installs the binary to the execution path.
func Install() error {
mg.Deps(Build)
installPrefix := os.Getenv("SPRUCE_INSTALL_PREFIX")
if installPrefix == "" {
installPrefix = defaultInstallPrefix
}
dest := filepath.Join(installPrefix, "bin", binary)
if err := sh.Copy(dest, binary); err != nil {
return fmt.Errorf("unable to install %s; %w", binary, err)
}
return nil
}
// Documentation generates AsciiDoc documentation.
func Documentation() error {
documentation, err := sh.Output("go", "run", "./cmd/spruce-docgen")
if err != nil {
return fmt.Errorf("unable to produce documentation; %w", err)
}
filename := "./docs/schema.asciidoc"
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("unable to create %s; %w", filename, err)
}
defer file.Close()
fmt.Fprint(file, documentation)
return nil
}
// Clean cleans the workspace
func Clean() error {
files := []string{binary, "cv.pdf", "cv.json"}
for i := range files {
if err := sh.Rm(files[i]); err != nil {
return fmt.Errorf("unable to remove %s; %w", binary, err)
}
}
if err := sh.Run("go", "clean", "./..."); err != nil {
return fmt.Errorf("unable to run 'go clean'; %w", err)
}
return nil
}
// ldflags returns the build flags.
func ldflags() string {
ldflagsfmt := "-s -w -X main.binaryVersion=%s -X main.gitCommit=%s -X main.goVersion=%s -X main.buildTime=%s"
buildTime := time.Now().UTC().Format(time.RFC3339)
return fmt.Sprintf(ldflagsfmt, version(), gitCommit(), runtime.Version(), buildTime)
}
// version returns the latest git tag using git describe.
func version() string {
version, err := sh.Output("git", "describe", "--tags")
if err != nil {
version = "N/A"
}
return version
}
// gitCommit returns the current git commit
func gitCommit() string {
commit, err := sh.Output("git", "rev-parse", "--short", "HEAD")
if err != nil {
commit = "N/A"
}
return commit
}