-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
166 lines (143 loc) · 5.23 KB
/
magefile.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//go:build mage
// +build mage
// ⚡ Core Mage Tasks
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
"time"
"github.com/hako/durafmt"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pterm/pterm"
"github.com/sheldonhull/magetools/ci"
"github.com/sheldonhull/magetools/fancy"
// mage:import
_ "github.com/sheldonhull/magetools/docgen"
// mage:import
"github.com/sheldonhull/magetools/gotools"
)
// Install is for installation commands to be grouped.
type Install mg.Namespace
// artifactDirectory is a directory containing artifacts for the project and shouldn't be committed to source.
const artifactDirectory = ".artifacts"
// cacheDirectory is a directory containing other artifacts for development that should persist between builds, such as temporary configs or testing charts.
const cacheDirectory = ".cache"
const permissionUserReadWriteExecute = 0o0777
// 📁 createDirectories creates the local working directories for build artifacts and tooling.
func createDirectories() error {
for _, dir := range []string{artifactDirectory, cacheDirectory} {
if err := os.MkdirAll(dir, permissionUserReadWriteExecute); err != nil {
pterm.Error.Printf("failed to create dir: [%s] with error: %v\n", dir, err)
return err
}
pterm.Success.Printf("✅ [%s] dir created\n", dir)
}
return nil
}
// ⚡ Init runs multiple tasks to initialize all the requirements for running a project for a new contributor.
func Init() {
fancy.IntroScreen(ci.IsCI())
pterm.Success.Println("running Init()...")
mg.SerialDeps(
Clean,
createDirectories,
)
pterm.DefaultSection.Println("CI Tooling")
if ci.IsCI() {
pterm.Success.Println("done with CI specific tooling. since detected in CI context, ending init early as core requirements met")
return
}
mg.SerialDeps(
(gotools.Go{}.Tidy),
(gotools.Go{}.Init),
)
pterm.DefaultSection.Println("Setup Project Specific Tools")
// Aqua install is run in devcontainer/codespace automatically.
// Might require setup outside of this project and then this will work.
if err := sh.RunV("aqua", "install"); err != nil {
pterm.Warning.Printfln("aqua install not successful.\n" +
"This is optional, but will ensure every tool for the project is installed and matching version." +
"To install see developer docs or go to https://aquaproj.github.io/docs/reference/install")
}
}
// 🗑️ Clean up after yourself.
func Clean() {
pterm.Success.Println("Cleaning...")
for _, dir := range []string{artifactDirectory, cacheDirectory} {
err := os.RemoveAll(dir)
if err != nil {
pterm.Error.Printf("failed to removeall: [%s] with error: %v\n", dir, err)
}
pterm.Success.Printf("🧹 [%s] dir removed\n", dir)
}
mg.Deps(createDirectories)
}
// Release using github cli (for now)
func Release() error {
version, changelogFile, err := getVersion()
if err != nil {
pterm.Error.Printfln("failed to get version: %v", err)
return err
}
return sh.Run("gh", "release", "create", version, "--title", version, "--notes-file", changelogFile, "--target", "main")
}
// getVersion returns the version and path for the changefile to use for the semver and release notes.
func getVersion() (releaseVersion, cleanPath string, err error) {
releaseVersion, err = sh.Output("changie", "latest")
if err != nil {
pterm.Error.Printfln("changie pulling latest release note version failure: %v", err)
return "", "", err
}
cleanVersion := strings.TrimSpace(releaseVersion)
cleanPath = filepath.Join(".changes", cleanVersion+".md")
if os.Getenv("GITHUB_WORKSPACE") != "" {
cleanPath = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".changes", cleanVersion+".md")
}
return cleanVersion, cleanPath, nil
}
// ⚙ Run builds the binary into the local artifact direction and launches for testing.
func Run() error {
start := time.Now()
defer func() {
duration := durafmt.Parse(time.Since(start))
pterm.Success.Printfln("✅ Run() took %s", duration)
}()
targetBuildFile := filepath.Join(artifactDirectory, "az-pr")
if err := sh.RunV("go", "build", "-o", targetBuildFile, "main.go"); err != nil {
return err
}
return sh.RunV(targetBuildFile, "shell", "--debug")
}
// 📦 Install the tool from remote.
// This can help catch odd issues with embed for example.
func (Install) Remote() error {
return sh.RunV("go", "install", "github.com/sheldonhull/az-pr@latest")
}
// 📦 Local Install
// Build and install to GOPATH/bin to run locally and make sure everything works great.
func (Install) Local() error {
start := time.Now()
defer func() {
duration := durafmt.Parse(time.Since(start))
pterm.Success.Printfln("✅ Run() took %s", duration)
}()
targetBuildFile := filepath.Join(artifactDirectory, "az-pr")
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
targetLocalBinaryPath := filepath.Join(gopath, "bin", "az-pr")
if err := sh.RunV("go", "build", "-o", targetBuildFile, "main.go"); err != nil {
return fmt.Errorf("failed to build: %w", err)
}
pterm.Info.Printfln("binary: %s", targetBuildFile)
if err := sh.Copy(targetLocalBinaryPath, targetBuildFile); err != nil {
return fmt.Errorf("failed to cp %s %s to: %w", targetBuildFile, targetLocalBinaryPath, err)
}
pterm.Success.Printfln("✅ az-pr installed to: %s", targetLocalBinaryPath)
return nil
}