2020-04-12 11:47:46 -03:00
|
|
|
// Package sourcearchive archives the source of the project using git-archive.
|
|
|
|
package sourcearchive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2022-08-25 01:10:26 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/archivefiles"
|
2020-04-12 11:47:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2022-08-25 01:10:26 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2020-04-12 11:47:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for source archive.
|
2020-04-12 11:47:46 -03:00
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func (Pipe) String() string {
|
|
|
|
return "creating source archive"
|
|
|
|
}
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
func (Pipe) Skip(ctx *context.Context) bool {
|
|
|
|
return !ctx.Config.Source.Enabled
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Run the pipe.
|
2020-04-12 11:47:46 -03:00
|
|
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
|
|
|
name, err := tmpl.New(ctx).Apply(ctx.Config.Source.NameTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-19 09:31:57 -03:00
|
|
|
filename := name + "." + ctx.Config.Source.Format
|
|
|
|
path := filepath.Join(ctx.Config.Dist, filename)
|
2020-04-12 11:47:46 -03:00
|
|
|
log.WithField("file", filename).Info("creating source archive")
|
2021-11-02 23:57:20 +01:00
|
|
|
args := []string{
|
|
|
|
"archive",
|
|
|
|
"-o", path,
|
2022-08-25 01:10:26 -03:00
|
|
|
"--format", ctx.Config.Source.Format,
|
2021-11-02 23:57:20 +01:00
|
|
|
}
|
2022-08-25 01:10:26 -03:00
|
|
|
|
2021-11-02 23:57:20 +01:00
|
|
|
if ctx.Config.Source.PrefixTemplate != "" {
|
|
|
|
prefix, err := tmpl.New(ctx).Apply(ctx.Config.Source.PrefixTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, "--prefix", prefix)
|
|
|
|
}
|
2022-08-25 01:10:26 -03:00
|
|
|
|
|
|
|
files, err := evalFiles(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
args = append(args, "--add-file", f)
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:57:20 +01:00
|
|
|
args = append(args, ctx.Git.FullCommit)
|
2022-04-12 08:35:19 -03:00
|
|
|
out, err := git.Clean(git.Run(ctx, args...))
|
2020-04-12 11:47:46 -03:00
|
|
|
log.Debug(out)
|
2022-08-25 01:10:26 -03:00
|
|
|
|
2020-04-12 11:47:46 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Type: artifact.UploadableSourceArchive,
|
|
|
|
Name: filename,
|
|
|
|
Path: path,
|
|
|
|
Extra: map[string]interface{}{
|
2021-10-16 22:46:11 -03:00
|
|
|
artifact.ExtraFormat: ctx.Config.Source.Format,
|
2020-04-12 11:47:46 -03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:10:26 -03:00
|
|
|
// to reuse the archivefiles packages, we do something funky:
|
|
|
|
// - convert the []string to []config.File
|
|
|
|
// - eval it in archivefiles
|
|
|
|
// - convert it back to []string
|
|
|
|
//
|
|
|
|
// we also handle files already tracked, as if we add them again,
|
|
|
|
// they'll get duplicated in the archive.
|
|
|
|
func evalFiles(ctx *context.Context) ([]string, error) {
|
|
|
|
var files []config.File
|
|
|
|
for _, f := range ctx.Config.Source.Files {
|
|
|
|
files = append(files, config.File{
|
|
|
|
Source: f,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
addFiles, err := archivefiles.Eval(tmpl.New(ctx), files)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
for _, f := range addFiles {
|
|
|
|
if isTracked(ctx, f.Source) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, f.Source)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if file is tracked, and, if it is we should not add it to the archive again.
|
|
|
|
func isTracked(ctx *context.Context, path string) bool {
|
|
|
|
_, err := git.Run(ctx, "ls-files", "--error-unmatch", path)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Default sets the pipe defaults.
|
2020-04-12 11:47:46 -03:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2021-04-19 09:31:57 -03:00
|
|
|
archive := &ctx.Config.Source
|
2020-04-12 11:47:46 -03:00
|
|
|
if archive.Format == "" {
|
|
|
|
archive.Format = "tar.gz"
|
|
|
|
}
|
|
|
|
|
|
|
|
if archive.NameTemplate == "" {
|
|
|
|
archive.NameTemplate = "{{ .ProjectName }}-{{ .Version }}"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|