Skip to content

Commit

Permalink
#161 initialize Preferences with express-generator first; show selected
Browse files Browse the repository at this point in the history
Express version in NodePreferencesPage
  • Loading branch information
paulvi committed Sep 25, 2014
1 parent 3ccd52b commit cf35818
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.nodeclipse.ui.preferences;

import java.io.File;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.nodeclipse.ui.Activator;
import org.nodeclipse.ui.util.ProcessUtils;
import org.nodeclipse.ui.util.VersionUtil;

/**
Expand All @@ -35,6 +41,7 @@ public class NodePreferencePage extends FieldEditorPreferencePage implements IWo
private IntegerFieldEditor nodeDebugPort;
//private FileFieldEditor nodeMonitorPath;
private FileFieldEditor expressPath;
private StringFieldEditor expressVersion;
private FileFieldEditor coffeePath;
//private BooleanFieldEditor coffeeJustCoffee;
private StringFieldEditor coffeeCompileOptions;
Expand Down Expand Up @@ -126,6 +133,10 @@ protected void createFieldEditors() {

expressPath = new FileFieldEditor(PreferenceConstants.EXPRESS_PATH, "Express path:", getFieldEditorParent());
addField(expressPath);

expressVersion = new StringFieldEditor(PreferenceConstants.EXPRESS_VERSION, "Selected Express version:", getFieldEditorParent());
expressVersion.setEnabled(false, getFieldEditorParent());
addField(expressVersion);

coffeePath = new FileFieldEditor(PreferenceConstants.COFFEE_PATH, "Coffee path:", getFieldEditorParent());
addField(coffeePath);
Expand All @@ -147,10 +158,45 @@ protected void createFieldEditors() {
addField(typescriptCompilerOptions);

}

@Override
public boolean isValid() {
return super.isValid();
protected void initialize() {
super.initialize();
expressPath.setPropertyChangeListener(new MyPropertyChangeListener(this));
}

private class MyPropertyChangeListener implements IPropertyChangeListener{

private FieldEditorPreferencePage page;
private boolean isValidPath = true;

public MyPropertyChangeListener(FieldEditorPreferencePage nodePreferencePage) {
page = nodePreferencePage;
}

@Override
public void propertyChange(PropertyChangeEvent event) {
page.propertyChange(event);

// The IS_VALID property is only fired when the valid state changes.
if (event.getProperty().equals(FieldEditor.IS_VALID)) {
isValidPath = ((Boolean) event.getNewValue()).booleanValue();
if (!isValidPath) {
expressVersion.setStringValue( "Selected path is not valid" );
}
return;
}
if (!isValidPath) { // if it is still not valid file, we don't need to check again
return;
}
if (event.getProperty().equals(FieldEditor.VALUE)) {
String newExecutablePath = (String) event.getNewValue();
// File file = new File(newExecutablePath);
// if (!file.exists() && file.isFile() ){
// return;
// }
expressVersion.setStringValue( ProcessUtils.getCurrentVersionOf(newExecutablePath) );
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void initializeDefaultPreferences() {
String node_path = "/usr/local/bin/node";
String node_monitor_path = "/usr/local/lib/node_modules/node-dev/bin/node-dev";
String express_path = "/usr/local/lib/node_modules/express/bin/express";
String express_generator_path = "/usr/local/lib/node_modules/express-generator/bin/express";
String coffee_path = "/usr/local/bin/coffee";
String typescript_compiler_path = "/usr/local/lib/node_modules/typescript/bin/tsc";

Expand All @@ -61,6 +62,7 @@ public void initializeDefaultPreferences() {
+ "/AppData/Roaming/npm/node_modules/";
node_monitor_path = (windowsNodeModulesPath+"node-dev/bin/node-dev").replace('/', File.separatorChar);
express_path = (windowsNodeModulesPath+"express/bin/express").replace('/', File.separatorChar);
express_generator_path = (windowsNodeModulesPath+"express-generator/bin/express").replace('/', File.separatorChar);
coffee_path = (windowsNodeModulesPath+"coffee-script/bin/coffee").replace('/', File.separatorChar);
typescript_compiler_path = (windowsNodeModulesPath+"typescript/bin/tsc").replace('/', File.separatorChar);
} else if (OSUtils.isMacOS()) {
Expand All @@ -76,6 +78,10 @@ public void initializeDefaultPreferences() {
if (!file.exists()) {
express_path = "/opt/local/lib/node_modules/express/bin/express";
}
file = new File(express_generator_path);
if (!file.exists()) {
express_generator_path = "/opt/local/lib/node_modules/express-generator/bin/express";
}
file = new File(coffee_path);
if (!file.exists()) {
coffee_path = "/opt/local/lib/node_modules/coffee-script/bin/coffee";
Expand All @@ -101,21 +107,32 @@ public void initializeDefaultPreferences() {
if (file.exists()) {
store.setDefault(PreferenceConstants.NODE_MONITOR_PATH, node_monitor_path);
}
// using bundles Node.js modules for Express & CoffeeScript
file = new File(express_path);

// using bundles Node.js modules for Express & CoffeeScript {

// Express: try to use express-generator (for Express 4.x)
file = new File(express_generator_path);
if (file.exists()) {
store.setDefault(PreferenceConstants.EXPRESS_PATH, express_path);
store.setDefault(PreferenceConstants.EXPRESS_PATH, express_generator_path);
store.setDefault(PreferenceConstants.EXPRESS_VERSION,
getExpressVersion(express_path));
ProcessUtils.getCurrentVersionOf(express_generator_path));
} else {
express_path = ProcessUtils.getBundledExpressPath();
file = new File(express_path);
if (file.exists()) {
store.setDefault(PreferenceConstants.EXPRESS_PATH, express_path);
store.setDefault(PreferenceConstants.EXPRESS_VERSION,
getExpressVersion(express_path));
ProcessUtils.getCurrentVersionOf(express_path));
} else {
express_path = ProcessUtils.getBundledExpressPath();
file = new File(express_path);
if (file.exists()) {
store.setDefault(PreferenceConstants.EXPRESS_PATH, express_path);
store.setDefault(PreferenceConstants.EXPRESS_VERSION,
ProcessUtils.getCurrentVersionOf(express_path));
}
}
}
//coffee
file = new File(coffee_path);
if (file.exists()) {
store.setDefault(PreferenceConstants.COFFEE_PATH, coffee_path);
Expand All @@ -126,6 +143,7 @@ public void initializeDefaultPreferences() {
store.setDefault(PreferenceConstants.COFFEE_PATH, coffee_path);
}
}
//}
store.setDefault(PreferenceConstants.COFFEE_COMPILE_OPTIONS, "--watch");
file = new File(typescript_compiler_path);
if (file.exists()) {
Expand All @@ -135,6 +153,13 @@ public void initializeDefaultPreferences() {
store.setDefault(PreferenceConstants.MONGODB_SHELL_OPTIONS, "--shell");
}

private static String getNodeFileName() {
if (OSUtils.isWindows()) {
return "node.exe";
}
return "node";
}

private static File findNode() {
String nodeFileName = getNodeFileName();
String path = System.getenv("PATH");
Expand Down Expand Up @@ -163,28 +188,4 @@ private static File findNode() {
LogUtil.error("Node.js executable can't be found!");
return null;
}

private static String getNodeFileName() {
if (OSUtils.isWindows()) {
return "node.exe";
}

return "node";
}


private String getExpressVersion(String express) {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add(ProcessUtils.getNodePath());
cmdLine.add(ProcessUtils.getExpressPath());
cmdLine.add("--version");
String ret = Constants.BLANK_STRING;
try {
ret = ProcessUtils.exec(cmdLine, null);
} catch (InvocationTargetException e) {
//e.printStackTrace();
NodeclipseConsole.write(e.getLocalizedMessage()+"\n");
}
return ret;
}
}
41 changes: 27 additions & 14 deletions org.nodeclipse.ui/src/org/nodeclipse/ui/util/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ public static String getExpressVersion() {
return Activator.getDefault().getPreferenceStore()
.getString(PreferenceConstants.EXPRESS_VERSION);
}
/* actually getting from PreferenceConstants.EXPRESS_VERSION */
public static int getExpressMajorVersion() {
String ver = getExpressVersion();
int idx = ver.indexOf('.');
if(idx < 0) {
return 3;
}
ver = ver.substring(0, idx);
int ret = Integer.parseInt(ver);
return ret;
}

