Skip to content

Commit

Permalink
feat: add Os and Arch template options to the Binary name field (#1936)
Browse files Browse the repository at this point in the history
* allow Os and Arch tmpl variables in binary name

* update documentation

* fix docs
  • Loading branch information
sylviamoss committed Jan 12, 2021
1 parent 4b738be commit 6d9abe6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 37 deletions.
35 changes: 18 additions & 17 deletions internal/pipe/build/build.go
Expand Up @@ -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

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).Apply(build.Binary)
binary, err := tmpl.New(ctx).WithBuildOptions(buildOpts).Apply(build.Binary)
if err != nil {
return nil, err
}
Expand All @@ -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 {
Expand Down
70 changes: 50 additions & 20 deletions internal/pipe/build/build_test.go
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions www/docs/customization/build.md
Expand Up @@ -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
Expand Down

1 comment on commit 6d9abe6

@vercel
Copy link

@vercel vercel bot commented on 6d9abe6 Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.