Skip to content

Commit

Permalink
Improve version number conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
xiwenc committed Sep 19, 2021
1 parent c3d7d63 commit 523bb88
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/mendix-userlib-cleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"

"archive/zip"
Expand Down Expand Up @@ -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" {
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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
}

0 comments on commit 523bb88

Please sign in to comment.