Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version_schema config option to allow controlling how the version string is parsed. #324

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 additions & 1 deletion nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestGet(t *testing.T) {

func TestDefaultsVersion(t *testing.T) {
info := &nfpm.Info{
Version: "v1.0.0",
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