Skip to content

Commit

Permalink
feat: add version_schema config option to allow controlling how the v…
Browse files Browse the repository at this point in the history
…ersion string is parsed.

closes #283
  • Loading branch information
Dj Gilcrease committed Apr 23, 2021
1 parent 39f5bf3 commit f197707
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type Info struct {
Platform string `yaml:"platform,omitempty"`
Epoch string `yaml:"epoch,omitempty"`
Version string `yaml:"version,omitempty"`
VersionSchema string `yaml:"version_schema,omitempty"`
Release string `yaml:"release,omitempty"`
Prerelease string `yaml:"prerelease,omitempty"`
VersionMetadata string `yaml:"version_metadata,omitempty"`
Expand Down Expand Up @@ -230,6 +231,21 @@ func (i *Info) GetChangeLog() (log *chglog.PackageChangeLog, err error) {
}, nil
}

func (i *Info) parseSemver() {
// parse the version as a semver so we can properly split the parts
// and support proper ordering for both rpm and deb
if v, err := semver.NewVersion(i.Version); err == nil {
i.Version = fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
if i.Prerelease == "" {
i.Prerelease = v.Prerelease()
}

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

// Overridables contain the field which are overridable in a package.
type Overridables struct {
Replaces []string `yaml:"replaces,omitempty"`
Expand Down Expand Up @@ -387,6 +403,16 @@ func WithDefaults(info *Info) *Info {
}
}

switch info.VersionSchema {
case "none":
// No change to the version or prerelease info set in the YAML file
break
case "semver":
fallthrough
default:
info.parseSemver()
}

return info
}

Expand Down
12 changes: 12 additions & 0 deletions nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestGet(t *testing.T) {
func TestDefaultsVersion(t *testing.T) {
info := &nfpm.Info{
Version: "v1.0.0",
VersionSchema: "semver",
}
info = nfpm.WithDefaults(info)
require.NotEmpty(t, info.Platform)
Expand Down Expand Up @@ -81,6 +82,17 @@ func TestDefaultsVersion(t *testing.T) {
require.Equal(t, "1.0.0", info.Version)
require.Equal(t, "2", info.Release)
require.Equal(t, "beta1", info.Prerelease)

info = &nfpm.Info{
Version: "this.is.my.version",
VersionSchema: "none",
Release: "2",
Prerelease: "beta1",
}
info = nfpm.WithDefaults(info)
require.Equal(t, "this.is.my.version", info.Version)
require.Equal(t, "2", info.Release)
require.Equal(t, "beta1", info.Prerelease)
}

func TestDefaults(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ platform: linux
# This will expand any env var you set in the field, eg version: v${SEMVER}
version: v1.2.3

# Version Schema allows you to specify how to parse the version String.
# Default is `semver`
# `semver` parse the version string as a valid semver version
# `none` skip trying to parse the version string and just use what is passed in
version_schema: semver

# Version Epoch.
# Default is extracted from `version` if it is semver compatible.
epoch: 2
Expand Down

0 comments on commit f197707

Please sign in to comment.