mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: add Os and Arch template options to the Binary name field (#1936)
* allow Os and Arch tmpl variables in binary name * update documentation * fix docs
This commit is contained in:
parent
4b738be8a1
commit
6d9abe6299
@ -164,8 +164,22 @@ func doBuild(ctx *context.Context, build config.Build, opts builders.Options) er
|
||||
|
||||
func buildOptionsForTarget(ctx *context.Context, build config.Build, target string) (*builders.Options, error) {
|
||||
var ext = extFor(target, build.Flags)
|
||||
var goos string
|
||||
var goarch string
|
||||
|
||||
binary, err := tmpl.New(ctx).Apply(build.Binary)
|
||||
if strings.Contains(target, "_") {
|
||||
goos = strings.Split(target, "_")[0]
|
||||
goarch = strings.Split(target, "_")[1]
|
||||
}
|
||||
|
||||
buildOpts := builders.Options{
|
||||
Target: target,
|
||||
Ext: ext,
|
||||
Os: goos,
|
||||
Arch: goarch,
|
||||
}
|
||||
|
||||
binary, err := tmpl.New(ctx).WithBuildOptions(buildOpts).Apply(build.Binary)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -183,23 +197,10 @@ func buildOptionsForTarget(ctx *context.Context, build config.Build, target stri
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var goos string
|
||||
var goarch string
|
||||
|
||||
if strings.Contains(target, "_") {
|
||||
goos = strings.Split(target, "_")[0]
|
||||
goarch = strings.Split(target, "_")[1]
|
||||
}
|
||||
|
||||
log.WithField("binary", path).Info("building")
|
||||
return &builders.Options{
|
||||
Target: target,
|
||||
Name: name,
|
||||
Path: path,
|
||||
Ext: ext,
|
||||
Os: goos,
|
||||
Arch: goarch,
|
||||
}, nil
|
||||
buildOpts.Name = name
|
||||
buildOpts.Path = path
|
||||
return &buildOpts, nil
|
||||
}
|
||||
|
||||
func extFor(target string, flags config.FlagArray) string {
|
||||
|
@ -706,28 +706,58 @@ func TestPipeOnBuild_invalidBinaryTpl(t *testing.T) {
|
||||
func TestBuildOptionsForTarget(t *testing.T) {
|
||||
var tmpDir = testlib.Mktmp(t)
|
||||
|
||||
build := config.Build{
|
||||
ID: "testid",
|
||||
Binary: "testbinary",
|
||||
Targets: []string{
|
||||
"linux_amd64",
|
||||
"darwin_amd64",
|
||||
"windows_amd64",
|
||||
testCases := []struct {
|
||||
name string
|
||||
build config.Build
|
||||
expectedOpts *api.Options
|
||||
}{
|
||||
{
|
||||
name: "simple options for target",
|
||||
build: config.Build{
|
||||
ID: "testid",
|
||||
Binary: "testbinary",
|
||||
Targets: []string{
|
||||
"linux_amd64",
|
||||
},
|
||||
},
|
||||
expectedOpts: &api.Options{
|
||||
Name: "testbinary",
|
||||
Path: filepath.Join(tmpDir, "testid_linux_amd64", "testbinary"),
|
||||
Target: "linux_amd64",
|
||||
Os: "linux",
|
||||
Arch: "amd64",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "binary name with Os and Arch template variables",
|
||||
build: config.Build{
|
||||
ID: "testid",
|
||||
Binary: "testbinary_{{.Os}}_{{.Arch}}",
|
||||
Targets: []string{
|
||||
"linux_amd64",
|
||||
},
|
||||
},
|
||||
expectedOpts: &api.Options{
|
||||
Name: "testbinary_linux_amd64",
|
||||
Path: filepath.Join(tmpDir, "testid_linux_amd64", "testbinary_linux_amd64"),
|
||||
Target: "linux_amd64",
|
||||
Os: "linux",
|
||||
Arch: "amd64",
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := context.New(config.Project{
|
||||
Dist: tmpDir,
|
||||
Builds: []config.Build{build},
|
||||
})
|
||||
opts, err := buildOptionsForTarget(ctx, build, "linux_amd64")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, &api.Options{
|
||||
Name: "testbinary",
|
||||
Path: filepath.Join(tmpDir, "testid_linux_amd64", "testbinary"),
|
||||
Target: "linux_amd64",
|
||||
Os: "linux",
|
||||
Arch: "amd64",
|
||||
}, opts)
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.New(config.Project{
|
||||
Dist: tmpDir,
|
||||
Builds: []config.Build{tc.build},
|
||||
})
|
||||
opts, err := buildOptionsForTarget(ctx, tc.build, tc.build.Targets[0])
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOpts, opts)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHookComplex(t *testing.T) {
|
||||
|
@ -159,6 +159,16 @@ builds:
|
||||
- windows
|
||||
```
|
||||
|
||||
The binary name field supports [templating](/customization/templates/). The following build details are exposed:
|
||||
|
||||
| Key | Description |
|
||||
|---------|----------------------------------|
|
||||
| .Os | `GOOS` |
|
||||
| .Arch | `GOARCH` |
|
||||
| .Arm | `GOARM` |
|
||||
| .Ext | Extension, e.g. `.exe` |
|
||||
| .Target | Build target, e.g. `darwin_amd64`|
|
||||
|
||||
## Passing environment variables to ldflags
|
||||
|
||||
You can do that by using `{{ .Env.VARIABLE_NAME }}` in the template, for
|
||||
|
Loading…
x
Reference in New Issue
Block a user