From 523bb884a9e142aae63a52418cfa6b0b732af6df Mon Sep 17 00:00:00 2001 From: Xiwen Cheng Date: Sun, 19 Sep 2021 12:03:42 +0200 Subject: [PATCH] Improve version number conversion --- cmd/mendix-userlib-cleaner/main.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/mendix-userlib-cleaner/main.go b/cmd/mendix-userlib-cleaner/main.go index ff6264a..0292a6c 100644 --- a/cmd/mendix-userlib-cleaner/main.go +++ b/cmd/mendix-userlib-cleaner/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "regexp" "strings" "archive/zip" @@ -170,8 +171,7 @@ func parseManifest(filePath string, text string) JarProperties { jarProp.packageName = pair[1] } else if pair[0] == "Bundle-Version" { jarProp.version = pair[1] - // FIXME: Use smart conversion - jarProp.versionNumber, _ = strconv.Atoi(strings.ReplaceAll(jarProp.version, ".", "")) + jarProp.versionNumber = convertVersionToNumber(jarProp.version) } else if pair[0] == "Bundle-Vendor" { jarProp.vendor = pair[1] } else if pair[0] == "Bundle-License" { @@ -197,8 +197,7 @@ func parsePOM(filePath string, text string) JarProperties { artifactId = pair[1] } else if pair[0] == "version" { jarProp.version = pair[1] - // FIXME: Use smart conversion - jarProp.versionNumber, _ = strconv.Atoi(strings.ReplaceAll(jarProp.version, ".", "")) + jarProp.versionNumber = convertVersionToNumber(jarProp.version) } } if groupId != "" && artifactId != "" { @@ -265,3 +264,20 @@ func cleanJars(remove bool, jars []JarProperties, keepJars map[string]JarPropert } return count } + +func convertVersionToNumber(version string) int { + // naive implementation. Feel free to suggest improvements + + re := regexp.MustCompile("[0-9]+") + + multiplier := 1000 + number := 0 + for _, c := range re.FindAllString(version, -1) { + t, _ := strconv.Atoi(c) + if number > 0 { + number = number * multiplier + } + number += t + } + return number +}