Repository: goreleaser/nfpm (go)

Issue:
APK: setting release creates invalid package version
### What happened?

I'm currently trying to package APK packages beside DEB and RPM packages. In my example I use the following configuration (explicitly set all versions)

```
name: testpackage
arch: "amd64"
platform: "linux"
version: 1.4.4
release: 1289492819
maintainer: "Test Maintainer <test@example.org>"
description: |
  My Test tool
contents:
- src: ./testbinary
  dst: /usr/bin/testbinary
```

When packaging this as deb, rpm and apk, the following package metadata will be created:

```
############################################################
# RPM good (valid)
############################################################
Epoch       : 0
Version     : 1.4.4
Release     : 1289492819
############################################################
# DEB good (valid)
############################################################
Version: 1.4.4-1289492819
############################################################
# APK not good (invalid)
# Expected -r1289492819 or _p1289492819, but not -1289492819 after the version
# (where Something is a timestamp)
############################################################
pkgver = 1.4.4-1289492819
```
The created pkgver is invalid which can be checked using apk version --check (empty response means everything is okay):

```
❯ podman run -ti --rm alpine:latest
/ # apk version --check 1.4.4-1289492819
1.4.4-1289492819
/ # apk version --check 1.4.4_p1289492819
/ # apk version --check 1.4.4-r1289492819
/ #
```

Expectation: nfpm generated a package with `-r<release>` or `_p<release>`  - assume that `-r<release>` is the best way here.

