-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmd_new.go
222 lines (207 loc) · 5.09 KB
/
cmd_new.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package maltmill
import (
"context"
"fmt"
"io"
"log"
"os"
"path"
"regexp"
"strings"
"text/template"
"github.com/Masterminds/semver"
"github.com/google/go-github/github"
"github.com/pkg/errors"
)
type cmdNew struct {
writer io.Writer
slug string
overwrite bool
outFile string
ghcli *github.Client
}
var _ runner = (*cmdNew)(nil)
var tmpl = `class {{.CapitalizedName}} < Formula
{{- if .Desc }}
desc '{{.Desc}}'
{{- end }}
version '{{.Version}}'
homepage 'https://github.com/{{.Owner}}/{{.Repo}}'
{{ if or (ne .Downloads.DarwinAmd64 nil) (ne .Downloads.DarwinArm64 nil) }}
on_macos do
{{- if .Downloads.DarwinArm64 }}
if Hardware::CPU.arm?
url '{{.Downloads.DarwinArm64.URL}}'
sha256 '{{.Downloads.DarwinArm64.SHA256}}'
end
{{- end }}
{{- if .Downloads.DarwinAmd64 }}
if Hardware::CPU.intel?
url '{{.Downloads.DarwinAmd64.URL}}'
sha256 '{{.Downloads.DarwinAmd64.SHA256}}'
end
{{- end }}
end
{{ end -}}
{{ if or (ne .Downloads.LinuxAmd64 nil) (ne .Downloads.LinuxArm64 nil) }}
on_linux do
{{- if .Downloads.LinuxArm64 }}
if Hardware::CPU.arm? && Hardware::CPU.is_64_bit?
url '{{.Downloads.LinuxArm64.URL}}'
sha256 '{{.Downloads.LinuxArm64.SHA256}}'
end
{{- end }}
{{- if .Downloads.LinuxAmd64 }}
if Hardware::CPU.intel?
url '{{.Downloads.LinuxAmd64.URL}}'
sha256 '{{.Downloads.LinuxAmd64.SHA256}}'
end
{{- end }}
end
{{ end }}
head do
url 'https://github.com/{{.Owner}}/{{.Repo}}.git'
depends_on 'go' => :build
end
def install
if build.head?
system 'make', 'build'
end
bin.install '{{.Name}}'
end
end
`
type formulaData struct {
Name, CapitalizedName string
Version string
Owner, Repo string
Desc string
Downloads formulaDataDownloads
}
type formulaDataDownloads struct {
DarwinAmd64 *formulaDownload
DarwinArm64 *formulaDownload
LinuxAmd64 *formulaDownload
LinuxArm64 *formulaDownload
}
type formulaDownload struct {
URL string
SHA256 string
OS string
Arch string
}
var formulaTmpl = template.Must(template.New("formulaTmpl").Parse(tmpl))
var osNameRe = regexp.MustCompile("(darwin|linux)")
func getDownloads(assets []github.ReleaseAsset) ([]formulaDownload, error) {
var downloads []formulaDownload
for _, asset := range assets {
u := asset.GetBrowserDownloadURL()
fname := path.Base(u)
arch, ok := detectArch(fname)
if !ok {
continue
}
osName := osNameRe.FindString(fname)
if osName == "" {
continue
}
digest, err := getSHA256FromURL(u)
if err != nil {
return nil, err
}
downloads = append(downloads, formulaDownload{
URL: u,
SHA256: digest,
OS: osName,
Arch: arch,
})
}
if len(downloads) == 0 {
return nil, errors.New("no assets found")
}
return downloads, nil
}
func detectArch(in string) (string, bool) {
archs := []string{"amd64", "arm64"}
for _, a := range archs {
if strings.Contains(in, a) {
return a, true
}
}
return "", false
}
func (cr *cmdNew) run(ctx context.Context) (err error) {
ownerAndRepo := strings.Split(cr.slug, "/")
if len(ownerAndRepo) != 2 {
return errors.Errorf("invalid slug: %s", cr.slug)
}
repoAndVer := strings.Split(ownerAndRepo[1], "@")
var tag string
if len(repoAndVer) > 1 {
tag = repoAndVer[1]
}
nf := &formulaData{
Owner: ownerAndRepo[0],
Repo: repoAndVer[0],
Name: repoAndVer[0],
CapitalizedName: strings.Replace(strings.Title(repoAndVer[0]), "-", "", -1),
Downloads: formulaDataDownloads{},
}
repo, resp, err := cr.ghcli.Repositories.Get(ctx, nf.Owner, nf.Repo)
if err != nil {
return errors.Wrapf(err, "create new formula failed")
}
nf.Desc = repo.GetDescription()
resp.Body.Close()
rele, resp, err := func(ctx context.Context) (*github.RepositoryRelease, *github.Response, error) {
if tag == "" {
return cr.ghcli.Repositories.GetLatestRelease(ctx, nf.Owner, nf.Repo)
}
return cr.ghcli.Repositories.GetReleaseByTag(ctx, nf.Owner, nf.Repo, tag)
}(ctx)
if err != nil {
return errors.Wrapf(err, "create new formula failed")
}
resp.Body.Close()
ver, err := semver.NewVersion(rele.GetTagName())
if err != nil {
return errors.Wrapf(err, "invalid tag name: %s", rele.GetTagName())
}
nf.Version = fmt.Sprintf("%d.%d.%d", ver.Major(), ver.Minor(), ver.Patch())
downloads, err := getDownloads(rele.Assets)
if err != nil {
return err
}
for _, d := range downloads {
dd := d
switch {
case d.OS == "darwin" && d.Arch == "amd64":
nf.Downloads.DarwinAmd64 = &dd
case d.OS == "darwin" && d.Arch == "arm64":
nf.Downloads.DarwinArm64 = &dd
case d.OS == "linux" && d.Arch == "amd64":
nf.Downloads.LinuxAmd64 = &dd
case d.OS == "linux" && d.Arch == "arm64":
nf.Downloads.LinuxArm64 = &dd
}
}
var wtr = cr.writer
if cr.overwrite || cr.outFile != "" {
fname := cr.outFile
if fname == "" {
fname = nf.Name + ".rb"
}
defer func() {
if err == nil {
log.Printf("created %q\n", fname)
}
}()
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
wtr = f
}
return formulaTmpl.Execute(wtr, nf)
}