summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 70481654bc16fc6713a118b9edc56efcc45bb022 (plain)
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

//go:build mage

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/magefile/mage/sh"
)

// Project description for build output
const binaryName = "gt"

// Default is the default target when no target is specified.
var Default = Build

// Build builds the gt binary.
func Build() error {
	fmt.Println("Building gt...")
	return sh.RunV("go", "build", "-o", binaryName, "./cmd/gt")
}

// Run runs the gt binary.
func Run() error {
	return sh.RunV("go", "run", "./cmd/gt")
}

// 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/...")
}

// RPN runs tests for the RPN package (alias for TestRPN).
func RPN() error {
	return TestRPN()
}

// Install installs the gt binary to GOBIN (or GOPATH/bin if GOBIN is not set).
func Install() error {
	fmt.Println("Installing gt...")
	return sh.RunV("go", "install", "./cmd/gt")
}

// Lint runs golangci-lint for code quality checks.
func Lint() error {
	fmt.Println("Running golangci-lint...")
	return sh.RunV("golangci-lint", "run", "./...")
}

// Release builds and packages the release for all platforms.
func Release() error {
	fmt.Println("Creating release...")
	fmt.Println("Note: Requires goreleaser to be installed and configured.")
	return sh.RunV("goreleaser", "release", "--clean")
}

// Repl starts the REPL mode.
func Repl() error {
	return sh.RunV("go", "run", "./cmd/gt", "--repl")
}

// Uninstall removes the gt binary from GOBIN (or GOPATH/bin if GOBIN is not set).
func Uninstall() error {
	fmt.Println("Uninstalling gt...")
	// Use the same logic as go install to determine the binary location
	// go install installs to GOBIN or GOPATH/bin (GOBIN takes precedence)
	gobin := os.Getenv("GOBIN")
	if gobin == "" {
		gobin = filepath.Join(getGOPATH(), "bin")
	}
	binPath := filepath.Join(gobin, 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
}