1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-09-27 23:09:52 +02:00
nfpm/rpm/rpm.go

152 lines
3.3 KiB
Go
Raw Normal View History

2018-02-03 20:42:56 +01:00
package rpm
import (
2018-02-05 00:00:20 +01:00
"bytes"
2018-02-03 20:42:56 +01:00
"fmt"
"io"
"io/ioutil"
"log"
"os"
2018-02-05 00:00:20 +01:00
"os/exec"
2018-02-03 20:42:56 +01:00
"path/filepath"
2018-02-05 00:00:20 +01:00
"strings"
2018-02-03 20:42:56 +01:00
2018-02-05 00:00:20 +01:00
"github.com/alecthomas/template"
2018-02-03 20:42:56 +01:00
"github.com/goreleaser/archive"
"github.com/goreleaser/packager"
)
var _ packager.Packager = Default
// Default deb packager
var Default = &RPM{}
// RPM is a RPM packager implementation
type RPM struct{}
// Package writes a new RPM package to the given writer using the given info
2018-02-05 00:19:00 +01:00
func (*RPM) Package(info packager.Info, w io.Writer) error {
if info.Arch == "amd64" {
info.Arch = "x86_64"
}
2018-02-03 20:42:56 +01:00
root, err := ioutil.TempDir("", info.Name)
if err != nil {
return err
}
for _, folder := range []string{
"RPMS",
"SRPMS",
"BUILD",
"SOURCES",
"SPECS",
"tmp",
} {
if err := os.Mkdir(filepath.Join(root, folder), 0744); err != nil {
return err
}
}
2018-02-05 00:00:20 +01:00
targz, err := os.Create(filepath.Join(root, "SOURCES", fmt.Sprintf("%s-%s.tar.gz", info.Name, info.Version)))
2018-02-03 20:42:56 +01:00
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 {
2018-02-05 00:00:20 +01:00
if err := archive.Add(fmt.Sprintf("%s-%s/%s", info.Name, info.Version, dst), src); err != nil {
2018-02-03 20:42:56 +01:00
return fmt.Errorf("failed to add file %s to tar.gz: %s", src, err)
}
}
2018-02-05 00:00:20 +01:00
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)
}
2018-02-03 20:42:56 +01:00
var args = []string{
"--define", fmt.Sprintf("_topdir %s", root),
"--define", fmt.Sprintf("_tmppath %s/tmp", root),
2018-02-05 00:00:20 +01:00
"-ba",
"SPECS/" + info.Name + ".spec",
2018-02-03 20:42:56 +01:00
}
2018-02-05 00:00:20 +01:00
cmd := exec.Command("rpmbuild", args...)
cmd.Dir = root
out, err := cmd.CombinedOutput()
log.Println(string(out))
2018-02-05 00:19:00 +01:00
rpm, err := os.Open(filepath.Join(
root,
"RPMS",
info.Arch,
fmt.Sprintf("%s-%s-1.%s.rpm", info.Name, info.Version, info.Arch),
))
if err != nil {
return err
}
_, err = io.Copy(w, rpm)
2018-02-05 00:00:20 +01:00
return err
2018-02-03 20:42:56 +01:00
}
2018-02-05 00:00:20 +01:00
// TODO: declare dependencies and etc here as well
2018-02-03 20:42:56 +01:00
const specTemplate = `
2018-02-05 00:00:20 +01:00
# 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
2018-02-03 20:42:56 +01:00
%define __spec_install_post %{nil}
%define debug_package %{nil}
%define __os_install_post %{_dbpath}/brp-compress
2018-02-05 00:00:20 +01:00
Name: {{ .Name }}
Summary: {{ first_line .Description }}
Version: {{ .Version }}
2018-02-03 20:42:56 +01:00
Release: 1
2018-02-05 00:00:20 +01:00
License: {{ .License }}
2018-02-03 20:42:56 +01:00
Group: Development/Tools
2018-02-05 00:00:20 +01:00
SOURCE0 : %{name}-%{version}.tar.gz
URL: {{ .Homepage }}
2018-02-03 20:42:56 +01:00
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
%description
2018-02-05 00:00:20 +01:00
{{ .Description }}
2018-02-03 20:42:56 +01:00
%prep
%setup -q
%build
# Empty section.
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}
# in builddir
cp -a * %{buildroot}
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
2018-02-05 00:19:00 +01:00
{{ range $index, $element := .ConfigFiles }}
#%config(noreplace) {{ . }}
{{ end }}
2018-02-03 20:42:56 +01:00
%{_bindir}/*
2018-02-05 00:00:20 +01:00
%changelog
# noop
2018-02-03 20:42:56 +01:00
`