Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adjusted the projects generated by the wizard.

  • Loading branch information...
commit 02dd4b67831622e86a4f9ee911be8f033d1351a3 1 parent 855e35d
Attila Kelemen authored
4 src/org/netbeans/gradle/project/StringUtils.java
View
@@ -17,6 +17,10 @@
public final class StringUtils {
private static final String SEPARATORS = ",./?;:'\"\\";
+ public static String emptyForNull(String str) {
+ return str != null ? str : "";
+ }
+
public static String stripSeperatorsFromEnd(String str) {
for (int i = str.length() - 1; i >= 0; i--) {
if (SEPARATORS.indexOf(str.charAt(i)) < 0) {
39 src/org/netbeans/gradle/project/newproject/GradleMultiProjectWizardIterator.java
View
@@ -4,7 +4,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
@@ -33,7 +35,11 @@
@StaticResource
private static final String MULTI_PROJECT_BUILD_GRADLE = "org/netbeans/gradle/project/resources/newproject/multi-project-root.gradle";
+
+ @StaticResource
private static final String MULTI_PROJECT_PARENT_GRADLE = "org/netbeans/gradle/project/resources/newproject/multi-project-parent.gradle";
+
+ @StaticResource
private static final String MULTI_PROJECT_SETTINGS_GRADLE = "org/netbeans/gradle/project/resources/newproject/multi-project-settings.gradle";
private final List<WizardDescriptor.Panel<WizardDescriptor>> descriptors;
@@ -46,22 +52,31 @@ public GradleMultiProjectWizardIterator() {
this.configRef = new AtomicReference<GradleMultiProjectConfig>(null);
}
+ private static void createSettingsGradle(File projectDir) throws IOException {
+ Map<String, String> varReplaceMap =
+ Collections.singletonMap("${PROJECT_NAME}", projectDir.getName());
+
+ NewProjectUtils.copyTemplateFile(
+ MULTI_PROJECT_SETTINGS_GRADLE,
+ new File(projectDir, GradleProjectConstants.SETTINGS_FILE_NAME),
+ NewProjectUtils.DEFAULT_FILE_ENCODING,
+ varReplaceMap);
+ }
+
private static void createParentGradle(
File projectDir,
GradleMultiProjectConfig config) throws IOException {
- String parentGradleContent = StringUtils.getResourceAsString(
- MULTI_PROJECT_PARENT_GRADLE,
- NewProjectUtils.DEFAULT_FILE_ENCODING);
- parentGradleContent = parentGradleContent.replace("${MAVEN_GROUP}",
- config.getMavenGroupId());
- parentGradleContent = parentGradleContent.replace("${MAVEN_VERSION}",
- config.getMavenVersion());
- parentGradleContent = parentGradleContent.replace("${SOURCE_LEVEL}",
- AbstractProjectProperties.getSourceLevelFromPlatform(JavaPlatform.getDefault()));
+ Map<String, String> varReplaceMap = new HashMap<String, String>();
+ varReplaceMap.put("${MAVEN_GROUP}", config.getMavenGroupId());
+ varReplaceMap.put("${MAVEN_VERSION}", config.getMavenVersion());
+ varReplaceMap.put("${SOURCE_LEVEL}", AbstractProjectProperties.getSourceLevelFromPlatform(JavaPlatform.getDefault()));
- File parentGradle = new File(projectDir, "parent.gradle");
- StringUtils.writeStringToFile(parentGradleContent, NewProjectUtils.DEFAULT_FILE_ENCODING, parentGradle);
+ NewProjectUtils.copyTemplateFile(
+ MULTI_PROJECT_PARENT_GRADLE,
+ new File(projectDir, "parent.gradle"),
+ NewProjectUtils.DEFAULT_FILE_ENCODING,
+ varReplaceMap);
}
private static void copyToProject(
@@ -89,7 +104,7 @@ private static void copyToProject(
NewProjectUtils.createDefaultSourceDirs(projectDir);
createParentGradle(projectDirAsFile, config);
copyToProject(projectDirAsFile, MULTI_PROJECT_BUILD_GRADLE, GradleProjectConstants.BUILD_FILE_NAME);
- copyToProject(projectDirAsFile, MULTI_PROJECT_SETTINGS_GRADLE, GradleProjectConstants.SETTINGS_FILE_NAME);
+ createSettingsGradle(projectDirAsFile);
return Collections.singleton(projectDir);
}
36 src/org/netbeans/gradle/project/newproject/GradleSingleProjectWizardIterator.java
View
@@ -4,7 +4,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
@@ -34,6 +36,9 @@
@StaticResource
private static final String SINGLE_PROJECT_BUILD_GRADLE = "org/netbeans/gradle/project/resources/newproject/single-project.gradle";
+ @StaticResource
+ private static final String SINGLE_PROJECT_SETTINGS_GRADLE = "org/netbeans/gradle/project/resources/newproject/single-project-settings.gradle";
+
private final List<WizardDescriptor.Panel<WizardDescriptor>> descriptors;
private final AtomicReference<GradleSingleProjectConfig> configRef;
private int descriptorIndex;
@@ -48,18 +53,28 @@ private static void createBuildGradle(
File projectDir,
GradleSingleProjectConfig config) throws IOException {
String mainClass = config.getMainClass();
+ String sourceLevel = AbstractProjectProperties.getSourceLevelFromPlatform(JavaPlatform.getDefault());
- String buildGradleContent = StringUtils.getResourceAsString(
+ Map<String, String> varReplaceMap = new HashMap<String, String>();
+ varReplaceMap.put("${MAIN_CLASS}", StringUtils.emptyForNull(mainClass));
+ varReplaceMap.put("${SOURCE_LEVEL}", sourceLevel);
+
+ NewProjectUtils.copyTemplateFile(
SINGLE_PROJECT_BUILD_GRADLE,
- NewProjectUtils.DEFAULT_FILE_ENCODING);
+ new File(projectDir, GradleProjectConstants.BUILD_FILE_NAME),
+ NewProjectUtils.DEFAULT_FILE_ENCODING,
+ varReplaceMap);
+ }
- buildGradleContent = buildGradleContent.replace("${MAIN_CLASS}",
- mainClass != null ? mainClass : "");
- buildGradleContent = buildGradleContent.replace("${SOURCE_LEVEL}",
- AbstractProjectProperties.getSourceLevelFromPlatform(JavaPlatform.getDefault()));
+ private static void createSettingsGradle(File projectDir) throws IOException {
+ Map<String, String> varReplaceMap =
+ Collections.singletonMap("${PROJECT_NAME}", projectDir.getName());
- File buildGradle = new File(projectDir, GradleProjectConstants.BUILD_FILE_NAME);
- StringUtils.writeStringToFile(buildGradleContent, NewProjectUtils.DEFAULT_FILE_ENCODING, buildGradle);
+ NewProjectUtils.copyTemplateFile(
+ SINGLE_PROJECT_SETTINGS_GRADLE,
+ new File(projectDir, GradleProjectConstants.SETTINGS_FILE_NAME),
+ NewProjectUtils.DEFAULT_FILE_ENCODING,
+ varReplaceMap);
}
@Override
@@ -74,16 +89,13 @@ private static void createBuildGradle(
NewProjectUtils.createDefaultSourceDirs(projectDir);
createBuildGradle(projectDirAsFile, config);
+ createSettingsGradle(projectDirAsFile);
String mainClass = config.getMainClass();
if (mainClass != null) {
NewProjectUtils.createMainClass(projectDirAsFile, mainClass);
}
- StringUtils.writeStringToFile("",
- NewProjectUtils.DEFAULT_FILE_ENCODING,
- new File(projectDirAsFile, GradleProjectConstants.SETTINGS_FILE_NAME));
-
return Collections.singleton(projectDir);
}
16 src/org/netbeans/gradle/project/newproject/NewProjectUtils.java
View
@@ -4,6 +4,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.util.Map;
import java.util.prefs.Preferences;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
@@ -36,6 +37,21 @@ public static Preferences getPreferences() {
return NbPreferences.forModule(NewProjectUtils.class);
}
+ public static void copyTemplateFile(
+ String resourcePath,
+ File destination,
+ Charset encoding,
+ Map<String, String> varReplaceMap) throws IOException {
+
+ String resourceContent = StringUtils.getResourceAsString(resourcePath, encoding);
+
+ for (Map.Entry<String, String> entry: varReplaceMap.entrySet()) {
+ resourceContent = resourceContent.replace(entry.getKey(), entry.getValue());
+ }
+
+ StringUtils.writeStringToFile(resourceContent, encoding, destination);
+ }
+
public static String getDefaultProjectDir() {
String defaultDir = NewProjectUtils.getPreferences().get(DEFAULT_PROJECTDIR_SETTINGS_KEY, "");
if (defaultDir.isEmpty()) {
5 src/org/netbeans/gradle/project/resources/newproject/multi-project-settings.gradle
View
@@ -1,3 +1,8 @@
+rootProject.name = '${PROJECT_NAME}';
+
+// Find the directories containing a "build.gradle" file in the root directory
+// of the project. That is, every directory containing a "build.gradle" will
+// be automatically the subproject of this project.
def subDirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (!file.isDirectory()) {
1  src/org/netbeans/gradle/project/resources/newproject/single-project-settings.gradle
View
@@ -0,0 +1 @@
+rootProject.name = '${PROJECT_NAME}';
2  src/org/netbeans/gradle/project/resources/newproject/single-project.gradle
View
@@ -22,5 +22,7 @@ repositories {
dependencies {
// TODO: Add dependencies here ...
+ // You can read more about how to add dependency here:
+ // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
testCompile group: 'junit', name: 'junit', version: '4.10';
}
9 src/org/netbeans/gradle/project/resources/newproject/subproject.gradle
View
@@ -1,8 +1,15 @@
+// Note that "mainClass" must be set before applying "parent.gradle" because
+// "parent.gradle" will define the "run" and "debug" task based on this value.
ext.mainClass = '${MAIN_CLASS}';
-apply from: rootProject.uri('parent.gradle');
+apply from: rootProject.file('parent.gradle');
dependencies {
// TODO: Add dependencies here
// but note that JUnit should have already been added in parent.gradle.
+ // By default, only the Maven Central Repository is specified in
+ // parent.gradle.
+ //
+ // You can read more about how to add dependency here:
+ // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
}
Please sign in to comment.
Something went wrong with that request. Please try again.