Skip to content

Commit ea3c0ad

Browse files
author
Kapil Borle
committed
Change copy directory implementation in module handler
Use powershell copy-item to copy directories instead of writing a routine to copy directories recursively.
1 parent f3cde07 commit ea3c0ad

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

Engine/Generic/ModuleDependencyHandler.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1212
{
1313
// TODO Use runspace pool
1414
// 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
1517
public class ModuleDependencyHandler : IDisposable
1618
{
1719
#region Private Variables
@@ -31,7 +33,7 @@ public class ModuleDependencyHandler : IDisposable
3133
#endregion Private Variables
3234

3335
#region Properties
34-
public string ModulePath
36+
public string TempModulePath
3537
{
3638
get { return tempDirPath; }
3739
}
@@ -156,30 +158,15 @@ private void SaveModule(PSObject module)
156158
}
157159

158160
// 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)
164162
{
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();
183170
}
184171

185172
#endregion Private Methods
@@ -243,8 +230,7 @@ public PSObject FindModule(string moduleName)
243230
ps.AddCommand("Find-Module", true)
244231
.AddParameter("Name", moduleName)
245232
.AddParameter("Repository", moduleRepository);
246-
modules = ps.Invoke<PSObject>();
247-
233+
modules = ps.Invoke<PSObject>();
248234
if (modules == null)
249235
{
250236
return null;
@@ -264,7 +250,9 @@ public bool ModuleExists(string moduleName)
264250
throw new NotImplementedException();
265251
}
266252

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
268256
public void SaveModule(string moduleName)
269257
{
270258
ThrowIfNull(moduleName, "moduleName");
@@ -275,7 +263,7 @@ public void SaveModule(string moduleName)
275263
if (modulesSavedInTempPath.Contains(moduleName))
276264
{
277265
// copy to local ps module path
278-
CopyDir(tempDirPath, moduleName, localPSModulePath, recurse: true);
266+
CopyDir(Path.Combine(tempDirPath, moduleName), localPSModulePath);
279267
modulesSavedInModulePath.Add(moduleName);
280268
return;
281269
}
@@ -293,7 +281,7 @@ public void SaveModule(string moduleName)
293281
modulesSavedInTempPath.Add(moduleName);
294282

295283
// copy to local ps module path
296-
CopyDir(tempDirPath, moduleName, localPSModulePath, recurse: true);
284+
CopyDir(Path.Combine(tempDirPath, moduleName), localPSModulePath);
297285
modulesSavedInModulePath.Add(moduleName);
298286
}
299287

0 commit comments

Comments
 (0)