diff options
author | Ilya Buziuk | 2016-01-22 15:53:58 +0000 |
---|---|---|
committer | Ilya Buziuk | 2016-01-26 09:54:25 +0000 |
commit | 98322722e435149d88e92f077a9c28ecce9ecc1b (patch) | |
tree | 5f0c3ab2af6e9ea90db26c6bde53718d6bc44c5a | |
parent | 2da78dd7e58acf5525228bc52cee52688756ff7f (diff) | |
download | webtools.jsdt-98322722e435149d88e92f077a9c28ecce9ecc1b.tar.gz webtools.jsdt-98322722e435149d88e92f077a9c28ecce9ecc1b.tar.xz webtools.jsdt-98322722e435149d88e92f077a9c28ecce9ecc1b.zip |
Bug 486340 - Adding bower / npm Launch Configuration Types in order to provide Progress Monitor for CLI invocation
Change-Id: I6e7520d1430f27227b6c393df3c43d56c3692ca5
17 files changed, 278 insertions, 172 deletions
diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/plugin.xml b/nodejs/org.eclipse.wst.jsdt.js.bower/plugin.xml index c01ac2fc6..14b923f18 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/plugin.xml +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/plugin.xml @@ -70,18 +70,11 @@ </wizard> </extension> <extension - point="org.eclipse.ui.preferencePages"> - <page - category="org.eclipse.wst.jsdt.js.JavaScriptTools" - class="org.eclipse.wst.jsdt.js.bower.internal.preference.BowerPreferencePage" - id="org.eclipse.wst.jsdt.js.bower.preferences.BowerPreferencesPage" - name="Bower"> - </page> - </extension> - <extension - point="org.eclipse.core.runtime.preferences"> - <initializer - class="org.eclipse.wst.jsdt.js.bower.internal.preference.BowerPreferenceInitializer"> - </initializer> - </extension> + point="org.eclipse.debug.core.launchConfigurationTypes"> + <launchConfigurationType + id="org.eclipse.wst.jsdt.js.bower.bowerLaunchConfigurationType" + delegate="org.eclipse.wst.jsdt.js.bower.internal.launch.BowerLaunchConfigurationDelegate" + modes="run"> + </launchConfigurationType> + </extension> </plugin> diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/BowerPlugin.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/BowerPlugin.java index e58aee2d8..84014b4d1 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/BowerPlugin.java +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/BowerPlugin.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,7 +22,7 @@ import org.osgi.framework.BundleContext; public class BowerPlugin extends AbstractUIPlugin { // The plug-in ID - public static final String PLUGIN_ID = "org.jboss.tools.jst.js.bower"; //$NON-NLS-1$ + public static final String PLUGIN_ID = "org.eclipse.wst.jsdt.js.bower"; //$NON-NLS-1$ // The shared instance private static BowerPlugin plugin; diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/BowerConstants.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/BowerConstants.java index bd1e53dc2..d7263ecde 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/BowerConstants.java +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/BowerConstants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.wst.jsdt.js.bower.internal; +import org.eclipse.wst.jsdt.js.bower.BowerPlugin; + /** * @author "Ilya Buziuk (ibuziuk)" */ @@ -18,12 +20,18 @@ public final class BowerConstants { private BowerConstants() { } + // Launch constants + public static final String LAUNCH_CONFIGURATION_ID = BowerPlugin.PLUGIN_ID + ".bowerLaunchConfigurationType"; //$NON-NLS-1$ + public static final String LAUNCH_PROJECT = BowerPlugin.PLUGIN_ID + ".PROJECT"; //$NON-NLS-1$ + public static final String LAUNCH_DIR = BowerPlugin.PLUGIN_ID + ".DIR"; //$NON-NLS-1$ + public static final String LAUNCH_COMMAND = BowerPlugin.PLUGIN_ID + ".COMMAND"; //$NON-NLS-1$ + public static final String BOWER = "bower"; //$NON-NLS-1$ - public static final String BOWERRC = ".bowerrc"; //$NON-NLS-1$ + public static final String BOWERRC = ".bowerrc"; //$NON-NLS-1$ public static final String BOWER_COMPONENTS = "bower_components"; //$NON-NLS-1$ public static final String BOWER_JSON = "bower.json"; //$NON-NLS-1$ public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$ - + // Default bower.json values public static final String DEFAULT_NAME = "Bower"; //$NON-NLS-1$ public static final String DEFAULT_VERSION = "0.0.0"; //$NON-NLS-1$ diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/BowerLaunchConfigurationDelegate.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/BowerLaunchConfigurationDelegate.java new file mode 100644 index 000000000..64499660e --- /dev/null +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/BowerLaunchConfigurationDelegate.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat Inc. - initial API and implementation and/or initial documentation + *******************************************************************************/ +package org.eclipse.wst.jsdt.js.bower.internal.launch; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.wst.jsdt.js.bower.BowerPlugin; +import org.eclipse.wst.jsdt.js.bower.internal.BowerConstants; +import org.eclipse.wst.jsdt.js.bower.internal.Messages; +import org.eclipse.wst.jsdt.js.cli.core.CLI; +import org.eclipse.wst.jsdt.js.cli.core.CLICommand; +import org.eclipse.wst.jsdt.js.common.util.WorkbenchResourceUtil; + +/** + * @author "Ilya Buziuk (ibuziuk)" + */ +public class BowerLaunchConfigurationDelegate implements ILaunchConfigurationDelegate { + + @Override + public void launch(ILaunchConfiguration configuration, String arg1, ILaunch arg2, IProgressMonitor monitor) throws CoreException { + String projectName = configuration.getAttribute(BowerConstants.LAUNCH_PROJECT, (String) null); + String dirPath = configuration.getAttribute(BowerConstants.LAUNCH_DIR, (String) null); + String commandName = configuration.getAttribute(BowerConstants.LAUNCH_COMMAND, (String) null); + + IProject project = WorkbenchResourceUtil.getProject(projectName); + if (project != null && project.exists()) { + IPath dir = (dirPath == null) ? project.getLocation() : new Path(dirPath); + CLICommand command = generateCommand(commandName); + launchBower(project, dir, command, monitor); + } + } + + private void launchBower(IProject project, IPath dir, CLICommand command, IProgressMonitor monitor) { + try { + new CLI(project, dir).execute(command, monitor); + } catch (CoreException e) { + BowerPlugin.logError(e); + ErrorDialog.openError(Display.getDefault().getActiveShell(), Messages.BowerLaunchError_Title, + Messages.BowerLaunchError_Message, e.getStatus()); + } + } + + private CLICommand generateCommand(String commandName) { + return new CLICommand(BowerConstants.BOWER, commandName, null, null); + } + +} diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerInstall.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerInstall.java index f28bd49dd..529c821a0 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerInstall.java +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerInstall.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,30 +11,15 @@ package org.eclipse.wst.jsdt.js.bower.internal.launch.shortcut; import org.eclipse.wst.jsdt.js.bower.BowerCommands; -import org.eclipse.wst.jsdt.js.bower.internal.BowerConstants; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; /** * @author "Ilya Buziuk (ibuziuk)" */ public class BowerInstall extends GenericBowerLaunch { - private static final String LAUNCH_NAME = "Bower Install"; //$NON-NLS-1$ - private static final CLICommand COMMAND = new CLICommand(BowerConstants.BOWER, BowerCommands.INSTALL.getValue(), - null, null); - - @Override - protected String getLaunchName() { - return LAUNCH_NAME; - } @Override protected String getCommandName() { return BowerCommands.INSTALL.getValue(); } - - @Override - protected CLICommand getCLICommand() { - return COMMAND; - } - + } diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerUpdate.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerUpdate.java index 368760421..0458289c0 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerUpdate.java +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/BowerUpdate.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,30 +11,15 @@ package org.eclipse.wst.jsdt.js.bower.internal.launch.shortcut; import org.eclipse.wst.jsdt.js.bower.BowerCommands; -import org.eclipse.wst.jsdt.js.bower.internal.BowerConstants; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; /** * @author "Ilya Buziuk (ibuziuk)" */ public class BowerUpdate extends GenericBowerLaunch { - private static final String LAUNCH_NAME = "Bower Update"; //$NON-NLS-1$ - private static final CLICommand COMMAND = new CLICommand(BowerConstants.BOWER, BowerCommands.UPDATE.getValue(), - null, null); - - @Override - protected String getLaunchName() { - return LAUNCH_NAME; - } @Override protected String getCommandName() { return BowerCommands.UPDATE.getValue(); } - @Override - protected CLICommand getCLICommand() { - return COMMAND; - } - } diff --git a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/GenericBowerLaunch.java b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/GenericBowerLaunch.java index 33f04d887..c52d79b39 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/GenericBowerLaunch.java +++ b/nodejs/org.eclipse.wst.jsdt.js.bower/src/org/eclipse/wst/jsdt/js/bower/internal/launch/shortcut/GenericBowerLaunch.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -19,39 +19,33 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.ILaunchShortcut; -import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ide.ResourceUtil; import org.eclipse.wst.jsdt.js.bower.BowerPlugin; import org.eclipse.wst.jsdt.js.bower.internal.BowerConstants; -import org.eclipse.wst.jsdt.js.bower.internal.Messages; import org.eclipse.wst.jsdt.js.bower.util.BowerUtil; -import org.eclipse.wst.jsdt.js.cli.core.CLI; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; /** * @author "Ilya Buziuk (ibuziuk)" */ public abstract class GenericBowerLaunch implements ILaunchShortcut { - protected abstract String getCommandName(); - protected abstract String getLaunchName(); - - protected abstract CLICommand getCLICommand(); - @Override public void launch(ISelection selection, String mode) { - if (selection instanceof IStructuredSelection) { + if (!selection.isEmpty() && selection instanceof IStructuredSelection) { Object element = ((IStructuredSelection)selection).getFirstElement(); - if (element != null && element instanceof IResource) { + if (element instanceof IResource) { IResource selectedResource = (IResource) element; - launchBower(selectedResource); + launch(selectedResource, mode); } } } @@ -61,20 +55,33 @@ public abstract class GenericBowerLaunch implements ILaunchShortcut { IEditorInput editorInput = editor.getEditorInput(); IFile file = ResourceUtil.getFile(editorInput); if (file != null && file.exists() && BowerConstants.BOWER_JSON.equals(file.getName())) { - launchBower(file); + launch(file, mode); } } - - private void launchBower(final IResource resource) { + + private void launch(final IResource resource, String mode) { try { - new CLI(resource.getProject(), getWorkingDirectory(resource)).execute(getCLICommand(), null); + IProject project = resource.getProject(); + IPath workingDirectory = getWorkingDirectory(resource); + if (project.exists() && workingDirectory != null) { + String projectName = project.getName(); + ILaunchConfigurationType bowerLaunchType = DebugPlugin.getDefault().getLaunchManager() + .getLaunchConfigurationType(BowerConstants.LAUNCH_CONFIGURATION_ID); + ILaunchConfigurationWorkingCopy bowerLaunch = bowerLaunchType.newInstance(null, generateLaunchName(projectName)); + bowerLaunch.setAttribute(BowerConstants.LAUNCH_COMMAND, getCommandName()); + bowerLaunch.setAttribute(BowerConstants.LAUNCH_PROJECT, projectName); + bowerLaunch.setAttribute(BowerConstants.LAUNCH_DIR, workingDirectory.toOSString()); + DebugUITools.launch(bowerLaunch, mode); + } } catch (CoreException e) { - BowerPlugin.logError(e); - ErrorDialog.openError(Display.getDefault().getActiveShell(), Messages.BowerLaunchError_Title, - Messages.BowerLaunchError_Message, e.getStatus()); + BowerPlugin.logError(e, e.getMessage()); } } + private String generateLaunchName(String projectName) { + return projectName + " [" + BowerConstants.BOWER + " " + getCommandName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + private IPath getWorkingDirectory(final IResource resource) throws CoreException { IPath workingDir = null; if (resource != null && resource.exists()) { @@ -99,7 +106,6 @@ public abstract class GenericBowerLaunch implements ILaunchShortcut { return workingDir; } - /** * Detects working directory for bower execution depending on .bowerrc file * @throws CoreException diff --git a/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLI.java b/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLI.java index 2cf76821d..0ef723539 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLI.java +++ b/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLI.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -64,12 +64,10 @@ public class CLI { throw new IllegalArgumentException(Messages.Error_NoProjectSpecified); } - if (workingDir == null) { - // use the project location as the working directory - this.workingDir = project.getLocation(); - } this.project = project; - this.workingDir = workingDir; + // Use the project's location as the working directory if dir is null + this.workingDir = (workingDir == null) ? project.getLocation() : workingDir; + } public CLIResult execute(CLICommand command, IProgressMonitor monitor) throws CoreException { @@ -90,7 +88,7 @@ public class CLI { try { ILaunchConfiguration cfg = type.newInstance(null, command.getToolName()); ILaunchConfigurationWorkingCopy wc = cfg.getWorkingCopy(); - wc.setAttribute(IProcess.ATTR_PROCESS_LABEL, command.getToolName() + command.getCommand()); + wc.setAttribute(IProcess.ATTR_PROCESS_LABEL, command.getToolName() + " " + command.getCommandName()); //$NON-NLS-1$ cfg = wc.doSave(); return cfg; } catch (CoreException e) { diff --git a/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLICommand.java b/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLICommand.java index d812ed71e..0dee9631d 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLICommand.java +++ b/nodejs/org.eclipse.wst.jsdt.js.cli/src/org/eclipse/wst/jsdt/js/cli/core/CLICommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -16,13 +16,13 @@ package org.eclipse.wst.jsdt.js.cli.core; public class CLICommand { private String toolName; - private String command; + private String commandName; private String subCommand; private String[] options; - public CLICommand(String toolName, String command, String subCommand, String[] options) { + public CLICommand(String toolName, String commandName, String subCommand, String[] options) { this.toolName = toolName; - this.command = command; + this.commandName = commandName; this.subCommand = subCommand; this.options = options; } @@ -32,7 +32,7 @@ public class CLICommand { StringBuilder builder = new StringBuilder(); builder.append(getToolName()); builder.append(" "); //$NON-NLS-1$ - builder.append(getCommand()); + builder.append(getCommandName()); if (getSubCommand() != null) { builder.append(" "); //$NON-NLS-1$ builder.append(getSubCommand()); @@ -53,8 +53,8 @@ public class CLICommand { return toolName; } - public String getCommand() { - return command; + public String getCommandName() { + return commandName; } public String getSubCommand() { diff --git a/nodejs/org.eclipse.wst.jsdt.js.common/src/org/eclipse/wst/jsdt/js/common/util/WorkbenchResourceUtil.java b/nodejs/org.eclipse.wst.jsdt.js.common/src/org/eclipse/wst/jsdt/js/common/util/WorkbenchResourceUtil.java index 5fa19f9d5..5ce03026a 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.common/src/org/eclipse/wst/jsdt/js/common/util/WorkbenchResourceUtil.java +++ b/nodejs/org.eclipse.wst.jsdt.js.common/src/org/eclipse/wst/jsdt/js/common/util/WorkbenchResourceUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -21,6 +21,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; @@ -92,10 +93,10 @@ public final class WorkbenchResourceUtil { return null; } - public static IProject getProject(String projectString) { - if (projectString != null) { + public static IProject getProject(String name) { + if (name != null) { try { - IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectString); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); if (project != null && project.exists()) { return project; } @@ -106,6 +107,20 @@ public final class WorkbenchResourceUtil { return null; } + public static IResource getResource(IProject project, String filePath) { + if (project != null && project.exists()) { + try { + IResource resource = project.findMember(new Path(filePath)); + if (resource != null && resource.exists()) { + return resource; + } + } catch (IllegalArgumentException e) { + } + } + + return null; + } + public static IContainer getContainerFromSelection(IStructuredSelection selection) { IContainer container = null; if (selection != null && !selection.isEmpty()) { @@ -156,4 +171,23 @@ public final class WorkbenchResourceUtil { return null; } + public static IPath getRelativePath(IContainer container, IResource resource) { + if (resource == null) { + return null; + } + if (container == null) { + return resource.getFullPath(); + } + + IPath containerPath = container.getFullPath(); + IPath resourcePath = resource.getFullPath(); + + if (containerPath.isPrefixOf(resourcePath)) { + int containerPathSegmentCount = containerPath.segmentCount(); + return resourcePath.removeFirstSegments(containerPathSegmentCount); + } + return null; + } + + } diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/plugin.xml b/nodejs/org.eclipse.wst.jsdt.js.npm/plugin.xml index b845fe545..7c46e929c 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/plugin.xml +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/plugin.xml @@ -70,18 +70,11 @@ </wizard> </extension> <extension - point="org.eclipse.ui.preferencePages"> - <page - category="org.eclipse.wst.jsdt.js.JavaScriptTools" - class="org.eclipse.wst.jsdt.js.npm.internal.preference.NpmPreferencePage" - id="org.eclipse.wst.jsdt.js.npm.preferences.NpmPreferencesPage" - name="npm"> - </page> - </extension> - <extension - point="org.eclipse.core.runtime.preferences"> - <initializer - class="org.eclipse.wst.jsdt.js.npm.internal.preference.NpmPreferenceInitializer"> - </initializer> - </extension> + point="org.eclipse.debug.core.launchConfigurationTypes"> + <launchConfigurationType + id="org.eclipse.wst.jsdt.js.npm.npmLaunchConfigurationType" + delegate="org.eclipse.wst.jsdt.js.npm.internal.launch.NpmLaunchConfigurationDelegate" + modes="run"> + </launchConfigurationType> + </extension> </plugin> diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/NpmPlugin.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/NpmPlugin.java index 43436d3c0..6b5b01a83 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/NpmPlugin.java +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/NpmPlugin.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,7 +22,7 @@ import org.osgi.framework.BundleContext; public class NpmPlugin extends AbstractUIPlugin { // The plug-in ID - public static final String PLUGIN_ID = "org.jboss.tools.jst.js.npm"; //$NON-NLS-1$ + public static final String PLUGIN_ID = "org.eclipse.wst.jsdt.js.npm"; //$NON-NLS-1$ // The shared instance private static NpmPlugin plugin; diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/NpmConstants.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/NpmConstants.java index 36139c55b..8cf4a710c 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/NpmConstants.java +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/NpmConstants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,6 +13,7 @@ package org.eclipse.wst.jsdt.js.npm.internal; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.eclipse.wst.jsdt.js.npm.NpmPlugin; /** * @author "Ilya Buziuk (ibuziuk)" @@ -21,6 +22,12 @@ public final class NpmConstants { private NpmConstants() { } + + // Launch constants + public static final String LAUNCH_CONFIGURATION_ID = NpmPlugin.PLUGIN_ID + ".npmLaunchConfigurationType"; //$NON-NLS-1$ + public static final String LAUNCH_PROJECT = NpmPlugin.PLUGIN_ID + ".PROJECT"; //$NON-NLS-1$ + public static final String LAUNCH_DIR = NpmPlugin.PLUGIN_ID + ".DIR"; //$NON-NLS-1$ + public static final String LAUNCH_COMMAND = NpmPlugin.PLUGIN_ID + ".COMMAND"; //$NON-NLS-1$ public static final String PACKAGE_JSON = "package.json"; //$NON-NLS-1$ public static final String NODE_MODULES = "node_modules"; //$NON-NLS-1$ diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/NpmLaunchConfigurationDelegate.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/NpmLaunchConfigurationDelegate.java new file mode 100644 index 000000000..17c6640bb --- /dev/null +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/NpmLaunchConfigurationDelegate.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat Inc. - initial API and implementation and/or initial documentation + *******************************************************************************/ +package org.eclipse.wst.jsdt.js.npm.internal.launch; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.wst.jsdt.js.cli.core.CLI; +import org.eclipse.wst.jsdt.js.cli.core.CLICommand; +import org.eclipse.wst.jsdt.js.common.util.WorkbenchResourceUtil; +import org.eclipse.wst.jsdt.js.npm.NpmPlugin; +import org.eclipse.wst.jsdt.js.npm.internal.Messages; +import org.eclipse.wst.jsdt.js.npm.internal.NpmConstants; + +/** + * @author "Ilya Buziuk (ibuziuk)" + */ +public class NpmLaunchConfigurationDelegate implements ILaunchConfigurationDelegate { + + @Override + public void launch(ILaunchConfiguration configuration, String arg1, ILaunch arg2, IProgressMonitor monitor) throws CoreException { + String projectName = configuration.getAttribute(NpmConstants.LAUNCH_PROJECT, (String) null); + String dirPath = configuration.getAttribute(NpmConstants.LAUNCH_DIR, (String) null); + String commandName = configuration.getAttribute(NpmConstants.LAUNCH_COMMAND, (String) null); + + IProject project = WorkbenchResourceUtil.getProject(projectName); + if (project != null && project.exists()) { + IPath dir = (dirPath == null) ? project.getLocation() : new Path(dirPath); + CLICommand command = generateCommand(commandName); + launchNpm(project, dir, command, monitor); + } + } + + private void launchNpm(IProject project, IPath dir, CLICommand command, IProgressMonitor monitor) { + try { + new CLI(project, dir).execute(command, monitor); + } catch (CoreException e) { + NpmPlugin.logError(e); + ErrorDialog.openError(Display.getDefault().getActiveShell(), Messages.NpmLaunchError_Title, + Messages.NpmLaunchError_Message, e.getStatus()); + } + } + + private CLICommand generateCommand(String commandName) { + return new CLICommand(NpmConstants.NPM, commandName, null, null); + } + +} + diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/GenericNpmLaunch.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/GenericNpmLaunch.java index 0f9fe4409..0d4012f39 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/GenericNpmLaunch.java +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/GenericNpmLaunch.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -15,18 +15,17 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.ILaunchShortcut; -import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ide.ResourceUtil; -import org.eclipse.wst.jsdt.js.cli.core.CLI; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; import org.eclipse.wst.jsdt.js.npm.NpmPlugin; -import org.eclipse.wst.jsdt.js.npm.internal.Messages; import org.eclipse.wst.jsdt.js.npm.internal.NpmConstants; import org.eclipse.wst.jsdt.js.npm.util.NpmUtil; @@ -34,24 +33,16 @@ import org.eclipse.wst.jsdt.js.npm.util.NpmUtil; * @author "Ilya Buziuk (ibuziuk)" */ public abstract class GenericNpmLaunch implements ILaunchShortcut { - - protected abstract CLICommand getCLICommand(); - + protected abstract String getCommandName(); - protected abstract String getLaunchName(); - @Override public void launch(ISelection selection, String mode) { - if (selection instanceof IStructuredSelection) { + if (!selection.isEmpty() && selection instanceof IStructuredSelection) { Object element = ((IStructuredSelection)selection).getFirstElement(); - if (element != null && element instanceof IResource) { - try { - IResource selectedResource = (IResource) element; - launchNpm(selectedResource); - } catch (CoreException e) { - NpmPlugin.logError(e); - } + if (element instanceof IResource) { + IResource selectedResource = (IResource) element; + launch(selectedResource, mode); } } } @@ -61,24 +52,33 @@ public abstract class GenericNpmLaunch implements ILaunchShortcut { IEditorInput editorInput = editor.getEditorInput(); IFile file = ResourceUtil.getFile(editorInput); if (file != null && file.exists() && NpmConstants.PACKAGE_JSON.equals(file.getName())) { - try { - launchNpm(file); - } catch (CoreException e) { - NpmPlugin.logError(e); - } + launch(file, mode); } } - private void launchNpm(IResource resource) throws CoreException { + private void launch(final IResource resource, final String mode) { try { - new CLI(resource.getProject(), getWorkingDirectory(resource)).execute(getCLICommand(), null); + IProject project = resource.getProject(); + IPath workingDirectory = getWorkingDirectory(resource); + if (project.exists() && workingDirectory != null) { + String projectName = project.getName(); + ILaunchConfigurationType npmLaunchType = DebugPlugin.getDefault().getLaunchManager() + .getLaunchConfigurationType(NpmConstants.LAUNCH_CONFIGURATION_ID); + ILaunchConfigurationWorkingCopy npmLaunch = npmLaunchType.newInstance(null, generateLaunchName(projectName)); + npmLaunch.setAttribute(NpmConstants.LAUNCH_COMMAND, getCommandName()); + npmLaunch.setAttribute(NpmConstants.LAUNCH_PROJECT, projectName); + npmLaunch.setAttribute(NpmConstants.LAUNCH_DIR, workingDirectory.toOSString()); + DebugUITools.launch(npmLaunch, mode); + } } catch (CoreException e) { - NpmPlugin.logError(e); - ErrorDialog.openError(Display.getDefault().getActiveShell(), Messages.NpmLaunchError_Title, - Messages.NpmLaunchError_Title, e.getStatus()); + NpmPlugin.logError(e, e.getMessage()); } } + private String generateLaunchName(String projectName) { + return projectName + " [" + NpmConstants.NPM + " " + getCommandName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + protected IPath getWorkingDirectory(IResource resource) throws CoreException { IPath workingDir = null; if (resource != null && resource.exists()) { diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmInstall.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmInstall.java index 2503b5029..8be40af0a 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmInstall.java +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmInstall.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,31 +10,16 @@ *******************************************************************************/ package org.eclipse.wst.jsdt.js.npm.internal.launch.shortcut; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; import org.eclipse.wst.jsdt.js.npm.NpmCommands; -import org.eclipse.wst.jsdt.js.npm.internal.NpmConstants; /** * @author "Ilya Buziuk (ibuziuk)" */ public class NpmInstall extends GenericNpmLaunch { - private static final String LAUNCH_NAME = "npm Install"; //$NON-NLS-1$ - private static final CLICommand COMMAND = new CLICommand(NpmConstants.NPM, NpmCommands.INSTALL.getValue(), - null, null); @Override protected String getCommandName() { return NpmCommands.INSTALL.getValue(); } - @Override - protected String getLaunchName() { - return LAUNCH_NAME; - } - - @Override - protected CLICommand getCLICommand() { - return COMMAND; - } - } diff --git a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmUpdate.java b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmUpdate.java index aa747cddd..7f65bdd8b 100644 --- a/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmUpdate.java +++ b/nodejs/org.eclipse.wst.jsdt.js.npm/src/org/eclipse/wst/jsdt/js/npm/internal/launch/shortcut/NpmUpdate.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015, 2016 Red Hat, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,31 +10,16 @@ *******************************************************************************/ package org.eclipse.wst.jsdt.js.npm.internal.launch.shortcut; -import org.eclipse.wst.jsdt.js.cli.core.CLICommand; import org.eclipse.wst.jsdt.js.npm.NpmCommands; -import org.eclipse.wst.jsdt.js.npm.internal.NpmConstants; /** * @author "Ilya Buziuk (ibuziuk)" */ public class NpmUpdate extends GenericNpmLaunch { - private static final String LAUNCH_NAME = "npm Update"; //$NON-NLS-1$ - private static final CLICommand COMMAND = new CLICommand(NpmConstants.NPM, NpmCommands.UPDATE.getValue(), - null, null); - - @Override - protected String getLaunchName() { - return LAUNCH_NAME; - } - + @Override protected String getCommandName() { return NpmCommands.UPDATE.getValue(); } - - @Override - protected CLICommand getCLICommand() { - return COMMAND; - } - + } |