forked from goreleaser/goreleaser
/
chocolatey.go
340 lines (275 loc) · 7.75 KB
/
chocolatey.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package chocolatey
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
// nuget package extension.
const nupkgFormat = "nupkg"
// custom chocolatey config placed in artifact.
const chocoConfigExtra = "ChocolateyConfig"
// cmd represents a command executor.
var cmd cmder = stdCmd{}
// Pipe for chocolatey packaging.
type Pipe struct{}
func (Pipe) String() string { return "chocolatey packages" }
func (Pipe) Skip(ctx *context.Context) bool { return len(ctx.Config.Chocolateys) == 0 }
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
for i := range ctx.Config.Chocolateys {
choco := &ctx.Config.Chocolateys[i]
if choco.Name == "" {
choco.Name = ctx.Config.ProjectName
}
if choco.Title == "" {
choco.Title = ctx.Config.ProjectName
}
if choco.Goamd64 == "" {
choco.Goamd64 = "v1"
}
if choco.SourceRepo == "" {
choco.SourceRepo = "https://push.chocolatey.org/"
}
}
return nil
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
client, err := client.New(ctx)
if err != nil {
return err
}
for _, choco := range ctx.Config.Chocolateys {
if err := doRun(ctx, client, choco); err != nil {
return err
}
}
return nil
}
// Publish packages.
func (Pipe) Publish(ctx *context.Context) error {
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
artifacts := ctx.Artifacts.Filter(
artifact.ByType(artifact.PublishableChocolatey),
).List()
for _, artifact := range artifacts {
if err := doPush(ctx, artifact); err != nil {
return err
}
}
return nil
}
func doRun(ctx *context.Context, cl client.Client, choco config.Chocolatey) error {
filters := []artifact.Filter{
artifact.ByGoos("windows"),
artifact.ByType(artifact.UploadableArchive),
artifact.Or(
artifact.And(
artifact.ByGoarch("amd64"),
artifact.ByGoamd64(choco.Goamd64),
),
artifact.ByGoarch("386"),
),
}
if len(choco.IDs) > 0 {
filters = append(filters, artifact.ByIDs(choco.IDs...))
}
artifacts := ctx.Artifacts.
Filter(artifact.And(filters...)).
List()
if len(artifacts) == 0 {
return errors.New("chocolatey requires a windows build and archive")
}
// folderDir is the directory that then will be compressed to make the
// chocolatey package.
folderPath := filepath.Join(ctx.Config.Dist, choco.Name+".choco")
toolsPath := filepath.Join(folderPath, "tools")
if err := os.MkdirAll(toolsPath, 0o755); err != nil {
return err
}
nuspecFile := filepath.Join(folderPath, choco.Name+".nuspec")
nuspec, err := buildNuspec(ctx, choco)
if err != nil {
return err
}
if err = os.WriteFile(nuspecFile, nuspec, 0o644); err != nil {
return err
}
data, err := dataFor(ctx, cl, choco, artifacts)
if err != nil {
return err
}
script, err := buildTemplate(choco.Name, scriptTemplate, data)
if err != nil {
return err
}
scriptFile := filepath.Join(toolsPath, "chocolateyinstall.ps1")
log.WithField("file", scriptFile).Debug("creating")
if err = os.WriteFile(scriptFile, script, 0o644); err != nil {
return err
}
log.WithField("nuspec", nuspecFile).Info("packing")
out, err := cmd.Exec(ctx, "choco", "pack", nuspecFile, "--out", ctx.Config.Dist)
if err != nil {
return fmt.Errorf("failed to generate chocolatey package: %w: %s", err, string(out))
}
if choco.SkipPublish {
return nil
}
pkgFile := fmt.Sprintf("%s.%s.%s", choco.Name, ctx.Version, nupkgFormat)
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.PublishableChocolatey,
Path: filepath.Join(ctx.Config.Dist, pkgFile),
Name: pkgFile,
Extra: map[string]interface{}{
artifact.ExtraFormat: nupkgFormat,
chocoConfigExtra: choco,
},
})
return nil
}
func doPush(ctx *context.Context, art *artifact.Artifact) error {
choco, err := artifact.Extra[config.Chocolatey](*art, chocoConfigExtra)
if err != nil {
return err
}
key, err := tmpl.New(ctx).Apply(choco.APIKey)
if err != nil {
return err
}
log := log.WithField("name", choco.Name)
if key == "" {
log.Warn("skip pushing: no api key")
return nil
}
log.Info("pushing package")
args := []string{
"push",
"--source",
choco.SourceRepo,
"--api-key",
key,
art.Path,
}
if out, err := cmd.Exec(ctx, "choco", args...); err != nil {
return fmt.Errorf("failed to push chocolatey package: %w: %s", err, string(out))
}
log.Info("package sent")
return nil
}
func buildNuspec(ctx *context.Context, choco config.Chocolatey) ([]byte, error) {
tpl := tmpl.New(ctx)
summary, err := tpl.Apply(choco.Summary)
if err != nil {
return nil, err
}
description, err := tpl.Apply(choco.Description)
if err != nil {
return nil, err
}
releaseNotes, err := tpl.Apply(choco.ReleaseNotes)
if err != nil {
return nil, err
}
m := &Nuspec{
Xmlns: schema,
Metadata: Metadata{
ID: choco.Name,
Version: ctx.Version,
PackageSourceURL: choco.PackageSourceURL,
Owners: choco.Owners,
Title: choco.Title,
Authors: choco.Authors,
ProjectURL: choco.ProjectURL,
IconURL: choco.IconURL,
Copyright: choco.Copyright,
LicenseURL: choco.LicenseURL,
RequireLicenseAcceptance: choco.RequireLicenseAcceptance,
ProjectSourceURL: choco.ProjectSourceURL,
DocsURL: choco.DocsURL,
BugTrackerURL: choco.BugTrackerURL,
Tags: choco.Tags,
Summary: summary,
Description: description,
ReleaseNotes: releaseNotes,
},
Files: Files{File: []File{
{Source: "tools\\**", Target: "tools"},
}},
}
deps := make([]Dependency, len(choco.Dependencies))
for i, dep := range choco.Dependencies {
deps[i] = Dependency{ID: dep.ID, Version: dep.Version}
}
if len(deps) > 0 {
m.Metadata.Dependencies = &Dependencies{Dependency: deps}
}
return m.Bytes()
}
func buildTemplate(name string, text string, data templateData) ([]byte, error) {
tp, err := template.New(name).Parse(text)
if err != nil {
return nil, err
}
var out bytes.Buffer
if err = tp.Execute(&out, data); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func dataFor(ctx *context.Context, cl client.Client, choco config.Chocolatey, artifacts []*artifact.Artifact) (templateData, error) {
result := templateData{}
if choco.URLTemplate == "" {
url, err := cl.ReleaseURLTemplate(ctx)
if err != nil {
return result, err
}
choco.URLTemplate = url
}
for _, artifact := range artifacts {
sum, err := artifact.Checksum("sha256")
if err != nil {
return result, err
}
url, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(choco.URLTemplate)
if err != nil {
return result, err
}
pkg := releasePackage{
DownloadURL: url,
Checksum: sum,
Arch: artifact.Goarch,
}
result.Packages = append(result.Packages, pkg)
}
return result, nil
}
// cmder is a special interface to execute external commands.
//
// The intention is to be used to wrap the standard exec and provide the
// ability to create a fake one for testing.
type cmder interface {
// Exec executes an command.
Exec(*context.Context, string, ...string) ([]byte, error)
}
// stdCmd uses the standard golang exec.
type stdCmd struct{}
var _ cmder = &stdCmd{}
func (stdCmd) Exec(ctx *context.Context, name string, args ...string) ([]byte, error) {
return exec.CommandContext(ctx, name, args...).CombinedOutput()
}