1
1
mirror of https://github.com/goreleaser/nfpm synced 2025-04-30 14:18:01 +02:00

feat: wip: rpm

This commit is contained in:
Carlos Alexandro Becker 2018-02-04 21:00:20 -02:00
parent 09e4e94e2a
commit 082e88ff8a
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
3 changed files with 50 additions and 14 deletions

1
pkg.go

@ -24,5 +24,6 @@ type Info struct {
Description string `yaml:"description,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Homepage string `yaml:"homepage,omitempty"`
License string `yaml:"license,omitempty"`
Files map[string]string `yaml:"files,omitempty"`
}

@ -1,13 +1,17 @@
package rpm
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/alecthomas/template"
"github.com/goreleaser/archive"
"github.com/goreleaser/packager"
)
@ -38,43 +42,71 @@ func (*RPM) Package(info packager.Info, deb io.Writer) error {
return err
}
}
targz, err := os.Create(filepath.Join(root, "SOURCES", info.Name+".tar.gz"))
targz, err := os.Create(filepath.Join(root, "SOURCES", fmt.Sprintf("%s-%s.tar.gz", info.Name, info.Version)))
if err != nil {
return fmt.Errorf("failed to create tar.gz file: %s", err)
}
archive := archive.New(targz)
defer archive.Close()
for src, dst := range info.Files {
if err := archive.Add(dst, src); err != nil {
if err := archive.Add(fmt.Sprintf("%s-%s/%s", info.Name, info.Version, dst), src); err != nil {
return fmt.Errorf("failed to add file %s to tar.gz: %s", src, err)
}
}
archive.Close()
var body bytes.Buffer
var tmpl = template.New("spec")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
return strings.Trim(strings.Join(strs, ", "), " ")
},
"first_line": func(str string) string {
return strings.Split(str, "\n")[0]
},
})
if err := template.Must(tmpl.Parse(specTemplate)).Execute(&body, info); err != nil {
return fmt.Errorf("failed to create spec file: %s", err)
}
if err := ioutil.WriteFile(filepath.Join(root, "SPECS", info.Name+".spec"), body.Bytes(), 0644); err != nil {
return fmt.Errorf("failed to write spec file: %s", err)
}
var args = []string{
"--define", fmt.Sprintf("_topdir %s", root),
"--define", fmt.Sprintf("_tmppath %s/tmp", root),
"-ba",
"SPECS/" + info.Name + ".spec",
}
log.Println(args)
return nil
cmd := exec.Command("rpmbuild", args...)
cmd.Dir = root
out, err := cmd.CombinedOutput()
log.Println(string(out))
return err
}
// TODO: declare dependencies and etc here as well
const specTemplate = `
# Don't try fancy stuff like debuginfo, which is useless on binary-only
# packages. Don't strip binary too
# Be sure buildpolicy set to do nothing
%define __spec_install_post %{nil}
%define debug_package %{nil}
%define __os_install_post %{_dbpath}/brp-compress
Summary: {{.Info.Description}}
Name: {{.Info.Name}}
Version: {{.Info.Version}}
Name: {{ .Name }}
Summary: {{ first_line .Description }}
Version: {{ .Version }}
Release: 1
License: {{.Info.License}}
License: {{ .License }}
Group: Development/Tools
SOURCE0 : %{name}.tar.gz
URL: {{.Info.Homepage}}
SOURCE0 : %{name}-%{version}.tar.gz
URL: {{ .Homepage }}
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
%description
%{summary}
{{ .Description }}
%prep
%setup -q
@ -96,7 +128,9 @@ rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
#%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%{_bindir}/*
%changelog
# noop
`

@ -23,9 +23,10 @@ func TestRPM(t *testing.T) {
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
License: "MIT",
Files: map[string]string{
"./testdata/fake": "/usr/local/bin/fake",
"./testdata/whatever.conf": "/etc/fake/fake.conf",
"./testdata/fake": "/usr/local/bin/fake",
// "./testdata/whatever.conf": "/etc/fake/fake.conf",
},
},
ioutil.Discard,