Skip to content

Commit 209fff8

Browse files
committed
Added mode which defaults to auto. Resolves #3
1 parent 188616a commit 209fff8

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

cmd/mendix-userlib-cleaner/main.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ func main() {
4141
flag.String("target", ".", "Path to userlib.")
4242
flag.Bool("clean", false, "Turn on to actually remove the duplicate JARs.")
4343
flag.Bool("verbose", false, "Turn on to see debug information.")
44+
flag.String("mode", "auto", "Jar parsing mode. Supported options: auto, strict")
4445

4546
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
4647
pflag.Parse()
4748
viper.BindPFlags(pflag.CommandLine)
4849

4950
targetDir := viper.GetString("target")
51+
mode := viper.GetString("mode")
5052
clean := viper.GetBool("clean")
5153
verbose := viper.GetBool("verbose")
5254

@@ -61,7 +63,7 @@ func main() {
6163
logging.SetLevel(logging.INFO, "main")
6264
}
6365

64-
jars := listAllJars(targetDir)
66+
jars := listAllJars(targetDir, mode)
6567
keepJars := computeJarsToKeep(jars)
6668
count := cleanJars(clean, jars, keepJars)
6769

@@ -74,7 +76,7 @@ func main() {
7476

7577
}
7678

77-
func listAllJars(targetDir string) []JarProperties {
79+
func listAllJars(targetDir string, mode string) []JarProperties {
7880
log.Info("Finding and parsing JARs")
7981
files, err := ioutil.ReadDir(targetDir)
8082
if err != nil {
@@ -85,7 +87,7 @@ func listAllJars(targetDir string) []JarProperties {
8587
if strings.HasSuffix(f.Name(), ".jar") {
8688
log.Debugf("Processing JAR: %v", f.Name())
8789
filePath := filepath.Join(targetDir, f.Name())
88-
jarProp := getJarProps(filePath)
90+
jarProp := getJarProps(filePath, mode)
8991
if strings.Compare(jarProp.filePath, "") != 0 {
9092
jars = append(jars, jarProp)
9193
}
@@ -94,7 +96,7 @@ func listAllJars(targetDir string) []JarProperties {
9496
return jars
9597
}
9698

97-
func getJarProps(filePath string) JarProperties {
99+
func getJarProps(filePath string, mode string) JarProperties {
98100

99101
archive, err := zip.OpenReader(filePath)
100102
if err != nil {
@@ -138,11 +140,6 @@ func getJarProps(filePath string) JarProperties {
138140
log.Warningf("Unable to read file: %v", err)
139141
}
140142

141-
// err = os.Remove(file.Name())
142-
// if err != nil {
143-
// log.Fatal(err)
144-
// }
145-
146143
// try manifest first
147144
text := string(b)
148145
jar1 := parseManifest(filePath, text)
@@ -156,6 +153,15 @@ func getJarProps(filePath string) JarProperties {
156153
return jar2
157154
}
158155
}
156+
157+
if mode == "auto" {
158+
jar3 := parseOptimistic(filePath)
159+
if jar3.packageName != "" {
160+
log.Debugf("Parsed properties optimistically: %v", jar3)
161+
return jar3
162+
}
163+
}
164+
159165
log.Warningf("Failed to parse metadata from %v", filePath)
160166

161167
return JarProperties{filePath: ""}
@@ -214,6 +220,43 @@ func parsePOM(filePath string, text string) JarProperties {
214220
return jarProp
215221
}
216222

223+
func parseOptimistic(filePath string) JarProperties {
224+
// filePath = junit-4.11.jar
225+
jarProp := JarProperties{filePath: filePath, packageName: "", fileName: filepath.Base(filePath)}
226+
227+
// version
228+
tokens := strings.Split(filePath, "-")
229+
if len(tokens) > 1 {
230+
jarProp.version = strings.Replace(tokens[len(tokens)-1], ".jar", "", 1)
231+
jarProp.versionNumber = convertVersionToNumber(jarProp.version)
232+
}
233+
234+
archive, err := zip.OpenReader(filePath)
235+
if err != nil {
236+
panic(err)
237+
}
238+
defer archive.Close()
239+
re := regexp.MustCompile(`(org|com)/.*\.class$`)
240+
241+
for _, f := range archive.File {
242+
if match := re.MatchString(f.Name); match {
243+
tokens = strings.Split(f.Name, "/")
244+
if len(tokens) > 3 {
245+
// eg. org/example/hello/MyClass.class
246+
tokens = tokens[:3]
247+
} else if len(tokens) > 2 {
248+
// eg. org/example/MyClass.class
249+
tokens = tokens[:2]
250+
} else {
251+
tokens = tokens[:1]
252+
}
253+
jarProp.packageName = strings.Join(tokens, ".")
254+
break
255+
}
256+
}
257+
return jarProp
258+
}
259+
217260
func computeJarsToKeep(jars []JarProperties) map[string]JarProperties {
218261
log.Info("Computing duplicates")
219262
var keepJars = make(map[string]JarProperties)

0 commit comments

Comments
 (0)