Permalink
Browse files

NewProject command automatically looks for and adds Strongback user l…

…ibrary if it is not there
  • Loading branch information...
rhauch committed Oct 18, 2015
1 parent 201276a commit 1bcbff1caefa3da9f73f76d2f90c4e52c494b463
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" output="build/test-classes" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="/libs/test/fest-assert-1.4.jar" sourcepath="/libs/test/fest-assert-1.4-sources.jar"/>
<classpathentry kind="lib" path="/libs/test/fest-util-1.1.6.jar" sourcepath="/libs/test/fest-util-1.1.6-sources.jar"/>
<classpathentry kind="lib" path="/libs/test/hamcrest-core-1.3.jar" sourcepath="/libs/test/hamcrest-core-1.3-sources.jar"/>
<classpathentry kind="output" path="build/test-classes"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
@@ -19,11 +19,18 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.InvalidParameterException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
@@ -105,6 +112,7 @@ public static void main(String[] args) {
}
/* Application Begins */
File strongbackHome = FileUtils.resolvePath("~/strongback");
// Source folders
File src = new File(projectRoot, "src" + File.separator + mainPackage.replace('.', File.separatorChar));
@@ -119,6 +127,8 @@ public static void main(String[] args) {
// Eclipse specific
File projectTemplate = new File(strongback.getProperty("strongback.templates.dir"), "project.template");
File classpathTemplate = new File(strongback.getProperty("strongback.templates.dir"), "classpath.template");
File userlibTemplate = new File(strongback.getProperty("strongback.templates.dir"), "Strongback.userlibraries.template");
File userlibImportTemplate = new File(strongback.getProperty("strongback.templates.dir"), "Strongback.userlibraries.import.template");
// Destination files
File buildProps = new File(projectRoot, "build.properties");
@@ -129,6 +139,9 @@ public static void main(String[] args) {
// Eclipse specific
File project = new File(projectRoot, ".project");
File classpath = new File(projectRoot, ".classpath");
File eclipseDir = FileUtils.resolvePath("~/strongback/java/eclipse");
File userlibraries = new File(eclipseDir, "Strongback.userlibraries");
File metadataDir = new File(projectRoot.getParentFile(), ".metadata");
// If any of the files to write to already exist, give up and write message about the overwrite flag
if(!params.containsKey("o")) {
@@ -162,6 +175,9 @@ public static void main(String[] args) {
if(!testsrc.exists() & !testsrc.mkdirs()) exit(Strings.FAILED_MKDIR + testsrc.getPath(), ExitCodes.FAILED_MKDIR);
// Copy templates
boolean eclipseProject = false;
boolean foundMetadata = false;
boolean updatedMetadata = false;
try {
copyTo(buildTemplate, buildXML, (line) -> line.replace("PROJECT_NAME", projectName));
copyTo(propsTemplate, buildProps, (line) -> line.replace("PACKAGE", mainPackage));
@@ -172,6 +188,41 @@ public static void main(String[] args) {
if(params.containsKey("e")) {
copyTo(projectTemplate, project, (line) -> line.replace("PROJECT_NAME", projectName));
copyTo(classpathTemplate, classpath, (line) -> line);
// Ensure the file for importing Eclipse userlibraries is generated ...
eclipseDir.mkdirs();
if(!userlibraries.exists()) {
copyTo(userlibImportTemplate, userlibraries, (line) -> line.replaceAll("STRONGBACKHOME", strongbackHome.getAbsolutePath()));
}
// See if the `Strongback` user library is in the workspace ...
if (metadataDir.exists()) {
foundMetadata = true;
File jdtPrefsFile = new File(metadataDir,".plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs");
if (jdtPrefsFile.exists()) {
Properties jdtPrefs = new Properties();
try ( InputStream is = new FileInputStream(jdtPrefsFile) ) {
jdtPrefs.load(is);
}
if (!jdtPrefs.isEmpty() && !jdtPrefs.containsKey("org.eclipse.jdt.core.userLibrary.Strongback")) {
// Make a backup of the original preferences file ...
File jdtPrefsFileCopy = new File(jdtPrefsFile.getParentFile(),"org.eclipse.jdt.core.prefs.backup");
copyTo(jdtPrefsFile, jdtPrefsFileCopy, (line) -> line);
// Read in the userlibrary file and escape all the required characters ...
List<String> lines = Files.readAllLines(userlibTemplate.toPath(), StandardCharsets.UTF_8);
String escapedContents = combineAndEscape(lines, strongbackHome.getAbsolutePath());
// Set the property and output the file ...
jdtPrefs.setProperty("org.eclipse.jdt.core.userLibrary.Strongback", escapedContents);
try ( OutputStream os = new FileOutputStream(jdtPrefsFile) ) {
jdtPrefs.store(os,"");
updatedMetadata = true;
}
}
}
}
eclipseProject = true;
}
} catch (IOException e) {
exit(Strings.IO_EXCEPTION + e.getLocalizedMessage(), ExitCodes.IO_EXCEPT);
@@ -184,9 +235,29 @@ public static void main(String[] args) {
} catch (IOException e) {
System.out.println(projectRoot.getPath());
}
if ( foundMetadata && updatedMetadata ) {
System.out.print(Strings.UPDATED_WORKSPACE);
System.out.println(metadataDir.getParentFile().getAbsolutePath());
System.out.print(Strings.RESTART_ECLIPSE);
} else if ( eclipseProject ) {
System.out.println(Strings.IMPORT_ECLIPSE);
if ( !foundMetadata ) {
System.out.print(Strings.IMPORT_USERLIB);
System.out.println(strongbackHome.getAbsolutePath() + "/java/eclipse/Strongback.userlibraries");
}
}
}
protected static String combineAndEscape( List<String> lines, String strongbackHome ) throws IOException {
StringBuilder sb = new StringBuilder();
lines.forEach(str->{
String replaced = str.replaceAll("STRONGBACKHOME",strongbackHome).replaceAll(" ", "\t");
sb.append(replaced).append("\n");
});
return sb.toString();
}
private static void copyTo(File input, File output, Function<String, String> each) throws IOException {
protected static void copyTo(File input, File output, Function<String, String> each) throws IOException {
BufferedReader testReader = new BufferedReader(new FileReader(input));
BufferedWriter testWriter = new BufferedWriter(new FileWriter(output));
try {
@@ -222,6 +293,10 @@ private static void exit() {
public static final String MISSING_STRONGBK = "Could not locate the strongback directory. Double check that the strongback"
+ " folder is in the same directory as your wpilib folder.";
public static final String SUCCESS = "Successfully created new project at: ";
public static final String UPDATED_WORKSPACE= "\nAdded the Strongback user library to your Eclipse workspace at ";
public static final String RESTART_ECLIPSE = "Restart this Eclipse workspace and import the project.\n";
public static final String IMPORT_ECLIPSE = "\nProject ready for importing into Eclipse workspace.";
public static final String IMPORT_USERLIB = "\nEnsure that the Strongback user library is defined in workspace; if not then import from ";
public static final String HELP = "usage: strongback newproject [options] -d <directory> -n <project_name>"
+ LS + " strongback newproject [options] -r <project_root>"
@@ -238,7 +313,8 @@ private static void exit() {
+ LS + " The directory to create the new project directory in"
+ LS + ""
+ LS + " -e"
+ LS + " Adds project metadata for the Eclipse Java IDE"
+ LS + " Adds project metadata for the Eclipse Java IDE, and looks for the Eclipse workspace"
+ LS + " .metadata folder to add the Strongback user library if not already defined."
+ LS + ""
+ LS + " -h"
+ LS + " Displays help information"
@@ -0,0 +1,64 @@
/*
* Strongback
* Copyright 2015, Strongback and individual contributors by the @authors tag.
* See the COPYRIGHT.txt in the distribution for a full listing of individual
* contributors.
*
* Licensed under the MIT License; you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.strongback.tools.newproject;
import static org.fest.assertions.Assertions.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import org.junit.Test;
/**
* @author Randall Hauch
*
*/
public class NewProjectTest {
@Test
public void shouldEscapeUserlibTemplateCorrectly() throws IOException {
boolean print = false;
File template = new File("../templates/Strongback.userlibraries.template");
if (!template.exists()) {
template = new File("templates/Strongback.userlibraries.template");
}
assertThat(template.exists()).isTrue();
List<String> lines = Files.readAllLines(template.toPath(), StandardCharsets.UTF_8);
List<String> updated = lines.stream().collect(Collectors.toList());
if (print) updated.forEach(System.out::println);
String content = NewProject.combineAndEscape(updated,"/Users/jsmith/strongback");
if (print) System.out.println(content);
assertThat(content.indexOf("STRONGBACK")).isEqualTo(-1);
// assertThat(content.indexOf(" ")).isEqualTo(-1);
Properties props = new Properties();
props.setProperty("template", content);
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
props.store(ostream, "");
String propFileContents = new String(ostream.toByteArray());
assertThat(propFileContents.contains(">\\n\\t\\t<attributes")).isTrue();
assertThat(propFileContents.contains("\"file\\:/Users/jsmith")).isTrue();
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<eclipse-userlibraries version="2">
<library name="Strongback" systemlibrary="false">
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib/strongback.jar" source="STRONGBACKHOME/java/lib/strongback-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/strongback-testing.jar" source="STRONGBACKHOME/java/lib-tests/strongback-testing-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/fest-assert-1.4.jar" source="STRONGBACKHOME/java/lib-tests/fest-assert-1.4-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/fest-util-1.1.6.jar" source="STRONGBACKHOME/java/lib-tests/fest-util-1.1.6-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/hamcrest-core-1.3.jar" source="STRONGBACKHOME/java/lib-tests/hamcrest-core-1.3-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/junit-4.11.jar" source="STRONGBACKHOME/java/lib-tests/junit-4.11-sources.jar"/>
<archive javadoc="file:STRONGBACKHOME/java/javadoc/" path="STRONGBACKHOME/java/lib-tests/metrics-core-3.1.0.jar" source="STRONGBACKHOME/java/lib-tests/metrics-core-3.1.0-sources.jar"/>
</library>
</eclipse-userlibraries>
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<userlibrary systemlibrary="false" version="2">
<archive path="STRONGBACKHOME/java/lib/strongback.jar" sourceattachment="STRONGBACKHOME/java/lib/strongback-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/strongback-testing.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/strongback-testing-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/fest-assert-1.4.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/fest-assert-1.4-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/fest-util-1.1.6.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/fest-util-1.1.6-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/hamcrest-core-1.3.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/hamcrest-core-1.3-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/junit-4.11.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/junit-4.11-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
<archive path="STRONGBACKHOME/java/lib-tests/metrics-core-3.1.0.jar" sourceattachment="STRONGBACKHOME/java/lib-tests/metrics-core-3.1.0-sources.jar">
<attributes>
<attribute name="javadoc_location" value="file:STRONGBACKHOME/java/javadoc/"/>
</attributes>
</archive>
</userlibrary>
@@ -1,13 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="testsrc"/>
<classpathentry kind="src" output="build/test-classes" path="testsrc"/>
<classpathentry kind="var" path="wpilib" sourcepath="wpilib.sources"/>
<classpathentry kind="var" path="networktables" sourcepath="networktables.sources"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Strongback"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

0 comments on commit 1bcbff1

Please sign in to comment.