Skip to content

Commit 944c5be

Browse files
authored
Ensure correct disposal with using statement (#1164)
1 parent 0574565 commit 944c5be

File tree

2 files changed

+79
-78
lines changed

2 files changed

+79
-78
lines changed

Engine/Helper.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -295,35 +295,36 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
295295
errorRecord = null;
296296
PSModuleInfo psModuleInfo = null;
297297
Collection<PSObject> psObj = null;
298-
var ps = System.Management.Automation.PowerShell.Create();
299-
try
300-
{
301-
ps.AddCommand("Test-ModuleManifest");
302-
ps.AddParameter("Path", filePath);
303-
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
304-
psObj = ps.Invoke();
305-
}
306-
catch (CmdletInvocationException e)
298+
using (var ps = System.Management.Automation.PowerShell.Create())
307299
{
308-
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
309-
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
310-
// we consume it to allow execution of the of this method.
311-
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
300+
try
312301
{
313-
throw;
302+
ps.AddCommand("Test-ModuleManifest");
303+
ps.AddParameter("Path", filePath);
304+
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
305+
psObj = ps.Invoke();
306+
}
307+
catch (CmdletInvocationException e)
308+
{
309+
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
310+
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
311+
// we consume it to allow execution of the of this method.
312+
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
313+
{
314+
throw;
315+
}
316+
}
317+
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
318+
{
319+
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
320+
ps.Streams.Error.CopyTo(errorRecordArr, 0);
321+
errorRecord = errorRecordArr;
322+
}
323+
if (psObj != null && psObj.Any() && psObj[0] != null)
324+
{
325+
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
314326
}
315327
}
316-
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
317-
{
318-
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
319-
ps.Streams.Error.CopyTo(errorRecordArr, 0);
320-
errorRecord = errorRecordArr;
321-
}
322-
if (psObj != null && psObj.Any() && psObj[0] != null)
323-
{
324-
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
325-
}
326-
ps.Dispose();
327328
return psModuleInfo;
328329
}
329330

Rules/AvoidUsingDeprecatedManifestFields.cs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,83 +40,83 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4040
}
4141
if (Helper.IsModuleManifest(fileName))
4242
{
43-
var ps = System.Management.Automation.PowerShell.Create();
44-
IEnumerable<PSObject> result = null;
45-
46-
// hash table in psd1
47-
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();
48-
49-
// no hash table means not a module manifest
50-
if (hashTableAst == null)
43+
using (var ps = System.Management.Automation.PowerShell.Create())
5144
{
52-
yield break;
53-
}
45+
IEnumerable<PSObject> result = null;
5446

55-
var table = hashTableAst as HashtableAst;
47+
// hash table in psd1
48+
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();
5649

57-
// needs to find the PowerShellVersion key
58-
foreach (var kvp in table.KeyValuePairs)
59-
{
60-
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
50+
// no hash table means not a module manifest
51+
if (hashTableAst == null)
6152
{
62-
var key = (kvp.Item1 as StringConstantExpressionAst).Value;
53+
yield break;
54+
}
6355

64-
// find the powershellversion key in the hashtable
65-
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
56+
var table = hashTableAst as HashtableAst;
57+
58+
// needs to find the PowerShellVersion key
59+
foreach (var kvp in table.KeyValuePairs)
60+
{
61+
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
6662
{
67-
// get the string value of the version
68-
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);
63+
var key = (kvp.Item1 as StringConstantExpressionAst).Value;
6964

70-
if (value != null)
65+
// find the powershellversion key in the hashtable
66+
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
7167
{
72-
Version psVersion = null;
68+
// get the string value of the version
69+
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);
7370

74-
// get the version
75-
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
71+
if (value != null)
7672
{
77-
// if version exists and version less than 3, don't raise rule
78-
if (psVersion.Major < 3)
73+
Version psVersion = null;
74+
75+
// get the version
76+
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
7977
{
80-
yield break;
78+
// if version exists and version less than 3, don't raise rule
79+
if (psVersion.Major < 3)
80+
{
81+
yield break;
82+
}
8183
}
8284
}
8385
}
8486
}
8587
}
86-
}
8788

88-
try
89-
{
90-
ps.AddCommand("Test-ModuleManifest");
91-
ps.AddParameter("Path", fileName);
92-
93-
// Suppress warnings emitted during the execution of Test-ModuleManifest
94-
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
95-
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
96-
ps.AddParameter("WarningVariable", "Message");
97-
ps.AddScript("$Message");
98-
result = ps.Invoke();
99-
}
100-
catch
101-
{}
89+
try
90+
{
91+
ps.AddCommand("Test-ModuleManifest");
92+
ps.AddParameter("Path", fileName);
93+
94+
// Suppress warnings emitted during the execution of Test-ModuleManifest
95+
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
96+
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
97+
ps.AddParameter("WarningVariable", "Message");
98+
ps.AddScript("$Message");
99+
result = ps.Invoke();
100+
}
101+
catch
102+
{}
102103

103-
if (result != null)
104-
{
105-
foreach (var warning in result)
104+
if (result != null)
106105
{
107-
if (warning.BaseObject != null)
106+
foreach (var warning in result)
108107
{
109-
yield return
110-
new DiagnosticRecord(
111-
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
112-
GetName(), DiagnosticSeverity.Warning, fileName);
108+
if (warning.BaseObject != null)
109+
{
110+
yield return
111+
new DiagnosticRecord(
112+
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
113+
GetName(), DiagnosticSeverity.Warning, fileName);
114+
}
113115
}
114116
}
115-
}
116117

117-
ps.Dispose();
118+
}
118119
}
119-
120120
}
121121

122122
/// <summary>

0 commit comments

Comments
 (0)