diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-28 09:29:12 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-28 09:29:12 +0100 |
commit | ce018589fe7e9d4b018f5797b7453a6ca16c6108 (patch) | |
tree | fbade8fe329b224d7a52cf4daf731288ec62d6e5 /src/core | |
parent | ad09ad1d29a30e618afe9752216d328161095883 (diff) | |
download | lombok-ce018589fe7e9d4b018f5797b7453a6ca16c6108.tar.gz lombok-ce018589fe7e9d4b018f5797b7453a6ca16c6108.tar.bz2 lombok-ce018589fe7e9d4b018f5797b7453a6ca16c6108.zip |
Redesigned how lombok runs as a command line app, added the ability to print information about and generate lombok-runtime.jar, fleshed out the 'install' and 'uninstall' command line tools.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lombok/core/LombokApp.java | 14 | ||||
-rw-r--r-- | src/core/lombok/core/Main.java | 159 | ||||
-rw-r--r-- | src/core/lombok/core/SpiLoadUtil.java | 15 | ||||
-rw-r--r-- | src/core/lombok/core/handlers/SneakyThrowsDependencyInfo.java | 44 | ||||
-rw-r--r-- | src/core/lombok/core/runtimeDependencies/CreateLombokRuntimeApp.java | 239 | ||||
-rw-r--r-- | src/core/lombok/core/runtimeDependencies/RuntimeDependencyInfo.java | 41 |
6 files changed, 511 insertions, 1 deletions
diff --git a/src/core/lombok/core/LombokApp.java b/src/core/lombok/core/LombokApp.java index b88db469..faadec96 100644 --- a/src/core/lombok/core/LombokApp.java +++ b/src/core/lombok/core/LombokApp.java @@ -21,6 +21,8 @@ */ package lombok.core; +import java.util.List; + /** * Implement this class, and add yourself as a provider for it, to become an app runnable by running lombok.jar as a jar. * @@ -31,10 +33,20 @@ public interface LombokApp { * @param args The arguments; analogous to what's passed to {@code public static void main(String[] args)} methods. * @return The return value. Don't call {@code System.exit} yourself. */ - public int runApp(String[] args) throws Exception; + public int runApp(List<String> args) throws Exception; /** * @return Your app name. For example {@code delombok}. */ public String getAppName(); + + /** + * @return Description of this app, for the command line. + */ + public String getAppDescription(); + + /** + * @return When lombok.jar is executed with any of these strings as first argument, your app will be started. + */ + public List<String> getAppAliases(); } diff --git a/src/core/lombok/core/Main.java b/src/core/lombok/core/Main.java new file mode 100644 index 00000000..b75d0513 --- /dev/null +++ b/src/core/lombok/core/Main.java @@ -0,0 +1,159 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.core; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.mangosdk.spi.ProviderFor; + +public class Main { + private static final Collection<?> HELP_SWITCHES = Collections.unmodifiableList(Arrays.asList( + "/?", "/h", "/help", "-h", "-help", "--help", "help", "h" + )); + + public static void main(String[] args) throws IOException { + int err = new Main(SpiLoadUtil.readAllFromIterator( + SpiLoadUtil.findServices(LombokApp.class)), Arrays.asList(args)).go(); + System.exit(err); + } + + @ProviderFor(LombokApp.class) + public static class VersionApp implements LombokApp { + @Override public String getAppName() { + return "version"; + } + + @Override public String getAppDescription() { + return "prints lombok's version."; + } + + @Override public List<String> getAppAliases() { + return Arrays.asList("version", "-version", "--version"); + } + + @Override public int runApp(List<String> args) { + System.out.println(Version.getVersion()); + return 0; + } + } + + @ProviderFor(LombokApp.class) + public static class LicenseApp implements LombokApp { + @Override public String getAppName() { + return "license"; + } + + @Override public String getAppDescription() { + return "prints license information."; + } + + @Override public List<String> getAppAliases() { + return Arrays.asList("license", "licence", "copyright", "copyleft", "gpl"); + } + + @Override public int runApp(List<String> args) { + try { + InputStream in = Main.class.getResourceAsStream("/LICENCE"); + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] b = new byte[65536]; + while (true) { + int r = in.read(b); + if (r == -1) break; + out.write(b, 0, r); + } + System.out.println(new String(out.toByteArray())); + return 0; + } finally { + in.close(); + } + } catch (Exception e) { + System.err.println("License file not found. Check http://projectlombok.org/LICENCE"); + return 1; + } + } + } + + private final List<LombokApp> apps; + private final List<String> args; + + public Main(List<LombokApp> apps, List<String> args) { + this.apps = apps; + this.args = args; + } + + public int go() { + if (!args.isEmpty() && HELP_SWITCHES.contains(args.get(0))) { + printHelp(null, System.out); + return 0; + } + + String command = args.isEmpty() ? "" : args.get(0).trim(); + if (command.startsWith("--")) command = command.substring(2); + else if (command.startsWith("-")) command = command.substring(1); + + List<String> subArgs = args.isEmpty() ? Collections.<String>emptyList() : Collections.unmodifiableList( + args.subList(1, args.size())); + + for (LombokApp app : apps) { + if (app.getAppName().equals(command) || app.getAppAliases().contains(command)) { + try { + return app.runApp(subArgs); + } catch (Exception e) { + e.printStackTrace(); + return 5; + } + } + } + + printHelp("Unknown command: " + command, System.err); + return 1; + } + + public void printHelp(String message, PrintStream out) { + if (message != null) { + out.println(message); + out.println("------------------------------"); + } + out.println("projectlombok.org v" + Version.getVersion()); + out.println("Copyright (C) 2009 Reinier Zwitserloot and Roel Spilker."); + out.println("Run 'lombok license' to see the lombok license agreement."); + out.println(); + out.println("Run lombok without any parameters to start the graphical installer."); + out.println("Other available commands:"); + for (LombokApp app : apps) { + String[] desc = app.getAppDescription().split("\n"); + for (int i = 0; i < desc.length; i++) { + out.printf(" %15s %s\n", i == 0 ? app.getAppName() : "", desc[i]); + } + } + out.println(); + out.println("Run lombok commandName --help for more info on each command."); + } +} diff --git a/src/core/lombok/core/SpiLoadUtil.java b/src/core/lombok/core/SpiLoadUtil.java index 0a97af7e..c068bf61 100644 --- a/src/core/lombok/core/SpiLoadUtil.java +++ b/src/core/lombok/core/SpiLoadUtil.java @@ -29,10 +29,12 @@ import java.lang.annotation.Annotation; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; +import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import lombok.Lombok; @@ -51,6 +53,19 @@ public class SpiLoadUtil { } /** + * Method that conveniently turn the {@code Iterable}s returned by the other methods in this class to a + * {@code List}. + * + * @see #findServices(Class) + * @see #findServices(Class, ClassLoader) + */ + public static <T> List<T> readAllFromIterator(Iterable<T> findServices) { + List<T> list = new ArrayList<T>(); + for (T t : findServices) list.add(t); + return list; + } + + /** * Returns an iterator of instances that, at least according to the spi discovery file, are implementations * of the stated class. * diff --git a/src/core/lombok/core/handlers/SneakyThrowsDependencyInfo.java b/src/core/lombok/core/handlers/SneakyThrowsDependencyInfo.java new file mode 100644 index 00000000..1b860f6d --- /dev/null +++ b/src/core/lombok/core/handlers/SneakyThrowsDependencyInfo.java @@ -0,0 +1,44 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.core.handlers; + +import java.util.Arrays; +import java.util.List; + +import lombok.core.runtimeDependencies.RuntimeDependencyInfo; + +import org.mangosdk.spi.ProviderFor; + +@ProviderFor(RuntimeDependencyInfo.class) +public class SneakyThrowsDependencyInfo implements RuntimeDependencyInfo { + @Override public List<String> getRuntimeDependencies() { + return Arrays.asList( + "/lombok/Lombok.class" + ); + } + + @Override public List<String> getRuntimeDependentsDescriptions() { + return Arrays.asList( + "@SneakyThrows" + ); + } +} diff --git a/src/core/lombok/core/runtimeDependencies/CreateLombokRuntimeApp.java b/src/core/lombok/core/runtimeDependencies/CreateLombokRuntimeApp.java new file mode 100644 index 00000000..6f39082e --- /dev/null +++ b/src/core/lombok/core/runtimeDependencies/CreateLombokRuntimeApp.java @@ -0,0 +1,239 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.core.runtimeDependencies; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.jar.JarOutputStream; +import java.util.zip.ZipEntry; + +import lombok.core.LombokApp; +import lombok.core.SpiLoadUtil; + +import org.mangosdk.spi.ProviderFor; + +import com.zwitserloot.cmdreader.CmdReader; +import com.zwitserloot.cmdreader.Description; +import com.zwitserloot.cmdreader.InvalidCommandLineException; +import com.zwitserloot.cmdreader.Mandatory; +import com.zwitserloot.cmdreader.Parameterized; +import com.zwitserloot.cmdreader.Requires; +import com.zwitserloot.cmdreader.Shorthand; + +@ProviderFor(LombokApp.class) +public class CreateLombokRuntimeApp implements LombokApp { + private List<RuntimeDependencyInfo> infoObjects; + + @Override public String getAppName() { + return "createRuntime"; + } + + @Override public String getAppDescription() { + return "Creates a small lombok-runtime.jar with the runtime\n" + + "dependencies of all lombok transformations that have them,\n" + + "and prints the names of each lombok transformation that\n" + + "requires the lombok-runtime.jar at runtime."; + } + + @Override public List<String> getAppAliases() { + return Arrays.asList("createRuntime", "runtime"); + } + + private static class CmdArgs { + @Shorthand("p") + @Description("Prints those lombok transformations that require lombok-runtime.jar.") + @Mandatory(onlyIfNot="create") + boolean print; + + @Shorthand("c") + @Description("Creates the lombok-runtime.jar.") + @Mandatory(onlyIfNot="print") + boolean create; + + @Shorthand("o") + @Description("Where to write the lombok-runtime.jar. Defaults to the current working directory.") + @Parameterized + @Requires("create") + String output; + + @Shorthand({"h", "?"}) + @Description("Shows this help text") + boolean help; + } + + @Override public int runApp(List<String> rawArgs) throws Exception { + CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class); + CmdArgs args; + try { + args = reader.make(rawArgs.toArray(new String[0])); + } catch (InvalidCommandLineException e) { + printHelp(reader, e.getMessage(), System.err); + return 1; + } + + if (args.help) { + printHelp(reader, null, System.out); + return 0; + } + + initializeInfoObjects(); + + if (args.print) { + printRuntimeDependents(); + } + + int errCode = 0; + + if (args.create) { + File out = new File("./lombok-runtime.jar"); + if (args.output != null) { + out = new File(args.output); + if (out.isDirectory()) out = new File(out, "lombok-runtime.jar"); + } + + try { + errCode = writeRuntimeJar(out); + } catch (Exception e) { + System.err.println("ERROR: Creating " + canonical(out) + " failed: "); + e.printStackTrace(); + return 1; + } + } + + return errCode; + } + + private void printRuntimeDependents() { + List<String> descriptions = new ArrayList<String>(); + for (RuntimeDependencyInfo info : infoObjects) descriptions.addAll(info.getRuntimeDependentsDescriptions()); + if (descriptions.isEmpty()) { + System.out.println("Not printing dependents: No lombok transformations currently have any runtime dependencies!"); + } else { + System.out.println("Using any of these lombok features means your app will need lombok-runtime.jar:"); + for (String desc : descriptions) { + System.out.println(desc); + } + } + } + + private int writeRuntimeJar(File outFile) throws Exception { + Map<Class<?>, List<String>> deps = new LinkedHashMap<Class<?>, List<String>>(); + for (RuntimeDependencyInfo info : infoObjects) { + List<String> depNames = info.getRuntimeDependencies(); + if (depNames != null && !depNames.isEmpty()) { + deps.put(info.getClass(), depNames); + } + } + + if (deps.isEmpty()) { + System.out.println("Not generating lombok-runtime.jar: No lombok transformations currently have any runtime dependencies!"); + return 1; + } + + OutputStream out = new FileOutputStream(outFile); + boolean success = false; + try { + JarOutputStream jar = new JarOutputStream(out); + for (Entry<Class<?>, List<String>> dep : deps.entrySet()) { + for (String depName : dep.getValue()) { + InputStream in = dep.getKey().getResourceAsStream(depName); + try { + if (in == null) { + throw new Fail(String.format("Dependency %s contributed by %s cannot be found", depName, dep.getKey().getName())); + } + writeIntoJar(jar, depName, in); + } finally { + if (in != null) in.close(); + } + } + } + jar.close(); + out.close(); + + System.out.println("Successfully created: " + canonical(outFile)); + + return 0; + } catch (Throwable t) { + try { out.close();} catch (Throwable ignore) {} + if (!success) outFile.delete(); + if (t instanceof Fail) { + System.err.println(t.getMessage()); + return 1; + } else if (t instanceof Exception) { + throw (Exception)t; + } else if (t instanceof Error) { + throw (Error)t; + } else { + throw new Exception(t); + } + } + } + + private void writeIntoJar(JarOutputStream jar, String depName, InputStream in) throws IOException { + jar.putNextEntry(new ZipEntry(depName)); + byte[] b = new byte[65536]; + while (true) { + int r = in.read(b); + if (r == -1) break; + jar.write(b, 0, r); + } + jar.closeEntry(); + in.close(); + } + + private static class Fail extends Exception { + Fail(String message) { + super(message); + } + } + + private void initializeInfoObjects() throws IOException { + infoObjects = SpiLoadUtil.readAllFromIterator( + SpiLoadUtil.findServices(RuntimeDependencyInfo.class)); + } + + private static String canonical(File out) { + try { + return out.getCanonicalPath(); + } catch (Exception e) { + return out.getAbsolutePath(); + } + } + + private void printHelp(CmdReader<CmdArgs> reader, String message, PrintStream out) { + if (message != null) { + out.println(message); + out.println("----------------------------"); + } + out.println(reader.generateCommandLineHelp("java -jar lombok.jar createRuntime")); + } +} diff --git a/src/core/lombok/core/runtimeDependencies/RuntimeDependencyInfo.java b/src/core/lombok/core/runtimeDependencies/RuntimeDependencyInfo.java new file mode 100644 index 00000000..e83332c8 --- /dev/null +++ b/src/core/lombok/core/runtimeDependencies/RuntimeDependencyInfo.java @@ -0,0 +1,41 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.core.runtimeDependencies; + +import java.util.List; + +/** + * Implement and provide this interface to specify which transformations have a runtime dependency on + * {@code lombok-runtime.jar}, as well as which files of your transformation must be in {@code lombok-runtime.jar}. + */ +public interface RuntimeDependencyInfo { + /** + * @return A list of strings describing each lombok transformation that has a runtime dependency. + */ + public List<String> getRuntimeDependentsDescriptions(); + + /** + * @return A list of files (findable via {@code yourClass.getResourceAsStream(NAME)}) to include in + * {@code lombok-runtime.jar}. + */ + public List<String> getRuntimeDependencies(); +} |