Skip to content

Commit b0226b9

Browse files
committed
Merge pull request #55 from rhauch/fix-windows-tools
Corrected the Windows problems when initializing Eclipse and creating new Eclipse projects (Issue 54)
2 parents 85ce9ac + a1e1a8f commit b0226b9

File tree

5 files changed

+235
-64
lines changed

5 files changed

+235
-64
lines changed

build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
strongback.version=1.1.0
1+
strongback.version=1.1.1
22
#
33
# The build will download a specific version of the WPILib given by the following URL
44
# and install it into the 'libs/wpilib' folder. To use a different version of WPILib,

strongback-tools/src/org/strongback/tools/newproject/NewProject.java

Lines changed: 134 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,120 @@
5555
*/
5656
public class NewProject {
5757

58-
public static void main(String[] args) {
59-
// Load strongback properties
60-
Properties strongback = null;
58+
private static boolean debug;
6159

62-
try {
63-
Properties stb = PropertiesUtils.load(FileUtils.resolvePath("~/strongback/strongback.properties"));
64-
PropertiesUtils.antify(stb);
65-
Properties wpi = PropertiesUtils.load(new File(stb.getProperty("wpilib.props")));
66-
PropertiesUtils.antify(wpi);
67-
strongback = PropertiesUtils.concat(stb, wpi);
68-
} catch (IOException e) {
69-
exit(Strings.MISSING_STRONGBK, ExitCodes.MISSING_FILE);
70-
} catch (InvalidPropertiesException e) {
71-
exit(Strings.BAD_PROPS + e.getLocalizedMessage(), ExitCodes.BAD_PROPS);
72-
}
73-
assert strongback != null;
60+
private static void debug( Object msg ) {
61+
if ( debug ) System.out.println("DEBUG: " + msg);
62+
}
7463

64+
public static void main(String[] args) {
65+
// Parse the parameters ...
7566
Map<String, String> params = null;
7667
try {
77-
params = Parser.parse(args, "npr", "v|h|nd!r|r!n!d");
68+
params = Parser.parse(args, "npr", "D|v|h|i|nd!r|r!n!d");
7869
} catch(InvalidParameterException e) {
7970
System.err.println(e.getLocalizedMessage());
8071
System.out.println(Strings.HELP);
8172
exit("", ExitCodes.BAD_ARGS);
8273
}
8374
assert params != null;
8475

76+
debug = params.containsKey("D");
8577
if(params.containsKey("h")) {
8678
System.out.println(Strings.HELP);
8779
exit();
8880
}
89-
9081
if(params.containsKey("v")) {
9182
System.out.println(Strings.VERSION_HEAD);
9283
System.out.println(Strings.VERSION);
9384
exit();
9485
}
9586

87+
// Resolve the Strongback installation directory ...
88+
debug("Resolving '~/strongback'");
89+
File strongbackDirectory = FileUtils.resolvePath("~/strongback");
90+
if ( strongbackDirectory == null || !strongbackDirectory.exists()) {
91+
exit("Unable to find the 'strongback' installation directory. Check the Strongback installation.", ExitCodes.BAD_ENV);
92+
}
93+
strongbackDirectory = strongbackDirectory.getAbsoluteFile();
94+
if ( !strongbackDirectory.isDirectory() ) {
95+
exit("Expecting '" + strongbackDirectory + "' to be a directory. Check the Strongback installation.",ExitCodes.BAD_ENV);
96+
}
97+
if ( !strongbackDirectory.canRead() ) {
98+
exit("Unable to read the 'strongback' installation directory at " + strongbackDirectory,ExitCodes.BAD_ENV);
99+
}
100+
// replace the windows backslashes with forward slashes so i) string.replaceAll works, and ii) Eclipse paths are correct
101+
final String strongbackPath = strongbackDirectory.getAbsolutePath().replaceAll("\\\\", "/");
102+
debug("Resolved '~/strongback' to '" + strongbackPath + "'");
103+
104+
// Load the strongback properties ...
105+
debug("Checking '~/strongback/strongback.properties' file");
106+
File strongbackPropsFile = new File(strongbackDirectory,"/strongback.properties");
107+
if ( !strongbackPropsFile.exists() ) {
108+
exit("Unable to find the 'strongback.properties' file in the installation directory. Check the Strongback installation.",ExitCodes.BAD_ENV);
109+
}
110+
strongbackPropsFile = strongbackPropsFile.getAbsoluteFile();
111+
if ( !strongbackPropsFile.isFile() ) {
112+
exit("Expecting '" + strongbackPropsFile + "' to be a file but was a directory. Check the Strongback installation.",ExitCodes.BAD_ENV);
113+
}
114+
if ( !strongbackPropsFile.canRead() ) {
115+
exit("Unable to read the '" + strongbackPropsFile + "' file. Check the Strongback installation.",ExitCodes.BAD_ENV);
116+
}
117+
Properties strongbackProperties = null;
118+
try {
119+
debug("Loading '" + strongbackPropsFile + "' file");
120+
strongbackProperties = PropertiesUtils.load(strongbackPropsFile);
121+
PropertiesUtils.antify(strongbackProperties);
122+
debug("Loaded '" + strongbackPropsFile.getAbsoluteFile() + "' file");
123+
} catch (IOException e) {
124+
exit("Unable to load the '" + strongbackPropsFile + "' file: " + e.getMessage(),ExitCodes.BAD_ENV);
125+
} catch (InvalidPropertiesException e) {
126+
exit("Invalid property field in '" + strongbackPropsFile + "' file: " + e.getMessage(),ExitCodes.BAD_ENV);
127+
}
128+
129+
// Resolve the WPILib installation directory ...
130+
String wpiPath = strongbackProperties.getProperty("wpilib.home");
131+
debug("Checking for the WPILib installation directory " + wpiPath);
132+
if ( wpiPath == null ) {
133+
exit("Strongback properties file '" + strongbackPropsFile + "' must specify the WPILIb directory in 'wpilib.home'",ExitCodes.BAD_ENV);
134+
}
135+
File wpiLibDir = new File(wpiPath).getAbsoluteFile();
136+
if ( !wpiLibDir.exists() ) {
137+
exit("Unable to find the '" + wpiLibDir.getName() + "' installation directory. Make sure the 'wpilib.home' property in '" + strongbackPropsFile + "' points to a valid version of WPILib installation.",ExitCodes.BAD_ENV);
138+
}
139+
if ( !wpiLibDir.isDirectory() ) {
140+
exit("Expecting '" + wpiLibDir + "' to be a directory but was a file. Make sure the 'wpilib.home' property in '" + strongbackPropsFile + "' points to a valid version of WPILib installation.",ExitCodes.BAD_ENV);
141+
}
142+
if ( !wpiLibDir.canRead() ) {
143+
exit("Unable to read the '" + wpiLibDir + "' file. Check the WPILib version and file permissions.",ExitCodes.BAD_ENV);
144+
}
145+
debug("Found valid WPILib installation directory: " + wpiLibDir);
146+
147+
// Load the WPILib properties (which may not exist anymore) ...
148+
debug("Looking for WPILib properties file");
149+
String wpiLibPropsPath = strongbackProperties.getProperty("wpilib.props", new File(wpiLibDir,"wpilib.properties").getAbsolutePath());
150+
debug("Checking '" + wpiLibPropsPath + "' file");
151+
File wpiLibPropsFile = new File(wpiLibPropsPath);
152+
Properties wpi = new Properties();
153+
if ( wpiLibPropsFile.exists() && wpiLibPropsFile.isFile() && wpiLibPropsFile.canRead() ) {
154+
wpiLibPropsFile = wpiLibPropsFile.getAbsoluteFile();
155+
try {
156+
debug("Loading '" + wpiLibPropsFile + "' file");
157+
wpi = PropertiesUtils.load(wpiLibPropsFile);
158+
PropertiesUtils.antify(wpi);
159+
debug("Loaded '" + wpiLibPropsFile.getAbsoluteFile() + "' file");
160+
} catch (IOException e) {
161+
exit("Unable to load the '" + wpiLibPropsFile + "' file: " + e.getMessage(),ExitCodes.BAD_PROPS);
162+
} catch (InvalidPropertiesException e) {
163+
exit("Invalid property field in '" + wpiLibPropsFile + "' file: " + e.getMessage(),ExitCodes.BAD_PROPS);
164+
}
165+
} else {
166+
debug("WPILib installation does not contain a properties file, so skipping this step");
167+
}
168+
169+
final Properties strongback = PropertiesUtils.concat(strongbackProperties, wpi);
170+
debug("The Strongback properties are: " + strongback);
171+
96172
File projectRoot;
97173
String projectName;
98174
String mainPackage;
@@ -104,15 +180,17 @@ public static void main(String[] args) {
104180
projectName = params.get("n");
105181
projectRoot = FileUtils.resolvePath(params.get("d") + File.separator + projectName);
106182
}
183+
debug("The project root will be: " + projectRoot);
184+
debug("The project name will be: " + projectName);
107185

108186
if(params.containsKey("p")) {
109187
mainPackage = params.get("p");
110188
} else {
111189
mainPackage = "org.usfirst.frc.team"+ strongback.getProperty("team-number") +".robot";
112190
}
191+
debug("The main package for the robot will be '" + mainPackage + "'");
113192

114193
/* Application Begins */
115-
File strongbackHome = FileUtils.resolvePath("~/strongback");
116194

117195
// Source folders
118196
File src = new File(projectRoot, "src" + File.separator + mainPackage.replace('.', File.separatorChar));
@@ -130,6 +208,12 @@ public static void main(String[] args) {
130208
File userlibTemplate = new File(strongback.getProperty("strongback.templates.dir"), "Strongback.userlibraries.template");
131209
File userlibImportTemplate = new File(strongback.getProperty("strongback.templates.dir"), "Strongback.userlibraries.import.template");
132210

211+
// Verify templates exist
212+
if(!buildTemplate.exists()) exit(Strings.MISSING_TEMPLATE + buildTemplate.getPath(), ExitCodes.MISSING_FILE);
213+
if(!propsTemplate.exists()) exit(Strings.MISSING_TEMPLATE + propsTemplate.getPath(), ExitCodes.MISSING_FILE);
214+
if(!robotTemplate.exists()) exit(Strings.MISSING_TEMPLATE + robotTemplate.getPath(), ExitCodes.MISSING_FILE);
215+
if(!testTemplate.exists()) exit(Strings.MISSING_TEMPLATE + testTemplate.getPath(), ExitCodes.MISSING_FILE);
216+
133217
// Destination files
134218
File buildProps = new File(projectRoot, "build.properties");
135219
File buildXML = new File(projectRoot, "build.xml");
@@ -139,10 +223,29 @@ public static void main(String[] args) {
139223
// Eclipse specific
140224
File project = new File(projectRoot, ".project");
141225
File classpath = new File(projectRoot, ".classpath");
142-
File eclipseDir = FileUtils.resolvePath("~/strongback/java/eclipse");
143-
File userlibraries = new File(eclipseDir, "Strongback.userlibraries");
144226
File metadataDir = new File(projectRoot.getParentFile(), ".metadata");
145227

228+
// Be sure to always generate the Eclipse files that are part of the installation ...
229+
try {
230+
File eclipseDir = FileUtils.resolvePath("~/strongback/java/eclipse");
231+
if ( !eclipseDir.exists()) {
232+
eclipseDir.mkdirs();
233+
debug("Created the '" + eclipseDir + "' directory to hold generated files.");
234+
// user libraries importable file ...
235+
File userlibraries = new File(eclipseDir, "Strongback.userlibraries");
236+
if(!userlibraries.exists()) { // don't overwrite
237+
copyTo(userlibImportTemplate, userlibraries, (line) -> line.replaceAll("STRONGBACKHOME", strongbackPath));
238+
}
239+
debug("Created the '" + userlibraries + "' file for manually importing the Strongback user libraries.");
240+
}
241+
} catch (IOException e) {
242+
exit(Strings.IO_EXCEPTION + e.getLocalizedMessage(), ExitCodes.IO_EXCEPT);
243+
}
244+
245+
// --------------------------
246+
// PROJECT-SPECIFIC FILES ...
247+
// --------------------------
248+
146249
// If any of the files to write to already exist, give up and write message about the overwrite flag
147250
if(!params.containsKey("o")) {
148251
if(buildProps.exists()) exit(Strings.OVERWRITE_WARN + buildProps.getPath(), ExitCodes.OVERWRITE);
@@ -157,12 +260,6 @@ public static void main(String[] args) {
157260
}
158261
}
159262

160-
// Verify templates exist
161-
if(!buildTemplate.exists()) exit(Strings.MISSING_TEMPLATE + buildTemplate.getPath(), ExitCodes.MISSING_FILE);
162-
if(!propsTemplate.exists()) exit(Strings.MISSING_TEMPLATE + propsTemplate.getPath(), ExitCodes.MISSING_FILE);
163-
if(!robotTemplate.exists()) exit(Strings.MISSING_TEMPLATE + robotTemplate.getPath(), ExitCodes.MISSING_FILE);
164-
if(!testTemplate.exists()) exit(Strings.MISSING_TEMPLATE + testTemplate.getPath(), ExitCodes.MISSING_FILE);
165-
166263
// Eclipse specific
167264
if(params.containsKey("e")) {
168265
if(!projectTemplate.exists()) exit(Strings.MISSING_TEMPLATE + projectTemplate.getPath(), ExitCodes.MISSING_FILE);
@@ -189,12 +286,6 @@ public static void main(String[] args) {
189286
copyTo(projectTemplate, project, (line) -> line.replace("PROJECT_NAME", projectName));
190287
copyTo(classpathTemplate, classpath, (line) -> line);
191288

192-
// Ensure the file for importing Eclipse userlibraries is generated ...
193-
eclipseDir.mkdirs();
194-
if(!userlibraries.exists()) {
195-
copyTo(userlibImportTemplate, userlibraries, (line) -> line.replaceAll("STRONGBACKHOME", strongbackHome.getAbsolutePath()));
196-
}
197-
198289
// See if the `Strongback` user library is in the workspace ...
199290
if (metadataDir.exists()) {
200291
foundMetadata = true;
@@ -205,18 +296,22 @@ public static void main(String[] args) {
205296
jdtPrefs.load(is);
206297
}
207298
if (!jdtPrefs.isEmpty() && !jdtPrefs.containsKey("org.eclipse.jdt.core.userLibrary.Strongback")) {
299+
debug("Adding the Strongback user library to the Eclipse workspace at " + metadataDir.getParent());
208300
// Make a backup of the original preferences file ...
209301
File jdtPrefsFileCopy = new File(jdtPrefsFile.getParentFile(),"org.eclipse.jdt.core.prefs.backup");
210302
copyTo(jdtPrefsFile, jdtPrefsFileCopy, (line) -> line);
303+
debug("Created backup of " + jdtPrefsFile);
211304

212305
// Read in the userlibrary file and escape all the required characters ...
213306
List<String> lines = Files.readAllLines(userlibTemplate.toPath(), StandardCharsets.UTF_8);
214-
String escapedContents = combineAndEscape(lines, strongbackHome.getAbsolutePath());
307+
String escapedContents = combineAndEscape(lines, strongbackPath);
308+
debug("Escaped contents of the preference file:" + System.lineSeparator() + escapedContents);
215309

216310
// Set the property and output the file ...
217311
jdtPrefs.setProperty("org.eclipse.jdt.core.userLibrary.Strongback", escapedContents);
218312
try ( OutputStream os = new FileOutputStream(jdtPrefsFile) ) {
219313
jdtPrefs.store(os,"");
314+
debug("Updated preference file");
220315
updatedMetadata = true;
221316
}
222317
}
@@ -243,26 +338,29 @@ public static void main(String[] args) {
243338
System.out.println(Strings.IMPORT_ECLIPSE);
244339
if ( !foundMetadata ) {
245340
System.out.print(Strings.IMPORT_USERLIB);
246-
System.out.println(strongbackHome.getAbsolutePath() + "/java/eclipse/Strongback.userlibraries");
341+
System.out.println(strongbackDirectory.getAbsolutePath() + "/java/eclipse/Strongback.userlibraries");
247342
}
248343
}
249344
}
250345

251346
protected static String combineAndEscape( List<String> lines, String strongbackHome ) throws IOException {
252347
StringBuilder sb = new StringBuilder();
253348
lines.forEach(str->{
349+
debug("Pre-escaped line: " + str);
254350
String replaced = str.replaceAll("STRONGBACKHOME",strongbackHome).replaceAll(" ", "\t");
255351
sb.append(replaced).append("\n");
352+
debug("Escaped line: " + replaced);
256353
});
257354
return sb.toString();
258355
}
259356

260357
protected static void copyTo(File input, File output, Function<String, String> each) throws IOException {
261358
BufferedReader testReader = new BufferedReader(new FileReader(input));
262359
BufferedWriter testWriter = new BufferedWriter(new FileWriter(output));
360+
String now = new Date().toString();
263361
try {
264362
while(testReader.ready()) {
265-
testWriter.write(each.apply(testReader.readLine().replace("DATE", new Date().toString())));
363+
testWriter.write(each.apply(testReader.readLine().replace("DATE", now)));
266364
testWriter.newLine();
267365
}
268366
} finally {
@@ -283,15 +381,12 @@ private static void exit() {
283381
private static final class Strings {
284382
public static final String LS = System.lineSeparator();
285383
/* Error Text */
286-
public static final String BAD_PROPS = "Error reading strongback.properties: ";
287384
public static final String FAILED_MKDIR = "Failed to create project directory at: ";
288385
public static final String OVERWRITE_WARN = "The file already exists, aborting job. To overwrite exisiting files run this"
289386
+ " application with the -o option. ";
290387
public static final String MISSING_TEMPLATE = "Cannot locate template file. Double check that the strongback folder is in"
291388
+ " the same directory as your wpilib folder. ";
292389
public static final String IO_EXCEPTION = "An IO Exception occured. ";
293-
public static final String MISSING_STRONGBK = "Could not locate the strongback directory. Double check that the strongback"
294-
+ " folder is in the same directory as your wpilib folder.";
295390
public static final String SUCCESS = "Successfully created new project at: ";
296391
public static final String UPDATED_WORKSPACE= "\nAdded the Strongback user library to your Eclipse workspace at ";
297392
public static final String RESTART_ECLIPSE = "Restart this Eclipse workspace and import the project.\n";
@@ -346,6 +441,6 @@ private static final class ExitCodes {
346441
public static final int FAILED_MKDIR = 4;
347442
public static final int OVERWRITE = 5;
348443
public static final int MISSING_FILE = 6;
349-
444+
public static final int BAD_ENV = 7;
350445
}
351446
}

0 commit comments

Comments
 (0)