Skip to content

Commit

Permalink
feat: Introduce version_metadata for all packagers and deprecate deb.…
Browse files Browse the repository at this point in the history
…metadata. (#215)
  • Loading branch information
erikgeiser authored Aug 20, 2020
1 parent 3c4244d commit 0c25629
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 51 deletions.
2 changes: 1 addition & 1 deletion acceptance/testdata/apk.env-var-version.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine
ARG package
COPY ${package} /tmp/foo.apk
RUN apk add --allow-untrusted /tmp/foo.apk
ENV EXPECTVER="foo-1.0.0~0.1.b1 description:"
ENV EXPECTVER="foo-1.0.0~0.1.b1+git.abcdefgh description:"
RUN apk info foo | grep "foo-" | grep " description:" > found
RUN export FOUND_VER="$(cat found)" && \
echo "Expected: '${EXPECTVER}' :: Found: '${FOUND_VER}'" && \
Expand Down
2 changes: 1 addition & 1 deletion acceptance/testdata/deb.env-var-version.dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM ubuntu
ARG package
COPY ${package} /tmp/foo.deb
ENV EXPECTVER=" Version: 1.0.0~0.1.b1"
ENV EXPECTVER=" Version: 1.0.0~0.1.b1+git.abcdefgh"
RUN dpkg --info /tmp/foo.deb | grep "Version" > found
RUN export FOUND_VER="$(cat found)" && \
echo "Expected: '${EXPECTVER}' :: Found: '${FOUND_VER}'" && \
Expand Down
2 changes: 1 addition & 1 deletion acceptance/testdata/rpm.env-var-version.dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM fedora
ARG package
COPY ${package} /tmp/foo.rpm
ENV EXPECTVER="Version : 1.0.0~0.1.b1" \
ENV EXPECTVER="Version : 1.0.0~0.1.b1+git.abcdefgh" \
EXPECTREL="Release : 1"
RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Version" > found.ver
RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Release" > found.rel
Expand Down
22 changes: 20 additions & 2 deletions apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ type Apk struct{}

func (a *Apk) ConventionalFileName(info *nfpm.Info) string {
// TODO: verify this
return fmt.Sprintf("%s_%s_%s.apk", info.Name, info.Version, info.Arch)
arch, ok := archToAlpine[info.Arch]
if !ok {
arch = info.Arch
}

version := info.Version
if info.Release != "" {
version += "-" + info.Release
}

if info.Prerelease != "" {
version += "~" + info.Prerelease
}

if info.VersionMetadata != "" {
version += "+" + info.VersionMetadata
}

return fmt.Sprintf("%s_%s_%s.apk", info.Name, version, arch)
}

// Package writes a new apk package to the given writer using the given info.
Expand Down Expand Up @@ -453,7 +471,7 @@ pkgname = {{.Info.Name}}
pkgver = {{ if .Info.Epoch}}{{ .Info.Epoch }}:{{ end }}{{.Info.Version}}
{{- if .Info.Release}}-{{ .Info.Release }}{{- end }}
{{- if .Info.Prerelease}}~{{ .Info.Prerelease }}{{- end }}
{{- if .Info.Deb.VersionMetadata}}+{{ .Info.Deb.VersionMetadata }}{{- end }}
{{- if .Info.VersionMetadata}}+{{ .Info.VersionMetadata }}{{- end }}
arch = {{.Info.Arch}}
size = {{.InstalledSize}}
pkgdesc = {{multiline .Info.Description}}
Expand Down
7 changes: 6 additions & 1 deletion deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ func (*Deb) ConventionalFileName(info *nfpm.Info) string {
if info.Release != "" {
version += "-" + info.Release
}

if info.Prerelease != "" {
version += "~" + info.Prerelease
}

if info.VersionMetadata != "" {
version += "+" + info.VersionMetadata
}

// package_version_architecture.package-type
return fmt.Sprintf("%s_%s_%s.deb", info.Name, version, arch)
}
Expand Down Expand Up @@ -502,7 +507,7 @@ Package: {{.Info.Name}}
Version: {{ if .Info.Epoch}}{{ .Info.Epoch }}:{{ end }}{{.Info.Version}}
{{- if .Info.Release}}-{{ .Info.Release }}{{- end }}
{{- if .Info.Prerelease}}~{{ .Info.Prerelease }}{{- end }}
{{- if .Info.Deb.VersionMetadata}}+{{ .Info.Deb.VersionMetadata }}{{- end }}
{{- if .Info.VersionMetadata}}+{{ .Info.VersionMetadata }}{{- end }}
Section: {{.Info.Section}}
Priority: {{.Info.Priority}}
Architecture: {{.Info.Arch}}
Expand Down
59 changes: 48 additions & 11 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,61 @@ func TestDebVersionWithRelease(t *testing.T) {
}

func TestDebVersionWithPrerelease(t *testing.T) {
var buf bytes.Buffer

info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Prerelease = "1"
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
err := writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
var v = extractDebVersion(&buf)
v := extractDebVersion(&buf)
assert.Equal(t, "1.0.0~1", v)
}

func TestDebVersionWithReleaseAndPrerelease(t *testing.T) {
var buf bytes.Buffer

info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Release = "2"
info.Prerelease = "rc1"
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
info.Prerelease = "rc1" //nolint:golint,goconst
err := writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
var v = extractDebVersion(&buf)
v := extractDebVersion(&buf)
assert.Equal(t, "1.0.0-2~rc1", v)
}

func TestDebVersionWithVersionMetadata(t *testing.T) {
var buf bytes.Buffer

info := exampleInfo()
info.Version = "1.0.0+meta" //nolint:golint,goconst
info.VersionMetadata = ""
err := writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
v := extractDebVersion(&buf)
assert.Equal(t, "1.0.0+meta", v)

buf.Reset()

info.Version = "1.0.0" //nolint:golint,goconst
info.VersionMetadata = "meta"
err = writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0+meta", v)

buf.Reset()

info.Version = "1.0.0+foo" //nolint:golint,goconst
info.Prerelease = "alpha"
info.VersionMetadata = "meta"
err = writeControl(&buf, controlData{nfpm.WithDefaults(info), 0})
require.NoError(t, err)
v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0~alpha+meta", v)
}

func TestControl(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Expand Down Expand Up @@ -399,21 +432,25 @@ func TestDEBConventionalFileName(t *testing.T) {
Release string
Prerelease string
Expected string
Metadata string
}{
{Version: "1.2.3", Release: "", Prerelease: "",
{Version: "1.2.3", Release: "", Prerelease: "", Metadata: "",
Expected: fmt.Sprintf("%s_1.2.3_%s.deb", info.Name, info.Arch)},
{Version: "1.2.3", Release: "4", Prerelease: "",
{Version: "1.2.3", Release: "4", Prerelease: "", Metadata: "",
Expected: fmt.Sprintf("%s_1.2.3-4_%s.deb", info.Name, info.Arch)},
{Version: "1.2.3", Release: "4", Prerelease: "5",
{Version: "1.2.3", Release: "4", Prerelease: "5", Metadata: "",
Expected: fmt.Sprintf("%s_1.2.3-4~5_%s.deb", info.Name, info.Arch)},
{Version: "1.2.3", Release: "", Prerelease: "5",
{Version: "1.2.3", Release: "", Prerelease: "5", Metadata: "",
Expected: fmt.Sprintf("%s_1.2.3~5_%s.deb", info.Name, info.Arch)},
{Version: "1.2.3", Release: "1", Prerelease: "5", Metadata: "git",
Expected: fmt.Sprintf("%s_1.2.3-1~5+git_%s.deb", info.Name, info.Arch)},
}

for _, testCase := range testCases {
info.Version = testCase.Version
info.Release = testCase.Release
info.Prerelease = testCase.Prerelease
info.VersionMetadata = testCase.Metadata

assert.Equal(t, testCase.Expected, Default.ConventionalFileName(info))
}
Expand Down
55 changes: 36 additions & 19 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,25 @@ func (c *Config) Validate() error {

// Info contains information about a single package.
type Info struct {
Overridables `yaml:",inline"`
Name string `yaml:"name,omitempty"`
Arch string `yaml:"arch,omitempty"`
Platform string `yaml:"platform,omitempty"`
Epoch string `yaml:"epoch,omitempty"`
Version string `yaml:"version,omitempty"`
Release string `yaml:"release,omitempty"`
Prerelease string `yaml:"prerelease,omitempty"`
Section string `yaml:"section,omitempty"`
Priority string `yaml:"priority,omitempty"`
Maintainer string `yaml:"maintainer,omitempty"`
Description string `yaml:"description,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Homepage string `yaml:"homepage,omitempty"`
License string `yaml:"license,omitempty"`
Bindir string `yaml:"bindir,omitempty"` // Deprecated: this does nothing. TODO: remove.
Changelog string `yaml:"changelog,omitempty"`
Target string `yaml:"-"`
Overridables `yaml:",inline"`
Name string `yaml:"name,omitempty"`
Arch string `yaml:"arch,omitempty"`
Platform string `yaml:"platform,omitempty"`
Epoch string `yaml:"epoch,omitempty"`
Version string `yaml:"version,omitempty"`
Release string `yaml:"release,omitempty"`
Prerelease string `yaml:"prerelease,omitempty"`
VersionMetadata string `yaml:"version_metadata,omitempty"`
Section string `yaml:"section,omitempty"`
Priority string `yaml:"priority,omitempty"`
Maintainer string `yaml:"maintainer,omitempty"`
Description string `yaml:"description,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Homepage string `yaml:"homepage,omitempty"`
License string `yaml:"license,omitempty"`
Bindir string `yaml:"bindir,omitempty"` // Deprecated: this does nothing. TODO: remove.
Changelog string `yaml:"changelog,omitempty"`
Target string `yaml:"-"`
}

// Overridables contain the field which are overridable in a package.
Expand Down Expand Up @@ -165,7 +166,7 @@ type RPM struct {
type Deb struct {
Scripts DebScripts `yaml:"scripts,omitempty"`
Triggers DebTriggers `yaml:"triggers,omitempty"`
VersionMetadata string `yaml:"metadata,omitempty"`
VersionMetadata string `yaml:"metadata,omitempty"` // Deprecated: Moved to Info
}

// DebTriggers contains triggers only available for deb packages.
Expand Down Expand Up @@ -213,6 +214,18 @@ func Validate(info *Info) error {
if info.Version == "" {
return ErrFieldEmpty{"version"}
}

// deprecation warnings
if info.Deb.VersionMetadata != "" {
fmt.Fprintln(os.Stderr,
"Warning: deb.metadata is deprecated and will be removed in a future version "+
"(moved to version_metadata)")
}

if info.Bindir != "" {
fmt.Fprintln(os.Stderr, "Warning: bindir is deprecated and will be removed in a future version")
}

return nil
}

Expand All @@ -232,6 +245,10 @@ func WithDefaults(info *Info) *Info {
if info.Prerelease == "" {
info.Prerelease = v.Prerelease()
}

if info.VersionMetadata == "" {
info.VersionMetadata = v.Metadata()
}
}

return info
Expand Down
12 changes: 9 additions & 3 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,17 @@ func buildRPMMeta(info *nfpm.Info) (*rpmpack.RPMMetaData, error) {
}

func formatVersion(info *nfpm.Info) string {
if info.Prerelease == "" {
return info.Version
version := info.Version

if info.Prerelease != "" {
version += "~" + info.Prerelease
}

if info.VersionMetadata != "" {
version += "+" + info.VersionMetadata
}

return info.Version + "~" + info.Prerelease
return version
}

func defaultTo(in, def string) string {
Expand Down
29 changes: 25 additions & 4 deletions rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ func TestRPMVersionWithReleaseAndPrerelease(t *testing.T) {
assert.Equal(t, "0.2", meta.Release)
}

func TestRPMVersionWithVersionMetadata(t *testing.T) {
// https://fedoraproject.org/wiki/Package_Versioning_Examples#Complex_versioning_examples
info := exampleInfo()

info.Version = "1.0.0+meta"
info.VersionMetadata = ""
meta, err := buildRPMMeta(nfpm.WithDefaults(info))
require.NoError(t, err)
assert.Equal(t, "1.0.0+meta", meta.Version)

info.Version = "1.0.0"
info.VersionMetadata = "meta"
meta, err = buildRPMMeta(nfpm.WithDefaults(info))
require.NoError(t, err)
assert.Equal(t, "1.0.0+meta", meta.Version)
}

func TestWithInvalidEpoch(t *testing.T) {
f, err := ioutil.TempFile("", "test.rpm")
defer func() {
Expand Down Expand Up @@ -378,21 +395,25 @@ func TestRPMConventionalFileName(t *testing.T) {
Release string
Prerelease string
Expected string
Metadata string
}{
{Version: "1.2.3", Release: "", Prerelease: "",
{Version: "1.2.3", Release: "", Prerelease: "", Metadata: "",
Expected: fmt.Sprintf("%s-1.2.3.%s.rpm", info.Name, info.Arch)},
{Version: "1.2.3", Release: "4", Prerelease: "",
{Version: "1.2.3", Release: "4", Prerelease: "", Metadata: "",
Expected: fmt.Sprintf("%s-1.2.3-4.%s.rpm", info.Name, info.Arch)},
{Version: "1.2.3", Release: "4", Prerelease: "5",
{Version: "1.2.3", Release: "4", Prerelease: "5", Metadata: "",
Expected: fmt.Sprintf("%s-1.2.3~5-4.%s.rpm", info.Name, info.Arch)},
{Version: "1.2.3", Release: "", Prerelease: "5",
{Version: "1.2.3", Release: "", Prerelease: "5", Metadata: "",
Expected: fmt.Sprintf("%s-1.2.3~5.%s.rpm", info.Name, info.Arch)},
{Version: "1.2.3", Release: "1", Prerelease: "5", Metadata: "git",
Expected: fmt.Sprintf("%s-1.2.3~5+git-1.%s.rpm", info.Name, info.Arch)},
}

for _, testCase := range testCases {
info.Version = testCase.Version
info.Release = testCase.Release
info.Prerelease = testCase.Prerelease
info.VersionMetadata = testCase.Metadata

assert.Equal(t, testCase.Expected, Default.ConventionalFileName(info))
}
Expand Down
16 changes: 8 additions & 8 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ version: v1.2.3
# Default is extracted from `version` if it is semver compatible.
epoch: 2

# Version Release.
# Default is extracted from `version` if it is semver compatible.
release: 1

# Version Prerelease.
# Default is extracted from `version` if it is semver compatible.
prerelease: beta1

# Version Metadata (previously deb.metadata).
# Default is extracted from `version` if it is semver compatible.
# Setting metadata might interfere with version comparisons depending on the packager.
version_metadata: git

# Version Release.
release: 1

# Section.
section: default

Expand Down Expand Up @@ -141,10 +145,6 @@ rpm:

# Custon configuration applied only to the Deb packager.
deb:
# Custom version metadata.
# Setting metadata might interfere with version comparisons.
metadata: xyz2

# Custom deb rules script.
scripts:
rules: foo.sh
Expand Down

1 comment on commit 0c25629

@vercel
Copy link

@vercel vercel bot commented on 0c25629 Aug 20, 2020

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.