github.com/sergiusens/goreleaser@v0.34.3-0.20171009111917-ae6f7c157c5c/internal/name/name.go (about) 1 // Package name provides name template logic for the final archive, formulae, 2 // etc. 3 package name 4 5 import ( 6 "bytes" 7 "text/template" 8 9 "github.com/goreleaser/goreleaser/config" 10 "github.com/goreleaser/goreleaser/context" 11 "github.com/goreleaser/goreleaser/internal/buildtarget" 12 ) 13 14 type nameData struct { 15 Os string 16 Arch string 17 Arm string 18 Version string 19 Tag string 20 Binary string // deprecated 21 ProjectName string 22 } 23 24 // ForBuild return the name for the given context, goos, goarch, goarm and 25 // build, using the build.Binary property instead of project_name. 26 func ForBuild(ctx *context.Context, build config.Build, target buildtarget.Target) (string, error) { 27 return apply( 28 nameData{ 29 Os: replace(ctx.Config.Archive.Replacements, target.OS), 30 Arch: replace(ctx.Config.Archive.Replacements, target.Arch), 31 Arm: replace(ctx.Config.Archive.Replacements, target.Arm), 32 Version: ctx.Version, 33 Tag: ctx.Git.CurrentTag, 34 Binary: build.Binary, 35 ProjectName: build.Binary, 36 }, 37 ctx.Config.Archive.NameTemplate, 38 ) 39 } 40 41 // For returns the name for the given context, goos, goarch and goarm. 42 func For(ctx *context.Context, target buildtarget.Target) (string, error) { 43 return apply( 44 nameData{ 45 Os: replace(ctx.Config.Archive.Replacements, target.OS), 46 Arch: replace(ctx.Config.Archive.Replacements, target.Arch), 47 Arm: replace(ctx.Config.Archive.Replacements, target.Arm), 48 Version: ctx.Version, 49 Tag: ctx.Git.CurrentTag, 50 Binary: ctx.Config.ProjectName, 51 ProjectName: ctx.Config.ProjectName, 52 }, 53 ctx.Config.Archive.NameTemplate, 54 ) 55 } 56 57 // ForChecksums returns the filename for the checksums file based on its 58 // template 59 func ForChecksums(ctx *context.Context) (string, error) { 60 return apply( 61 nameData{ 62 ProjectName: ctx.Config.ProjectName, 63 Tag: ctx.Git.CurrentTag, 64 Version: ctx.Version, 65 }, 66 ctx.Config.Checksum.NameTemplate, 67 ) 68 } 69 70 func apply(data nameData, templateStr string) (string, error) { 71 var out bytes.Buffer 72 t, err := template.New(data.ProjectName).Parse(templateStr) 73 if err != nil { 74 return "", err 75 } 76 err = t.Execute(&out, data) 77 return out.String(), err 78 } 79 80 func replace(replacements map[string]string, original string) string { 81 result := replacements[original] 82 if result == "" { 83 return original 84 } 85 return result 86 }