Skip to content

Commit 8499e4c

Browse files
committed
adds clean dir flag
updates readme
1 parent a4424fd commit 8499e4c

File tree

16 files changed

+136
-24
lines changed

16 files changed

+136
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Full list of arguments:
5858
tvdpi)
5959
-antiAliasing Anti-aliases images creating a little more blurred result; useful
6060
for very small images
61+
-clean Deletes all file and folders in out dir that would be used in
62+
current configuration before converting.
6163
-compressionQuality <0.0-1.0> Only used with compression 'jpg' sets the quality [0-1.0] where 1.0
6264
is the highest quality. Default is 0.9
6365
-dryRun Will not create any images or folder. Useful as fast preview in log

src/main/java/at/favre/tools/dconvert/DConvert.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public void execute(Arguments args, boolean blockingWaitForFinish, HandlerCallba
7171
converters.add(ePlatform.getConverter());
7272
}
7373

74+
if (args.clearDirBeforeConvert) {
75+
logStringBuilder.append("clear out dirs before convert\n");
76+
for (IPlatformConverter converter : converters) {
77+
converter.clean(args);
78+
}
79+
}
80+
7481
if (args.enablePngCrush) {
7582
IPostProcessor postProcessor = new PngCrushProcessor();
7683
if (postProcessor.isSupported()) {

src/main/java/at/favre/tools/dconvert/arg/Arguments.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Handles all the arguments that can be set in the dconvert
3131
*/
3232
public class Arguments implements Serializable {
33-
private static final long serialVersionUID = 5;
33+
private static final long serialVersionUID = 6;
3434

3535
public static final float DEFAULT_SCALE = 3f;
3636
public static final float DEFAULT_COMPRESSION_QUALITY = 0.9f;
@@ -43,7 +43,7 @@ public class Arguments implements Serializable {
4343

4444

4545
public final static Arguments START_GUI = new Arguments(null, null, 0.27346f, null, null, null, 0.9362f, 996254, false,
46-
false, false, false, false, false, false, false, false, false, false, false, false, null, false);
46+
false, false, false, false, false, false, false, false, false, false, false, false, null, false, false);
4747

4848
public final File src;
4949
public final File dst;
@@ -68,13 +68,14 @@ public class Arguments implements Serializable {
6868
public final RoundingHandler.Strategy roundingHandler;
6969
public final boolean iosCreateImagesetFolders;
7070
public final boolean guiAdvancedOptions;
71+
public final boolean clearDirBeforeConvert;
7172
public transient final List<File> filesToProcess;
7273

7374

7475
public Arguments(File src, File dst, float scale, Set<EPlatform> platform, EOutputCompressionMode outputCompressionMode,
7576
EScaleType scaleType, float compressionQuality, int threadCount, boolean skipExistingFiles, boolean skipUpscaling,
7677
boolean verboseLog, boolean includeAndroidLdpiTvdpi, boolean haltOnError, boolean createMipMapInsteadOfDrawableDir,
77-
boolean iosCreateImagesetFolders, boolean enablePngCrush, boolean enableMozJpeg, boolean postConvertWebp, boolean enableAntiAliasing, boolean dryRun, boolean keepUnoptimizedFilesPostProcessor, RoundingHandler.Strategy roundingHandler, boolean guiAdvancedOptions) {
78+
boolean iosCreateImagesetFolders, boolean enablePngCrush, boolean enableMozJpeg, boolean postConvertWebp, boolean enableAntiAliasing, boolean dryRun, boolean keepUnoptimizedFilesPostProcessor, RoundingHandler.Strategy roundingHandler, boolean guiAdvancedOptions, boolean clearDirBeforeConvert) {
7879
this.dst = dst;
7980
this.src = src;
8081
this.scale = scale;
@@ -98,6 +99,7 @@ public Arguments(File src, File dst, float scale, Set<EPlatform> platform, EOutp
9899
this.keepUnoptimizedFilesPostProcessor = keepUnoptimizedFilesPostProcessor;
99100
this.roundingHandler = roundingHandler;
100101
this.guiAdvancedOptions = guiAdvancedOptions;
102+
this.clearDirBeforeConvert = clearDirBeforeConvert;
101103

102104
this.filesToProcess = new ArrayList<>();
103105

@@ -119,7 +121,7 @@ public Arguments(File src, File dst, float scale, Set<EPlatform> platform, EOutp
119121

120122
public Arguments() {
121123
this(null, null, DEFAULT_SCALE, DEFAULT_PLATFORM, DEFAULT_OUT_COMPRESSION, DEFAULT_SCALE_TYPE, DEFAULT_COMPRESSION_QUALITY, DEFAULT_THREAD_COUNT,
122-
false, false, true, false, false, false, false, false, false, false, false, false, false, DEFAULT_ROUNDING_STRATEGY, false);
124+
false, false, true, false, false, false, false, false, false, false, false, false, false, DEFAULT_ROUNDING_STRATEGY, false, false);
123125
}
124126

125127
public double round(double raw) {
@@ -152,6 +154,7 @@ public String toString() {
152154
", roundingHandler=" + roundingHandler +
153155
", iosCreateImagesetFolders=" + iosCreateImagesetFolders +
154156
", guiAdvancedOptions=" + guiAdvancedOptions +
157+
", clearDirBeforeConvert=" + clearDirBeforeConvert +
155158
", filesToProcess=" + filesToProcess +
156159
'}';
157160
}
@@ -180,6 +183,7 @@ public boolean equals(Object o) {
180183
if (keepUnoptimizedFilesPostProcessor != arguments.keepUnoptimizedFilesPostProcessor) return false;
181184
if (iosCreateImagesetFolders != arguments.iosCreateImagesetFolders) return false;
182185
if (guiAdvancedOptions != arguments.guiAdvancedOptions) return false;
186+
if (clearDirBeforeConvert != arguments.clearDirBeforeConvert) return false;
183187
if (src != null ? !src.equals(arguments.src) : arguments.src != null) return false;
184188
if (dst != null ? !dst.equals(arguments.dst) : arguments.dst != null) return false;
185189
if (platform != null ? !platform.equals(arguments.platform) : arguments.platform != null) return false;
@@ -215,6 +219,7 @@ public int hashCode() {
215219
result = 31 * result + (roundingHandler != null ? roundingHandler.hashCode() : 0);
216220
result = 31 * result + (iosCreateImagesetFolders ? 1 : 0);
217221
result = 31 * result + (guiAdvancedOptions ? 1 : 0);
222+
result = 31 * result + (clearDirBeforeConvert ? 1 : 0);
218223
result = 31 * result + (filesToProcess != null ? filesToProcess.hashCode() : 0);
219224
return result;
220225
}
@@ -255,6 +260,7 @@ public static class Builder {
255260
private boolean keepUnoptimizedFilesPostProcessor;
256261
private boolean iosCreateImagesetFolders = false;
257262
private boolean guiAdvancedOptions;
263+
private boolean clearDirBeforeConvert;
258264

259265
public Builder(File src, float srcScale) {
260266
this.src = src;
@@ -372,6 +378,11 @@ public Builder guiAdvancedOptions(boolean b) {
372378
return this;
373379
}
374380

381+
public Builder clearDirBeforeConvert(boolean b) {
382+
this.clearDirBeforeConvert = b;
383+
return this;
384+
}
385+
375386
public Arguments build() throws InvalidArgumentException {
376387
if (!internalSkipParamValidation) {
377388
ResourceBundle bundle = ResourceBundle.getBundle("bundles.strings", Locale.getDefault());
@@ -416,7 +427,7 @@ public Arguments build() throws InvalidArgumentException {
416427
}
417428
return new Arguments(src, dst, srcScale, platform, outputCompressionMode, scaleType, compressionQuality, threadCount,
418429
skipExistingFiles, skipUpscaling, verboseLog, includeAndroidLdpiTvdpi, haltOnError, createMipMapInsteadOfDrawableDir,
419-
iosCreateImagesetFolders, enablePngCrush, enableMozJpeg, postConvertWebp, enableAntiAliasing, dryRun, keepUnoptimizedFilesPostProcessor, roundingStrategy, guiAdvancedOptions);
430+
iosCreateImagesetFolders, enablePngCrush, enableMozJpeg, postConvertWebp, enableAntiAliasing, dryRun, keepUnoptimizedFilesPostProcessor, roundingStrategy, guiAdvancedOptions, clearDirBeforeConvert);
420431
}
421432
}
422433

src/main/java/at/favre/tools/dconvert/converters/AndroidConverter.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@
3232
*/
3333
public class AndroidConverter extends APlatformConverter<AndroidDensityDescriptor> {
3434

35+
private static final String ANDROID_FOLDER_NAME = "android";
36+
3537
@Override
3638
public List<AndroidDensityDescriptor> usedOutputDensities(Arguments arguments) {
3739
return getAndroidDensityDescriptors(arguments);
3840
}
3941

4042
public static List<AndroidDensityDescriptor> getAndroidDensityDescriptors(Arguments arguments) {
4143
List<AndroidDensityDescriptor> list = new ArrayList<>();
44+
String dirPrefix = arguments.createMipMapInsteadOfDrawableDir ? "mipmap" : "drawable";
4245
if (arguments.includeAndroidLdpiTvdpi) {
43-
list.add(new AndroidDensityDescriptor(0.75f, "ldpi", "drawable-ldpi"));
44-
list.add(new AndroidDensityDescriptor(1.33f, "tvdpi", "drawable-tvdpi"));
46+
list.add(new AndroidDensityDescriptor(0.75f, "ldpi", dirPrefix + "-ldpi"));
47+
list.add(new AndroidDensityDescriptor(1.33f, "tvdpi", dirPrefix + "-tvdpi"));
4548
}
46-
list.add(new AndroidDensityDescriptor(1, "mdpi", "drawable-mdpi"));
47-
list.add(new AndroidDensityDescriptor(1.5f, "hdpi", "drawable-hdpi"));
48-
list.add(new AndroidDensityDescriptor(2, "xhdpi", "drawable-xhdpi"));
49-
list.add(new AndroidDensityDescriptor(3, "xxhdpi", "drawable-xxhdpi"));
50-
list.add(new AndroidDensityDescriptor(4, "xxxhdpi", "drawable-xxxhdpi"));
49+
list.add(new AndroidDensityDescriptor(1, "mdpi", dirPrefix + "-mdpi"));
50+
list.add(new AndroidDensityDescriptor(1.5f, "hdpi", dirPrefix + "-hdpi"));
51+
list.add(new AndroidDensityDescriptor(2, "xhdpi", dirPrefix + "-xhdpi"));
52+
list.add(new AndroidDensityDescriptor(3, "xxhdpi", dirPrefix + "-xxhdpi"));
53+
list.add(new AndroidDensityDescriptor(4, "xxxhdpi", dirPrefix + "-xxxhdpi"));
5154
return list;
5255
}
5356

@@ -59,16 +62,15 @@ public String getConverterName() {
5962
@Override
6063
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
6164
if (arguments.platform.size() > 1) {
62-
return MiscUtil.createAndCheckFolder(new File(destinationFolder, "android").getAbsolutePath(), arguments.dryRun);
65+
return MiscUtil.createAndCheckFolder(new File(destinationFolder, ANDROID_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
6366
} else {
6467
return destinationFolder;
6568
}
6669
}
6770

6871
@Override
6972
public File createFolderForOutputFile(File mainSubFolder, AndroidDensityDescriptor density, Dimension dimension, String targetFileName, Arguments arguments) {
70-
String folderName = (arguments.createMipMapInsteadOfDrawableDir ? density.folderName.replaceAll("drawable", "mipmap") : density.folderName);
71-
return MiscUtil.createAndCheckFolder(new File(mainSubFolder, folderName).getAbsolutePath(), arguments.dryRun);
73+
return MiscUtil.createAndCheckFolder(new File(mainSubFolder, density.folderName).getAbsolutePath(), arguments.dryRun);
7274
}
7375

7476
@Override
@@ -89,4 +91,16 @@ public void onPostExecute(Arguments arguments) {
8991
public static boolean isNinePatch(File file) {
9092
return file.getName().endsWith(".9.png");
9193
}
94+
95+
@Override
96+
public void clean(Arguments arguments) {
97+
if (arguments.platform.size() == 1) {
98+
for (AndroidDensityDescriptor androidDensityDescriptor : getAndroidDensityDescriptors(arguments)) {
99+
File dir = new File(arguments.dst, androidDensityDescriptor.folderName);
100+
MiscUtil.deleteFolder(dir);
101+
}
102+
} else {
103+
MiscUtil.deleteFolder(new File(arguments.dst, ANDROID_FOLDER_NAME));
104+
}
105+
}
92106
}

src/main/java/at/favre/tools/dconvert/converters/IOSConverter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*/
3535
public class IOSConverter extends APlatformConverter<PostfixDescriptor> {
3636
public static final String ROOT_FOLDER = "AssetCatalog";
37+
private static final String IOS_FOLDER_NAME = "ios";
3738

3839
@Override
3940
public List<PostfixDescriptor> usedOutputDensities(Arguments arguments) {
@@ -56,7 +57,7 @@ public String getConverterName() {
5657
@Override
5758
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
5859
if (arguments.platform.size() > 1) {
59-
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, "ios").getAbsolutePath(), arguments.dryRun);
60+
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, IOS_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
6061
}
6162
if (arguments.iosCreateImagesetFolders) {
6263
return MiscUtil.createAndCheckFolder(new File(destinationFolder, targetImageFileName + ".imageset").getAbsolutePath(), arguments.dryRun);
@@ -116,4 +117,19 @@ private String createContentJson(String targetFileName, List<PostfixDescriptor>
116117

117118
return sb.toString();
118119
}
120+
121+
@Override
122+
public void clean(Arguments arguments) {
123+
if (arguments.platform.size() == 1) {
124+
if (arguments.iosCreateImagesetFolders) {
125+
for (File filesToProcess : arguments.filesToProcess) {
126+
MiscUtil.deleteFolder(new File(arguments.dst, MiscUtil.getFileNameWithoutExtension(filesToProcess) + ".imageset"));
127+
}
128+
} else {
129+
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
130+
}
131+
} else {
132+
MiscUtil.deleteFolder(new File(arguments.dst, IOS_FOLDER_NAME));
133+
}
134+
}
119135
}

src/main/java/at/favre/tools/dconvert/converters/IPlatformConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public interface IPlatformConverter {
3434
* @return result
3535
*/
3636
Result convert(File srcImageFile, Arguments arguments);
37+
38+
/**
39+
* Cleans (ie. deletes) all dirs that would be generated with this converter and arguments
40+
*
41+
* @param arguments
42+
*/
43+
void clean(Arguments arguments);
3744
}

src/main/java/at/favre/tools/dconvert/converters/WebConverter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Converts and creates css image-set style images
1515
*/
1616
public class WebConverter extends APlatformConverter<PostfixDescriptor> {
17+
private static final String WEB_FOLDER_NAME = "web";
1718
public static final String ROOT_FOLDER = "img";
1819

1920
@Override
@@ -36,7 +37,7 @@ public String getConverterName() {
3637
@Override
3738
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
3839
if (arguments.platform.size() > 1) {
39-
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, "web").getAbsolutePath(), arguments.dryRun);
40+
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, WEB_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
4041
}
4142
return MiscUtil.createAndCheckFolder(new File(destinationFolder, ROOT_FOLDER).getAbsolutePath(), arguments.dryRun);
4243
}
@@ -60,4 +61,13 @@ public void onPreExecute(File dstFolder, String targetFileName, List<PostfixDesc
6061
public void onPostExecute(Arguments arguments) {
6162

6263
}
64+
65+
@Override
66+
public void clean(Arguments arguments) {
67+
if (arguments.platform.size() == 1) {
68+
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
69+
} else {
70+
MiscUtil.deleteFolder(new File(new File(arguments.dst, WEB_FOLDER_NAME), ROOT_FOLDER));
71+
}
72+
}
6373
}

src/main/java/at/favre/tools/dconvert/converters/WindowsConverter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Needed info to convert for Windows
3232
*/
3333
public class WindowsConverter extends APlatformConverter<PostfixDescriptor> {
34-
34+
private static final String WINDOWS_FOLDER_NAME = "windows";
3535
public static final String ROOT_FOLDER = "Assets";
3636

3737
@Override
@@ -56,7 +56,7 @@ public String getConverterName() {
5656
@Override
5757
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
5858
if (arguments.platform.size() > 1) {
59-
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, "windows").getAbsolutePath(), arguments.dryRun);
59+
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, WINDOWS_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
6060
}
6161
return MiscUtil.createAndCheckFolder(new File(destinationFolder, WindowsConverter.ROOT_FOLDER).getAbsolutePath(), arguments.dryRun);
6262
}
@@ -80,4 +80,13 @@ public void onPreExecute(File dstFolder, String targetFileName, List<PostfixDesc
8080
public void onPostExecute(Arguments arguments) {
8181

8282
}
83+
84+
@Override
85+
public void clean(Arguments arguments) {
86+
if (arguments.platform.size() == 1) {
87+
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
88+
} else {
89+
MiscUtil.deleteFolder(new File(new File(arguments.dst, WINDOWS_FOLDER_NAME), ROOT_FOLDER));
90+
}
91+
}
8392
}

src/main/java/at/favre/tools/dconvert/ui/CLIInterpreter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public static Arguments parse(String[] args) {
176176
builder.enableMozJpeg(commandLine.hasOption("postProcessorMozJpeg"));
177177
builder.keepUnoptimizedFilesPostProcessor(commandLine.hasOption("keepOriginalPostProcessedFiles"));
178178
builder.iosCreateImagesetFolders(commandLine.hasOption("iosCreateImagesetFolders"));
179+
builder.clearDirBeforeConvert(commandLine.hasOption("clean"));
179180

180181
return builder.build();
181182
} catch (Exception e) {
@@ -218,6 +219,7 @@ private static Options setupOptions(ResourceBundle bundle) {
218219
Option dpScaleIsHeight = Option.builder(SCALE_IS_HEIGHT_DP_ARG).desc(bundle.getString("arg.descr.cmd.dpIsHeight")).build();
219220
Option dryRun = Option.builder("dryRun").desc(bundle.getString("arg.descr.dryrun")).build();
220221
Option enableMozJpeg = Option.builder("postProcessorMozJpeg").desc(bundle.getString("arg.descr.mozjpeg")).build();
222+
Option cleanBeforeConvert = Option.builder("clean").desc(bundle.getString("arg.descr.clean")).build();
221223

222224
Option help = Option.builder("h").longOpt("help").desc(bundle.getString("arg.descr.cmd.help")).build();
223225
Option version = Option.builder("v").longOpt("version").desc(bundle.getString("arg.descr.cmd.version")).build();
@@ -232,7 +234,7 @@ private static Options setupOptions(ResourceBundle bundle) {
232234
options.addOption(skipExistingFiles).addOption(skipUpscaling).addOption(androidIncludeLdpiTvdpi).addOption(verboseLog)
233235
.addOption(antiAliasing).addOption(dryRun).addOption(haltOnError).addOption(mipmapInsteadOfDrawable)
234236
.addOption(enablePngCrush).addOption(postWebpConvert).addOption(dpScaleIsHeight).addOption(enableMozJpeg)
235-
.addOption(keepUnPostProcessed).addOption(iosCreateImagesetFolders);
237+
.addOption(keepUnPostProcessed).addOption(iosCreateImagesetFolders).addOption(cleanBeforeConvert);
236238

237239
options.addOptionGroup(mainArgs);
238240

src/main/java/at/favre/tools/dconvert/ui/GUIController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class GUIController {
6161
public Label labelThreads;
6262
public HBox hboxWhy;
6363
public VBox vboxFillFreeSpace;
64+
public CheckBox cbCleanBeforeConvert;
6465
private IPreferenceStore preferenceStore;
6566
private final FileChooser srcFileChooser = new FileChooser();
6667
private final DirectoryChooser srcDirectoryChooser = new DirectoryChooser();
@@ -417,8 +418,10 @@ private void loadPrefs() {
417418
cbEnableMozJpeg.setSelected(args.enableMozJpeg);
418419
cbKeepUnoptimized.setSelected(args.keepUnoptimizedFilesPostProcessor);
419420
cbIosCreateImageset.setSelected(args.iosCreateImagesetFolders);
421+
cbCleanBeforeConvert.setSelected(args.clearDirBeforeConvert);
420422
rbOptAdvanced.setSelected(args.guiAdvancedOptions);
421423
rbOptSimple.setSelected(!args.guiAdvancedOptions);
424+
422425
}
423426
}
424427

@@ -470,6 +473,7 @@ public Arguments getFromUI(boolean skipValidation) throws InvalidArgumentExcepti
470473
builder.keepUnoptimizedFilesPostProcessor(cbKeepUnoptimized.isSelected());
471474
builder.iosCreateImagesetFolders(cbIosCreateImageset.isSelected());
472475
builder.guiAdvancedOptions(rbOptAdvanced.isSelected());
476+
builder.clearDirBeforeConvert(cbCleanBeforeConvert.isSelected());
473477

474478
return builder.skipParamValidation(skipValidation).build();
475479
}

0 commit comments

Comments
 (0)