github.com/windmeup/goreleaser@v1.21.95/internal/pipe/metadata/metadata.go (about) 1 // Package metadata provides the pipe implementation that creates an artifacts.json file in the dist folder. 2 package metadata 3 4 import ( 5 "encoding/json" 6 "os" 7 "path/filepath" 8 "time" 9 10 "github.com/caarlos0/log" 11 "github.com/windmeup/goreleaser/internal/artifact" 12 "github.com/windmeup/goreleaser/internal/gio" 13 "github.com/windmeup/goreleaser/internal/tmpl" 14 "github.com/windmeup/goreleaser/pkg/context" 15 ) 16 17 // Pipe implementation. 18 type Pipe struct{} 19 20 func (Pipe) String() string { return "storing release metadata" } 21 func (Pipe) Skip(_ *context.Context) bool { return false } 22 23 // Run the pipe. 24 func (Pipe) Run(ctx *context.Context) error { 25 if err := tmpl.New(ctx).ApplyAll( 26 &ctx.Config.Metadata.ModTimestamp, 27 ); err != nil { 28 return err 29 } 30 if err := writeArtifacts(ctx); err != nil { 31 return err 32 } 33 return writeMetadata(ctx) 34 } 35 36 func writeMetadata(ctx *context.Context) error { 37 return writeJSON(ctx, metadata{ 38 ProjectName: ctx.Config.ProjectName, 39 Tag: ctx.Git.CurrentTag, 40 PreviousTag: ctx.Git.PreviousTag, 41 Version: ctx.Version, 42 Commit: ctx.Git.Commit, 43 Date: ctx.Date, 44 Runtime: metaRuntime{ 45 Goos: ctx.Runtime.Goos, 46 Goarch: ctx.Runtime.Goarch, 47 }, 48 }, "metadata.json") 49 } 50 51 func writeArtifacts(ctx *context.Context) error { 52 _ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error { 53 a.TypeS = a.Type.String() 54 a.Path = filepath.ToSlash(filepath.Clean(a.Path)) 55 return nil 56 }) 57 return writeJSON(ctx, ctx.Artifacts.List(), "artifacts.json") 58 } 59 60 func writeJSON(ctx *context.Context, j interface{}, name string) error { 61 bts, err := json.Marshal(j) 62 if err != nil { 63 return err 64 } 65 path := filepath.Join(ctx.Config.Dist, name) 66 log.Log.WithField("file", path).Info("writing") 67 if err := os.WriteFile(path, bts, 0o644); err != nil { 68 return err 69 } 70 71 return gio.Chtimes(path, ctx.Config.Metadata.ModTimestamp) 72 } 73 74 type metadata struct { 75 ProjectName string `json:"project_name"` 76 Tag string `json:"tag"` 77 PreviousTag string `json:"previous_tag"` 78 Version string `json:"version"` 79 Commit string `json:"commit"` 80 Date time.Time `json:"date"` 81 Runtime metaRuntime `json:"runtime"` 82 } 83 84 type metaRuntime struct { 85 Goos string `json:"goos"` 86 Goarch string `json:"goarch"` 87 }