1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/pipe/sourcearchive/source.go
Carlos Alexandro Becker df2f00fc8b
refactor: put common extra keys in the artifact package (#2580)
* refactor: put common extra keys in the artifact package

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* refactor: common extra fields have their own funcs

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* refactor: 2 more

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: review

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
2021-10-16 22:46:11 -03:00

59 lines
1.5 KiB
Go

// Package sourcearchive archives the source of the project using git-archive.
package sourcearchive
import (
"path/filepath"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe for source archive.
type Pipe struct{}
func (Pipe) String() string {
return "creating source archive"
}
func (Pipe) Skip(ctx *context.Context) bool {
return !ctx.Config.Source.Enabled
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) (err error) {
name, err := tmpl.New(ctx).Apply(ctx.Config.Source.NameTemplate)
if err != nil {
return err
}
filename := name + "." + ctx.Config.Source.Format
path := filepath.Join(ctx.Config.Dist, filename)
log.WithField("file", filename).Info("creating source archive")
out, err := git.Clean(git.Run("archive", "-o", path, ctx.Git.FullCommit))
log.Debug(out)
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableSourceArchive,
Name: filename,
Path: path,
Extra: map[string]interface{}{
artifact.ExtraFormat: ctx.Config.Source.Format,
},
})
return err
}
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
archive := &ctx.Config.Source
if archive.Format == "" {
archive.Format = "tar.gz"
}
if archive.NameTemplate == "" {
archive.NameTemplate = "{{ .ProjectName }}-{{ .Version }}"
}
return nil
}