/
source.go
117 lines (101 loc) · 2.88 KB
/
source.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
// Package sourcearchive archives the source of the project using git-archive.
package sourcearchive
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/archivefiles"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/archive"
"github.com/goreleaser/goreleaser/pkg/config"
"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.Run(ctx, "ls-files")
if err != nil {
return fmt.Errorf("could not list source files: %w", err)
}
prefix, err := tmpl.New(ctx).Apply(ctx.Config.Source.PrefixTemplate)
if err != nil {
return err
}
af, err := os.Create(path)
if err != nil {
return fmt.Errorf("could not create archive: %w", err)
}
defer af.Close() //nolint:errcheck
arch, err := archive.New(af, ctx.Config.Source.Format)
if err != nil {
return err
}
var ff []config.File
for _, f := range strings.Split(out, "\n") {
if strings.TrimSpace(f) == "" {
continue
}
ff = append(ff, config.File{
Source: f,
})
}
files, err := archivefiles.Eval(tmpl.New(ctx), append(ff, ctx.Config.Source.Files...))
if err != nil {
return err
}
for _, f := range files {
f.Destination = filepath.Join(prefix, f.Destination)
if err := arch.Add(f); err != nil {
return fmt.Errorf("could not add %q to archive: %w", f.Source, err)
}
}
if err := arch.Close(); err != nil {
return fmt.Errorf("could not close archive file: %w", err)
}
if err := af.Close(); err != nil {
return fmt.Errorf("could not close archive file: %w", err)
}
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableSourceArchive,
Name: filename,
Path: path,
Extra: map[string]interface{}{
artifact.ExtraFormat: ctx.Config.Source.Format,
},
})
return err
}
// 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
}
// 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
}