Skip to content

Commit 77c0f67

Browse files
committed
Add proper logging for PNG optimization
1 parent 3921a49 commit 77c0f67

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

build.gradle

+15-6
Original file line numberDiff line numberDiff line change
@@ -416,23 +416,32 @@ static String getShortenedAssetPath(File file) {
416416
return file.getPath()
417417
.replaceFirst('^.*?resources.(assets.immersiveengineering.)?', '') // remove start
418418
.replaceAll('[/\\\\]+', '_') // replace slashes with underscores
419+
.replaceAll('.png+', '') // trim the ending
419420
}
420421

421422
for (dir in project.sourceSets.main.resources.srcDirs) {
422423
fileTree(dir: dir, includes: pngPatterns).each { file ->
423-
tasks.register("optimizePng_" + getShortenedAssetPath(file), Exec) {
424+
def path = getShortenedAssetPath(file);
425+
tasks.register("optimizePng_" + path, Exec) {
424426
executable "optipng"
425-
args "-q", "-o7", "-zm1-9", "-strip", "all", file
426-
// It'd be awesome if we could do logging here, but routing standardOutput to a ByteArrayOutputStream
427-
// doesn't work...
428-
// Todo: try with file output!
427+
args "-q", "-log", "optipng_${path}.log", "-o7", "-zm1-9", "-strip", "all", file
429428
}
430429
}
431430
}
432431
task optimizePng {
433432
for (dir in project.sourceSets.main.resources.srcDirs) {
434433
fileTree(dir: dir, includes: pngPatterns).each { file ->
435-
dependsOn "optimizePng_" + getShortenedAssetPath(file)
434+
def path = getShortenedAssetPath(file);
435+
dependsOn "optimizePng_${path}"
436+
doLast {
437+
def logfile = new File("optipng_${path}.log")
438+
if (logfile.exists()) {
439+
def decrease = logfile.text.trim().find('[\\d\\.]+% decrease')
440+
if (decrease)
441+
println "${file.name} optimized:\n\t${decrease} in size"
442+
logfile.delete()
443+
}
444+
}
436445
}
437446
}
438447
}

0 commit comments

Comments
 (0)