semantically bump version.txt for alskartv

This commit is contained in:
nomadics9 2024-12-20 01:03:45 +03:00
parent 2472c82f36
commit 5b1dfe1b9c
5 changed files with 118 additions and 1 deletions

Binary file not shown.

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.23.3
require ( require (
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/go-resty/resty/v2 v2.16.2 github.com/go-resty/resty/v2 v2.16.2
golang.org/x/mod v0.17.0
) )
require ( require (

2
go.sum
View file

@ -68,6 +68,8 @@ golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg=
golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

39
main.go
View file

@ -1,10 +1,13 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -12,12 +15,12 @@ import (
func updateVersion(repoPath string, serviceName string) error { func updateVersion(repoPath string, serviceName string) error {
versionPath := fmt.Sprintf("%s/version.txt", repoPath) versionPath := fmt.Sprintf("%s/version.txt", repoPath)
versionFile := "version.txt" versionFile := "version.txt"
newVersion := getVersion(serviceName) newVersion := getVersion(serviceName)
if repoPath == "/data/alaskartv/androidtv-ci" { if repoPath == "/data/alaskartv/androidtv-ci" {
versionPath = fmt.Sprintf("%s/release.txt", repoPath) versionPath = fmt.Sprintf("%s/release.txt", repoPath)
versionFile = "release.txt" versionFile = "release.txt"
bumpVersionTv(repoPath)
} }
os.WriteFile(versionPath, []byte(newVersion), 0644) os.WriteFile(versionPath, []byte(newVersion), 0644)
@ -37,6 +40,40 @@ func updateVersion(repoPath string, serviceName string) error {
return nil return nil
} }
func bumpVersionTv(repoPath string) {
filePath := fmt.Sprintf("%s/version.txt", repoPath)
file, _ := os.Open(filePath)
defer file.Close()
var updatedContent string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, "VERSION_NAME="):
parts := strings.Split(line, "=")
currentPatch := parts[1]
newVersion, _ := Bump(currentPatch, "patch")
updatedContent += fmt.Sprintf("VERSION_NAME=%s\n", newVersion)
case strings.HasPrefix(line, "VERSION_CODE="):
parts := strings.Split(line, "=")
currentCode, _ := strconv.Atoi(parts[1])
newVersionCode := currentCode + 1
updatedContent += fmt.Sprintf("VERSION_CODE=%d\n", newVersionCode)
default:
updatedContent += line + "\n"
}
}
if err := scanner.Err(); err != nil {
fmt.Errorf("error reading %s: %w", filePath, err)
}
}
func main() { func main() {
router := gin.Default() router := gin.Default()

77
semverbump.go Normal file
View file

@ -0,0 +1,77 @@
// Package semverbump provides a function to bump a semVer tag, extending golang.org/x/mod/semver library.
//
// The function Bump() takes a semVer tag and a bump type (major, minor, patch) and returns the new semVer tag.
// For example if the current semVer tag is v1.0.0 and the bump type is patch, the new semVer tag will be v1.0.1.
// If the current semVer tag is v1.0.1 and the bump type is minor, the new semVer tag will be v1.1.0
// (note that the patch version is reset to 0).
//
// Shortened semVer tags are supported, for example if the current semVer tag is v1.0 and the bump type is patch,
// the bumped semVer tag will be v1.0.1.
//
// If the current semVer tag does not have a leading v, it will be added.
// If it is an empty string, it will behave as if the current semVer tag is v0.0.0, e.g. if the bump type is patch,
// the bumped semVer tag will be v0.0.1.
//
// If the current semVer tag is invalid, such as "x.y.z", an error will be returned.
//
// The library currently does not handle pre-release or build metadata.
package main
import (
"fmt"
"golang.org/x/mod/semver"
"strconv"
"strings"
)
// Bump takes a semVer tag and a bump type (major, minor, patch) and returns a new semVer tag
// whose value is incremented according to the bump type. If the current semVer tag does not have a leading v,
// it will be added. If it is an empty string, it will behave as if the current semVer tag is v0.0.0
// Invalid semVer tags will return an error.
func Bump(currentVersion, majorMinorPatch string) (string, error) {
if currentVersion == "" {
currentVersion = "v0.0.0"
} else {
if !semver.IsValid(currentVersion) {
if semver.IsValid("v" + currentVersion) {
currentVersion = "v" + currentVersion
} else {
return "", fmt.Errorf("invalid semVer tag '%s'", currentVersion)
}
}
// semVer lib allows v1 and v1.0, so check for that
if currentVersion == semver.Major(currentVersion) {
currentVersion = currentVersion + ".0.0"
}
if currentVersion == semver.MajorMinor(currentVersion) {
currentVersion = currentVersion + ".0"
}
}
// now we have a valid currentVersion
newMajor, newMinor, newPatch := majorMinorPatchFromSemVer(currentVersion)
if majorMinorPatch == "major" {
newMajor++
newMinor = 0
newPatch = 0
} else if majorMinorPatch == "minor" {
newMinor++
newPatch = 0
} else if majorMinorPatch == "patch" {
newPatch++
} else {
return "", fmt.Errorf("invalid arg for majorMinorPatch, expect 'major', 'minor' or 'patch', got '%s'", majorMinorPatch)
}
return semVerFromMajorMinorPatch(newMajor, newMinor, newPatch), nil
}
func majorMinorPatchFromSemVer(semVer string) (int, int, int) {
parts := strings.Split(semVer, ".")
major, _ := strconv.Atoi(strings.TrimPrefix(parts[0], "v"))
minor, _ := strconv.Atoi(parts[1])
patch, _ := strconv.Atoi(parts[2])
return major, minor, patch
}
func semVerFromMajorMinorPatch(major, minor, patch int) string {
return "v" + strconv.Itoa(major) + "." + strconv.Itoa(minor) + "." + strconv.Itoa(patch)
}