|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Management.Automation; |
| 7 | +using System.Management.Automation.Language; |
| 8 | +using System.Management.Automation.Runspaces; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic |
| 12 | +{ |
| 13 | + // TODO Use runspace pool |
| 14 | + // TODO Create a new process for the runspace |
| 15 | + public class ModuleDependencyHandler : IDisposable |
| 16 | + { |
| 17 | + #region Private Variables |
| 18 | + private Runspace runspace; |
| 19 | + private readonly string moduleRepository; |
| 20 | + private string tempDirPath; |
| 21 | + Dictionary<string, PSObject> modulesFound; |
| 22 | + HashSet<string> modulesSaved; |
| 23 | + private string oldPSModulePath; |
| 24 | + private string currentModulePath; |
| 25 | + |
| 26 | + #endregion Private Variables |
| 27 | + |
| 28 | + #region Properties |
| 29 | + public string ModulePath |
| 30 | + { |
| 31 | + get { return tempDirPath; } |
| 32 | + } |
| 33 | + |
| 34 | + public Runspace Runspace |
| 35 | + { |
| 36 | + get { return runspace; } |
| 37 | + } |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Private Methods |
| 42 | + private static void ThrowIfNull<T>(T obj, string name) |
| 43 | + { |
| 44 | + if (obj == null) |
| 45 | + { |
| 46 | + throw new ArgumentNullException(name); |
| 47 | + } |
| 48 | + } |
| 49 | + private void SetupTempDir() |
| 50 | + { |
| 51 | + //var tempPath = Path.GetTempPath(); |
| 52 | + //do |
| 53 | + //{ |
| 54 | + // tempDirPath = Path.Combine( |
| 55 | + // tempPath, |
| 56 | + // Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); |
| 57 | + //} while (Directory.Exists(tempDirPath)); |
| 58 | + //Directory.CreateDirectory(tempDirPath); |
| 59 | + tempDirPath = "C:\\Users\\kabawany\\tmp\\modules\\"; |
| 60 | + } |
| 61 | + |
| 62 | + private void RemoveTempDir() |
| 63 | + { |
| 64 | + //Directory.Delete(tempDirPath, true); |
| 65 | + } |
| 66 | + |
| 67 | + private void SetupPSModulePath() |
| 68 | + { |
| 69 | + oldPSModulePath = Environment.GetEnvironmentVariable("PSModulePath", EnvironmentVariableTarget.Process); |
| 70 | + var sb = new StringBuilder(); |
| 71 | + sb.Append(oldPSModulePath) |
| 72 | + .Append(Path.DirectorySeparatorChar) |
| 73 | + .Append(tempDirPath); |
| 74 | + currentModulePath = sb.ToString(); |
| 75 | + } |
| 76 | + |
| 77 | + private void CleanUp() |
| 78 | + { |
| 79 | + runspace.Dispose(); |
| 80 | + RemoveTempDir(); |
| 81 | + RestorePSModulePath(); |
| 82 | + } |
| 83 | + |
| 84 | + private void RestorePSModulePath() |
| 85 | + { |
| 86 | + Environment.SetEnvironmentVariable("PSModulePath", oldPSModulePath, EnvironmentVariableTarget.Process); |
| 87 | + } |
| 88 | + |
| 89 | + private void SaveModule(PSObject module) |
| 90 | + { |
| 91 | + ThrowIfNull(module, "module"); |
| 92 | + // TODO validate module |
| 93 | + var ps = System.Management.Automation.PowerShell.Create(); |
| 94 | + ps.Runspace = runspace; |
| 95 | + ps.AddCommand("Save-Module") |
| 96 | + .AddParameter("Path", tempDirPath) |
| 97 | + .AddParameter("InputObject", module); |
| 98 | + ps.Invoke(); |
| 99 | + } |
| 100 | + |
| 101 | + #endregion Private Methods |
| 102 | + |
| 103 | + #region Public Methods |
| 104 | + |
| 105 | + public ModuleDependencyHandler() |
| 106 | + { |
| 107 | + runspace = null; |
| 108 | + moduleRepository = "PSGallery"; |
| 109 | + modulesSaved = new HashSet<string>(); |
| 110 | + modulesFound = new Dictionary<string, PSObject>(); |
| 111 | + SetupTempDir(); |
| 112 | + //SetupPSModulePath(); |
| 113 | + } |
| 114 | + |
| 115 | + public ModuleDependencyHandler(Runspace runspace) : this() |
| 116 | + { |
| 117 | + ThrowIfNull(runspace, "runspace"); |
| 118 | + this.runspace = runspace; |
| 119 | + this.runspace.Open(); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + public void SetupDefaultRunspace() |
| 124 | + { |
| 125 | + runspace = RunspaceFactory.CreateRunspace(); |
| 126 | + } |
| 127 | + |
| 128 | + public void SetupDefaultRunspace(Runspace runspace) |
| 129 | + { |
| 130 | + ThrowIfNull(runspace, "runspace"); |
| 131 | + if (runspace != null) |
| 132 | + { |
| 133 | + this.runspace = runspace; |
| 134 | + } |
| 135 | + Runspace.DefaultRunspace = this.runspace; |
| 136 | + } |
| 137 | + |
| 138 | + public PSObject FindModule(string moduleName) |
| 139 | + { |
| 140 | + ThrowIfNull(moduleName, "moduleName"); |
| 141 | + if (modulesFound.ContainsKey(moduleName)) |
| 142 | + { |
| 143 | + return modulesFound[moduleName]; |
| 144 | + } |
| 145 | + var ps = System.Management.Automation.PowerShell.Create(); |
| 146 | + Collection<PSObject> modules = null; |
| 147 | + ps.Runspace = runspace; |
| 148 | + ps.AddCommand("Find-Module", true) |
| 149 | + .AddParameter("Name", moduleName) |
| 150 | + .AddParameter("Repository", moduleRepository); |
| 151 | + modules = ps.Invoke<PSObject>(); |
| 152 | + |
| 153 | + if (modules == null) |
| 154 | + { |
| 155 | + return null; |
| 156 | + } |
| 157 | + var module = modules.FirstOrDefault(); |
| 158 | + if (module == null ) |
| 159 | + { |
| 160 | + return null; |
| 161 | + } |
| 162 | + modulesFound.Add(moduleName, module); |
| 163 | + return module; |
| 164 | + } |
| 165 | + |
| 166 | + public void SaveModule(string moduleName) |
| 167 | + { |
| 168 | + ThrowIfNull(moduleName, "moduleName"); |
| 169 | + if (modulesSaved.Contains(moduleName)) |
| 170 | + { |
| 171 | + return; |
| 172 | + } |
| 173 | + var module = FindModule(moduleName); |
| 174 | + if (module == null) |
| 175 | + { |
| 176 | + throw new ItemNotFoundException( |
| 177 | + string.Format( |
| 178 | + "Cannot find {0} in {1} repository.", |
| 179 | + moduleName, |
| 180 | + moduleRepository)); |
| 181 | + } |
| 182 | + SaveModule(module); |
| 183 | + modulesSaved.Add(moduleName); |
| 184 | + } |
| 185 | + |
| 186 | + public static string GetModuleNameFromErrorExtent(ParseError error, ScriptBlockAst ast) |
| 187 | + { |
| 188 | + ThrowIfNull(error, "error"); |
| 189 | + ThrowIfNull(ast, "ast"); |
| 190 | + var statement = ast.Find(x => x.Extent.Equals(error.Extent), true); |
| 191 | + var dynamicKywdAst = statement as DynamicKeywordStatementAst; |
| 192 | + if (dynamicKywdAst == null) |
| 193 | + { |
| 194 | + return null; |
| 195 | + } |
| 196 | + // check if the command name is import-dscmodule |
| 197 | + // right now we handle only the following form |
| 198 | + // Import-DSCResource -ModuleName xActiveDirectory |
| 199 | + if (dynamicKywdAst.CommandElements.Count != 3) |
| 200 | + { |
| 201 | + return null; |
| 202 | + } |
| 203 | + |
| 204 | + var dscKeywordAst = dynamicKywdAst.CommandElements[0] as StringConstantExpressionAst; |
| 205 | + if (dscKeywordAst == null || !dscKeywordAst.Value.Equals("Import-DscResource", StringComparison.OrdinalIgnoreCase)) |
| 206 | + { |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + var paramAst = dynamicKywdAst.CommandElements[1] as CommandParameterAst; |
| 211 | + if (paramAst == null || !paramAst.ParameterName.Equals("ModuleName", StringComparison.OrdinalIgnoreCase)) |
| 212 | + { |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + var paramValAst = dynamicKywdAst.CommandElements[2] as StringConstantExpressionAst; |
| 217 | + if (paramValAst == null) |
| 218 | + { |
| 219 | + return null; |
| 220 | + } |
| 221 | + |
| 222 | + return paramValAst.Value; |
| 223 | + } |
| 224 | + |
| 225 | + public void Dispose() |
| 226 | + { |
| 227 | + CleanUp(); |
| 228 | + } |
| 229 | + |
| 230 | + #endregion Public Methods |
| 231 | + } |
| 232 | +} |
0 commit comments