<commit_before>// Package archive implements the pipe interface with the intent of
// archiving and compressing the binaries, readme, and other artifacts. It
// also provides an Archive interface which represents an archiving format.
package archive

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/apex/log"
	"github.com/mattn/go-zglob"
	"golang.org/x/sync/errgroup"

	"github.com/goreleaser/archive"
	"github.com/goreleaser/goreleaser/context"
	"github.com/goreleaser/goreleaser/internal/artifact"
	"github.com/goreleaser/goreleaser/internal/nametemplate"
)

// Pipe for archive
type Pipe struct{}

func (Pipe) String() string {
	return "creating archives"
}

// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
	var g errgroup.Group
	var filtered = ctx.Artifacts.Filter(artifact.ByType(artifact.Binary))
	for _, artifacts := range filtered.GroupByPlatform() {
		artifacts := artifacts
		g.Go(func() error {
			if ctx.Config.Archive.Format == "binary" {
				return skip(ctx, artifacts)
			}
			return create(ctx, artifacts)
		})
	}
	return g.Wait()
}

// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
	if ctx.Config.Archive.NameTemplate == "" {
		ctx.Config.Archive.NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
	}
	if ctx.Config.Archive.Format == "" {
		ctx.Config.Archive.Format = "tar.gz"
	}
	if len(ctx.Config.Archive.Files) == 0 {
		ctx.Config.Archive.Files = []string{
			"licence*",
			"LICENCE*",
			"license*",
			"LICENSE*",
			"readme*",
			"README*",
			"changelog*",
			"CHANGELOG*",
		}
	}
	return nil
}

func create(ctx *context.Context, artifacts []artifact.Artifact) error {
	var format = packageFormat(ctx, artifacts[0].Goos)
	folder, err := nametemplate.Apply(ctx, artifacts[0], ctx.Config.ProjectName)
	if err != nil {
		return err
	}
	archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
	archiveFile, err := os.Create(archivePath)
	if err != nil {
		return fmt.Errorf("failed to create directory %s: %s", archivePath, err.Error())
	}
	defer archiveFile.Close() // nolint: errcheck
	log.WithField("archive", archivePath).Info("creating")
	var a = archive.New(archiveFile)
	defer a.Close() // nolint: errcheck

	files, err := findFiles(ctx)
	if err != nil {
		return fmt.Errorf("failed to find files to archive: %s", err.Error())
	}
	for _, f := range files {
		if err = a.Add(wrap(ctx, f, folder), f); err != nil {
			return fmt.Errorf("failed to add %s to the archive: %s", f, err.Error())
		}
	}
	for _, binary := range artifacts {
		if err := a.Add(wrap(ctx, binary.Name, folder), binary.Path); err != nil {
			return fmt.Errorf("failed to add %s -> %s to the archive: %s", binary.Path, binary.Name, err.Error())
		}
	}
	ctx.Artifacts.Add(artifact.Artifact{
		Type:   artifact.UploadableArchive,
		Name:   folder + "." + format,
		Path:   archivePath,
		Goos:   artifacts[0].Goos,
		Goarch: artifacts[0].Goarch,
		Goarm:  artifacts[0].Goarm,
	})
	return nil
}

func skip(ctx *context.Context, artifacts []artifact.Artifact) error {
	for _, a := range artifacts {
		log.WithField("binary", a.Name).Info("skip archiving")
		// TODO: this should not happen here, maybe add another extra field for the extension and/or name without extension?
		name, err := nametemplate.Apply(ctx, a, strings.TrimSuffix(a.Name, ".exe"))
		if err != nil {
			return err
		}
		a.Type = artifact.UploadableBinary
		a.Name = name + a.Extra["Ext"]
		ctx.Artifacts.Add(a)
	}
	return nil
}
<commit_msg>fix: archive default name template<commit_after>// Package archive implements the pipe interface with the intent of
// archiving and compressing the binaries, readme, and other artifacts. It
// also provides an Archive interface which represents an archiving format.
package archive

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/apex/log"
	"github.com/mattn/go-zglob"
	"golang.org/x/sync/errgroup"

	"github.com/goreleaser/archive"
	"github.com/goreleaser/goreleaser/context"
	"github.com/goreleaser/goreleaser/internal/artifact"
	"github.com/goreleaser/goreleaser/internal/nametemplate"
)

const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"

// Pipe for archive
type Pipe struct{}

func (Pipe) String() string {
	return "creating archives"
}

// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
	var g errgroup.Group
	var filtered = ctx.Artifacts.Filter(artifact.ByType(artifact.Binary))
	for _, artifacts := range filtered.GroupByPlatform() {
		artifacts := artifacts
		g.Go(func() error {
			if ctx.Config.Archive.Format == "binary" {
				return skip(ctx, artifacts)
			}
			return create(ctx, artifacts)
		})
	}
	return g.Wait()
}

// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
	if ctx.Config.Archive.NameTemplate == "" {
		ctx.Config.Archive.NameTemplate = defaultNameTemplate
	}
	if ctx.Config.Archive.Format == "" {
		ctx.Config.Archive.Format = "tar.gz"
	}
	if len(ctx.Config.Archive.Files) == 0 {
		ctx.Config.Archive.Files = []string{
			"licence*",
			"LICENCE*",
			"license*",
			"LICENSE*",
			"readme*",
			"README*",
			"changelog*",
			"CHANGELOG*",
		}
	}
	return nil
}

func create(ctx *context.Context, artifacts []artifact.Artifact) error {
	var format = packageFormat(ctx, artifacts[0].Goos)
	folder, err := nametemplate.Apply(ctx, artifacts[0], ctx.Config.ProjectName)
	if err != nil {
		return err
	}
	archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
	archiveFile, err := os.Create(archivePath)
	if err != nil {
		return fmt.Errorf("failed to create directory %s: %s", archivePath, err.Error())
	}
	defer archiveFile.Close() // nolint: errcheck
	log.WithField("archive", archivePath).Info("creating")
	var a = archive.New(archiveFile)
	defer a.Close() // nolint: errcheck

	files, err := findFiles(ctx)
	if err != nil {
		return fmt.Errorf("failed to find files to archive: %s", err.Error())
	}
	for _, f := range files {
		if err = a.Add(wrap(ctx, f, folder), f); err != nil {
			return fmt.Errorf("failed to add %s to the archive: %s", f, err.Error())
		}
	}
	for _, binary := range artifacts {
		if err := a.Add(wrap(ctx, binary.Name, folder), binary.Path); err != nil {
			return fmt.Errorf("failed to add %s -> %s to the archive: %s", binary.Path, binary.Name, err.Error())
		}
	}
	ctx.Artifacts.Add(artifact.Artifact{
		Type:   artifact.UploadableArchive,
		Name:   folder + "." + format,
		Path:   archivePath,
		Goos:   artifacts[0].Goos,
		Goarch: artifacts[0].Goarch,
		Goarm:  artifacts[0].Goarm,
	})
	return nil
}

func skip(ctx *context.Context, artifacts []artifact.Artifact) error {
	for _, a := range artifacts {
		log.WithField("binary", a.Name).Info("skip archiving")
		name, err := nametemplate.Apply(ctx, a, a.Extra["Binary"])
		if err != nil {
			return err
		}
		a.Type = artifact.UploadableBinary
		a.Name = name + a.Extra["Ext"]
		ctx.Artifacts.Add(a)
	}
	return nil
}

{'fraction_non_alphanumeric': 0.15563813062741175, 'fraction_numerical': 0.0017150207231670716, 'mean_word_length': 3.8902865129280224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2732277', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2443, 'n_words': 612}