From ef655dfc526a614bdce0dec8e16729a0e959a268 Mon Sep 17 00:00:00 2001 From: Scott Resnik Date: Thu, 1 Dec 2016 15:53:10 -0600 Subject: [PATCH 1/6] Added linkedResource method to add linked workspace resources. --- .../gradle/oomph/OomphIdeExtension.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java index 78952e70e..3489064ef 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java @@ -213,6 +213,30 @@ public void addSetupActionLazy(Action> lazyInternalSetupAction setupActions.addLazyAction(lazyInternalSetupAction); } + final static String CORE_RES_PREFS_FILE = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs"; + final static String WS_PATHVAR_FMT = "pathvariable.%s"; + + public void linkedResource(String linkName, File linkTarget) { + workspaceProp(CORE_RES_PREFS_FILE, new LinkedResourceAction(linkName, linkTarget)); + } + + static class LinkedResourceAction implements Action> { + private String linkName; + private File linkTarget; + + public LinkedResourceAction(String linkName, File linkTarget) { + super(); + this.linkName = linkName; + this.linkTarget = linkTarget; + } + + @Override + public void execute(Map props) { + //Eclipse cannot handle backslashes in this value. It expects path separators to be '/' + props.put(String.format(WS_PATHVAR_FMT, linkName), linkTarget.getAbsolutePath().replace("\\", "/")); + } + } + //////////////// // ideSetupP2 // //////////////// From e4cd89812c975dffddb79e83ea0d3cafecf380dd Mon Sep 17 00:00:00 2001 From: Scott Resnik Date: Thu, 1 Dec 2016 15:54:00 -0600 Subject: [PATCH 2/6] Added compilerComplianceLevel method, and classpathVariables method. --- .../diffplug/gradle/oomph/ConventionJdt.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index c9a5455f3..460e2f7ef 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -15,10 +15,15 @@ */ package com.diffplug.gradle.oomph; +import java.util.Arrays; import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Set; import org.gradle.api.Action; +import org.gradle.api.JavaVersion; /** * Adding the JDT convention to your project @@ -33,17 +38,24 @@ * ```gradle * oomphIde { * jdt { + * * installedJre { * version = '1.6.0_45' * installedLocation = new File('C:/jdk1.6.0_45') * markDefault = true // or false * executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here. * } + * compilerComplianceLevel('1.6') + * classpathVariable('myClasspath', '/var/lib/repo') * } * } * ``` */ public class ConventionJdt extends OomphConvention { + public final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; + public final static List JDT_COMPLIANCE_PROPS = Arrays.asList("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); + public final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; + ConventionJdt(OomphIdeExtension extension) { super(extension); requireIUs(IUs.IDE, IUs.JDT, IUs.ERROR_LOG); @@ -51,6 +63,8 @@ public class ConventionJdt extends OomphConvention { } final Set installedJres = new HashSet<>(); + final Map classpathVariables = new LinkedHashMap<>(); + private JavaVersion compilerComplianceLevel; /** Adds an installed JRE with the given content. */ public void installedJre(Action action) { @@ -59,11 +73,59 @@ public void installedJre(Action action) { installedJres.add(instance); } + /** Sets default compliance level */ + public void compilerComplianceLevel(String compilerComplianceLevel) { + this.compilerComplianceLevel = JavaVersion.toVersion(compilerComplianceLevel); + } + + /** Adds a compiler class path variable. */ + public void classpathVariable(String name, String value) { + classpathVariables.put(name, value); + } + @Override public void close() { // add installed jres if (!installedJres.isEmpty()) { extension.addSetupAction(new InstalledJreAdder(installedJres)); } + if (!classpathVariables.isEmpty()) { + extension.workspaceProp(JDT_CORE_PREFS, new JavaClasspathVariableAction(classpathVariables)); + } + if (compilerComplianceLevel != null) { + extension.workspaceProp(JDT_CORE_PREFS, new JavaComplianceAction(compilerComplianceLevel)); + } + } + + static class JavaComplianceAction implements Action> { + private final JavaVersion complianceLevel; + + public JavaComplianceAction(JavaVersion complianceLevel) { + super(); + this.complianceLevel = complianceLevel; + } + + @Override + public void execute(Map props) { + JDT_COMPLIANCE_PROPS.forEach(p -> props.put(p, complianceLevel.toString())); + //Use default compliance settings. + props.put("org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode", "enabled"); + props.put("org.eclipse.jdt.core.compiler.problem.assertIdentifier", "error"); + props.put("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error"); + } + } + + static class JavaClasspathVariableAction implements Action> { + private final Map classpathVariables; + + public JavaClasspathVariableAction(Map classpathVariables) { + super(); + this.classpathVariables = classpathVariables; + } + + @Override + public void execute(Map props) { + classpathVariables.forEach((key, value) -> props.put(String.format(JDT_CLASSPATH_VAR_FMT, key), value)); + } } } From 10aa587d48ce4664fbd8316c41bf4b940464607d Mon Sep 17 00:00:00 2001 From: Scott Resnik Date: Thu, 1 Dec 2016 17:25:59 -0600 Subject: [PATCH 3/6] Restrict access to constants. --- src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index 460e2f7ef..99b1d6f06 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -16,6 +16,7 @@ package com.diffplug.gradle.oomph; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -52,9 +53,9 @@ * ``` */ public class ConventionJdt extends OomphConvention { - public final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; - public final static List JDT_COMPLIANCE_PROPS = Arrays.asList("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); - public final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; + final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; + final static List JDT_COMPLIANCE_PROPS = Collections.unmodifiableList(Arrays.asList("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source")); + final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; ConventionJdt(OomphIdeExtension extension) { super(extension); From 5c53ab4e4d94bf12abfd07603414ead8a2aac6f0 Mon Sep 17 00:00:00 2001 From: Scott Resnik Date: Thu, 1 Dec 2016 18:07:04 -0600 Subject: [PATCH 4/6] Use ImmutableList. --- src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index 99b1d6f06..c9b434f4d 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -15,8 +15,6 @@ */ package com.diffplug.gradle.oomph; -import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -26,6 +24,8 @@ import org.gradle.api.Action; import org.gradle.api.JavaVersion; +import com.diffplug.common.collect.ImmutableList; + /** * Adding the JDT convention to your project * adds the following features: @@ -54,7 +54,7 @@ */ public class ConventionJdt extends OomphConvention { final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; - final static List JDT_COMPLIANCE_PROPS = Collections.unmodifiableList(Arrays.asList("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source")); + final static List JDT_COMPLIANCE_PROPS = ImmutableList.of("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; ConventionJdt(OomphIdeExtension extension) { From 7434506f7fd3a069f899c1d535eaf0a876306c6f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 1 Dec 2016 16:20:21 -0800 Subject: [PATCH 5/6] Moved new JdtConvention actions to be local, where possible. When a DSL method gets called (e.g. compilerComplicanceLevel), ideally we would call extension.workspaceProp() or extension.addSetupAction() right there, immediately. That keeps everything local, so there are fewer places to debug. There are cases, such as installedJres, where you have aggregate several calls to a method into a single action. In that case, we can use the `close()` method to know that the user has finished calling methods, and we can now take our aggregated actions. --- .../diffplug/gradle/oomph/ConventionJdt.java | 59 +++++-------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index c9b434f4d..9cf914ddc 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -54,8 +54,6 @@ */ public class ConventionJdt extends OomphConvention { final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; - final static List JDT_COMPLIANCE_PROPS = ImmutableList.of("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); - final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; ConventionJdt(OomphIdeExtension extension) { super(extension); @@ -64,8 +62,6 @@ public class ConventionJdt extends OomphConvention { } final Set installedJres = new HashSet<>(); - final Map classpathVariables = new LinkedHashMap<>(); - private JavaVersion compilerComplianceLevel; /** Adds an installed JRE with the given content. */ public void installedJre(Action action) { @@ -76,12 +72,25 @@ public void installedJre(Action action) { /** Sets default compliance level */ public void compilerComplianceLevel(String compilerComplianceLevel) { - this.compilerComplianceLevel = JavaVersion.toVersion(compilerComplianceLevel); + List JDT_COMPLIANCE_PROPS = ImmutableList.of( + "org.eclipse.jdt.core.compiler.codegen.targetPlatform", + "org.eclipse.jdt.core.compiler.compliance", + "org.eclipse.jdt.core.compiler.source"); + extension.workspaceProp(JDT_CORE_PREFS, props -> { + JDT_COMPLIANCE_PROPS.forEach(p -> props.put(p, compilerComplianceLevel.toString())); + //Use default compliance settings. + props.put("org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode", "enabled"); + props.put("org.eclipse.jdt.core.compiler.problem.assertIdentifier", "error"); + props.put("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error"); + }); } /** Adds a compiler class path variable. */ public void classpathVariable(String name, String value) { - classpathVariables.put(name, value); + String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; + extension.workspaceProp(JDT_CORE_PREFS, props -> { + props.put(String.format(JDT_CLASSPATH_VAR_FMT, name), value); + }); } @Override @@ -90,43 +99,5 @@ public void close() { if (!installedJres.isEmpty()) { extension.addSetupAction(new InstalledJreAdder(installedJres)); } - if (!classpathVariables.isEmpty()) { - extension.workspaceProp(JDT_CORE_PREFS, new JavaClasspathVariableAction(classpathVariables)); - } - if (compilerComplianceLevel != null) { - extension.workspaceProp(JDT_CORE_PREFS, new JavaComplianceAction(compilerComplianceLevel)); - } - } - - static class JavaComplianceAction implements Action> { - private final JavaVersion complianceLevel; - - public JavaComplianceAction(JavaVersion complianceLevel) { - super(); - this.complianceLevel = complianceLevel; - } - - @Override - public void execute(Map props) { - JDT_COMPLIANCE_PROPS.forEach(p -> props.put(p, complianceLevel.toString())); - //Use default compliance settings. - props.put("org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode", "enabled"); - props.put("org.eclipse.jdt.core.compiler.problem.assertIdentifier", "error"); - props.put("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error"); - } - } - - static class JavaClasspathVariableAction implements Action> { - private final Map classpathVariables; - - public JavaClasspathVariableAction(Map classpathVariables) { - super(); - this.classpathVariables = classpathVariables; - } - - @Override - public void execute(Map props) { - classpathVariables.forEach((key, value) -> props.put(String.format(JDT_CLASSPATH_VAR_FMT, key), value)); - } } } From 503cde429b1eefee7431a34ef7d91c2604bd72dc Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 1 Dec 2016 16:39:35 -0800 Subject: [PATCH 6/6] Refactored OomphIdeExtension.linkedResource to be local, and to lazily resolve the file. In a gradle DSL, anytime you want a File, you can do `project.file(Object input)` and it will give you a file. That way the input can be a File or a relative path, and gradle will resolve it to relative to the project. --- .../gradle/oomph/OomphIdeExtension.java | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java index 3489064ef..1349f2c3c 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java @@ -213,28 +213,14 @@ public void addSetupActionLazy(Action> lazyInternalSetupAction setupActions.addLazyAction(lazyInternalSetupAction); } - final static String CORE_RES_PREFS_FILE = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs"; - final static String WS_PATHVAR_FMT = "pathvariable.%s"; - - public void linkedResource(String linkName, File linkTarget) { - workspaceProp(CORE_RES_PREFS_FILE, new LinkedResourceAction(linkName, linkTarget)); - } - - static class LinkedResourceAction implements Action> { - private String linkName; - private File linkTarget; - - public LinkedResourceAction(String linkName, File linkTarget) { - super(); - this.linkName = linkName; - this.linkTarget = linkTarget; - } - - @Override - public void execute(Map props) { + /** Links the given target into the workspace with the given name, see [eclipse manual](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fconcepts-13.htm). */ + public void linkedResource(String linkName, Object linkTarget) { + final String CORE_RES_PREFS_FILE = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs"; + final String WS_PATHVAR_FMT = "pathvariable.%s"; + workspaceProp(CORE_RES_PREFS_FILE, props -> { //Eclipse cannot handle backslashes in this value. It expects path separators to be '/' - props.put(String.format(WS_PATHVAR_FMT, linkName), linkTarget.getAbsolutePath().replace("\\", "/")); - } + props.put(String.format(WS_PATHVAR_FMT, linkName), project.file(linkTarget).getAbsolutePath().replace("\\", "/")); + }); } ////////////////