Skip to content

Commit f2fc945

Browse files
committed
feat: batch process
1 parent 100aa69 commit f2fc945

File tree

7 files changed

+137
-6
lines changed

7 files changed

+137
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
.gradle/
3+
build/
34
out/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# Sewing
2-
🖌 Sewing is a SVG to VectorDrawable Converter
2+
3+
🖌 Sewing is a SVG to VectorDrawable batch processor
4+

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
plugins {
22
id 'java'
3+
id 'application'
4+
id 'de.fuerstenau.buildconfig' version '1.1.8'
5+
id 'idea'
36
}
47

58
group 'cn.quickit'
6-
version '1.0-SNAPSHOT'
9+
version '1.0.2'
10+
11+
mainClassName = 'cn.quickits.sewing.Sewing'
712

813
sourceCompatibility = 1.8
914

@@ -14,4 +19,5 @@ repositories {
1419
dependencies {
1520
testCompile group: 'junit', name: 'junit', version: '4.12'
1621
compile 'commons-cli:commons-cli:1.4'
22+
compile 'com.android.tools:sdk-common:25.3.0'
1723
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cn.quickits.sewing;
2+
3+
import cn.quickits.sewing.util.FileUtils;
4+
import com.android.ide.common.vectordrawable.Svg2Vector;
5+
6+
import java.io.File;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileOutputStream;
9+
import java.util.List;
10+
11+
@SuppressWarnings("ALL")
12+
class Processor {
13+
14+
private List<File> inputPath;
15+
16+
private File outputPath;
17+
18+
Processor(List<File> inputPath, File outputPath) {
19+
this.inputPath = inputPath;
20+
this.outputPath = outputPath;
21+
22+
if (!outputPath.exists()) outputPath.mkdirs();
23+
}
24+
25+
void process() {
26+
for (File file : inputPath) {
27+
if (!file.exists()) {
28+
System.err.println(file.getAbsolutePath() + " not found");
29+
continue;
30+
}
31+
process(file);
32+
}
33+
}
34+
35+
private void process(File file) {
36+
if (file.isDirectory()) {
37+
File[] files = file.listFiles();
38+
if (files != null) {
39+
for (File childFile : files) {
40+
process(childFile);
41+
}
42+
}
43+
return;
44+
}
45+
46+
convertToVectorDrawable(file);
47+
}
48+
49+
private void convertToVectorDrawable(File file) {
50+
if (FileUtils.getFileExtension(file).toLowerCase().equals("svg")) {
51+
File outputFile = new File(outputPath, FileUtils.getFileName(file) + ".xml");
52+
53+
System.out.println(">> Process: " + file);
54+
55+
try {
56+
Svg2Vector.parseSvgToXml(file, new FileOutputStream(outputFile));
57+
} catch (FileNotFoundException e) {
58+
System.err.println("<< Error: " + file.getAbsolutePath() + "not found");
59+
}
60+
61+
System.out.println("<< Success: " + outputFile);
62+
}
63+
}
64+
65+
}

src/main/java/cn/quickits/sewing/Sewing.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package cn.quickits.sewing;
22

3+
import cn.quickit.BuildConfig;
34
import org.apache.commons.cli.*;
45

6+
import java.io.File;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
511
public class Sewing {
612

713
public static void main(String[] args) throws ParseException {
@@ -14,14 +20,21 @@ public static void main(String[] args) throws ParseException {
1420
);
1521
options.addOption(
1622
Option.builder("v")
17-
.desc("version name")
23+
.desc("show version name")
1824
.longOpt("version")
1925
.build()
2026
);
2127
options.addOption(
2228
Option.builder("i")
23-
.desc("svg file or dir")
29+
.desc("input dirs or svg files")
2430
.longOpt("input")
31+
.hasArgs()
32+
.build()
33+
);
34+
options.addOption(
35+
Option.builder("o")
36+
.desc("output dir")
37+
.longOpt("output")
2538
.hasArg()
2639
.build()
2740
);
@@ -37,13 +50,42 @@ public static void main(String[] args) throws ParseException {
3750
}
3851

3952
if (cmd.hasOption("v")) {
40-
System.out.println("v0.1.1");
53+
System.out.println(BuildConfig.VERSION);
4154
return;
4255
}
4356

57+
List<File> inputPath = new ArrayList<>();
58+
59+
File outputPath = null;
60+
4461
if (cmd.hasOption("i")) {
62+
String[] values = cmd.getOptionValues("i");
63+
for (String s : values) {
64+
inputPath.add(Paths.get(s).toFile());
65+
}
66+
}
4567

68+
if (cmd.hasOption("o")) {
69+
String value = cmd.getOptionValue("o");
70+
outputPath = Paths.get(value).toFile();
4671
}
72+
73+
if (inputPath.size() > 1 && outputPath == null) {
74+
System.err.println("ERROR: Miss of output dir. please add -o <arg>");
75+
return;
76+
} else if (outputPath == null) {
77+
File file = inputPath.get(0);
78+
if (file.isFile()) {
79+
outputPath = inputPath.get(0).getParentFile();
80+
} else if (file.isDirectory()) {
81+
outputPath = inputPath.get(0);
82+
} else {
83+
System.err.println("ERROR: Miss of output dir. please add -o <arg>");
84+
return;
85+
}
86+
}
87+
88+
new Processor(inputPath, outputPath).process();
4789
}
4890

4991
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.quickits.sewing.util;
2+
3+
import java.io.File;
4+
5+
public class FileUtils {
6+
7+
public static String getFileName(File file) {
8+
return file.getName().substring(0, file.getName().lastIndexOf("."));
9+
}
10+
11+
public static String getFileExtension(File file) {
12+
return file.getName().substring(file.getName().lastIndexOf(".") + 1);
13+
}
14+
15+
}

src/test/java/cn/quickits/sewing/SewingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class SewingTest {
77

88
@Test
99
public void testOptions() throws ParseException {
10-
String args[] = {"-i xxx"};
10+
String args[] = {"-h"};
1111
Sewing.main(args);
1212
}
1313
}

0 commit comments

Comments
 (0)