2023-06-14 00:13:21 -03:00
// Package scoop provides a Pipe that generates a scoop.sh App Manifest and pushes it to a bucket.
2018-02-10 12:38:07 +00:00
package scoop
import (
"bytes"
"encoding/json"
2021-05-24 13:30:31 -04:00
"fmt"
"os"
2021-06-26 17:14:42 +00:00
"path"
2020-04-21 16:11:18 -03:00
"path/filepath"
2020-02-05 19:05:43 -06:00
"strings"
2018-02-10 12:38:07 +00:00
2022-06-21 21:11:15 -03:00
"github.com/caarlos0/log"
2024-05-26 15:02:57 -03:00
"github.com/goreleaser/goreleaser/v2/internal/artifact"
"github.com/goreleaser/goreleaser/v2/internal/client"
"github.com/goreleaser/goreleaser/v2/internal/commitauthor"
"github.com/goreleaser/goreleaser/v2/internal/pipe"
"github.com/goreleaser/goreleaser/v2/internal/skips"
"github.com/goreleaser/goreleaser/v2/internal/tmpl"
"github.com/goreleaser/goreleaser/v2/pkg/config"
"github.com/goreleaser/goreleaser/v2/pkg/context"
2018-02-10 12:38:07 +00:00
)
2023-04-30 21:29:36 -03:00
// ErrIncorrectArchiveCount happens when a given filter evaluates 0 or more
// than 1 archives.
type ErrIncorrectArchiveCount struct {
goamd64 string
ids [ ] string
archives [ ] * artifact . Artifact
2023-03-29 10:18:40 -03:00
}
2022-12-21 01:07:07 -03:00
2023-04-30 21:29:36 -03:00
func ( e ErrIncorrectArchiveCount ) Error ( ) string {
b := strings . Builder { }
_ , _ = b . WriteString ( "scoop requires a single windows archive, " )
if len ( e . archives ) == 0 {
_ , _ = b . WriteString ( "but no archives " )
} else {
_ , _ = b . WriteString ( fmt . Sprintf ( "but found %d archives " , len ( e . archives ) ) )
}
2023-07-17 21:26:18 -03:00
_ , _ = b . WriteString ( fmt . Sprintf ( "matching the given filters: goos=windows goarch=[386 amd64 arm64] goamd64=%s ids=%s" , e . goamd64 , e . ids ) )
2023-04-30 21:29:36 -03:00
if len ( e . archives ) > 0 {
names := make ( [ ] string , 0 , len ( e . archives ) )
for _ , a := range e . archives {
names = append ( names , a . Name )
}
_ , _ = b . WriteString ( fmt . Sprintf ( ": %s" , names ) )
}
_ , _ = b . WriteString ( "\nLearn more at https://goreleaser.com/errors/scoop-archive\n" )
return b . String ( )
2023-03-29 10:18:40 -03:00
}
2018-02-10 12:38:07 +00:00
2021-09-18 10:21:29 -03:00
const scoopConfigExtra = "ScoopConfig"
// Pipe that builds and publishes scoop manifests.
2018-02-10 12:38:07 +00:00
type Pipe struct { }
2023-06-20 09:33:59 -03:00
func ( Pipe ) String ( ) string { return "scoop manifests" }
func ( Pipe ) ContinueOnError ( ) bool { return true }
2023-04-30 21:29:36 -03:00
func ( Pipe ) Skip ( ctx * context . Context ) bool {
2024-05-25 14:09:49 -03:00
return skips . Any ( ctx , skips . Scoop ) || len ( ctx . Config . Scoops ) == 0
2023-04-30 21:29:36 -03:00
}
2021-09-18 10:21:29 -03:00
// Run creates the scoop manifest locally.
func ( Pipe ) Run ( ctx * context . Context ) error {
feat: allow goreleaser to run in gerrit, soft-serve and others (#4271)
Currently, GoReleaser will assume you're running against github, gitea
or gitlab.
You could set `release.disable: true`, but it would still set and try to
use some defaults that could break things up.
Now, if you disable the release, goreleaser will not set these defaults.
It'll also hard error in some cases in which it would happily produce
invalid resources before, namely, if `release.disable` is set, and, for
example, `brews.url_template` is empty (in which case it would try to
use the one from the release, usually github).
closes #4208
2023-09-04 11:23:38 -03:00
cli , err := client . NewReleaseClient ( ctx )
2021-09-18 10:21:29 -03:00
if err != nil {
return err
}
feat: allow goreleaser to run in gerrit, soft-serve and others (#4271)
Currently, GoReleaser will assume you're running against github, gitea
or gitlab.
You could set `release.disable: true`, but it would still set and try to
use some defaults that could break things up.
Now, if you disable the release, goreleaser will not set these defaults.
It'll also hard error in some cases in which it would happily produce
invalid resources before, namely, if `release.disable` is set, and, for
example, `brews.url_template` is empty (in which case it would try to
use the one from the release, usually github).
closes #4208
2023-09-04 11:23:38 -03:00
return runAll ( ctx , cli )
2018-02-10 12:38:07 +00:00
}
2020-05-26 00:48:10 -03:00
// Publish scoop manifest.
2018-10-10 12:47:31 -03:00
func ( Pipe ) Publish ( ctx * context . Context ) error {
2019-06-29 16:02:40 +02:00
client , err := client . New ( ctx )
2018-02-10 12:38:07 +00:00
if err != nil {
return err
}
2023-04-30 21:29:36 -03:00
return publishAll ( ctx , client )
2018-02-10 12:38:07 +00:00
}
2020-05-26 00:48:10 -03:00
// Default sets the pipe defaults.
2018-02-10 12:38:07 +00:00
func ( Pipe ) Default ( ctx * context . Context ) error {
2023-04-30 21:29:36 -03:00
for i := range ctx . Config . Scoops {
scoop := & ctx . Config . Scoops [ i ]
if scoop . Name == "" {
scoop . Name = ctx . Config . ProjectName
}
scoop . CommitAuthor = commitauthor . Default ( scoop . CommitAuthor )
if scoop . CommitMessageTemplate == "" {
scoop . CommitMessageTemplate = "Scoop update for {{ .ProjectName }} version {{ .Tag }}"
}
if scoop . Goamd64 == "" {
scoop . Goamd64 = "v1"
}
2020-04-29 13:45:18 -07:00
}
2023-04-30 21:29:36 -03:00
return nil
}
feat: allow goreleaser to run in gerrit, soft-serve and others (#4271)
Currently, GoReleaser will assume you're running against github, gitea
or gitlab.
You could set `release.disable: true`, but it would still set and try to
use some defaults that could break things up.
Now, if you disable the release, goreleaser will not set these defaults.
It'll also hard error in some cases in which it would happily produce
invalid resources before, namely, if `release.disable` is set, and, for
example, `brews.url_template` is empty (in which case it would try to
use the one from the release, usually github).
closes #4208
2023-09-04 11:23:38 -03:00
func runAll ( ctx * context . Context , cl client . ReleaseURLTemplater ) error {
2023-04-30 21:29:36 -03:00
for _ , scoop := range ctx . Config . Scoops {
err := doRun ( ctx , scoop , cl )
if err != nil {
return err
}
2022-04-11 22:43:22 -03:00
}
2018-02-10 12:38:07 +00:00
return nil
}
feat: allow goreleaser to run in gerrit, soft-serve and others (#4271)
Currently, GoReleaser will assume you're running against github, gitea
or gitlab.
You could set `release.disable: true`, but it would still set and try to
use some defaults that could break things up.
Now, if you disable the release, goreleaser will not set these defaults.
It'll also hard error in some cases in which it would happily produce
invalid resources before, namely, if `release.disable` is set, and, for
example, `brews.url_template` is empty (in which case it would try to
use the one from the release, usually github).
closes #4208
2023-09-04 11:23:38 -03:00
func doRun ( ctx * context . Context , scoop config . Scoop , cl client . ReleaseURLTemplater ) error {
2023-04-30 21:29:36 -03:00
filters := [ ] artifact . Filter {
artifact . ByGoos ( "windows" ) ,
artifact . ByType ( artifact . UploadableArchive ) ,
artifact . Or (
artifact . And (
artifact . ByGoarch ( "amd64" ) ,
artifact . ByGoamd64 ( scoop . Goamd64 ) ,
2022-04-11 22:43:22 -03:00
) ,
2023-07-17 21:26:18 -03:00
artifact . ByGoarch ( "arm64" ) ,
2023-04-30 21:29:36 -03:00
artifact . ByGoarch ( "386" ) ,
2018-02-10 12:38:07 +00:00
) ,
2023-04-30 21:29:36 -03:00
}
if len ( scoop . IDs ) > 0 {
filters = append ( filters , artifact . ByIDs ( scoop . IDs ... ) )
}
filtered := ctx . Artifacts . Filter ( artifact . And ( filters ... ) )
archives := filtered . List ( )
for _ , platArchives := range filtered . GroupByPlatform ( ) {
// there might be multiple archives, but only of for each platform
if len ( platArchives ) != 1 {
return ErrIncorrectArchiveCount { scoop . Goamd64 , scoop . IDs , archives }
}
}
// handle no archives found whatsoever
2018-02-10 12:38:07 +00:00
if len ( archives ) == 0 {
2023-04-30 21:29:36 -03:00
return ErrIncorrectArchiveCount { scoop . Goamd64 , scoop . IDs , archives }
2018-02-10 12:38:07 +00:00
}
2023-06-30 19:46:53 +02:00
tp := tmpl . New ( ctx )
if err := tp . ApplyAll (
& scoop . Name ,
& scoop . Description ,
& scoop . Homepage ,
& scoop . SkipUpload ,
) ; err != nil {
2023-05-19 14:25:39 +00:00
return err
}
2023-05-30 13:41:24 -03:00
2023-06-14 00:13:21 -03:00
ref , err := client . TemplateRef ( tmpl . New ( ctx ) . Apply , scoop . Repository )
2023-05-19 14:25:39 +00:00
if err != nil {
return err
}
2023-06-14 00:13:21 -03:00
scoop . Repository = ref
2023-05-19 14:25:39 +00:00
2023-04-30 21:29:36 -03:00
data , err := dataFor ( ctx , scoop , cl , archives )
2020-07-06 14:48:17 +01:00
if err != nil {
return err
}
content , err := doBuildManifest ( data )
2018-02-10 12:38:07 +00:00
if err != nil {
return err
}
2023-05-19 14:25:39 +00:00
filename := scoop . Name + ".json"
2024-04-01 10:01:56 -03:00
path := filepath . Join ( ctx . Config . Dist , "scoop" , scoop . Directory , filename )
2023-06-24 23:38:09 -03:00
if err := os . MkdirAll ( filepath . Dir ( path ) , 0 o755 ) ; err != nil {
return err
}
2021-09-18 10:21:29 -03:00
log . WithField ( "manifest" , path ) . Info ( "writing" )
if err := os . WriteFile ( path , content . Bytes ( ) , 0 o644 ) ; err != nil {
2021-05-24 13:30:31 -04:00
return fmt . Errorf ( "failed to write scoop manifest: %w" , err )
2020-02-05 19:05:43 -06:00
}
2021-05-24 13:30:31 -04:00
2021-09-18 10:21:29 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
Name : filename ,
Path : path ,
Type : artifact . ScoopManifest ,
Extra : map [ string ] interface { } {
scoopConfigExtra : scoop ,
} ,
} )
return nil
}
2023-04-30 21:29:36 -03:00
func publishAll ( ctx * context . Context , cli client . Client ) error {
2023-05-19 14:10:06 +00:00
// even if one of them skips, we run them all, and then show return the
// skips all at once. this is needed so we actually create the
// `dist/foo.json` file, which is useful for debugging.
2023-04-30 21:29:36 -03:00
skips := pipe . SkipMemento { }
for _ , manifest := range ctx . Artifacts . Filter ( artifact . ByType ( artifact . ScoopManifest ) ) . List ( ) {
err := doPublish ( ctx , manifest , cli )
if err != nil && pipe . IsSkip ( err ) {
skips . Remember ( err )
continue
}
if err != nil {
return err
}
2021-09-18 10:21:29 -03:00
}
2023-04-30 21:29:36 -03:00
return skips . Evaluate ( )
}
2021-09-18 10:21:29 -03:00
2023-04-30 21:29:36 -03:00
func doPublish ( ctx * context . Context , manifest * artifact . Artifact , cl client . Client ) error {
2022-06-23 23:36:19 -03:00
scoop , err := artifact . Extra [ config . Scoop ] ( * manifest , scoopConfigExtra )
if err != nil {
return err
}
2020-07-06 21:12:41 +01:00
if strings . TrimSpace ( scoop . SkipUpload ) == "true" {
2020-02-05 19:05:43 -06:00
return pipe . Skip ( "scoop.skip_upload is true" )
2020-02-05 22:10:16 -03:00
}
2020-07-06 21:12:41 +01:00
if strings . TrimSpace ( scoop . SkipUpload ) == "auto" && ctx . Semver . Prerelease != "" {
2020-02-05 22:10:16 -03:00
return pipe . Skip ( "release is prerelease" )
2018-02-10 12:38:07 +00:00
}
2023-01-28 23:21:43 -03:00
2021-09-01 10:27:31 -03:00
commitMessage , err := tmpl . New ( ctx ) . Apply ( scoop . CommitMessageTemplate )
2020-04-29 13:45:18 -07:00
if err != nil {
return err
}
2022-01-11 09:15:28 -03:00
author , err := commitauthor . Get ( ctx , scoop . CommitAuthor )
if err != nil {
return err
}
2021-09-18 10:21:29 -03:00
content , err := os . ReadFile ( manifest . Path )
if err != nil {
return err
}
2023-06-14 00:13:21 -03:00
repo := client . RepoFromRef ( scoop . Repository )
2024-04-01 10:01:56 -03:00
gpath := path . Join ( scoop . Directory , manifest . Name )
2023-04-07 11:48:49 -03:00
2023-06-14 00:13:21 -03:00
if scoop . Repository . Git . URL != "" {
2023-04-30 10:18:13 -03:00
return client . NewGitUploadClient ( repo . Branch ) .
CreateFile ( ctx , author , repo , content , gpath , commitMessage )
}
2023-06-14 00:13:21 -03:00
cl , err = client . NewIfToken ( ctx , cl , scoop . Repository . Token )
2023-04-30 10:18:13 -03:00
if err != nil {
return err
}
2024-03-26 23:05:09 -03:00
base := client . Repo {
Name : scoop . Repository . PullRequest . Base . Name ,
Owner : scoop . Repository . PullRequest . Base . Owner ,
Branch : scoop . Repository . PullRequest . Base . Branch ,
2023-04-07 11:48:49 -03:00
}
2024-03-26 23:05:09 -03:00
// try to sync branch
fscli , ok := cl . ( client . ForkSyncer )
if ok && scoop . Repository . PullRequest . Enabled {
if err := fscli . SyncFork ( ctx , repo , base ) ; err != nil {
log . WithError ( err ) . Warn ( "could not sync fork" )
}
2023-04-07 11:48:49 -03:00
}
if err := cl . CreateFile ( ctx , author , repo , content , gpath , commitMessage ) ; err != nil {
return err
}
2024-03-26 23:05:09 -03:00
if ! scoop . Repository . PullRequest . Enabled {
log . Debug ( "scoop.pull_request disabled" )
return nil
}
log . Info ( "scoop.pull_request enabled, creating a PR" )
pcl , ok := cl . ( client . PullRequestOpener )
if ! ok {
return fmt . Errorf ( "client does not support pull requests" )
}
return pcl . OpenPullRequest ( ctx , base , repo , commitMessage , scoop . Repository . PullRequest . Draft )
2018-02-10 12:38:07 +00:00
}
2020-05-26 00:48:10 -03:00
// Manifest represents a scoop.sh App Manifest.
// more info: https://github.com/lukesampson/scoop/wiki/App-Manifests
2018-02-10 12:38:07 +00:00
type Manifest struct {
2020-12-05 07:16:32 -08:00
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
PreInstall [ ] string ` json:"pre_install,omitempty" ` // An array of strings, of the commands to be executed before an application is installed.
PostInstall [ ] string ` json:"post_install,omitempty" ` // An array of strings, of the commands to be executed after an application is installed.
2023-02-22 09:18:44 -03:00
Depends [ ] string ` json:"depends,omitempty" ` // A string or an array of strings.
2023-03-20 20:21:44 +08:00
Shortcuts [ ] [ ] string ` json:"shortcuts,omitempty" ` // A two-dimensional array of string, specifies the shortcut values to make available in the startmenu.
2018-02-10 12:38:07 +00:00
}
2020-05-26 00:48:10 -03:00
// Resource represents a combination of a url and a binary name for an architecture.
2018-02-10 12:38:07 +00:00
type Resource struct {
2019-01-05 12:30:26 -02:00
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
2018-02-10 12:38:07 +00:00
}
2020-07-06 14:48:17 +01:00
func doBuildManifest ( manifest Manifest ) ( bytes . Buffer , error ) {
2018-08-20 22:20:04 -03:00
var result bytes . Buffer
2020-07-06 14:48:17 +01:00
data , err := json . MarshalIndent ( manifest , "" , " " )
if err != nil {
return result , err
}
_ , err = result . Write ( data )
return result , err
}
feat: allow goreleaser to run in gerrit, soft-serve and others (#4271)
Currently, GoReleaser will assume you're running against github, gitea
or gitlab.
You could set `release.disable: true`, but it would still set and try to
use some defaults that could break things up.
Now, if you disable the release, goreleaser will not set these defaults.
It'll also hard error in some cases in which it would happily produce
invalid resources before, namely, if `release.disable` is set, and, for
example, `brews.url_template` is empty (in which case it would try to
use the one from the release, usually github).
closes #4208
2023-09-04 11:23:38 -03:00
func dataFor ( ctx * context . Context , scoop config . Scoop , cl client . ReleaseURLTemplater , artifacts [ ] * artifact . Artifact ) ( Manifest , error ) {
2021-04-19 09:31:57 -03:00
manifest := Manifest {
2018-02-10 12:38:07 +00:00
Version : ctx . Version ,
2018-12-12 18:24:22 -02:00
Architecture : map [ string ] Resource { } ,
2023-04-30 21:29:36 -03:00
Homepage : scoop . Homepage ,
License : scoop . License ,
Description : scoop . Description ,
Persist : scoop . Persist ,
PreInstall : scoop . PreInstall ,
PostInstall : scoop . PostInstall ,
Depends : scoop . Depends ,
Shortcuts : scoop . Shortcuts ,
2018-02-10 12:38:07 +00:00
}
2023-04-30 21:29:36 -03:00
if scoop . URLTemplate == "" {
2020-07-06 14:48:17 +01:00
url , err := cl . ReleaseURLTemplate ( ctx )
if err != nil {
return manifest , err
2019-08-13 20:28:03 +02:00
}
2023-04-30 21:29:36 -03:00
scoop . URLTemplate = url
2019-08-13 20:28:03 +02:00
}
2018-02-10 12:38:07 +00:00
for _ , artifact := range artifacts {
2021-04-18 20:40:23 +02:00
if artifact . Goos != "windows" {
continue
}
var arch string
2023-04-30 13:22:46 +00:00
switch artifact . Goarch {
case "386" :
2018-02-15 00:13:57 -02:00
arch = "32bit"
2023-04-30 13:22:46 +00:00
case "amd64" :
2021-04-18 20:40:23 +02:00
arch = "64bit"
2023-07-17 21:26:18 -03:00
case "arm64" :
arch = "arm64"
2021-04-18 20:40:23 +02:00
default :
continue
2018-02-15 00:13:57 -02:00
}
2018-08-21 04:06:55 +03:00
2023-04-30 21:29:36 -03:00
url , err := tmpl . New ( ctx ) . WithArtifact ( artifact ) . Apply ( scoop . URLTemplate )
2018-08-21 04:06:55 +03:00
if err != nil {
2020-07-06 14:48:17 +01:00
return manifest , err
2018-08-21 04:06:55 +03:00
}
2019-02-04 17:27:51 -02:00
sum , err := artifact . Checksum ( "sha256" )
2018-09-06 13:20:12 +03:00
if err != nil {
2020-07-06 14:48:17 +01:00
return manifest , err
2018-09-06 13:20:12 +03:00
}
2023-05-02 09:06:35 -03:00
log .
WithField ( "artifactExtras" , artifact . Extra ) .
WithField ( "fromURLTemplate" , scoop . URLTemplate ) .
WithField ( "templatedBrewURL" , url ) .
WithField ( "sum" , sum ) .
Debug ( "scoop url templating" )
2020-04-27 21:51:05 -03:00
2022-06-23 23:36:19 -03:00
binaries , err := binaries ( * artifact )
if err != nil {
return manifest , err
}
2018-02-15 00:13:57 -02:00
manifest . Architecture [ arch ] = Resource {
2018-09-06 13:20:12 +03:00
URL : url ,
2022-06-23 23:36:19 -03:00
Bin : binaries ,
2018-09-06 13:20:12 +03:00
Hash : sum ,
2018-02-10 12:38:07 +00:00
}
}
2020-07-06 14:48:17 +01:00
return manifest , nil
2018-02-10 12:38:07 +00:00
}
2019-01-05 12:30:26 -02:00
2022-06-23 23:36:19 -03:00
func binaries ( a artifact . Artifact ) ( [ ] string , error ) {
2024-05-12 20:11:11 +03:00
//nolint:prealloc
2023-04-30 21:29:36 -03:00
var result [ ] string
2022-06-23 23:36:19 -03:00
wrap := artifact . ExtraOr ( a , artifact . ExtraWrappedIn , "" )
2023-04-30 21:29:36 -03:00
bins , err := artifact . Extra [ [ ] string ] ( a , artifact . ExtraBinaries )
2022-06-23 23:36:19 -03:00
if err != nil {
return nil , err
}
2023-04-30 21:29:36 -03:00
for _ , b := range bins {
result = append ( result , filepath . Join ( wrap , b ) )
2019-01-05 12:30:26 -02:00
}
2023-04-30 21:29:36 -03:00
return result , nil
2019-01-05 12:30:26 -02:00
}