Skip to content

Commit de9bc65

Browse files
committed
Clean up metadata files also along with the jar. Resolves #2
1 parent 209fff8 commit de9bc65

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

cmd/mendix-userlib-cleaner/main.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ func main() {
6363
logging.SetLevel(logging.INFO, "main")
6464
}
6565

66-
jars := listAllJars(targetDir, mode)
66+
filePaths := listAllFiles(targetDir)
67+
jars := listAllJars(filePaths, mode)
6768
keepJars := computeJarsToKeep(jars)
68-
count := cleanJars(clean, jars, keepJars)
69+
count := cleanJars(clean, filePaths, jars, keepJars)
6970

7071
if clean {
7172
log.Infof("Total files removed: %d", count)
@@ -76,18 +77,29 @@ func main() {
7677

7778
}
7879

79-
func listAllJars(targetDir string, mode string) []JarProperties {
80-
log.Info("Finding and parsing JARs")
80+
func listAllFiles(targetDir string) []string {
81+
log.Infof("Listing all files in target directory: %v", targetDir)
8182
files, err := ioutil.ReadDir(targetDir)
8283
if err != nil {
8384
log.Fatal(err)
8485
}
85-
jars := []JarProperties{}
86+
filePaths := []string{}
8687
for _, f := range files {
87-
if strings.HasSuffix(f.Name(), ".jar") {
88-
log.Debugf("Processing JAR: %v", f.Name())
88+
if !f.IsDir() {
8989
filePath := filepath.Join(targetDir, f.Name())
90-
jarProp := getJarProps(filePath, mode)
90+
filePaths = append(filePaths, filePath)
91+
}
92+
}
93+
return filePaths
94+
}
95+
96+
func listAllJars(filePaths []string, mode string) []JarProperties {
97+
log.Info("Finding and parsing JARs")
98+
jars := []JarProperties{}
99+
for _, f := range filePaths {
100+
if strings.HasSuffix(f, ".jar") {
101+
log.Debugf("Processing JAR: %v", f)
102+
jarProp := getJarProps(f, mode)
91103
if strings.Compare(jarProp.filePath, "") != 0 {
92104
jars = append(jars, jarProp)
93105
}
@@ -294,26 +306,36 @@ func computeJarsToKeep(jars []JarProperties) map[string]JarProperties {
294306
return keepJars
295307
}
296308

297-
func cleanJars(remove bool, jars []JarProperties, keepJars map[string]JarProperties) int {
309+
func cleanJars(remove bool, filePaths []string, jars []JarProperties, keepJars map[string]JarProperties) int {
298310
log.Info("Cleaning...")
299-
count := 0
311+
jarsCount := 0
312+
metafilesCount := 0
300313
for _, jar := range jars {
301314
jarToKeep := keepJars[jar.packageName]
302315
if strings.Compare(jar.filePath, jarToKeep.filePath) != 0 {
303-
if _, err := os.Stat(jar.filePath); err == nil {
304-
if remove {
305-
log.Warningf("Removing duplicate of %v: %v", jar.packageName, jar.fileName)
306-
os.Remove(jar.filePath)
307-
} else {
308-
log.Warningf("Would remove duplicate of %v: %v", jar.packageName, jar.fileName)
316+
for _, filePath := range filePaths {
317+
if _, err := os.Stat(filePath); err == nil {
318+
if strings.HasPrefix(filePath, jar.filePath) {
319+
if remove {
320+
log.Warningf("Removing file %v: %v", jar.packageName, filePath)
321+
os.Remove(filePath)
322+
} else {
323+
log.Warningf("Would remove file %v: %v", jar.packageName, filePath)
324+
}
325+
if strings.HasSuffix(filePath, ".jar") {
326+
jarsCount++
327+
} else {
328+
metafilesCount++
329+
}
330+
}
309331
}
310-
count++
311332
}
312333
} else {
313334
log.Debugf("Keeping jar: %v", jar)
314335
}
315336
}
316-
return count
337+
log.Infof("Clean up %v jars and %v meta files", jarsCount, metafilesCount)
338+
return jarsCount + metafilesCount
317339
}
318340

319341
func convertVersionToNumber(version string) int {

0 commit comments

Comments
 (0)