From 110434ff60a14d2dfe8014459ffbea3ce7d6388b Mon Sep 17 00:00:00 2001 From: JohnPaulTaylorII Date: Fri, 18 Mar 2022 16:54:53 -0400 Subject: [fixes #3143] Maven+ECJ Agent Bootstrap * Adds AgentBootstrap, the actual bootstrapping agent * Adds MavenEcjBootstrapApp, the command line app for generating the appropriate files * Updates the build to package these correctly * Updates the documentation for setup/ecj --- .../lombok/eclipse/agent/MavenEcjBootstrapApp.java | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/eclipseAgent/lombok/eclipse/agent/MavenEcjBootstrapApp.java (limited to 'src/eclipseAgent/lombok/eclipse/agent') diff --git a/src/eclipseAgent/lombok/eclipse/agent/MavenEcjBootstrapApp.java b/src/eclipseAgent/lombok/eclipse/agent/MavenEcjBootstrapApp.java new file mode 100644 index 00000000..d08422be --- /dev/null +++ b/src/eclipseAgent/lombok/eclipse/agent/MavenEcjBootstrapApp.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2009-2022 The Project Lombok Authors. + * + * 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.eclipse.agent; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.List; + +import com.zwitserloot.cmdreader.CmdReader; +import com.zwitserloot.cmdreader.Description; +import com.zwitserloot.cmdreader.InvalidCommandLineException; +import com.zwitserloot.cmdreader.Shorthand; + +import lombok.core.LombokApp; +import lombok.spi.Provides; + +@Provides +public class MavenEcjBootstrapApp extends LombokApp { + @Override public String getAppName() { + return "createMavenECJBootstrap"; + } + + @Override public String getAppDescription() { + return "Creates .mvn/jvm.config and .mvn/lombok-bootstrap.jar for\n" + + "use with the ECJ compiler."; + } + + @Override public List getAppAliases() { + return Arrays.asList("ecj"); + } + + private static class CmdArgs { + @Shorthand("w") + @Description("Overwrite existing files. Defaults to false.") + boolean overwrite = false; + + @Shorthand("o") + @Description("The root of a Maven project. Defaults to the current working directory.") + String output; + + @Shorthand({"h", "?"}) + @Description("Shows this help text") + boolean help; + } + + @Override public int runApp(List rawArgs) throws Exception { + CmdReader 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; + } + + return createBootstrap(args.output, args.overwrite); + } + + private int createBootstrap(String root, boolean overwrite) { + File mvn = new File(root, ".mvn"); + int result = 0; + if (result == 0) result = makeMvn(mvn); + if (result == 0) result = makeJvmConfig(mvn, overwrite); + if (result == 0) result = makeJar(mvn, overwrite); + return result; + } + + private int makeMvn(File mvn) { + int result = 0; + Exception err = null; + try { + if (!mvn.exists() && !mvn.mkdirs()) result = 1; + } catch (Exception e) { + result = 1; + err = e; + } + if (result != 0) { + System.err.println("Could not create "+mvn.getPath()); + if (err != null) err.printStackTrace(System.err); + } + return result; + } + + private int makeJvmConfig(File mvn, boolean overwrite) { + File jvmConfig = new File(mvn, "jvm.config"); + if (jvmConfig.exists() && !overwrite) { + System.err.println(canonical(jvmConfig) + " exists but '-w' not specified."); + return 1; + } + try { + FileWriter writer = new FileWriter(jvmConfig); + writer.write("-javaagent:.mvn/lombok-bootstrap.jar"); + writer.flush(); + writer.close(); + System.out.println("Successfully created: " + canonical(jvmConfig)); + return 0; + } catch (Exception e) { + System.err.println("Could not create: " + canonical(jvmConfig)); + e.printStackTrace(System.err); + return 1; + } + } + + private int makeJar(File mvn, boolean overwrite) { + File jar = new File(mvn, "lombok-bootstrap.jar"); + if (jar.exists() && !overwrite) { + System.err.println(canonical(jar) + " but '-w' not specified."); + return 1; + } + try { + InputStream input = this.getClass().getResourceAsStream("/lombok/eclipse/agent/lombok-bootstrap.jar"); + FileOutputStream output = new FileOutputStream(jar); + byte[] buffer = new byte[4096]; + int length; + while ((length = input.read(buffer)) > 0) output.write(buffer, 0, length); + output.flush(); + output.close(); + System.out.println("Successfully created: " + canonical(jar)); + return 0; + } catch (Exception e) { + System.err.println("Could not create: " + canonical(jar)); + e.printStackTrace(System.err); + return 1; + } + } + + private static String canonical(File out) { + try { + return out.getCanonicalPath(); + } catch (Exception e) { + return out.getAbsolutePath(); + } + } + + private void printHelp(CmdReader reader, String message, PrintStream out) { + if (message != null) { + out.println(message); + out.println("----------------------------"); + } + out.println(reader.generateCommandLineHelp("java -jar lombok.jar createMavenECJBootstrap")); + } +} -- cgit