diff --git a/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/NodePreferencePage.java b/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/NodePreferencePage.java index d3f83168..f27a3b2c 100644 --- a/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/NodePreferencePage.java +++ b/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/NodePreferencePage.java @@ -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; /** @@ -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; @@ -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); @@ -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) ); + } + } } - } \ No newline at end of file diff --git a/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/PreferenceInitializer.java b/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/PreferenceInitializer.java index 235a5c12..073b2957 100644 --- a/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/PreferenceInitializer.java +++ b/org.nodeclipse.ui/src/org/nodeclipse/ui/preferences/PreferenceInitializer.java @@ -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"; @@ -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()) { @@ -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"; @@ -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); @@ -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()) { @@ -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"); @@ -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 cmdLine = new ArrayList(); - 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; - } } \ No newline at end of file diff --git a/org.nodeclipse.ui/src/org/nodeclipse/ui/util/ProcessUtils.java b/org.nodeclipse.ui/src/org/nodeclipse/ui/util/ProcessUtils.java index f046391b..7ff48b5b 100644 --- a/org.nodeclipse.ui/src/org/nodeclipse/ui/util/ProcessUtils.java +++ b/org.nodeclipse.ui/src/org/nodeclipse/ui/util/ProcessUtils.java @@ -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 cmdLine = new ArrayList(); + 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() @@ -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 { @@ -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"); @@ -152,6 +164,7 @@ public static boolean npmInstall(String name) { return true; } + /* run command and return output as String*/ public static String exec(List cmdLine, File dir) throws InvocationTargetException { String[] cmds = {};