Skip to content

Commit

Permalink
Prefer metadata info from filename over optimistic parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
xiwenc committed Nov 8, 2024
1 parent 40511ca commit 74220a6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ Bundle-SymbolicName: com.google.gson
Archiver-Version: Plexus Archiver
```

### File name parsing

When no usable metadata is found in the manifest or POM files, we try to parse the filename itself to determine the package name and version. For example:

```
eventTrackingLibrary-1.0.2.jar -> local.eventtracking
```

And version would be `1.0.2`.


### Optimistic parsing

Sometimes there is no usable metadata included in the package. In that case we try to compose the package name by finding the first class file in the jar of which resides in the directory `org` or `com`. e.g. `org.junit`
Expand Down
45 changes: 41 additions & 4 deletions cmd/mendix-userlib-cleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,17 @@ func getJarProps(filePath string, mode string) JarProperties {
}
}

jar3 := parseFileName(filePath)
if jar3.packageName != "" {
log.Debugf("Parsed properties optimistically: %v", jar3)
return jar3
}

if mode == "auto" {
jar3 := parseOptimistic(filePath)
if jar3.packageName != "" {
log.Debugf("Parsed properties optimistically: %v", jar3)
return jar3
jar4 := parseOptimistic(filePath)
if jar4.packageName != "" {
log.Debugf("Parsed properties optimistically: %v", jar4)
return jar4
}
}

Expand Down Expand Up @@ -289,6 +295,37 @@ func parseOptimistic(filePath string) JarProperties {
return jarProp
}

func parseFileName(filePath string) JarProperties {
// Initialize empty JarProperties with filepath and filename
jarProp := JarProperties{
filePath: filePath,
fileName: filepath.Base(filePath),
packageName: "",
}

// Split filename on - to get name and version parts
// e.g. "eventTrackingLibrary-1.0.2.jar" -> ["eventTrackingLibrary", "1.0.2.jar"]
parts := strings.Split(filepath.Base(filePath), "-")
if len(parts) < 2 {
return jarProp
}

// Get the version by removing .jar from last part
version := strings.TrimSuffix(parts[len(parts)-1], ".jar")
jarProp.version = version
jarProp.versionNumber = convertVersionToNumber(version)

// Join all parts except last one to get name
name := strings.Join(parts[:len(parts)-1], "-")
jarProp.name = name

// Convert name to package format (assuming Java package naming convention)
// e.g. "eventTrackingLibrary" -> "local.eventtracking"
jarProp.packageName = "local." + strings.ToLower(name)

return jarProp
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
Binary file added resources/jars/Email_Connector-1.0.0.jar
Binary file not shown.
Binary file added resources/jars/eventTrackingLibrary-1.0.2.jar
Binary file not shown.
Binary file added resources/jars/org.hsqldb.hsqldb-2.5.0.jar
Binary file not shown.
Binary file added resources/jars/org.hsqldb.hsqldb-2.7.1.jar
Binary file not shown.

0 comments on commit 74220a6

Please sign in to comment.