github.com/amane3/goreleaser@v0.182.0/internal/pipe/sourcearchive/source.go (about)

     1  // Package sourcearchive archives the source of the project using git-archive.
     2  package sourcearchive
     3  
     4  import (
     5  	"path/filepath"
     6  
     7  	"github.com/amane3/goreleaser/internal/artifact"
     8  	"github.com/amane3/goreleaser/internal/git"
     9  	"github.com/amane3/goreleaser/internal/pipe"
    10  	"github.com/amane3/goreleaser/internal/tmpl"
    11  	"github.com/amane3/goreleaser/pkg/context"
    12  	"github.com/apex/log"
    13  )
    14  
    15  // Pipe for source archive.
    16  type Pipe struct{}
    17  
    18  func (Pipe) String() string {
    19  	return "creating source archive"
    20  }
    21  
    22  // Run the pipe.
    23  func (Pipe) Run(ctx *context.Context) (err error) {
    24  	if !ctx.Config.Source.Enabled {
    25  		return pipe.Skip("source pipe is disabled")
    26  	}
    27  
    28  	name, err := tmpl.New(ctx).Apply(ctx.Config.Source.NameTemplate)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	var filename = name + "." + ctx.Config.Source.Format
    33  	var path = filepath.Join(ctx.Config.Dist, filename)
    34  	log.WithField("file", filename).Info("creating source archive")
    35  	out, err := git.Clean(git.Run("archive", "-o", path, ctx.Git.FullCommit))
    36  	log.Debug(out)
    37  	ctx.Artifacts.Add(&artifact.Artifact{
    38  		Type: artifact.UploadableSourceArchive,
    39  		Name: filename,
    40  		Path: path,
    41  		Extra: map[string]interface{}{
    42  			"Format": ctx.Config.Source.Format,
    43  		},
    44  	})
    45  	return err
    46  }
    47  
    48  // Default sets the pipe defaults.
    49  func (Pipe) Default(ctx *context.Context) error {
    50  	var archive = &ctx.Config.Source
    51  	if archive.Format == "" {
    52  		archive.Format = "tar.gz"
    53  	}
    54  
    55  	if archive.NameTemplate == "" {
    56  		archive.NameTemplate = "{{ .ProjectName }}-{{ .Version }}"
    57  	}
    58  	return nil
    59  }