/
scoop.go
202 lines (176 loc) · 5.94 KB
/
scoop.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
// Package scoop provides a Pipe that generates a scoop.sh App Manifest and pushes it to a bucket
package scoop
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"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/context"
)
// ErrNoWindows when there is no build for windows (goos doesn't contain windows)
var ErrNoWindows = errors.New("scoop requires a windows build")
// ErrTokenTypeNotImplementedForScoop indicates that a new token type was not implemented for this pipe
var ErrTokenTypeNotImplementedForScoop = errors.New("token type not implemented for scoop pipe")
// Pipe for build
type Pipe struct{}
func (Pipe) String() string {
return "scoop manifest"
}
// Publish scoop manifest
func (Pipe) Publish(ctx *context.Context) error {
client, err := client.New(ctx)
if err != nil {
return err
}
return doRun(ctx, client)
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Scoop.Name == "" {
ctx.Config.Scoop.Name = ctx.Config.ProjectName
}
if ctx.Config.Scoop.CommitAuthor.Name == "" {
ctx.Config.Scoop.CommitAuthor.Name = "goreleaserbot"
}
if ctx.Config.Scoop.CommitAuthor.Email == "" {
ctx.Config.Scoop.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}
return nil
}
func doRun(ctx *context.Context, client client.Client) error {
if ctx.Config.Scoop.Bucket.Name == "" {
return pipe.Skip("scoop section is not configured")
}
// TODO mavogel: in another PR
// check if release pipe is not configured!
// if ctx.Config.Release.Disable {
// }
// TODO: multiple archives
if ctx.Config.Archives[0].Format == "binary" {
return pipe.Skip("archive format is binary")
}
var archives = ctx.Artifacts.Filter(
artifact.And(
artifact.ByGoos("windows"),
artifact.ByType(artifact.UploadableArchive),
),
).List()
if len(archives) == 0 {
return ErrNoWindows
}
var path = ctx.Config.Scoop.Name + ".json"
content, err := buildManifest(ctx, archives)
if err != nil {
return err
}
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
if strings.TrimSpace(ctx.Config.Scoop.SkipUpload) == "true" {
return pipe.Skip("scoop.skip_upload is true")
}
if strings.TrimSpace(ctx.Config.Scoop.SkipUpload) == "auto" && ctx.Semver.Prerelease != "" {
return pipe.Skip("release is prerelease")
}
if ctx.Config.Release.Draft {
return pipe.Skip("release is marked as draft")
}
if ctx.Config.Release.Disable {
return pipe.Skip("release is disabled")
}
return client.CreateFile(
ctx,
ctx.Config.Scoop.CommitAuthor,
ctx.Config.Scoop.Bucket,
content.Bytes(),
path,
fmt.Sprintf("Scoop update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag),
)
}
// Manifest represents a scoop.sh App Manifest, more info:
// https://github.com/lukesampson/scoop/wiki/App-Manifests
type Manifest struct {
Version string `json:"version"` // The version of the app that this manifest installs.
Architecture map[string]Resource `json:"architecture"` // `architecture`: If the app has 32- and 64-bit versions, architecture can be used to wrap the differences.
Homepage string `json:"homepage,omitempty"` // `homepage`: The home page for the program.
License string `json:"license,omitempty"` // `license`: The software license for the program. For well-known licenses, this will be a string like "MIT" or "GPL2". For custom licenses, this should be the URL of the license.
Description string `json:"description,omitempty"` // Description of the app
Persist []string `json:"persist,omitempty"` // Persist data between updates
}
// Resource represents a combination of a url and a binary name for an architecture
type Resource struct {
URL string `json:"url"` // URL to the archive
Bin []string `json:"bin"` // name of binary inside the archive
Hash string `json:"hash"` // the archive checksum
}
func buildManifest(ctx *context.Context, artifacts []*artifact.Artifact) (bytes.Buffer, error) {
var result bytes.Buffer
var manifest = Manifest{
Version: ctx.Version,
Architecture: map[string]Resource{},
Homepage: ctx.Config.Scoop.Homepage,
License: ctx.Config.Scoop.License,
Description: ctx.Config.Scoop.Description,
Persist: ctx.Config.Scoop.Persist,
}
if ctx.Config.Scoop.URLTemplate == "" {
switch ctx.TokenType {
case context.TokenTypeGitHub:
ctx.Config.Scoop.URLTemplate = fmt.Sprintf(
"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
ctx.Config.GitHubURLs.Download,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
)
case context.TokenTypeGitLab:
ctx.Config.Scoop.URLTemplate = fmt.Sprintf(
"%s/%s/%s/uploads/{{ .ArtifactUploadHash }}/{{ .ArtifactName }}",
ctx.Config.GitLabURLs.Download,
ctx.Config.Release.GitLab.Owner,
ctx.Config.Release.GitLab.Name,
)
default:
return result, ErrTokenTypeNotImplementedForScoop
}
}
for _, artifact := range artifacts {
var arch = "64bit"
if artifact.Goarch == "386" {
arch = "32bit"
}
url, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(ctx.Config.Scoop.URLTemplate)
if err != nil {
return result, err
}
sum, err := artifact.Checksum("sha256")
if err != nil {
return result, err
}
manifest.Architecture[arch] = Resource{
URL: url,
Bin: binaries(artifact),
Hash: sum,
}
}
data, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return result, err
}
_, err = result.Write(data)
return result, err
}
func binaries(a *artifact.Artifact) []string {
// nolint: prealloc
var bins []string
for _, b := range a.ExtraOr("Builds", []*artifact.Artifact{}).([]*artifact.Artifact) {
bins = append(bins, b.ExtraOr("Binary", "").(string)+".exe")
}
return bins
}