Additionally, the resulting file name should not contain an architecture but only the version string (e.g. `dhcp-doc-4.4.3_p1-r4.apk` (name of the package, `_p` if there is some kind of patch(?) version and then `-r` if there is a package version specified)

### How can we reproduce this?

Build an apk package with the described configuration above, then:

```
  mkdir /tmp/extracted
  cp *.apk /tmp/extracted
  cd /tmp/extracted
  for i in *.apk; do mv $i "${i}.gz"; gunzip "${i}.gz"; tar -xf "${i}"; done
  cat .PKGINFO
```


### nfpm version

```bash
       _____ ____  __  __
 _ __ |  ___|  _ \|  \/  |
| '_ \| |_  | |_) | |\/| |
| | | |  _| |  __/| |  | |
|_| |_|_|   |_|   |_|  |_|
nfpm: a simple and 0-dependencies deb, rpm, apk and arch linux packager written in Go
https://nfpm.goreleaser.com

GitVersion:    2.35.3
GitCommit:     5ace4da2fd3162383a16218e2ef405dc2676bdfc
GitTreeState:  false
BuildDate:     2024-01-31T17:53:18Z
BuiltBy:       goreleaser
GoVersion:     go1.21.6
Compiler:      gc
ModuleSum:     h1:YGEygriY8hbsNdCBUif6RLb5xPISDHc+d22rRGXV4Zk=
Platform:      linux/amd64
{code}
```


### Search

- [X] I did search for other open and closed issues before opening this.

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct

### Additional context

[Source code of the apk tools (version.c)](https://gitlab.alpinelinux.org/alpine/apk-tools/-/blob/2.14-stable/src/version.c)

Pull request:
fix(apk): conventional name template
closes #793

Patch:
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
index 025b22d..9b60f34 100644
--- a/.github/workflows/docs.yml
+++ b/.github/workflows/docs.yml
@@ -20,7 +20,7 @@ jobs:
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v4
-      - uses: cachix/install-nix-action@v25
+      - uses: cachix/install-nix-action@v26
         with:
           nix_path: nixpkgs=channel:nixos-unstable
           github_access_token: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index f720691..307fec6 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -35,10 +35,10 @@ jobs:
         with:
           repo-token: ${{ secrets.GITHUB_TOKEN }}
       - uses: sigstore/cosign-installer@v3.4.0
-      - uses: anchore/sbom-action/download-syft@v0.15.8
+      - uses: anchore/sbom-action/download-syft@v0.15.9
       - uses: docker/setup-qemu-action@v3
       - uses: docker/setup-buildx-action@v3
-      - uses: cachix/install-nix-action@v25
+      - uses: cachix/install-nix-action@v26
         with:
           github_access_token: ${{ secrets.GITHUB_TOKEN }}
       - run: task setup
diff --git a/apk/apk.go b/apk/apk.go
index 33304bc..d2c987b 100644
--- a/apk/apk.go
+++ b/apk/apk.go
@@ -88,16 +88,7 @@ type Apk struct{}
 
 func (a *Apk) ConventionalFileName(info *nfpm.Info) string {
 	info = ensureValidArch(info)
-	version := info.Version
-
-	if info.Prerelease != "" {
-		version += "_" + info.Prerelease
-	}
-
-	if info.Release != "" {
-		version += "_p" + info.Release
-	}
-
+	version := pkgver(info)
 	return fmt.Sprintf("%s_%s_%s.apk", info.Name, version, info.Arch)
 }
 
@@ -469,9 +460,7 @@ func copyToTarAndDigest(file *files.Content, tw *tar.Writer, sizep *int64) error
 const controlTemplate = `
 {{- /* Mandatory fields */ -}}
 pkgname = {{.Info.Name}}
-pkgver = {{.Info.Version}}
-		 {{- if .Info.Prerelease}}{{ .Info.Prerelease }}{{- end }}
-         {{- if .Info.Release}}-{{ .Info.Release }}{{- end }}
+pkgver = {{ pkgver .Info }}
 arch = {{.Info.Arch}}
 size = {{.InstalledSize}}
 pkgdesc = {{multiline .Info.Description}}
@@ -509,6 +498,33 @@ func writeControl(w io.Writer, data controlData) error {
 			ret := strings.ReplaceAll(strs, "\n", "\n  ")
 			return strings.Trim(ret, " \n")
 		},
+		"pkgver": pkgver,
 	})
 	return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
 }
+
+func pkgver(info *nfpm.Info) string {
+	version := info.Version
+
+	if info.Prerelease != "" {
+		version += "_" + info.Prerelease
+	}
+
+	if rel := info.Release; rel != "" {
+		if !strings.HasPrefix(rel, "r") {
+			rel = "r" + rel
+		}
+		version += "-" + rel
+	}
+	if meta := info.VersionMetadata; meta != "" {
+		if !strings.HasPrefix(meta, "p") &&
+			!strings.HasPrefix(meta, "cvs") &&
+			!strings.HasPrefix(meta, "svn") &&
+			!strings.HasPrefix(meta, "git") &&
+			!strings.HasPrefix(meta, "hg") {
+			meta = "p" + meta
+		}
+		version += "-" + meta
+	}
+	return version
+}


Test patch:
diff --git a/apk/apk_test.go b/apk/apk_test.go
index a631011..b3b6900 100644
--- a/apk/apk_test.go
+++ b/apk/apk_test.go
@@ -477,12 +477,16 @@ func TestAPKConventionalFileName(t *testing.T) {
 			Expect: "default_1.2.3_x86_64.apk",
 		},
 		{
-			Arch: "386", Version: "1.2.3", Prerelease: "git",
-			Expect: "default_1.2.3_git_x86.apk",
+			Arch: "386", Version: "1.2.3", Meta: "git",
+			Expect: "default_1.2.3-git_x86.apk",
+		},
+		{
+			Arch: "386", Version: "1.2.3", Meta: "1", Release: "10",
+			Expect: "default_1.2.3-r10-p1_x86.apk",
 		},
 		{
 			Arch: "386", Version: "1.2.3", Prerelease: "git", Release: "1",
-			Expect: "default_1.2.3_git_p1_x86.apk",
+			Expect: "default_1.2.3_git-r1_x86.apk",
 		},
 		{
 			Arch: "all", Version: "1.2.3",
@@ -490,7 +494,11 @@ func TestAPKConventionalFileName(t *testing.T) {
 		},
 		{
 			Arch: "386", Version: "1.2.3", Release: "1", Prerelease: "beta1",
-			Expect: "default_1.2.3_beta1_p1_x86.apk",
+			Expect: "default_1.2.3_beta1-r1_x86.apk",
+		},
+		{
+			Arch: "amd64", Version: "1.2.3a", Prerelease: "alpha1", Release: "47", Meta: "git-aaaccc",
+			Expect: "default_1.2.3a_alpha1-r47-git-aaaccc_x86_64.apk",
 		},
 	}
 
diff --git a/apk/testdata/TestControl.golden b/apk/testdata/TestControl.golden
index b7a4177..6f8901d 100644
--- a/apk/testdata/TestControl.golden
+++ b/apk/testdata/TestControl.golden
@@ -1,5 +1,5 @@
 pkgname = foo
-pkgver = 1.0.0beta1-r1
+pkgver = 1.0.0_beta1-r1
 arch = amd64
 size = 10
 pkgdesc = Foo does things
diff --git a/apk/testdata/TestCreateBuilderControl.golden b/apk/testdata/TestCreateBuilderControl.golden
index 549e459..181c72c 100644
--- a/apk/testdata/TestCreateBuilderControl.golden
+++ b/apk/testdata/TestCreateBuilderControl.golden
@@ -1,5 +1,5 @@
 pkgname = foo
-pkgver = 1.0.0beta1-r1
+pkgver = 1.0.0_beta1-r1
 arch = amd64
 size = 12345
 pkgdesc = Foo does things
diff --git a/apk/testdata/TestCreateBuilderControlScripts.golden b/apk/testdata/TestCreateBuilderControlScripts.golden
index 549e459..181c72c 100644
--- a/apk/testdata/TestCreateBuilderControlScripts.golden
+++ b/apk/testdata/TestCreateBuilderControlScripts.golden
@@ -1,5 +1,5 @@
 pkgname = foo
-pkgver = 1.0.0beta1-r1
+pkgver = 1.0.0_beta1-r1
 arch = amd64
 size = 12345
 pkgdesc = Foo does things