Skip to content

Commit

Permalink
Ensure correct disposal with using statement (PowerShell#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmeister committed Mar 22, 2019
1 parent 00b828f commit 60fd8b9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 78 deletions.
51 changes: 26 additions & 25 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,36 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
errorRecord = null;
PSModuleInfo psModuleInfo = null;
Collection<PSObject> psObj = null;
var ps = System.Management.Automation.PowerShell.Create();
try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", filePath);
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
using (var ps = System.Management.Automation.PowerShell.Create())
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
try
{
throw;
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", filePath);
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
{
throw;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
ps.Dispose();
return psModuleInfo;
}

Expand Down
106 changes: 53 additions & 53 deletions Rules/AvoidUsingDeprecatedManifestFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,83 +40,83 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
}
if (Helper.IsModuleManifest(fileName))
{
var ps = System.Management.Automation.PowerShell.Create();
IEnumerable<PSObject> result = null;

// hash table in psd1
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();

// no hash table means not a module manifest
if (hashTableAst == null)
using (var ps = System.Management.Automation.PowerShell.Create())
{
yield break;
}
IEnumerable<PSObject> result = null;

var table = hashTableAst as HashtableAst;
// hash table in psd1
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();

// needs to find the PowerShellVersion key
foreach (var kvp in table.KeyValuePairs)
{
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
// no hash table means not a module manifest
if (hashTableAst == null)
{
var key = (kvp.Item1 as StringConstantExpressionAst).Value;
yield break;
}

// find the powershellversion key in the hashtable
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
var table = hashTableAst as HashtableAst;

// needs to find the PowerShellVersion key
foreach (var kvp in table.KeyValuePairs)
{
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
{
// get the string value of the version
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);
var key = (kvp.Item1 as StringConstantExpressionAst).Value;

if (value != null)
// find the powershellversion key in the hashtable
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
{
Version psVersion = null;
// get the string value of the version
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
if (value != null)
{
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
Version psVersion = null;

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
{
yield break;
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
{
yield break;
}
}
}
}
}
}
}

try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", fileName);

// Suppress warnings emitted during the execution of Test-ModuleManifest
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();
}
catch
{}
try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", fileName);

// Suppress warnings emitted during the execution of Test-ModuleManifest
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();
}
catch
{}

if (result != null)
{
foreach (var warning in result)
if (result != null)
{
if (warning.BaseObject != null)
foreach (var warning in result)
{
yield return
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
GetName(), DiagnosticSeverity.Warning, fileName);
if (warning.BaseObject != null)
{
yield return
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}

ps.Dispose();
}
}

}

/// <summary>
Expand Down

0 comments on commit 60fd8b9

Please sign in to comment.