Skip to content

Commit

Permalink
fix(nix): description, path and homepage should allow templates (#4156)
Browse files Browse the repository at this point in the history
- nix.description, nix.path, and nix.homepage should allow templates
- nix.path was defaulting to wrong value, fixed

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Jun 29, 2023
1 parent f883131 commit 5f7be84
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
33 changes: 21 additions & 12 deletions internal/pipe/nix/nix.go
Expand Up @@ -130,30 +130,42 @@ func (p Pipe) doRun(ctx *context.Context, nix config.Nix, cl client.ReleaserURLT
return errNoRepoName
}

name, err := tmpl.New(ctx).Apply(nix.Name)
var err error

nix.Name, err = tmpl.New(ctx).Apply(nix.Name)
if err != nil {
return err
}
nix.Name = name

ref, err := client.TemplateRef(tmpl.New(ctx).Apply, nix.Repository)
nix.Repository, err = client.TemplateRef(tmpl.New(ctx).Apply, nix.Repository)
if err != nil {
return err
}
nix.Repository = ref

skipUpload, err := tmpl.New(ctx).Apply(nix.SkipUpload)
nix.SkipUpload, err = tmpl.New(ctx).Apply(nix.SkipUpload)
if err != nil {
return err
}
nix.SkipUpload = skipUpload

nix.Homepage, err = tmpl.New(ctx).Apply(nix.Homepage)
if err != nil {
return err
}

nix.Description, err = tmpl.New(ctx).Apply(nix.Description)
if err != nil {
return err
}

nix.Path, err = tmpl.New(ctx).Apply(nix.Path)
if err != nil {
return err
}
if nix.Path == "" {
nix.Path = nix.Name + ".nix"
nix.Path = path.Join("pkgs", nix.Name, "default.nix")
}

path := filepath.Join(ctx.Config.Dist, "nix", nix.Path)
filename := filepath.Base(path)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
Expand All @@ -169,7 +181,7 @@ func (p Pipe) doRun(ctx *context.Context, nix config.Nix, cl client.ReleaserURLT
}

ctx.Artifacts.Add(&artifact.Artifact{
Name: filename,
Name: filepath.Base(path),
Path: path,
Type: artifact.Nixpkg,
Extra: map[string]interface{}{
Expand Down Expand Up @@ -328,9 +340,6 @@ func doPublish(ctx *context.Context, prefetcher shaPrefetcher, cl client.Client,
repo := client.RepoFromRef(nix.Repository)

gpath := nix.Path
if gpath == "" {
gpath = path.Join("pkgs", nix.Name, "default.nix")
}

msg, err := tmpl.New(ctx).Apply(nix.CommitMessageTemplate)
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions internal/pipe/nix/nix_test.go
Expand Up @@ -209,6 +209,28 @@ func TestRunPipe(t *testing.T) {
},
},
},
{
name: "bad-description-tmpl",
expectRunErrorIs: &template.Error{},
nix: config.Nix{
Description: "{{ .Nope }}",
Repository: config.RepoRef{
Owner: "foo",
Name: "bar",
},
},
},
{
name: "bad-homepage-tmpl",
expectRunErrorIs: &template.Error{},
nix: config.Nix{
Homepage: "{{ .Nope }}",
Repository: config.RepoRef{
Owner: "foo",
Name: "bar",
},
},
},
{
name: "bad-repo-tmpl",
expectRunErrorIs: &template.Error{},
Expand Down Expand Up @@ -256,6 +278,18 @@ func TestRunPipe(t *testing.T) {
},
},
},
{
name: "bad-path-tmpl",
expectRunErrorIs: &template.Error{},
nix: config.Nix{
Name: "foo",
Path: `{{.Foo}}/bar/foo.nix`,
Repository: config.RepoRef{
Owner: "foo",
Name: "bar",
},
},
},
{
name: "bad-release-url-tmpl",
expectRunErrorIs: &template.Error{},
Expand Down Expand Up @@ -406,6 +440,11 @@ func TestRunPipe(t *testing.T) {
}
if tt.nix.Path != "" {
require.Equal(t, tt.nix.Path, client.Path)
} else {
if tt.nix.Name == "" {
tt.nix.Name = "foo"
}
require.Equal(t, "pkgs/"+tt.nix.Name+"/default.nix", client.Path)
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions www/docs/customization/nix.md
Expand Up @@ -48,9 +48,12 @@ nix:
# Path for the file inside the repository.
#
# Default: pkgs/<name>/default.nix
# Templates: allowed
path: pkgs/foo.nix

# Your app's homepage.
#
# Templates: allowed
homepage: "https://example.com/"

# Your app's description.
Expand Down

0 comments on commit 5f7be84

Please sign in to comment.