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 | |
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.
-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 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokApp.java | 16 | ||||
-rw-r--r-- | src/installer/lombok/installer/Installer.java | 275 |
8 files changed, 697 insertions, 106 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(); +} diff --git a/src/delombok/lombok/delombok/DelombokApp.java b/src/delombok/lombok/delombok/DelombokApp.java index 199534e4..71fda96f 100644 --- a/src/delombok/lombok/delombok/DelombokApp.java +++ b/src/delombok/lombok/delombok/DelombokApp.java @@ -7,7 +7,9 @@ import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Arrays; import java.util.Enumeration; +import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -17,7 +19,7 @@ import org.mangosdk.spi.ProviderFor; @ProviderFor(LombokApp.class) public class DelombokApp implements LombokApp { - @Override public int runApp(String[] args) throws Exception { + @Override public int runApp(List<String> args) throws Exception { try { Class.forName("com.sun.tools.javac.main.JavaCompiler"); runDirectly(args); @@ -117,8 +119,8 @@ public class DelombokApp implements LombokApp { } } - private void runDirectly(String[] args) { - Delombok.main(args); + private void runDirectly(List<String> args) { + Delombok.main(args.toArray(new String[0])); } private static File findToolsJar() { @@ -168,5 +170,13 @@ public class DelombokApp implements LombokApp { @Override public String getAppName() { return "delombok"; } + + @Override public List<String> getAppAliases() { + return Arrays.asList("unlombok", "delombok"); + } + + @Override public String getAppDescription() { + return "Applies lombok transformations without compiling your\njava code (so, 'unpacks' lombok annotations and such)."; + } } diff --git a/src/installer/lombok/installer/Installer.java b/src/installer/lombok/installer/Installer.java index 4e96b219..d4b31251 100644 --- a/src/installer/lombok/installer/Installer.java +++ b/src/installer/lombok/installer/Installer.java @@ -40,9 +40,9 @@ import java.awt.event.ActionListener; import java.awt.font.TextAttribute; import java.io.File; import java.io.FilenameFilter; -import java.io.IOException; import java.net.URI; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -65,21 +65,29 @@ import javax.swing.UIManager; import javax.swing.filechooser.FileFilter; import lombok.core.LombokApp; -import lombok.core.SpiLoadUtil; import lombok.core.Version; import lombok.installer.EclipseFinder.OS; import lombok.installer.EclipseLocation.InstallException; import lombok.installer.EclipseLocation.NotAnEclipseException; import lombok.installer.EclipseLocation.UninstallException; +import org.mangosdk.spi.ProviderFor; + +import com.zwitserloot.cmdreader.CmdReader; +import com.zwitserloot.cmdreader.Description; +import com.zwitserloot.cmdreader.InvalidCommandLineException; +import com.zwitserloot.cmdreader.Parameterized; +import com.zwitserloot.cmdreader.Shorthand; + /** * The lombok installer proper. * Uses swing to show a simple GUI that can add and remove the java agent to Eclipse installations. * Also offers info on what this installer does in case people want to instrument their Eclipse manually, - * and looks in some common places on Mac OS X and Windows. + * and looks in some common places on Mac OS X, Linux and Windows. */ public class Installer { private static final URI ABOUT_LOMBOK_URL = URI.create("http://projectlombok.org"); + private static final AtomicReference<Integer> exitMarker = new AtomicReference<Integer>(); private JFrame appWindow; @@ -97,103 +105,64 @@ public class Installer { private JLabel uninstallPlaceholder; private JButton installButton; - public static void main(String[] args) { - if (args.length > 0) { - String appName = args[0]; - String[] newArgs = new String[args.length-1]; - System.arraycopy(args, 1, newArgs, 0, newArgs.length); - Iterable<LombokApp> services; - try { - services = SpiLoadUtil.findServices(LombokApp.class); - } catch (IOException e) { - System.err.println("Your lombok installation appears to be corrupted! Please let us know by clicking 'report bugs' on projectlombok.org. Include this stack trace:"); - e.printStackTrace(); - System.exit(2); - return; - } - for (LombokApp app : services) { - if (appName.equals(app.getAppName())) { - try { - int returnCode = app.runApp(newArgs); - System.exit(returnCode); - } catch (Exception e) { - e.printStackTrace(); - System.exit(10); - } - return; - } - } + @ProviderFor(LombokApp.class) + public static class GraphicalInstallerApp implements LombokApp { + @Override public String getAppName() { + return "installer"; } - if (args.length > 0 && (args[0].equals("install") || args[0].equals("uninstall"))) { - boolean uninstall = args[0].equals("uninstall"); - if (args.length < 3 || !args[1].equals("eclipse")) { - System.err.printf("Run java -jar lombok.jar %1$s eclipse path/to/eclipse/executable (or 'auto' to %1$s to all auto-discovered eclipse locations)\n", uninstall ? "uninstall" : "install"); - System.exit(1); - } - String path = args[2]; - try { - final List<EclipseLocation> locations = new ArrayList<EclipseLocation>(); - final List<NotAnEclipseException> problems = new ArrayList<NotAnEclipseException>(); - if (path.equals("auto")) { - EclipseFinder.findEclipses(locations, problems); - } else { - locations.add(EclipseLocation.create(path)); - } - int validLocations = locations.size(); - for (EclipseLocation loc : locations) { - try { - if (uninstall) { - loc.uninstall(); - } else { - loc.install(); - } - System.out.printf("Lombok %s %s: %s\n", uninstall ? "uninstalled" : "installed", uninstall ? "from" : "to", loc.getName()); - } catch (InstallException e) { - System.err.printf("Installation at %s failed:\n", loc.getName()); - System.err.println(e.getMessage()); - validLocations--; - } catch (UninstallException e) { - System.err.printf("Uninstall at %s failed:\n", loc.getName()); - System.err.println(e.getMessage()); - validLocations--; - } - } - for (NotAnEclipseException problem : problems) { - System.err.println("WARNING: " + problem.getMessage()); - } - if (validLocations == 0) { - System.err.println("WARNING: Zero valid locations found; so nothing was done."); - } - System.exit(0); - } catch (NotAnEclipseException e) { - System.err.println("Not a valid eclipse location:"); - System.err.println(e.getMessage()); - System.exit(2); - } + + @Override public String getAppDescription() { + return "Runs the graphical installer tool (default)."; } - if (args.length > 0 && args[0].equals("uninstall")) { - if (args.length < 3 || !args[1].equals("eclipse")) { - System.err.println("Run java -jar lombok.jar uninstall eclipse path/to/eclipse/executable (or 'auto' to uninstall all auto-discovered eclipse locations)"); - System.exit(1); - } - String path = args[2]; - try { - EclipseLocation loc = EclipseLocation.create(path); - loc.uninstall(); - System.out.println("Uninstalled from: " + loc.getName()); - System.exit(0); - } catch (NotAnEclipseException e) { - System.err.println("Not a valid eclipse location:"); - System.err.println(e.getMessage()); - System.exit(2); - } catch (UninstallException e) { - System.err.println("Uninstall failed:"); - System.err.println(e.getMessage()); - System.exit(1); - } + @Override public List<String> getAppAliases() { + return Arrays.asList("installer", ""); + } + + @Override public int runApp(List<String> args) throws Exception { + return guiInstaller(); + } + } + + @ProviderFor(LombokApp.class) + public static class CommandLineInstallerApp implements LombokApp { + @Override public String getAppName() { + return "install"; + } + + @Override public String getAppDescription() { + return "Runs the 'handsfree' command line scriptable installer."; + } + + @Override public List<String> getAppAliases() { + return Arrays.asList("install"); + } + + @Override public int runApp(List<String> args) throws Exception { + return cliInstaller(false, args); + } + } + + @ProviderFor(LombokApp.class) + public static class CommandLineUninstallerApp implements LombokApp { + @Override public String getAppName() { + return "uninstall"; + } + + @Override public String getAppDescription() { + return "Runs the 'handsfree' command line scriptable uninstaller."; + } + + @Override public List<String> getAppAliases() { + return Arrays.asList("uninstall"); } + @Override public int runApp(List<String> args) throws Exception { + return cliInstaller(true, args); + } + } + + private static int guiInstaller() { if (EclipseFinder.getOS() == OS.MAC_OS_X) { System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Lombok Installer"); System.setProperty("com.apple.macos.use-file-dialog-packages", "true"); @@ -213,11 +182,108 @@ public class Installer { } } }); + + synchronized (exitMarker) { + while (!Thread.interrupted() && exitMarker.get() == null) { + try { + exitMarker.wait(); + } catch (InterruptedException e) { + return 1; + } + } + Integer errCode = exitMarker.get(); + return errCode == null ? 1 : errCode; + } } catch (HeadlessException e) { printHeadlessInfo(); + return 1; } } + private static class CmdArgs { + @Shorthand("e") + @Description("Specify a path to an eclipse location to install/uninstall. Use 'auto' to apply to all automatically discoverable eclipse installations.") + @Parameterized + List<String> eclipse = new ArrayList<String>(); + + @Shorthand({"?", "h"}) + @Description("Shows this help text") + boolean help; + } + + public static int cliInstaller(boolean uninstall, List<String> rawArgs) { + CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class); + CmdArgs args; + try { + args = reader.make(rawArgs.toArray(new String[0])); + } catch (InvalidCommandLineException e) { + System.err.println(e.getMessage()); + System.err.println("--------------------------"); + System.err.println(generateCliHelp(uninstall, reader)); + return 1; + } + + if (args.help) { + System.out.println(generateCliHelp(uninstall, reader)); + return 0; + } + + if (args.eclipse.isEmpty()) { + System.err.println("ERROR: Nothing to do!"); + System.err.println("--------------------------"); + System.err.println(generateCliHelp(uninstall, reader)); + return 1; + } + + final List<EclipseLocation> locations = new ArrayList<EclipseLocation>(); + final List<NotAnEclipseException> problems = new ArrayList<NotAnEclipseException>(); + + for (String rawPath : args.eclipse) { + if (rawPath.equals("auto")) { + EclipseFinder.findEclipses(locations, problems); + } else { + try { + locations.add(EclipseLocation.create(rawPath)); + } catch (NotAnEclipseException e) { + problems.add(e); + } + } + } + + int validLocations = locations.size(); + for (EclipseLocation loc : locations) { + try { + if (uninstall) { + loc.uninstall(); + } else { + loc.install(); + } + System.out.printf("Lombok %s %s: %s\n", uninstall ? "uninstalled" : "installed", uninstall ? "from" : "to", loc.getName()); + } catch (InstallException e) { + System.err.printf("Installation at %s failed:\n", loc.getName()); + System.err.println(e.getMessage()); + validLocations--; + } catch (UninstallException e) { + System.err.printf("Uninstall at %s failed:\n", loc.getName()); + System.err.println(e.getMessage()); + validLocations--; + } + } + + for (NotAnEclipseException problem : problems) { + System.err.println("WARNING: " + problem.getMessage()); + } + + if (validLocations == 0) { + System.err.println("WARNING: Zero valid locations found; so nothing was done!"); + } + return 0; + } + + private static String generateCliHelp(boolean uninstall, CmdReader<CmdArgs> reader) { + return reader.generateCommandLineHelp("java -jar lombok.jar " + (uninstall ? "uninstall" : "install")); + } + /** * If run in headless mode, the installer can't show its fancy GUI. There's little point in running * the installer without a GUI environment, as Eclipse doesn't run in headless mode either, so @@ -228,13 +294,12 @@ public class Installer { "Lombok makes java better by providing very spicy additions to the Java programming language," + "such as using @Getter to automatically generate a getter method for any field.\n\n" + "Browse to %s for more information. To install lombok on Eclipse, re-run this jar file on a " + - "graphical computer system - this message is being shown because your terminal is not graphics capable." + + "graphical computer system - this message is being shown because your terminal is not graphics capable.\n" + + "Alternatively, use the command line installer (java -jar lombok.jar install --help).\n" + "If you are just using 'javac' or a tool that calls on javac, no installation is neccessary; just " + "make sure lombok.jar is in the classpath when you compile. Example:\n\n" + - " java -cp lombok.jar MyCode.java\n\n\n" + - "If for whatever reason you can't run the graphical installer but you do want to install lombok into eclipse," + - "start this jar with the following syntax:\n\n" + - " java -jar lombok.jar install eclipse path/to/your/eclipse/executable", Version.getVersion(), ABOUT_LOMBOK_URL); + " java -cp lombok.jar MyCode.java\n", + Version.getVersion(), ABOUT_LOMBOK_URL); } /** @@ -625,13 +690,19 @@ public class Installer { "as parameter as well.</html>", "Install successful", JOptionPane.INFORMATION_MESSAGE); appWindow.setVisible(false); - System.exit(0); + synchronized (exitMarker) { + exitMarker.set(0); + exitMarker.notifyAll(); + } } }); if (!success.get()) SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - System.exit(0); + synchronized (exitMarker) { + exitMarker.set(1); + exitMarker.notifyAll(); + } } }); } |