Repository: goreleaser/nfpm
Pull request #324 (2021-04-22 20:00:09)

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

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/nfpm/Hn8xpyzzHqVwTqVBcZp5TknNBPBZ](https://vercel.com/goreleaser/nfpm/Hn8xpyzzHqVwTqVBcZp5TknNBPBZ)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#324](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (4b608ad) into [master](https://codecov.io/gh/goreleaser/nfpm/commit/1fb26910a00fb1e7625f2763db819521f87f1134?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (1fb2691) will **decrease** coverage by `0.02%`.
> The diff coverage is `72.41%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/nfpm/pull/324/graphs/tree.svg?width=650&height=150&src=pr&token=9QKGIxn7wg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
- Coverage   69.37%   69.34%   -0.03%     
==========================================
  Files           9        9              
  Lines        1159     1184      +25     
==========================================
+ Hits          804      821      +17     
- Misses        223      229       +6     
- Partials      132      134       +2     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [rpm/rpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cnBtL3JwbS5nbw==) | `73.20% <60.00%> (-0.67%)` | :arrow_down: |
| [nfpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-bmZwbS5nbw==) | `83.60% <69.23%> (-1.72%)` | :arrow_down: |
| [apk/apk.go](https://codecov.io/gh/goreleaser/nfpm/pull/324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-YXBrL2Fway5nbw==) | `70.29% <100.00%> (+0.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [39f5bf3...4b608ad](https://codecov.io/gh/goreleaser/nfpm/pull/324?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).

caarlos0: can we add tests for it as well?

Patch:
diff --git a/nfpm.go b/nfpm.go
index 8c25078..353dfdc 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -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"`
@@ -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"`
@@ -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
 }
 
diff --git a/www/docs/configuration.md b/www/docs/configuration.md
index 16abb6c..97eeea1 100644
--- a/www/docs/configuration.md
+++ b/www/docs/configuration.md
@@ -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


Test patch:
diff --git a/nfpm_test.go b/nfpm_test.go
index 964deae..fb1f68d 100644
--- a/nfpm_test.go
+++ b/nfpm_test.go
@@ -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)
@@ -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) {