diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-27 09:49:48 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-27 09:49:48 +0100 |
commit | 2b61c2534b22f07a13d8cb4c97c2ad323c6c4597 (patch) | |
tree | 1aec17de05371b3a5ca02e25793e43e33c70e9b3 /src/delombok | |
parent | 9b55af61dbd585a745e24a43ac27574a69ed15f2 (diff) | |
download | lombok-2b61c2534b22f07a13d8cb4c97c2ad323c6c4597.tar.gz lombok-2b61c2534b22f07a13d8cb4c97c2ad323c6c4597.tar.bz2 lombok-2b61c2534b22f07a13d8cb4c97c2ad323c6c4597.zip |
Delombok now works from the command line as:
java -jar lombok.jar delombok (args)
Diffstat (limited to 'src/delombok')
-rw-r--r-- | src/delombok/lombok/delombok/CommentPreservingParser.java | 9 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 132 |
2 files changed, 129 insertions, 12 deletions
diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index d353a9a6..c776a224 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -82,19 +82,19 @@ public class CommentPreservingParser { private static final Messager messager = new Messager() { @Override public void printMessage(Kind kind, CharSequence msg) { - System.out.printf("M: %s: %s\n", kind, msg); + System.out.printf("%s: %s\n", kind, msg); } @Override public void printMessage(Kind kind, CharSequence msg, Element e) { - System.out.printf("E: %s: %s\n", kind, msg); + System.out.printf("%s: %s\n", kind, msg); } @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) { - System.out.printf("A: %s: %s\n", kind, msg); + System.out.printf("%s: %s\n", kind, msg); } @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) { - System.out.printf("V: %s: %s\n", kind, msg); + System.out.printf("%s: %s\n", kind, msg); } }; @@ -122,7 +122,6 @@ public class CommentPreservingParser { JavaFileObject sourceFile = compilationUnit.getSourceFile(); if (sourceFile != null) { out.write(sourceFile.getCharContent(true).toString()); - System.out.println(out.toString()); return; } } diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index c6f9ecf9..e05f4982 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -5,14 +5,26 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.Writer; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; +import java.util.ArrayList; +import java.util.List; import lombok.delombok.CommentPreservingParser.ParseResult; +import com.zwitserloot.cmdreader.CmdReader; +import com.zwitserloot.cmdreader.Description; +import com.zwitserloot.cmdreader.Excludes; +import com.zwitserloot.cmdreader.InvalidCommandLineException; +import com.zwitserloot.cmdreader.Mandatory; +import com.zwitserloot.cmdreader.Parameterized; +import com.zwitserloot.cmdreader.Sequential; +import com.zwitserloot.cmdreader.Shorthand; + public class Delombok { private Charset charset = Charset.defaultCharset(); private CommentPreservingParser parser = new CommentPreservingParser(); @@ -23,6 +35,107 @@ public class Delombok { /** If null, output to standard out. */ private File output = null; + private static class CmdArgs { + @Shorthand("v") + @Description("Print the name of each file as it is being delombok-ed.") + @Excludes("quiet") + private boolean verbose; + + @Shorthand("q") + @Description("No warnings or errors will be emitted to standard error") + @Excludes("verbose") + private boolean quiet; + + @Shorthand("e") + @Description("Sets the encoding of your source files. Defaults to the system default charset. Example: \"UTF-8\"") + @Parameterized + private String encoding; + + @Shorthand("p") + @Description("Print delombok-ed code to standard output instead of saving it in target directory") + private boolean print; + + @Shorthand("d") + @Description("Directory to save delomboked files to") + @Mandatory(onlyIfNot="print") + @Parameterized + private String target; + + @Description("Files to delombok. Provide either a file, or a directory. If you use a directory, all files in it (recursive) are delombok-ed") + @Sequential + @Parameterized + private List<String> input = new ArrayList<String>(); + + private boolean help; + } + + public static void main(String[] rawArgs) { + CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class); + CmdArgs args; + try { + args = reader.make(rawArgs); + } catch (InvalidCommandLineException e) { + System.err.println("ERROR: " + e.getMessage()); + System.err.println(reader.generateCommandLineHelp("delombok")); + System.exit(1); + return; + } + + if (args.help || args.input.isEmpty()) { + if (args.input.isEmpty()) System.err.println("ERROR: no files or directories to delombok specified."); + System.err.println(reader.generateCommandLineHelp("delombok")); + System.exit(args.input.isEmpty() ? 1 : 0); + return; + } + + Delombok delombok = new Delombok(); + + if (args.quiet) delombok.setFeedback(new PrintStream(new OutputStream() { + @Override public void write(int b) throws IOException { + //dummy - do nothing. + } + })); + + if (args.encoding != null) { + try { + delombok.setCharset(args.encoding); + } catch (UnsupportedCharsetException e) { + System.err.println("ERROR: Not a known charset: " + args.encoding); + System.exit(1); + return; + } + } + + if (args.verbose) delombok.setVerbose(true); + if (args.print) delombok.setOutputToStandardOut(); + else delombok.setOutput(new File(args.target)); + + for (String in : args.input) { + try { + File f = new File(in); + if (f.isFile()) { + delombok.delombok(f.getParentFile(), f.getName()); + } else if (f.isDirectory()) { + delombok.delombok(f); + } else if (!f.exists()) { + if (!args.quiet) System.err.println("WARNING: does not exist - skipping: " + f); + } else { + if (!args.quiet) System.err.println("WARNING: not a standard file or directory - skipping: " + f); + } + } catch (Exception e) { + if (!args.quiet) { + String msg = e.getMessage(); + if (msg != null && msg.startsWith("DELOMBOK: ")) System.err.println(msg.substring("DELOMBOK: ".length())); + else { + e.printStackTrace(); + } + System.exit(1); + return; + } + } + } + } + public void setCharset(String charsetName) throws UnsupportedCharsetException { charset = Charset.forName(charsetName); } @@ -40,7 +153,7 @@ public class Delombok { } public void setOutput(File dir) { - if (dir.isFile()) throw new IllegalArgumentException( + if (dir.isFile() || (!dir.isDirectory() && dir.getName().endsWith(".java"))) throw new IllegalArgumentException( "DELOMBOK: delombok will only write to a directory. " + "If you want to delombok a single file, use -p to output to standard output, then redirect this to a file:\n" + "delombok MyJavaFile.java -p >MyJavaFileDelombok.java"); @@ -57,20 +170,23 @@ public class Delombok { private void delombok0(File base, String suffix, int loop) throws IOException { File dir = suffix.isEmpty() ? base : new File(base, suffix); - String name = suffix + File.separator + dir.getName(); + String name = suffix; if (dir.isDirectory()) { if (loop >= 100) { feedback.printf("Over 100 subdirectories? I'm guessing there's a loop in your directory structure. Skipping: %s\n", suffix); } else { - dir.mkdir(); - delombok0(base, name, loop + 1); + for (File f : dir.listFiles()) { + delombok0(base, suffix + (suffix.isEmpty() ? "" : File.separator) + f.getName(), loop + 1); + } } } else if (dir.isFile()) { String extension = getExtension(dir); - if (extension.equals(".java")) delombok(base, name); - else if (extension.equals(".class")) skipClass(name); + if (extension.equals("java")) delombok(base, name); + else if (extension.equals("class")) skipClass(name); else copy(base, name); + } else if (!dir.exists()) { + feedback.printf("Skipping %s because it does not exist.\n", canonical(dir)); } else { feedback.printf("Skipping %s because it is a special file type.\n", canonical(dir)); } @@ -87,7 +203,9 @@ public class Delombok { } if (verbose) feedback.printf("Copying resource file: %s\n", fileName); byte[] b = new byte[65536]; - FileInputStream in = new FileInputStream(new File(base, fileName)); + File outFile = new File(base, fileName); + outFile.getParentFile().mkdirs(); + FileInputStream in = new FileInputStream(outFile); try { FileOutputStream out = new FileOutputStream(new File(output, fileName)); try { |