<commit_before>package nametemplate

import (
	"bytes"
	"regexp"
	"text/template"

	"github.com/apex/log"
	"github.com/goreleaser/goreleaser/context"
	"github.com/goreleaser/goreleaser/internal/artifact"
)

var deprecatedBinary = regexp.MustCompile("\\{\\{ ?\\.Binary ?\\}\\}")

// Apply applies the name template to the given artifact and name
// TODO: this should be refactored alongside with other name template related todos
func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, error) {
	if deprecatedBinary.MatchString(ctx.Config.Archive.NameTemplate) {
		log.WithField("field", "{{.Binary}}").Warn("you are using a deprecated field on your template, please check the documentation")
	}
	var out bytes.Buffer
	t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate)
	if err != nil {
		return "", err
	}
	data := struct {
		Os, Arch, Arm, Version, Tag, Binary, ProjectName string
		Env                                              map[string]string
	}{
		Os:          replace(ctx.Config.Archive.Replacements, a.Goos),
		Arch:        replace(ctx.Config.Archive.Replacements, a.Goarch),
		Arm:         replace(ctx.Config.Archive.Replacements, a.Goarm),
		Version:     ctx.Version,
		Tag:         ctx.Git.CurrentTag,
		ProjectName: name,
		Binary:      name, // TODO: deprecated, remove soon
		Env:         ctx.Env,
	}
	err = t.Execute(&out, data)
	return out.String(), err
}

func replace(replacements map[string]string, original string) string {
	result := replacements[original]<commit_msg>fix: avoiding double escapes<commit_after>package nametemplate

import (
	"bytes"
	"regexp"
	"text/template"

	"github.com/apex/log"
	"github.com/goreleaser/goreleaser/context"
	"github.com/goreleaser/goreleaser/internal/artifact"
)

var deprecatedBinary = regexp.MustCompile(`\{\{ ?\.Binary ?\}\}`)

// Apply applies the name template to the given artifact and name
// TODO: this should be refactored alongside with other name template related todos
func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, error) {
	if deprecatedBinary.MatchString(ctx.Config.Archive.NameTemplate) {
		log.WithField("field", "{{.Binary}}").Warn("you are using a deprecated field on your template, please check the documentation")
	}
	var out bytes.Buffer
	t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate)
	if err != nil {
		return "", err
	}
	data := struct {
		Os, Arch, Arm, Version, Tag, Binary, ProjectName string
		Env                                              map[string]string
	}{
		Os:          replace(ctx.Config.Archive.Replacements, a.Goos),
		Arch:        replace(ctx.Config.Archive.Replacements, a.Goarch),
		Arm:         replace(ctx.Config.Archive.Replacements, a.Goarm),
		Version:     ctx.Version,
		Tag:         ctx.Git.CurrentTag,
		ProjectName: name,
		Binary:      name, // TODO: deprecated, remove soon
		Env:         ctx.Env,
	}
	err = t.Execute(&out, data)
	return out.String(), err
}

func replace(replacements map[string]string, original string) string {
	result := replacements[original]
{'fraction_non_alphanumeric': 0.12694300518134716, 'fraction_numerical': 0.0, 'mean_word_length': 3.4446043165467626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5369446', 'n_tokens_mistral': 1007, 'n_tokens_neox': 948, 'n_words': 244}