github.com/goreleaser/goreleaser@v1.25.1/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/goreleaser/goreleaser/internal/artifact"
    12  	"github.com/goreleaser/goreleaser/internal/gio"
    13  	"github.com/goreleaser/goreleaser/internal/tmpl"
    14  	"github.com/goreleaser/goreleaser/pkg/context"
    15  )
    16  
    17  type (
    18  	// Pipe implementation.
    19  	Pipe struct{}
    20  	// MetaPipe implementation.
    21  	MetaPipe struct{}
    22  	// ArtifactsPipe implementation.
    23  	ArtifactsPipe struct{}
    24  )
    25  
    26  func (Pipe) String() string { return "setting up metadata" }
    27  func (Pipe) Run(ctx *context.Context) error {
    28  	return tmpl.New(ctx).ApplyAll(&ctx.Config.Metadata.ModTimestamp)
    29  }
    30  
    31  func (MetaPipe) String() string                 { return "storing release metadata" }
    32  func (MetaPipe) Run(ctx *context.Context) error { return writeMetadata(ctx) }
    33  
    34  func (ArtifactsPipe) String() string                 { return "storing artifacts metadata" }
    35  func (ArtifactsPipe) Run(ctx *context.Context) error { return writeArtifacts(ctx) }
    36  
    37  func writeMetadata(ctx *context.Context) error {
    38  	const name = "metadata.json"
    39  	path, err := writeJSON(ctx, metadata{
    40  		ProjectName: ctx.Config.ProjectName,
    41  		Tag:         ctx.Git.CurrentTag,
    42  		PreviousTag: ctx.Git.PreviousTag,
    43  		Version:     ctx.Version,
    44  		Commit:      ctx.Git.Commit,
    45  		Date:        ctx.Date,
    46  		Runtime: metaRuntime{
    47  			Goos:   ctx.Runtime.Goos,
    48  			Goarch: ctx.Runtime.Goarch,
    49  		},
    50  	}, name)
    51  	ctx.Artifacts.Add(&artifact.Artifact{
    52  		Name: name,
    53  		Path: path,
    54  		Type: artifact.Metadata,
    55  	})
    56  	return err
    57  }
    58  
    59  func writeArtifacts(ctx *context.Context) error {
    60  	_ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error {
    61  		a.TypeS = a.Type.String()
    62  		a.Path = filepath.ToSlash(filepath.Clean(a.Path))
    63  		return nil
    64  	})
    65  	_, err := writeJSON(ctx, ctx.Artifacts.List(), "artifacts.json")
    66  	return err
    67  }
    68  
    69  func writeJSON(ctx *context.Context, j interface{}, name string) (string, error) {
    70  	bts, err := json.Marshal(j)
    71  	if err != nil {
    72  		return "", err
    73  	}
    74  	path := filepath.Join(ctx.Config.Dist, name)
    75  	log.Log.WithField("file", path).Info("writing")
    76  	if err := os.WriteFile(path, bts, 0o644); err != nil {
    77  		return "", err
    78  	}
    79  
    80  	return path, gio.Chtimes(path, ctx.Config.Metadata.ModTimestamp)
    81  }
    82  
    83  type metadata struct {
    84  	ProjectName string      `json:"project_name"`
    85  	Tag         string      `json:"tag"`
    86  	PreviousTag string      `json:"previous_tag"`
    87  	Version     string      `json:"version"`
    88  	Commit      string      `json:"commit"`
    89  	Date        time.Time   `json:"date"`
    90  	Runtime     metaRuntime `json:"runtime"`
    91  }
    92  
    93  type metaRuntime struct {
    94  	Goos   string `json:"goos"`
    95  	Goarch string `json:"goarch"`
    96  }