6
6
using System . Management . Automation ;
7
7
using System . Management . Automation . Language ;
8
8
using System . Management . Automation . Runspaces ;
9
- using System . Text ;
10
9
11
10
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic
12
11
{
13
12
// TODO Use runspace pool
14
13
// TODO Create a new process for the runspace
15
- // TODO Support for verbose mode
16
- // TODO Try changing the psmodulepath variable through powershell layer. This will save copying and removing the modules
14
+ // TODO Support for verbose mode
17
15
public class ModuleDependencyHandler : IDisposable
18
16
{
19
17
#region Private Variables
20
18
private Runspace runspace ;
21
19
private readonly string moduleRepository ;
22
- private string tempDirPath ;
23
- private string localPSModulePath ;
20
+ private string tempDirPath ;
24
21
Dictionary < string , PSObject > modulesFound ;
25
- HashSet < string > modulesSavedInModulePath ;
26
22
HashSet < string > modulesSavedInTempPath ;
27
23
private string localAppdataPath ;
28
24
private string pssaAppdataPath ;
29
25
private const string symLinkName = "TempModuleDir" ;
30
26
private const string tempPrefix = "PSSAModules-" ;
31
27
private string symLinkPath ;
28
+ private string oldPSModulePath ;
29
+ private string curPSModulePath ;
32
30
33
31
#endregion Private Variables
34
32
@@ -144,20 +142,13 @@ private string GetTempDirPath(string symLinkPath)
144
142
private void CleanUp ( )
145
143
{
146
144
runspace . Dispose ( ) ;
147
-
148
- // remove the modules from local psmodule path
149
- foreach ( var dir in Directory . EnumerateDirectories ( localPSModulePath ) )
150
- {
151
- if ( IsModuleNamePresent ( modulesSavedInModulePath , Path . GetFileName ( dir ) ) )
152
- {
153
- Directory . Delete ( dir , true ) ;
154
- }
155
- }
145
+ RestorePSModulePath ( ) ;
156
146
}
157
147
158
148
private void SaveModule ( PSObject module )
159
149
{
160
150
ThrowIfNull ( module , "module" ) ;
151
+
161
152
// TODO validate module
162
153
var ps = System . Management . Automation . PowerShell . Create ( ) ;
163
154
ps . Runspace = runspace ;
@@ -166,19 +157,6 @@ private void SaveModule(PSObject module)
166
157
. AddParameter ( "InputObject" , module ) ;
167
158
ps . Invoke ( ) ;
168
159
}
169
-
170
- // TODO Use powershell copy-item
171
- private void CopyDir ( string srcPath , string dstPath )
172
- {
173
- var ps = System . Management . Automation . PowerShell . Create ( ) ;
174
- ps . Runspace = runspace ;
175
- ps . AddCommand ( "Copy-Item" )
176
- . AddParameter ( "Recurse" )
177
- . AddParameter ( "Path" , srcPath )
178
- . AddParameter ( "Destination" , dstPath ) ;
179
- ps . Invoke ( ) ;
180
- }
181
-
182
160
#endregion Private Methods
183
161
184
162
#region Public Methods
@@ -187,21 +165,27 @@ public ModuleDependencyHandler()
187
165
{
188
166
runspace = null ;
189
167
moduleRepository = "PSGallery" ;
190
- modulesSavedInModulePath = new HashSet < string > ( ) ;
191
168
modulesSavedInTempPath = new HashSet < string > ( ) ;
192
- modulesFound = new Dictionary < string , PSObject > ( ) ;
193
-
194
- // TODO search it in the $psmodulepath instead of constructing it
195
- localPSModulePath = Path . Combine (
196
- Environment . GetEnvironmentVariable ( "USERPROFILE" ) ,
197
- "Documents\\ WindowsPowerShell\\ Modules" ) ;
169
+ modulesFound = new Dictionary < string , PSObject > ( ) ;
198
170
localAppdataPath = Environment . GetEnvironmentVariable ( "LOCALAPPDATA" ) ;
199
171
200
172
// TODO Add PSSA Version in the path
201
173
pssaAppdataPath = Path . Combine ( localAppdataPath , "PSScriptAnalyzer" ) ;
202
174
symLinkPath = Path . Combine ( pssaAppdataPath , symLinkName ) ;
203
-
204
175
SetupCache ( ) ;
176
+ SetupPSModulePath ( ) ;
177
+ }
178
+
179
+ private void SetupPSModulePath ( )
180
+ {
181
+ oldPSModulePath = Environment . GetEnvironmentVariable ( "PSModulePath" ) ;
182
+ curPSModulePath = oldPSModulePath + ";" + tempDirPath ;
183
+ Environment . SetEnvironmentVariable ( "PSModulePath" , curPSModulePath , EnvironmentVariableTarget . Process ) ;
184
+ }
185
+
186
+ private void RestorePSModulePath ( )
187
+ {
188
+ Environment . SetEnvironmentVariable ( "PSModulePath" , oldPSModulePath , EnvironmentVariableTarget . Process ) ;
205
189
}
206
190
207
191
public ModuleDependencyHandler ( Runspace runspace ) : this ( )
@@ -267,14 +251,8 @@ public bool ModuleExists(string moduleName)
267
251
public void SaveModule ( string moduleName )
268
252
{
269
253
ThrowIfNull ( moduleName , "moduleName" ) ;
270
- if ( IsModuleNamePresent ( modulesSavedInModulePath , moduleName ) )
271
- {
272
- return ;
273
- }
274
254
if ( IsModuleNamePresent ( modulesSavedInTempPath , moduleName ) )
275
- {
276
- // copy to local ps module path
277
- CopyToPSModulePath ( moduleName ) ;
255
+ {
278
256
return ;
279
257
}
280
258
@@ -289,25 +267,8 @@ public void SaveModule(string moduleName)
289
267
}
290
268
SaveModule ( module ) ;
291
269
AddModuleName ( modulesSavedInTempPath , moduleName ) ;
292
- CopyToPSModulePath ( moduleName ) ;
293
270
}
294
271
295
- private void CopyToPSModulePath ( string moduleName , bool checkModulePresence = false )
296
- {
297
- if ( checkModulePresence )
298
- {
299
- foreach ( var dir in Directory . EnumerateDirectories ( localPSModulePath ) )
300
- {
301
- if ( Path . GetFileName ( dir ) . Equals ( moduleName , StringComparison . OrdinalIgnoreCase ) )
302
- {
303
- return ;
304
- }
305
- }
306
- }
307
- CopyDir ( Path . Combine ( tempDirPath , moduleName ) , localPSModulePath ) ;
308
- AddModuleName ( modulesSavedInModulePath , moduleName ) ;
309
- }
310
-
311
272
public static string GetModuleNameFromErrorExtent ( ParseError error , ScriptBlockAst ast )
312
273
{
313
274
ThrowIfNull ( error , "error" ) ;
0 commit comments