//go:build mage package main import ( "fmt" "os" "path/filepath" "runtime" "time" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" ) const binaryName = "csv2excel" func ldflags() (string, error) { version, err := sh.Output("git", "describe", "--tags", "--always") if err != nil { version = "dev" } commit, err := sh.Output("git", "rev-parse", "--short", "HEAD") if err != nil { commit = "none" } buildDate := time.Now().UTC().Format(time.RFC3339) return fmt.Sprintf( `-X code.beautifulmachines.dev/jakoubek/csv2excel/internal/version.Version=%s -X code.beautifulmachines.dev/jakoubek/csv2excel/internal/version.Commit=%s -X code.beautifulmachines.dev/jakoubek/csv2excel/internal/version.BuildDate=%s`, version, commit, buildDate, ), nil } // Build builds the binary for the current platform. func Build() error { switch runtime.GOOS { case "windows": return BuildWindows() default: return BuildLinux() } } // BuildLinux builds a static binary for Linux amd64. func BuildLinux() error { mg.Deps(ensureDistDir) flags, err := ldflags() if err != nil { return err } out := filepath.Join("dist", binaryName) env := map[string]string{ "GOOS": "linux", "GOARCH": "amd64", "CGO_ENABLED": "0", } return sh.RunWithV(env, "go", "build", "-ldflags", flags, "-o", out, ".") } // BuildWindows builds a binary for Windows amd64. func BuildWindows() error { mg.Deps(ensureDistDir) flags, err := ldflags() if err != nil { return err } out := filepath.Join("dist", binaryName+".exe") env := map[string]string{ "GOOS": "windows", "GOARCH": "amd64", "CGO_ENABLED": "0", } return sh.RunWithV(env, "go", "build", "-ldflags", flags, "-o", out, ".") } // Install installs the binary to $GOBIN or $GOPATH/bin. func Install() error { mg.Deps(Build) fmt.Println("Installing", binaryName, "...") flags, err := ldflags() if err != nil { return err } err = sh.RunV("go", "install", "-ldflags", flags, ".") if err != nil { return err } fmt.Println("Done.") return nil } // Clean removes the dist/ directory. func Clean() error { return sh.Rm("dist") } func ensureDistDir() error { return os.MkdirAll("dist", 0o755) }