Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Nodejs/Product/Nodejs/Commands/ImportWizardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ namespace Microsoft.NodejsTools.Commands {
/// Provides the command to import a project from existing code.
/// </summary>
class ImportWizardCommand : Command {

public override void DoCommand(object sender, EventArgs args) {
var statusBar = (IVsStatusbar)CommonPackage.GetGlobalService(typeof(SVsStatusbar));
statusBar.SetText("Importing project...");

var dlg = new Microsoft.NodejsTools.Project.ImportWizard.ImportWizard();
int commandIdToRaise = (int)VSConstants.VSStd97CmdID.OpenProject;

Microsoft.VisualStudio.Shell.OleMenuCmdEventArgs oleArgs = args as Microsoft.VisualStudio.Shell.OleMenuCmdEventArgs;
if (oleArgs != null) {
string projectArgs = oleArgs.InValue as string;
if (projectArgs != null) {
var argItems = projectArgs.Split('|');
if (argItems.Length == 2) {
if (argItems.Length == 3) {
dlg.ImportSettings.ProjectPath = CommonUtils.GetAvailableFilename(
argItems[1],
argItems[0],
".njsproj"
);
dlg.ImportSettings.SourcePath = argItems[1];
commandIdToRaise = int.Parse(argItems[2]);
}
}
}
Expand Down Expand Up @@ -81,7 +84,7 @@ public override void DoCommand(object sender, EventArgs args) {
}
if (File.Exists(path)) {
object outRef = null, pathRef = "\"" + path + "\"";
NodejsPackage.Instance.DTE.Commands.Raise(VSConstants.GUID_VSStandardCommandSet97.ToString("B"), (int)VSConstants.VSStd97CmdID.OpenProject, ref pathRef, ref outRef);
NodejsPackage.Instance.DTE.Commands.Raise(VSConstants.GUID_VSStandardCommandSet97.ToString("B"), commandIdToRaise, ref pathRef, ref outRef);
statusBar.SetText(String.Empty);
} else {
statusBar.SetText("An error occurred and your project was not created.");
Expand Down
28 changes: 27 additions & 1 deletion Nodejs/Product/Nodejs/NodejsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using Microsoft.NodejsTools.Logging;
using Microsoft.NodejsTools.Options;
using Microsoft.NodejsTools.Project;
using Microsoft.NodejsTools.ProjectWizard;
using Microsoft.NodejsTools.Repl;
using Microsoft.NodejsTools.Telemetry;
using Microsoft.VisualStudio;
Expand Down Expand Up @@ -110,6 +111,9 @@ internal sealed partial class NodejsPackage : CommonPackage {
internal VsProjectAnalyzer _analyzer;
private NodejsToolsLogger _logger;
private ITelemetryLogger _telemetryLogger;
// Hold references for the subscribed events. Otherwise the callbacks will be garbage collected
// after the initialization
private List<EnvDTE.CommandEvents> _subscribedCommandEvents = new List<EnvDTE.CommandEvents>();

/// <summary>
/// Default constructor of the package.
Expand Down Expand Up @@ -190,7 +194,13 @@ protected override void Initialize() {
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize();

var langService = new NodejsLanguageInfo(this);
SubscribeToVsCommandEvents(
(int)VSConstants.VSStd97CmdID.AddNewProject,
delegate { NewProjectFromExistingWizard.IsAddNewProjectCmd = true; },
delegate { NewProjectFromExistingWizard.IsAddNewProjectCmd = false; }
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth splitting this logic out into a private method, SubscribeToVsCommandEvents

var langService = new NodejsLanguageInfo(this);
((IServiceContainer)this).AddService(langService.GetType(), langService, true);

((IServiceContainer)this).AddService(typeof(ClipboardServiceBase), new ClipboardService(), true);
Expand Down Expand Up @@ -246,6 +256,22 @@ protected override void Initialize() {
Environment.SetEnvironmentVariable(NodejsConstants.NodeToolsProcessIdEnvironmentVariable, Process.GetCurrentProcess().Id.ToString());
}

private void SubscribeToVsCommandEvents(
int eventId,
EnvDTE._dispCommandEvents_BeforeExecuteEventHandler beforeExecute = null,
EnvDTE._dispCommandEvents_AfterExecuteEventHandler afterExecute = null) {
var commandEventGuid = typeof(VSConstants.VSStd97CmdID).GUID.ToString("B");
var targetEvent = DTE.Events.CommandEvents[commandEventGuid, eventId];
if (beforeExecute != null) {
targetEvent.BeforeExecute += beforeExecute;
}
if (afterExecute != null) {
targetEvent.AfterExecute += afterExecute;
}
_subscribedCommandEvents.Add(targetEvent);
}


private void IntellisenseOptionsPage_AnalysisLogMaximumChanged(object sender, EventArgs e) {
if (_analyzer != null) {
_analyzer.MaxLogLength = IntellisenseOptionsPage.AnalysisLogMax;
Expand Down
14 changes: 11 additions & 3 deletions Nodejs/Product/ProjectWizard/ImportWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
using System.IO;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TemplateWizard;

using Microsoft.VisualStudio.TemplateWizard;
namespace Microsoft.NodejsTools.ProjectWizard {
public sealed class NewProjectFromExistingWizard : IWizard {
public static Boolean IsAddNewProjectCmd { get; set; }
public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem) { }
public void ProjectFinishedGenerating(EnvDTE.Project project) { }
public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem) { }
Expand All @@ -45,6 +47,8 @@ public void RunStarted(object automationObject, Dictionary<string, string> repla
dte = new ServiceProvider(provider).GetService(typeof(DTE)) as DTE;
}
}

bool addingNewProject = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for? Shouldn't we be using AddingNewProject here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to record the AddingNewProject before it gets reset. The AddingNewProject will be set in the AfterExecute before the ImportWizardCommand gets raised, so I need to record the value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only gets reset after WizardCancelledException.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're also not actually recording or using the value of AddingNewProject anywhere.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I used it at line 79. It captures value of AddingNewProject at line 84. The one that gets reset is the property, not the local variable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dte.Commands.Raise(...) gets called before line 84 and throw new WizardCancelledException, correct?

if (dte == null) {
MessageBox.Show("Unable to start wizard: no automation object available.", "Node.js Tools for Visual Studio");
} else {
Expand Down Expand Up @@ -73,10 +77,14 @@ out package
directory = Path.GetDirectoryName(Path.GetDirectoryName(replacementsDictionary["$destinationdirectory$"]));
}

object inObj = projName + "|" + directory, outObj = null;
var context = addingNewProject ?
(int)VSConstants.VSStd97CmdID.AddExistingProject :
(int)VSConstants.VSStd97CmdID.OpenProject;
object inObj = projName + "|" + directory + "|" + context, outObj = null;
dte.Commands.Raise(Guids.NodejsCmdSet.ToString("B"), (int)PkgCmdId.cmdidImportWizard, ref inObj, ref outObj);
});
}
addingNewProject = IsAddNewProjectCmd;
throw new WizardCancelledException();
}

Expand Down