Repository navigation
Expand file tree
/
Copy pathPSScriptExecutor.cs
More file actions
135 lines (116 loc) · 4.97 KB
/
Copy pathPSScriptExecutor.cs
File metadata and controls
135 lines (116 loc) · 4.97 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Runtime.Versioning;
using EnvDTE;
using NuGet.Resources;
using NuGet.VisualStudio.Resources;
using NuGetConsole;
namespace NuGet.VisualStudio
{
[Export(typeof(IScriptExecutor))]
public class PSScriptExecutor : IScriptExecutor
{
private readonly Lazy<IHost> _host;
public PSScriptExecutor()
{
_host = new Lazy<IHost>(GetHost);
}
private IHost Host
{
get
{
return _host.Value;
}
}
[Import]
public IOutputConsoleProvider OutputConsoleProvider
{
get;
set;
}
public bool Execute(string installPath, string scriptFileName, IPackage package, Project project, ILogger logger)
{
return Execute(installPath, scriptFileName, package, project, project.GetTargetFrameworkName(), logger);
}
public bool Execute(string installPath, string scriptFileName, IPackage package, Project project, FrameworkName targetFramework, ILogger logger)
{
string fullPath;
IPackageFile scriptFile;
if (package.FindCompatibleToolFiles(scriptFileName, targetFramework, out scriptFile))
{
fullPath = Path.Combine(installPath, scriptFile.Path);
}
else
{
return false;
}
return ExecuteCore(
fullPath,
installPath,
package,
project,
targetFramework,
logger,
scriptFile);
}
// Used in the V3 API (VsPowerShellExecutionFeature).
public bool ExecuteResolvedScript(string fullScriptPath, string installPath, IPackage package, Project project, FrameworkName targetFramework, ILogger logger)
{
return ExecuteCore(fullScriptPath, installPath, package, project, targetFramework, logger, scriptFile: null);
}
private bool ExecuteCore(
string fullScriptPath,
string installPath,
IPackage package,
Project project,
FrameworkName targetFramework,
ILogger logger,
IPackageFile scriptFile)
{
if (File.Exists(fullScriptPath))
{
if (project != null && scriptFile != null)
{
// targetFramework can be null for unknown project types
string shortFramework = targetFramework == null ? string.Empty : VersionUtility.GetShortFrameworkName(targetFramework);
logger.Log(MessageLevel.Debug, NuGetResources.Debug_TargetFrameworkInfoPrefix, package.GetFullName(),
project.Name, shortFramework);
logger.Log(MessageLevel.Debug, NuGetResources.Debug_TargetFrameworkInfo_PowershellScripts,
Path.GetDirectoryName(scriptFile.Path), VersionUtility.GetTargetFrameworkLogString(scriptFile.TargetFramework));
}
string toolsPath = Path.GetDirectoryName(fullScriptPath);
string logMessage = String.Format(CultureInfo.CurrentCulture, VsResources.ExecutingScript, fullScriptPath);
// logging to both the Output window and progress window.
logger.Log(MessageLevel.Info, logMessage);
IConsole console = OutputConsoleProvider.CreateOutputConsole(requirePowerShellHost: true);
Host.Execute(console,
"$__pc_args=@(); $input|%{$__pc_args+=$_}; & " + PathHelper.EscapePSPath(fullScriptPath) + " $__pc_args[0] $__pc_args[1] $__pc_args[2] $__pc_args[3]; Remove-Variable __pc_args -Scope 0",
new object[] { installPath, toolsPath, package, project });
return true;
}
return false;
}
private IHost GetHost()
{
// create the console and instantiate the PS host on demand
IConsole console = OutputConsoleProvider.CreateOutputConsole(requirePowerShellHost: true);
IHost host = console.Host;
// start the console
console.Dispatcher.Start();
// gives the host a chance to do initialization works before dispatching commands to it
host.Initialize(console);
// after the host initializes, it may set IsCommandEnabled = false
if (host.IsCommandEnabled)
{
return host;
}
else
{
// the PowerShell host fails to initialize if group policy restricts to AllSigned
throw new InvalidOperationException(VsResources.Console_InitializeHostFails);
}
}
}
}