|
1 | | -package org.nodeclipse.ui.npm; |
2 | | - |
3 | | -import java.io.File; |
4 | | -import java.util.ArrayList; |
5 | | -import java.util.List; |
6 | | - |
7 | | -import org.eclipse.core.resources.ResourcesPlugin; |
8 | | -import org.eclipse.core.runtime.CoreException; |
9 | | -import org.eclipse.core.runtime.IProgressMonitor; |
10 | | -import org.eclipse.debug.core.DebugPlugin; |
11 | | -import org.eclipse.debug.core.ILaunch; |
12 | | -import org.eclipse.debug.core.ILaunchConfiguration; |
13 | | -import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; |
14 | | -import org.nodeclipse.ui.Activator; |
15 | | -import org.nodeclipse.ui.preferences.PreferenceConstants; |
16 | | -import org.nodeclipse.ui.util.Constants; |
17 | | -import org.nodeclipse.ui.util.OSUtils; |
18 | | - |
19 | | -public class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate { |
20 | | - |
21 | | - /* |
22 | | - * (non-Javadoc) |
23 | | - * |
24 | | - * @see |
25 | | - * org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org. |
26 | | - * eclipse.debug.core.ILaunchConfiguration, java.lang.String, |
27 | | - * org.eclipse.debug.core.ILaunch, |
28 | | - * org.eclipse.core.runtime.IProgressMonitor) |
29 | | - */ |
30 | | - @Override |
31 | | - public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { |
32 | | - // Using configuration to build command line |
33 | | - List<String> cmdLine = new ArrayList<String>(); |
34 | | - String nodePath = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.NODE_PATH); |
35 | | - cmdLine.add(findNpm(nodePath)); |
36 | | - |
37 | | - String goal = configuration.getAttribute(Constants.KEY_GOAL, Constants.BLANK_STRING); |
38 | | - cmdLine.add(goal); |
39 | | - String file = configuration.getAttribute(Constants.KEY_FILE_PATH, Constants.BLANK_STRING); |
40 | | - String filePath = ResourcesPlugin.getWorkspace().getRoot().findMember(file).getLocation().toOSString(); |
41 | | - |
42 | | - String[] cmds = {}; |
43 | | - cmds = cmdLine.toArray(cmds); |
44 | | - Process p = null; |
45 | | - if(OSUtils.isMacOS()) { |
46 | | - String[] env = {"PATH=" + nodePath.substring(0, nodePath.lastIndexOf(File.separator))}; |
47 | | - p = DebugPlugin.exec(cmds, (new File(filePath)).getParentFile(), env); |
48 | | - } else { |
49 | | - p = DebugPlugin.exec(cmds, (new File(filePath)).getParentFile()); |
50 | | - } |
51 | | - DebugPlugin.newProcess(launch, p, Constants.NPM_PROCESS_MESSAGE + goal); |
52 | | - } |
53 | | - |
54 | | - private static String findNpm(String nodePath) { |
55 | | - // Application path should be stored in preference. |
56 | | - String npmFileName = getNpmFileName(); |
57 | | - String npmPath = nodePath.substring(0, nodePath.lastIndexOf(File.separator) + 1) + npmFileName; |
58 | | - File npmFile = new File(npmPath); |
59 | | - if(npmFile.exists()) { |
60 | | - return npmFile.getAbsolutePath(); |
61 | | - } |
62 | | - |
63 | | - String path = System.getenv("PATH"); |
64 | | - String[] paths = path.split("" + File.pathSeparatorChar, 0); |
65 | | - List<String> directories = new ArrayList<String>(); |
66 | | - for(String p : paths) { |
67 | | - directories.add(p); |
68 | | - } |
69 | | - |
70 | | - // ensure /usr/local/bin is included for OS X |
71 | | - if (OSUtils.isMacOS()) { |
72 | | - directories.add("/usr/local/bin"); |
73 | | - } |
74 | | - |
75 | | - // search for Node.js in the PATH directories |
76 | | - for (String directory : directories) { |
77 | | - npmFile = new File(directory, npmFileName); |
78 | | - |
79 | | - if (npmFile.exists()) { |
80 | | - return npmFile.getAbsolutePath(); |
81 | | - } |
82 | | - } |
83 | | - |
84 | | - throw new IllegalStateException("Could not find npm."); |
85 | | - } |
86 | | - |
87 | | - private static String getNpmFileName() { |
88 | | - if (OSUtils.isWindows()) { |
89 | | - return "npm.cmd"; |
90 | | - } |
91 | | - |
92 | | - return "npm"; |
93 | | - } |
94 | | -} |
| 1 | +package org.nodeclipse.ui.npm; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 8 | +import org.eclipse.core.runtime.CoreException; |
| 9 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 10 | +import org.eclipse.debug.core.DebugPlugin; |
| 11 | +import org.eclipse.debug.core.ILaunch; |
| 12 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 13 | +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; |
| 14 | +import org.nodeclipse.ui.Activator; |
| 15 | +import org.nodeclipse.ui.preferences.PreferenceConstants; |
| 16 | +import org.nodeclipse.ui.util.Constants; |
| 17 | +import org.nodeclipse.ui.util.OSUtils; |
| 18 | + |
| 19 | +public class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate { |
| 20 | + |
| 21 | + /* |
| 22 | + * (non-Javadoc) |
| 23 | + * |
| 24 | + * @see |
| 25 | + * org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org. |
| 26 | + * eclipse.debug.core.ILaunchConfiguration, java.lang.String, |
| 27 | + * org.eclipse.debug.core.ILaunch, |
| 28 | + * org.eclipse.core.runtime.IProgressMonitor) |
| 29 | + */ |
| 30 | + @Override |
| 31 | + public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { |
| 32 | + // Using configuration to build command line |
| 33 | + List<String> cmdLine = new ArrayList<String>(); |
| 34 | + String nodePath = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.NODE_PATH); |
| 35 | + cmdLine.add(findNpm(nodePath)); |
| 36 | + |
| 37 | + String goal = configuration.getAttribute(Constants.KEY_GOAL, Constants.BLANK_STRING); |
| 38 | + cmdLine.add(goal); |
| 39 | + String file = configuration.getAttribute(Constants.KEY_FILE_PATH, Constants.BLANK_STRING); |
| 40 | + String filePath = ResourcesPlugin.getWorkspace().getRoot().findMember(file).getLocation().toOSString(); |
| 41 | + |
| 42 | + String[] cmds = {}; |
| 43 | + cmds = cmdLine.toArray(cmds); |
| 44 | + Process p = null; |
| 45 | + if(OSUtils.isMacOS()) { |
| 46 | + String[] env = {"PATH=" + nodePath.substring(0, nodePath.lastIndexOf(File.separator))}; |
| 47 | + p = DebugPlugin.exec(cmds, (new File(filePath)).getParentFile(), env); |
| 48 | + } else { |
| 49 | + p = DebugPlugin.exec(cmds, (new File(filePath)).getParentFile()); |
| 50 | + } |
| 51 | + DebugPlugin.newProcess(launch, p, Constants.NPM_PROCESS_MESSAGE + goal); |
| 52 | + } |
| 53 | + |
| 54 | + private static String findNpm(String nodePath) { |
| 55 | + //TODO npm application path should be stored in preference. |
| 56 | + String npmFileName = getNpmFileName(); |
| 57 | + String npmPath = nodePath.substring(0, nodePath.lastIndexOf(File.separator) + 1) + npmFileName; |
| 58 | + File npmFile = new File(npmPath); |
| 59 | + if(npmFile.exists()) { |
| 60 | + return npmFile.getAbsolutePath(); |
| 61 | + } |
| 62 | + |
| 63 | + String path = System.getenv("PATH"); |
| 64 | + String[] paths = path.split("" + File.pathSeparatorChar, 0); |
| 65 | + List<String> directories = new ArrayList<String>(); |
| 66 | + for(String p : paths) { |
| 67 | + directories.add(p); |
| 68 | + } |
| 69 | + |
| 70 | + // ensure /usr/local/bin is included for OS X |
| 71 | + if (OSUtils.isMacOS()) { |
| 72 | + directories.add("/usr/local/bin"); |
| 73 | + } |
| 74 | + |
| 75 | + // search for Node.js in the PATH directories |
| 76 | + for (String directory : directories) { |
| 77 | + npmFile = new File(directory, npmFileName); |
| 78 | + |
| 79 | + if (npmFile.exists()) { |
| 80 | + return npmFile.getAbsolutePath(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + throw new IllegalStateException("Could not find npm."); |
| 85 | + } |
| 86 | + |
| 87 | + private static String getNpmFileName() { |
| 88 | + if (OSUtils.isWindows()) { |
| 89 | + return "npm.cmd"; |
| 90 | + } |
| 91 | + |
| 92 | + return "npm"; |
| 93 | + } |
| 94 | +} |
0 commit comments