forked from madskristensen/OpenCommandLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenCommandLinePackage.cs
More file actions
118 lines (93 loc) · 4.69 KB
/
Copy pathOpenCommandLinePackage.cs
File metadata and controls
118 lines (93 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace MadsKristensen.OpenCommandLine
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", Version, IconResourceID = 400)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists)]
[ProvideOptionPage(typeof(Options), "Environment", "Command Line", 101, 104, true, new[] { "cmd", "powershell", "bash" })]
[Guid(GuidList.guidOpenCommandLinePkgString)]
public sealed class OpenCommandLinePackage : Package
{
public const string Version = "1.6";
private static DTE2 _dte;
public Package Instance;
protected override void Initialize()
{
base.Initialize();
_dte = GetService(typeof(DTE)) as DTE2;
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
CommandID cmdCustom = new CommandID(GuidList.guidOpenCommandLineCmdSet, (int)PkgCmdIDList.cmdidOpenCommandLine);
OleMenuCommand customItem = new OleMenuCommand(OpenCustom, cmdCustom);
customItem.BeforeQueryStatus += BeforeQueryStatus;
mcs.AddCommand(customItem);
CommandID cmdCmd = new CommandID(GuidList.guidOpenCommandLineCmdSet, (int)PkgCmdIDList.cmdidOpenCmd);
MenuCommand cmdItem = new MenuCommand(OpenCmd, cmdCmd);
mcs.AddCommand(cmdItem);
CommandID cmdPowershell = new CommandID(GuidList.guidOpenCommandLineCmdSet, (int)PkgCmdIDList.cmdidOpenPowershell);
MenuCommand powershellItem = new MenuCommand(OpenPowershell, cmdPowershell);
mcs.AddCommand(powershellItem);
CommandID cmdOptions = new CommandID(GuidList.guidOpenCommandLineCmdSet, (int)PkgCmdIDList.cmdidOpenOptions);
MenuCommand optionsItem = new MenuCommand((s, e) => { ShowOptionPage(typeof(Options)); }, cmdOptions);
mcs.AddCommand(optionsItem);
}
private void BeforeQueryStatus(object sender, EventArgs e)
{
OleMenuCommand button = (OleMenuCommand)sender;
Options options = GetDialogPage(typeof(Options)) as Options;
button.Text = "Default (" + options.Preset + ")";
}
private void OpenCustom(object sender, EventArgs e)
{
Options options = GetDialogPage(typeof(Options)) as Options;
string folder = VsHelpers.GetFolderPath(options, _dte);
string arguments = (options.Arguments ?? string.Empty).Replace("%folder%", folder);
StartProcess(folder, options.Command, arguments);
}
private void OpenCmd(object sender, EventArgs e)
{
string installDir = VsHelpers.GetInstallDirectory(this);
string devPromptFile = Path.Combine(installDir, @"..\Tools\VsDevCmd.bat");
SetupProcess("cmd.exe", "/k \"" + devPromptFile + "\"");
}
private void OpenPowershell(object sender, EventArgs e)
{
SetupProcess("powershell.exe", "-ExecutionPolicy Bypass -NoExit");
}
private void SetupProcess(string command, string arguments)
{
Options options = GetDialogPage(typeof(Options)) as Options;
string folder = VsHelpers.GetFolderPath(options, _dte);
StartProcess(folder, command, arguments);
}
private static void StartProcess(string workingDirectory, string command, string arguments)
{
command = Environment.ExpandEnvironmentVariables(command ?? string.Empty);
arguments = Environment.ExpandEnvironmentVariables(arguments ?? string.Empty);
ProcessStartInfo start = new ProcessStartInfo(command, arguments);
start.WorkingDirectory = workingDirectory;
ModifyPathVariable(start);
System.Diagnostics.Process.Start(start);
}
private static void ModifyPathVariable(ProcessStartInfo start)
{
string path = ".\\node_modules\\.bin" + ";" + start.EnvironmentVariables["PATH"];
string toolsDir = Environment.GetEnvironmentVariable("VS140COMNTOOLS");
if (Directory.Exists(toolsDir))
{
string parent = Directory.GetParent(toolsDir).Parent.FullName;
path += ";" + Path.Combine(parent, @"IDE\Extensions\Microsoft\Web Tools\External");
}
start.UseShellExecute = false;
start.EnvironmentVariables["PATH"] = path;
}
}
}