public static String getCurrentVersionOf(String nodeAppPath) {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add(getNodePath());
cmdLine.add(nodeAppPath);
cmdLine.add("--version");
String ret = Constants.BLANK_STRING;
try {
ret = exec(cmdLine, null);
} catch (InvocationTargetException e) {
NodeclipseConsole.write(e.getLocalizedMessage()+"\n");
}
return ret;
}

public static String getCompletionsJsonPath() {
return Activator.getDefault().getPreferenceStore()
Expand Down Expand Up @@ -81,16 +106,6 @@ public static String getSourcesAllJsonPath() {
return path;
}

public static int getExpressMajorVersion() {
String ver = getExpressVersion();
int idx = ver.indexOf('.');
if(idx < 0) {
return 3;
}
ver = ver.substring(0, idx);
int ret = Integer.parseInt(ver);
return ret;
}

public static String getBundledExpressPath() {
try {
Expand All @@ -103,10 +118,7 @@ public static String getBundledExpressPath() {
return "";
// return getBundledPath("node_modules/express/bin/express");
}





public static String getBundledCoffeePath() {
try {
Class clazz = Class.forName("org.nodeclipse.bundle.coffee.BundlePath");
Expand Down Expand Up @@ -152,6 +164,7 @@ public static boolean npmInstall(String name) {
return true;
}

/* run command and return output as String*/
public static String exec(List<String> cmdLine, File dir)
throws InvocationTargetException {
String[] cmds = {};
Expand Down

0 comments on commit cf35818

Please sign in to comment.