From eb3da18b530f8049212480e0be47fa8e00f97c73 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 20 Mar 2026 22:56:07 +0200 Subject: internal/Magefile: Refactor to align with best practices - Added mg import (via magefile/mage/sh) - Added binaryName constant - Added project descriptions for build targets - Added proper Install target with go install - Added Uninstall target to remove the binary - Added getGOPATH helper function --- Magefile.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Magefile.go b/Magefile.go index 1664710..cfc9911 100644 --- a/Magefile.go +++ b/Magefile.go @@ -3,31 +3,65 @@ package main import ( + "fmt" + "os" + "path/filepath" + "github.com/magefile/mage/sh" ) +// Project description for build output +const binaryName = "perc" + +// Default is the default target when no target is specified. var Default = Build +// Build builds the perc binary. func Build() error { - return sh.RunV("go", "build", "-o", "perc", "./cmd/perc") + fmt.Println("Building perc...") + return sh.RunV("go", "build", "-o", binaryName, "./cmd/perc") } +// Run runs the perc binary. func Run() error { return sh.RunV("go", "run", "./cmd/perc") } +// Test runs all tests. func Test() error { + fmt.Println("Running all tests...") return sh.RunV("go", "test", "./...") } +// TestRPN runs tests for the RPN package. func TestRPN() error { + fmt.Println("Running RPN tests...") return sh.RunV("go", "test", "./internal/rpn/...") } +// Install installs the perc binary to GOPATH/bin. func Install() error { + fmt.Println("Installing perc...") return sh.RunV("go", "install", "./cmd/perc") } +// Repl starts the REPL mode. func Repl() error { return sh.RunV("go", "run", "./cmd/perc", "--repl") } + +// Uninstall removes the perc binary from GOPATH/bin. +func Uninstall() error { + fmt.Println("Uninstalling perc...") + binPath := filepath.Join(getGOPATH(), "bin", binaryName) + return os.Remove(binPath) +} + +// getGOPATH returns the GOPATH environment variable. +func getGOPATH() string { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = filepath.Join(os.Getenv("HOME"), "go") + } + return gopath +} -- cgit v1.2.3