/src/AddIns/Misc/PackageManagement/PowerShell/Project/Src/PowerShellHost.cs

https://github.com/ajadex/SharpDevelop · C# · 198 lines · 150 code · 31 blank · 17 comment · 2 complexity · b658090e179bd03cafcce92d2cc49275 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections;
  20. using System.Collections.Generic;
  21. using System.Globalization;
  22. using System.Management.Automation;
  23. using System.Management.Automation.Host;
  24. using System.Management.Automation.Runspaces;
  25. using System.Threading;
  26. using ICSharpCode.Scripting;
  27. namespace ICSharpCode.PackageManagement.Scripting
  28. {
  29. public class PowerShellHost : PSHost, IPowerShellHost
  30. {
  31. public static readonly string EnvironmentPathVariableName = "env:path";
  32. IScriptingConsole scriptingConsole;
  33. CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
  34. CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
  35. Guid instanceId = Guid.NewGuid();
  36. Runspace runspace;
  37. PowerShellHostUserInterface userInterface;
  38. List<string> modulesToImport = new List<string>();
  39. PSObject privateData;
  40. object dte;
  41. Version version;
  42. public PowerShellHost(
  43. IScriptingConsole scriptingConsole,
  44. Version version,
  45. object privateData,
  46. object dte)
  47. {
  48. this.scriptingConsole = scriptingConsole;
  49. this.version = version;
  50. this.privateData = new PSObject(privateData);
  51. this.dte = dte;
  52. userInterface = new PowerShellHostUserInterface(scriptingConsole);
  53. }
  54. public override PSObject PrivateData {
  55. get { return privateData; }
  56. }
  57. public IList<string> ModulesToImport {
  58. get { return modulesToImport; }
  59. }
  60. public void SetRemoteSignedExecutionPolicy()
  61. {
  62. ExecuteCommand("Set-ExecutionPolicy RemoteSigned -Scope 0 -Force");
  63. }
  64. public void UpdateFormatting(IEnumerable<string> formattingFiles)
  65. {
  66. foreach (string file in formattingFiles) {
  67. string command = String.Format("Update-FormatData '{0}'", file);
  68. ExecuteCommand(command);
  69. }
  70. }
  71. public void ExecuteCommand(string command)
  72. {
  73. try {
  74. CreateRunspace();
  75. using (Pipeline pipeline = CreatePipeline(command)) {
  76. pipeline.Invoke();
  77. }
  78. } catch (Exception ex) {
  79. scriptingConsole.WriteLine(ex.Message, ScriptingStyle.Error);
  80. }
  81. }
  82. Pipeline CreatePipeline(string command)
  83. {
  84. Pipeline pipeline = runspace.CreatePipeline();
  85. pipeline.Commands.AddScript(command);
  86. pipeline.Commands.Add("out-default");
  87. pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
  88. return pipeline;
  89. }
  90. void CreateRunspace()
  91. {
  92. if (runspace == null) {
  93. InitialSessionState initialSessionState = CreateInitialSessionState();
  94. runspace = RunspaceFactory.CreateRunspace(this, initialSessionState);
  95. runspace.Open();
  96. }
  97. }
  98. InitialSessionState CreateInitialSessionState()
  99. {
  100. var initialSessionState = InitialSessionState.CreateDefault();
  101. initialSessionState.ImportPSModule(modulesToImport.ToArray());
  102. SessionStateVariableEntry variable = CreateDTESessionVariable();
  103. initialSessionState.Variables.Add(variable);
  104. return initialSessionState;
  105. }
  106. SessionStateVariableEntry CreateDTESessionVariable()
  107. {
  108. var options = ScopedItemOptions.AllScope | ScopedItemOptions.Constant;
  109. return new SessionStateVariableEntry("DTE", dte, "SharpDevelop DTE object", options);
  110. }
  111. public override Version Version {
  112. get { return version; }
  113. }
  114. public override PSHostUserInterface UI {
  115. get { return userInterface; }
  116. }
  117. public override void SetShouldExit(int exitCode)
  118. {
  119. }
  120. public override void NotifyEndApplication()
  121. {
  122. }
  123. public override void NotifyBeginApplication()
  124. {
  125. }
  126. public override string Name {
  127. get { return "Package Manager Host"; }
  128. }
  129. public override Guid InstanceId {
  130. get { return instanceId; }
  131. }
  132. public override void ExitNestedPrompt()
  133. {
  134. throw new NotImplementedException();
  135. }
  136. public override void EnterNestedPrompt()
  137. {
  138. throw new NotImplementedException();
  139. }
  140. public override CultureInfo CurrentUICulture {
  141. get { return currentUICulture; }
  142. }
  143. public override CultureInfo CurrentCulture {
  144. get { return currentCulture; }
  145. }
  146. public void RunScript(string fileName, IEnumerable<object> input)
  147. {
  148. try {
  149. CreateRunspace();
  150. string command =
  151. "$__args = @(); " +
  152. "$input | ForEach-Object {$__args += $_}; " +
  153. "& '" + fileName + "' $__args[0] $__args[1] $__args[2] $__args[3]" +
  154. "Remove-Variable __args -Scope 0";
  155. using (Pipeline pipeline = CreatePipeline(command)) {
  156. pipeline.Invoke(input);
  157. }
  158. } catch (Exception ex) {
  159. scriptingConsole.WriteLine(ex.Message, ScriptingStyle.Error);
  160. }
  161. }
  162. public void SetDefaultRunspace()
  163. {
  164. Runspace.DefaultRunspace = runspace;
  165. }
  166. }
  167. }