@@ -12,6 +12,8 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
12
12
{
13
13
// TODO Use runspace pool
14
14
// 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
15
17
public class ModuleDependencyHandler : IDisposable
16
18
{
17
19
#region Private Variables
@@ -31,7 +33,7 @@ public class ModuleDependencyHandler : IDisposable
31
33
#endregion Private Variables
32
34
33
35
#region Properties
34
- public string ModulePath
36
+ public string TempModulePath
35
37
{
36
38
get { return tempDirPath ; }
37
39
}
@@ -156,30 +158,15 @@ private void SaveModule(PSObject module)
156
158
}
157
159
158
160
// TODO Use powershell copy-item
159
- private void CopyDir ( string srcParentPath ,
160
- string srcName ,
161
- string dstParentPath ,
162
- string dstName = null ,
163
- bool recurse = false )
161
+ private void CopyDir ( string srcPath , string dstPath )
164
162
{
165
- if ( dstName == null )
166
- {
167
- dstName = srcName ;
168
- }
169
- var srcPath = Path . Combine ( srcParentPath , srcName ) ;
170
- var dstPath = Path . Combine ( dstParentPath , dstName ) ;
171
- Directory . CreateDirectory ( dstPath ) ;
172
- foreach ( var file in Directory . EnumerateFiles ( srcPath ) )
173
- {
174
- File . Copy ( file , Path . Combine ( dstPath , Path . GetFileName ( file ) ) ) ;
175
- }
176
- foreach ( var dir in Directory . EnumerateDirectories ( srcPath ) )
177
- {
178
- CopyDir ( srcPath ,
179
- Path . GetFileName ( dir ) ,
180
- dstPath ,
181
- recurse : true ) ;
182
- }
163
+ var ps = System . Management . Automation . PowerShell . Create ( ) ;
164
+ ps . Runspace = runspace ;
165
+ ps . AddCommand ( "Copy-Item" )
166
+ . AddParameter ( "Recurse" )
167
+ . AddParameter ( "Path" , srcPath )
168
+ . AddParameter ( "Destination" , dstPath ) ;
169
+ ps . Invoke ( ) ;
183
170
}
184
171
185
172
#endregion Private Methods
@@ -243,8 +230,7 @@ public PSObject FindModule(string moduleName)
243
230
ps . AddCommand ( "Find-Module" , true )
244
231
. AddParameter ( "Name" , moduleName )
245
232
. AddParameter ( "Repository" , moduleRepository ) ;
246
- modules = ps . Invoke < PSObject > ( ) ;
247
-
233
+ modules = ps . Invoke < PSObject > ( ) ;
248
234
if ( modules == null )
249
235
{
250
236
return null ;
@@ -264,7 +250,9 @@ public bool ModuleExists(string moduleName)
264
250
throw new NotImplementedException ( ) ;
265
251
}
266
252
267
-
253
+ // TODO Do not use find module because it leads to two queries to the server
254
+ // instead use save module and check if it couldn't find the module
255
+ // TODO Add a TrySaveModule method
268
256
public void SaveModule ( string moduleName )
269
257
{
270
258
ThrowIfNull ( moduleName , "moduleName" ) ;
@@ -275,7 +263,7 @@ public void SaveModule(string moduleName)
275
263
if ( modulesSavedInTempPath . Contains ( moduleName ) )
276
264
{
277
265
// copy to local ps module path
278
- CopyDir ( tempDirPath , moduleName , localPSModulePath , recurse : true ) ;
266
+ CopyDir ( Path . Combine ( tempDirPath , moduleName ) , localPSModulePath ) ;
279
267
modulesSavedInModulePath . Add ( moduleName ) ;
280
268
return ;
281
269
}
@@ -293,7 +281,7 @@ public void SaveModule(string moduleName)
293
281
modulesSavedInTempPath . Add ( moduleName ) ;
294
282
295
283
// copy to local ps module path
296
- CopyDir ( tempDirPath , moduleName , localPSModulePath , recurse : true ) ;
284
+ CopyDir ( Path . Combine ( tempDirPath , moduleName ) , localPSModulePath ) ;
297
285
modulesSavedInModulePath . Add ( moduleName ) ;
298
286
}
299
287
0 commit comments