mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-09 13:36:56 +02:00
fix: allow snapcraft apps: directive
to use custom commands (#1267)
* enhc: allow snapcraft apps: directive to utilize custom commands, defaults to binary name if no apps: are specified * refactor: snapcraft tests Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
1d7098ed42
commit
eb528971b9
@ -150,6 +150,7 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// prime is the directory that then will be compressed to make the .snap package.
|
||||
var folderDir = filepath.Join(ctx.Config.Dist, folder)
|
||||
var primeDir = filepath.Join(folderDir, "prime")
|
||||
@ -185,34 +186,26 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
|
||||
metadata.Name = snap.Name
|
||||
}
|
||||
|
||||
for _, binary := range binaries {
|
||||
_, name := filepath.Split(binary.Name)
|
||||
log.WithField("path", binary.Path).
|
||||
WithField("name", binary.Name).
|
||||
Debug("passed binary to snapcraft")
|
||||
appMetadata := AppMetadata{
|
||||
Command: name,
|
||||
// if the user didn't specify any apps then
|
||||
// default to the main binary being the command:
|
||||
if len(snap.Apps) == 0 {
|
||||
metadata.Apps[binaries[0].Name] = AppMetadata{
|
||||
Command: filepath.Base(binaries[0].Name),
|
||||
}
|
||||
completerPath := ""
|
||||
if configAppMetadata, ok := snap.Apps[name]; ok {
|
||||
appMetadata.Plugs = configAppMetadata.Plugs
|
||||
appMetadata.Daemon = configAppMetadata.Daemon
|
||||
appMetadata.Command = strings.TrimSpace(strings.Join([]string{
|
||||
appMetadata.Command,
|
||||
configAppMetadata.Args,
|
||||
}, " "))
|
||||
if configAppMetadata.Completer != "" {
|
||||
completerPath = configAppMetadata.Completer
|
||||
appMetadata.Completer = filepath.Base(completerPath)
|
||||
}
|
||||
metadata.Apps[snap.Name] = AppMetadata{
|
||||
Command: filepath.Base(binaries[0].Name),
|
||||
}
|
||||
metadata.Apps[name] = appMetadata
|
||||
metadata.Plugs = snap.Plugs
|
||||
}
|
||||
|
||||
for _, binary := range binaries {
|
||||
|
||||
// build the binaries and link resources
|
||||
completerPath := ""
|
||||
destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path))
|
||||
log.WithField("src", binary.Path).
|
||||
WithField("dst", destBinaryPath).
|
||||
Debug("linking")
|
||||
|
||||
if err = os.Link(binary.Path, destBinaryPath); err != nil {
|
||||
return errors.Wrap(err, "failed to link binary")
|
||||
}
|
||||
@ -232,11 +225,36 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
|
||||
return errors.Wrap(err, "failed to change completer permissions")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := metadata.Apps[metadata.Name]; !ok {
|
||||
_, name := filepath.Split(binaries[0].Name)
|
||||
metadata.Apps[metadata.Name] = metadata.Apps[name]
|
||||
// setup the apps: directive for each binary
|
||||
for name := range snap.Apps {
|
||||
log.WithField("path", binary.Path).
|
||||
WithField("name", binary.Name).
|
||||
Debug("passed binary to snapcraft")
|
||||
|
||||
appMetadata := AppMetadata{
|
||||
Command: binary.Name,
|
||||
}
|
||||
|
||||
if configAppMetadata, ok := snap.Apps[name]; ok {
|
||||
appMetadata.Plugs = configAppMetadata.Plugs
|
||||
appMetadata.Daemon = configAppMetadata.Daemon
|
||||
|
||||
appMetadata.Command = strings.TrimSpace(strings.Join([]string{
|
||||
binary.Name,
|
||||
configAppMetadata.Args,
|
||||
}, " "))
|
||||
|
||||
if configAppMetadata.Completer != "" {
|
||||
completerPath = configAppMetadata.Completer
|
||||
appMetadata.Completer = filepath.Base(completerPath)
|
||||
}
|
||||
}
|
||||
|
||||
metadata.Apps[name] = appMetadata
|
||||
metadata.Plugs = snap.Plugs
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
out, err := yaml.Marshal(metadata)
|
||||
|
@ -81,7 +81,8 @@ func TestRunPipe(t *testing.T) {
|
||||
addBinaries(t, ctx, "foo", filepath.Join(dist, "foo"), "foo")
|
||||
addBinaries(t, ctx, "bar", filepath.Join(dist, "bar"), "bar")
|
||||
assert.NoError(t, Pipe{}.Run(ctx))
|
||||
assert.Len(t, ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List(), 9)
|
||||
list := ctx.Artifacts.Filter(artifact.ByType(artifact.PublishableSnapcraft)).List()
|
||||
assert.Len(t, list, 9)
|
||||
}
|
||||
|
||||
func TestRunPipeInvalidNameTemplate(t *testing.T) {
|
||||
@ -176,7 +177,6 @@ func TestRunPipeWithBinaryInDir(t *testing.T) {
|
||||
assert.Equal(t, "testsnapname", metadata.Name)
|
||||
assert.Equal(t, "", metadata.Base)
|
||||
assert.Equal(t, "", metadata.License)
|
||||
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
|
||||
assert.Equal(t, "mybin", metadata.Apps["testsnapname"].Command)
|
||||
}
|
||||
|
||||
@ -191,6 +191,7 @@ func TestRunPipeMetadata(t *testing.T) {
|
||||
Dist: dist,
|
||||
Snapcrafts: []config.Snapcraft{
|
||||
{
|
||||
Name: "testprojectname",
|
||||
NameTemplate: "foo_{{.Arch}}",
|
||||
Summary: "test summary",
|
||||
Description: "test description",
|
||||
@ -222,9 +223,9 @@ func TestRunPipeMetadata(t *testing.T) {
|
||||
assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["mybin"].Plugs)
|
||||
assert.Equal(t, "simple", metadata.Apps["mybin"].Daemon)
|
||||
assert.Equal(t, "mybin --foo --bar", metadata.Apps["mybin"].Command)
|
||||
assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["testprojectname"].Plugs)
|
||||
assert.Equal(t, "simple", metadata.Apps["testprojectname"].Daemon)
|
||||
assert.Equal(t, "mybin --foo --bar", metadata.Apps["testprojectname"].Command)
|
||||
assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["mybin"].Plugs)
|
||||
assert.Equal(t, "simple", metadata.Apps["mybin"].Daemon)
|
||||
assert.Equal(t, "mybin --foo --bar", metadata.Apps["mybin"].Command)
|
||||
assert.Equal(t, map[interface{}]interface{}(map[interface{}]interface{}{"read": []interface{}{"$HOME/test"}}), metadata.Plugs["personal-files"])
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user