Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer metadata info from filename over optimistic parsing #16

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading