Skip to content

Commit

Permalink
adds clean dir flag
Browse files Browse the repository at this point in the history
updates readme
  • Loading branch information
patrickfav committed Mar 22, 2016
1 parent a4424fd commit 8499e4c
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Full list of arguments:
tvdpi)
-antiAliasing Anti-aliases images creating a little more blurred result; useful
for very small images
-clean Deletes all file and folders in out dir that would be used in
current configuration before converting.
-compressionQuality <0.0-1.0> Only used with compression 'jpg' sets the quality [0-1.0] where 1.0
is the highest quality. Default is 0.9
-dryRun Will not create any images or folder. Useful as fast preview in log
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/at/favre/tools/dconvert/DConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public void execute(Arguments args, boolean blockingWaitForFinish, HandlerCallba
converters.add(ePlatform.getConverter());
}

if (args.clearDirBeforeConvert) {
logStringBuilder.append("clear out dirs before convert\n");
for (IPlatformConverter converter : converters) {
converter.clean(args);
}
}

if (args.enablePngCrush) {
IPostProcessor postProcessor = new PngCrushProcessor();
if (postProcessor.isSupported()) {
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/at/favre/tools/dconvert/arg/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Handles all the arguments that can be set in the dconvert
*/
public class Arguments implements Serializable {
private static final long serialVersionUID = 5;
private static final long serialVersionUID = 6;

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


public final static Arguments START_GUI = new Arguments(null, null, 0.27346f, null, null, null, 0.9362f, 996254, false,
false, false, false, false, false, false, false, false, false, false, false, false, null, false);
false, false, false, false, false, false, false, false, false, false, false, false, null, false, false);

public final File src;
public final File dst;
Expand All @@ -68,13 +68,14 @@ public class Arguments implements Serializable {
public final RoundingHandler.Strategy roundingHandler;
public final boolean iosCreateImagesetFolders;
public final boolean guiAdvancedOptions;
public final boolean clearDirBeforeConvert;
public transient final List<File> filesToProcess;


public Arguments(File src, File dst, float scale, Set<EPlatform> platform, EOutputCompressionMode outputCompressionMode,
EScaleType scaleType, float compressionQuality, int threadCount, boolean skipExistingFiles, boolean skipUpscaling,
boolean verboseLog, boolean includeAndroidLdpiTvdpi, boolean haltOnError, boolean createMipMapInsteadOfDrawableDir,
boolean iosCreateImagesetFolders, boolean enablePngCrush, boolean enableMozJpeg, boolean postConvertWebp, boolean enableAntiAliasing, boolean dryRun, boolean keepUnoptimizedFilesPostProcessor, RoundingHandler.Strategy roundingHandler, boolean guiAdvancedOptions) {
boolean iosCreateImagesetFolders, boolean enablePngCrush, boolean enableMozJpeg, boolean postConvertWebp, boolean enableAntiAliasing, boolean dryRun, boolean keepUnoptimizedFilesPostProcessor, RoundingHandler.Strategy roundingHandler, boolean guiAdvancedOptions, boolean clearDirBeforeConvert) {
this.dst = dst;
this.src = src;
this.scale = scale;
Expand All @@ -98,6 +99,7 @@ public Arguments(File src, File dst, float scale, Set<EPlatform> platform, EOutp
this.keepUnoptimizedFilesPostProcessor = keepUnoptimizedFilesPostProcessor;
this.roundingHandler = roundingHandler;
this.guiAdvancedOptions = guiAdvancedOptions;
this.clearDirBeforeConvert = clearDirBeforeConvert;

this.filesToProcess = new ArrayList<>();

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

public Arguments() {
this(null, null, DEFAULT_SCALE, DEFAULT_PLATFORM, DEFAULT_OUT_COMPRESSION, DEFAULT_SCALE_TYPE, DEFAULT_COMPRESSION_QUALITY, DEFAULT_THREAD_COUNT,
false, false, true, false, false, false, false, false, false, false, false, false, false, DEFAULT_ROUNDING_STRATEGY, false);
false, false, true, false, false, false, false, false, false, false, false, false, false, DEFAULT_ROUNDING_STRATEGY, false, false);
}

public double round(double raw) {
Expand Down Expand Up @@ -152,6 +154,7 @@ public String toString() {
", roundingHandler=" + roundingHandler +
", iosCreateImagesetFolders=" + iosCreateImagesetFolders +
", guiAdvancedOptions=" + guiAdvancedOptions +
", clearDirBeforeConvert=" + clearDirBeforeConvert +
", filesToProcess=" + filesToProcess +
'}';
}
Expand Down Expand Up @@ -180,6 +183,7 @@ public boolean equals(Object o) {
if (keepUnoptimizedFilesPostProcessor != arguments.keepUnoptimizedFilesPostProcessor) return false;
if (iosCreateImagesetFolders != arguments.iosCreateImagesetFolders) return false;
if (guiAdvancedOptions != arguments.guiAdvancedOptions) return false;
if (clearDirBeforeConvert != arguments.clearDirBeforeConvert) return false;
if (src != null ? !src.equals(arguments.src) : arguments.src != null) return false;
if (dst != null ? !dst.equals(arguments.dst) : arguments.dst != null) return false;
if (platform != null ? !platform.equals(arguments.platform) : arguments.platform != null) return false;
Expand Down Expand Up @@ -215,6 +219,7 @@ public int hashCode() {
result = 31 * result + (roundingHandler != null ? roundingHandler.hashCode() : 0);
result = 31 * result + (iosCreateImagesetFolders ? 1 : 0);
result = 31 * result + (guiAdvancedOptions ? 1 : 0);
result = 31 * result + (clearDirBeforeConvert ? 1 : 0);
result = 31 * result + (filesToProcess != null ? filesToProcess.hashCode() : 0);
return result;
}
Expand Down Expand Up @@ -255,6 +260,7 @@ public static class Builder {
private boolean keepUnoptimizedFilesPostProcessor;
private boolean iosCreateImagesetFolders = false;
private boolean guiAdvancedOptions;
private boolean clearDirBeforeConvert;

public Builder(File src, float srcScale) {
this.src = src;
Expand Down Expand Up @@ -372,6 +378,11 @@ public Builder guiAdvancedOptions(boolean b) {
return this;
}

public Builder clearDirBeforeConvert(boolean b) {
this.clearDirBeforeConvert = b;
return this;
}

public Arguments build() throws InvalidArgumentException {
if (!internalSkipParamValidation) {
ResourceBundle bundle = ResourceBundle.getBundle("bundles.strings", Locale.getDefault());
Expand Down Expand Up @@ -416,7 +427,7 @@ public Arguments build() throws InvalidArgumentException {
}
return new Arguments(src, dst, srcScale, platform, outputCompressionMode, scaleType, compressionQuality, threadCount,
skipExistingFiles, skipUpscaling, verboseLog, includeAndroidLdpiTvdpi, haltOnError, createMipMapInsteadOfDrawableDir,
iosCreateImagesetFolders, enablePngCrush, enableMozJpeg, postConvertWebp, enableAntiAliasing, dryRun, keepUnoptimizedFilesPostProcessor, roundingStrategy, guiAdvancedOptions);
iosCreateImagesetFolders, enablePngCrush, enableMozJpeg, postConvertWebp, enableAntiAliasing, dryRun, keepUnoptimizedFilesPostProcessor, roundingStrategy, guiAdvancedOptions, clearDirBeforeConvert);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@
*/
public class AndroidConverter extends APlatformConverter<AndroidDensityDescriptor> {

private static final String ANDROID_FOLDER_NAME = "android";

@Override
public List<AndroidDensityDescriptor> usedOutputDensities(Arguments arguments) {
return getAndroidDensityDescriptors(arguments);
}

public static List<AndroidDensityDescriptor> getAndroidDensityDescriptors(Arguments arguments) {
List<AndroidDensityDescriptor> list = new ArrayList<>();
String dirPrefix = arguments.createMipMapInsteadOfDrawableDir ? "mipmap" : "drawable";
if (arguments.includeAndroidLdpiTvdpi) {
list.add(new AndroidDensityDescriptor(0.75f, "ldpi", "drawable-ldpi"));
list.add(new AndroidDensityDescriptor(1.33f, "tvdpi", "drawable-tvdpi"));
list.add(new AndroidDensityDescriptor(0.75f, "ldpi", dirPrefix + "-ldpi"));
list.add(new AndroidDensityDescriptor(1.33f, "tvdpi", dirPrefix + "-tvdpi"));
}
list.add(new AndroidDensityDescriptor(1, "mdpi", "drawable-mdpi"));
list.add(new AndroidDensityDescriptor(1.5f, "hdpi", "drawable-hdpi"));
list.add(new AndroidDensityDescriptor(2, "xhdpi", "drawable-xhdpi"));
list.add(new AndroidDensityDescriptor(3, "xxhdpi", "drawable-xxhdpi"));
list.add(new AndroidDensityDescriptor(4, "xxxhdpi", "drawable-xxxhdpi"));
list.add(new AndroidDensityDescriptor(1, "mdpi", dirPrefix + "-mdpi"));
list.add(new AndroidDensityDescriptor(1.5f, "hdpi", dirPrefix + "-hdpi"));
list.add(new AndroidDensityDescriptor(2, "xhdpi", dirPrefix + "-xhdpi"));
list.add(new AndroidDensityDescriptor(3, "xxhdpi", dirPrefix + "-xxhdpi"));
list.add(new AndroidDensityDescriptor(4, "xxxhdpi", dirPrefix + "-xxxhdpi"));
return list;
}

Expand All @@ -59,16 +62,15 @@ public String getConverterName() {
@Override
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
if (arguments.platform.size() > 1) {
return MiscUtil.createAndCheckFolder(new File(destinationFolder, "android").getAbsolutePath(), arguments.dryRun);
return MiscUtil.createAndCheckFolder(new File(destinationFolder, ANDROID_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
} else {
return destinationFolder;
}
}

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

@Override
Expand All @@ -89,4 +91,16 @@ public void onPostExecute(Arguments arguments) {
public static boolean isNinePatch(File file) {
return file.getName().endsWith(".9.png");
}

@Override
public void clean(Arguments arguments) {
if (arguments.platform.size() == 1) {
for (AndroidDensityDescriptor androidDensityDescriptor : getAndroidDensityDescriptors(arguments)) {
File dir = new File(arguments.dst, androidDensityDescriptor.folderName);
MiscUtil.deleteFolder(dir);
}
} else {
MiscUtil.deleteFolder(new File(arguments.dst, ANDROID_FOLDER_NAME));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
public class IOSConverter extends APlatformConverter<PostfixDescriptor> {
public static final String ROOT_FOLDER = "AssetCatalog";
private static final String IOS_FOLDER_NAME = "ios";

@Override
public List<PostfixDescriptor> usedOutputDensities(Arguments arguments) {
Expand All @@ -56,7 +57,7 @@ public String getConverterName() {
@Override
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
if (arguments.platform.size() > 1) {
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, "ios").getAbsolutePath(), arguments.dryRun);
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, IOS_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
}
if (arguments.iosCreateImagesetFolders) {
return MiscUtil.createAndCheckFolder(new File(destinationFolder, targetImageFileName + ".imageset").getAbsolutePath(), arguments.dryRun);
Expand Down Expand Up @@ -116,4 +117,19 @@ private String createContentJson(String targetFileName, List<PostfixDescriptor>

return sb.toString();
}

@Override
public void clean(Arguments arguments) {
if (arguments.platform.size() == 1) {
if (arguments.iosCreateImagesetFolders) {
for (File filesToProcess : arguments.filesToProcess) {
MiscUtil.deleteFolder(new File(arguments.dst, MiscUtil.getFileNameWithoutExtension(filesToProcess) + ".imageset"));
}
} else {
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
}
} else {
MiscUtil.deleteFolder(new File(arguments.dst, IOS_FOLDER_NAME));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public interface IPlatformConverter {
* @return result
*/
Result convert(File srcImageFile, Arguments arguments);

/**
* Cleans (ie. deletes) all dirs that would be generated with this converter and arguments
*
* @param arguments
*/
void clean(Arguments arguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Converts and creates css image-set style images
*/
public class WebConverter extends APlatformConverter<PostfixDescriptor> {
private static final String WEB_FOLDER_NAME = "web";
public static final String ROOT_FOLDER = "img";

@Override
Expand All @@ -36,7 +37,7 @@ public String getConverterName() {
@Override
public File createMainSubFolder(File destinationFolder, String targetImageFileName, Arguments arguments) {
if (arguments.platform.size() > 1) {
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, "web").getAbsolutePath(), arguments.dryRun);
destinationFolder = MiscUtil.createAndCheckFolder(new File(destinationFolder, WEB_FOLDER_NAME).getAbsolutePath(), arguments.dryRun);
}
return MiscUtil.createAndCheckFolder(new File(destinationFolder, ROOT_FOLDER).getAbsolutePath(), arguments.dryRun);
}
Expand All @@ -60,4 +61,13 @@ public void onPreExecute(File dstFolder, String targetFileName, List<PostfixDesc
public void onPostExecute(Arguments arguments) {

}

@Override
public void clean(Arguments arguments) {
if (arguments.platform.size() == 1) {
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
} else {
MiscUtil.deleteFolder(new File(new File(arguments.dst, WEB_FOLDER_NAME), ROOT_FOLDER));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Needed info to convert for Windows
*/
public class WindowsConverter extends APlatformConverter<PostfixDescriptor> {

private static final String WINDOWS_FOLDER_NAME = "windows";
public static final String ROOT_FOLDER = "Assets";

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

}

@Override
public void clean(Arguments arguments) {
if (arguments.platform.size() == 1) {
MiscUtil.deleteFolder(new File(arguments.dst, ROOT_FOLDER));
} else {
MiscUtil.deleteFolder(new File(new File(arguments.dst, WINDOWS_FOLDER_NAME), ROOT_FOLDER));
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/at/favre/tools/dconvert/ui/CLIInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public static Arguments parse(String[] args) {
builder.enableMozJpeg(commandLine.hasOption("postProcessorMozJpeg"));
builder.keepUnoptimizedFilesPostProcessor(commandLine.hasOption("keepOriginalPostProcessedFiles"));
builder.iosCreateImagesetFolders(commandLine.hasOption("iosCreateImagesetFolders"));
builder.clearDirBeforeConvert(commandLine.hasOption("clean"));

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

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

options.addOptionGroup(mainArgs);

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/at/favre/tools/dconvert/ui/GUIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class GUIController {
public Label labelThreads;
public HBox hboxWhy;
public VBox vboxFillFreeSpace;
public CheckBox cbCleanBeforeConvert;
private IPreferenceStore preferenceStore;
private final FileChooser srcFileChooser = new FileChooser();
private final DirectoryChooser srcDirectoryChooser = new DirectoryChooser();
Expand Down Expand Up @@ -417,8 +418,10 @@ private void loadPrefs() {
cbEnableMozJpeg.setSelected(args.enableMozJpeg);
cbKeepUnoptimized.setSelected(args.keepUnoptimizedFilesPostProcessor);
cbIosCreateImageset.setSelected(args.iosCreateImagesetFolders);
cbCleanBeforeConvert.setSelected(args.clearDirBeforeConvert);
rbOptAdvanced.setSelected(args.guiAdvancedOptions);
rbOptSimple.setSelected(!args.guiAdvancedOptions);

}
}

Expand Down Expand Up @@ -470,6 +473,7 @@ public Arguments getFromUI(boolean skipValidation) throws InvalidArgumentExcepti
builder.keepUnoptimizedFilesPostProcessor(cbKeepUnoptimized.isSelected());
builder.iosCreateImagesetFolders(cbIosCreateImageset.isSelected());
builder.guiAdvancedOptions(rbOptAdvanced.isSelected());
builder.clearDirBeforeConvert(cbCleanBeforeConvert.isSelected());

return builder.skipParamValidation(skipValidation).build();
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/at/favre/tools/dconvert/util/MiscUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,20 @@ public static <T> Set<T> toSet(T elem) {
set.add(elem);
return set;
}

public static void deleteFolder(File folder) {
if (folder != null && folder.exists()) {
File[] files = folder.listFiles();
if (files != null) { //some JVMs return null for empty dirs
for (File f : files) {
if (f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/bundles/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ main.cb.antialiasing=anti aliasing
main.cb.androidmipmap=android: mipmap instead drawable
main.cb.keepUnoptimized=keep unprocessed
main.cb.ioimageset=iOS: create .imageset folders + Content.json
main.cb.clean=clean out dir before convert

main.header.postprocessors=Post Processors
main.cb.pngcrush=compress with pngcrush
Expand Down Expand Up @@ -88,6 +89,7 @@ arg.descr.btnsrcfolder=Select a source folder for batch converting
arg.descr.btndstfolder=Select the destination folder where the scaled image will be created
arg.descr.btnsrcFile=Select the source image file to scale
arg.descr.btnopendstfolder=Open the destination folder
arg.descr.clean=Deletes all file and folders in out dir that would be used in current configuration before converting.

error.parse.dp=could not parse dp: {0} must be a number
error.missing.src=src file/directory must be passed and should exist: {0}
Expand Down
Loading

0 comments on commit 8499e4c

Please sign in to comment.