-
Notifications
You must be signed in to change notification settings - Fork 10
/
magefile.go
191 lines (157 loc) · 4.18 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//go:build mage
// +build mage
package main
import (
"ahkpm/src/cmd"
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/sh"
"github.com/princjef/mageutil/bintool"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var linter = bintool.Must(bintool.New(
"golangci-lint{{.BinExt}}",
"1.49.0",
"https://github.com/golangci/golangci-lint/releases/download/v{{.Version}}/golangci-lint-{{.Version}}-{{.GOOS}}-{{.GOARCH}}{{.ArchiveExt}}",
))
func Lint() error {
if err := linter.Ensure(); err != nil {
return err
}
return linter.Command("run --timeout=5m").Run()
}
func Test() error {
return sh.RunV(
"go", "test",
"-covermode", "atomic",
"-coverprofile", "coverage.out",
"-timeout", "30s",
"-coverpkg", "./...",
"-cover",
"./...",
)
}
func Build() error {
return sh.Run("go", "build", "-o", "bin/ahkpm.exe", "./src")
}
func BuildWithVersion(version string) error {
err := os.WriteFile("src/constants/ahkpm-version.txt", []byte(version), 0644)
if err != nil {
return err
}
return Build()
}
func Verify() error {
if err := Lint(); err != nil {
return err
}
if err := Test(); err != nil {
return err
}
if err := Build(); err != nil {
return err
}
return nil
}
// This command requires both Wix and go-msi to be installed on the system.
// GitHub actions has Wix installed by default, but go-msi must be installed.
func Msi(version string) error {
return sh.Run("go-msi", "make", "--src", "wix-templates", "--msi", "bin/ahkpm-"+version+".msi", "--version", version)
}
func VerifyVersion(version string) error {
out, err := sh.Output("bin/ahkpm.exe", "--version")
if err != nil {
return err
}
if !strings.Contains(out, version) {
return errors.New("Version mismatch.\n Expected: " + version + "\n Actual: " + out)
}
return nil
}
func BuildRelease(version string) error {
if err := BuildWithVersion(version); err != nil {
return err
}
if err := VerifyVersion(version); err != nil {
return err
}
if err := Msi(version); err != nil {
return err
}
return nil
}
func MarkdownDocs(dir string) error {
return generateAhkpmMarkdownTree(cmd.RootCmd, dir)
}
func generateAhkpmMarkdownTree(cmd *cobra.Command, dir string) error {
md := generateMarkdownDocs(cmd)
basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".md"
filename := filepath.Join(dir, basename)
err := os.WriteFile(filename, []byte(md), 0644)
if err != nil {
return err
}
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
err := generateAhkpmMarkdownTree(c, dir)
if err != nil {
return err
}
}
return nil
}
func generateMarkdownDocs(cmd *cobra.Command) string {
buf := new(bytes.Buffer)
name := cmd.CommandPath()
yamlFrontMatterTemplate := `---
title: %s
description: %s
lead: %s
menu:
docs:
parent: "commands"
toc: true
---
`
buf.WriteString(fmt.Sprintf(yamlFrontMatterTemplate, name, cmd.Short, cmd.Short))
if (cmd.Long != "") && (cmd.Long != cmd.Short) {
buf.WriteString("## Synopsis\n\n")
buf.WriteString(cmd.Long + "\n\n")
}
buf.WriteString("## Usage\n\n")
buf.WriteString("```text\n")
if cmd.HasAvailableSubCommands() {
buf.WriteString(cmd.CommandPath() + " [command]\n")
}
buf.WriteString(cmd.UseLine() + "\n")
buf.WriteString("```\n\n")
if len(cmd.Example) > 0 {
buf.WriteString("## Examples\n\n")
buf.WriteString(fmt.Sprintf("```text\n%s\n```\n\n", cmd.Example))
}
if cmd.HasAvailableSubCommands() {
buf.WriteString("## Available subcommands\n\n")
for _, c := range cmd.Commands() {
buf.WriteString(fmt.Sprintf("- `%s`: %s\n", c.Name(), c.Short))
}
buf.WriteString("\n")
}
if cmd.HasAvailableFlags() {
buf.WriteString("## Options\n\n")
cmd.Flags().VisitAll(func(f *pflag.Flag) {
buf.WriteString(fmt.Sprintf("- `--%s`, `-%s`: %s\n", f.Name, f.Shorthand, f.Usage))
})
}
if len(cmd.Aliases) > 0 {
buf.WriteString("## Aliases\n\n")
buf.WriteString(fmt.Sprintf("`%s`\n\n", strings.Join(cmd.Aliases, "`, `")))
}
return buf.String()
}