Skip to content

Commit 2f69abf

Browse files
author
Kapil Borle
committed
Add a method to check valid module manifest
1 parent e8df57e commit 2f69abf

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Engine/Helper.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,76 @@ public bool GetNamedArgumentAttributeValue(NamedAttributeArgumentAst namedAttrib
16021602
return false;
16031603
}
16041604

1605+
public static IEnumerable<string> GetModuleManifestKeys(Version powershellVersion)
1606+
{
1607+
string[] keys = new string[] { };
1608+
1609+
// default to version 5.0.10586.494
1610+
if (powershellVersion == null)
1611+
{
1612+
keys = new string[] {
1613+
"RootModule",
1614+
"ModuleVersion",
1615+
"GUID",
1616+
"Author",
1617+
"CompanyName",
1618+
"Copyright",
1619+
"Description",
1620+
"PowerShellVersion",
1621+
"PowerShellHostName",
1622+
"PowerShellHostVersion",
1623+
"DotNetFrameworkVersion",
1624+
"CLRVersion",
1625+
"ProcessorArchitecture",
1626+
"RequiredModules",
1627+
"RequiredAssemblies",
1628+
"ScriptsToProcess",
1629+
"TypesToProcess",
1630+
"FormatsToProcess",
1631+
"NestedModules",
1632+
"FunctionsToExport",
1633+
"CmdletsToExport",
1634+
"VariablesToExport",
1635+
"AliasesToExport",
1636+
"DscResourcesToExport",
1637+
"ModuleList",
1638+
"FileList",
1639+
"PrivateData",
1640+
"HelpInfoURI",
1641+
"DefaultCommandPrefix"};
1642+
}
1643+
return keys;
1644+
}
1645+
1646+
public static bool IsModuleManifest(string filepath)
1647+
{
1648+
Token[] tokens;
1649+
ParseError[] errors;
1650+
1651+
//using parsefile causes the parser to crash!
1652+
string fileContent = File.ReadAllText(filepath);
1653+
var ast = Parser.ParseInput(fileContent, out tokens, out errors);
1654+
var hast = ast.Find(x => x is HashtableAst, false) as HashtableAst;
1655+
var keys = GetModuleManifestKeys(null);
1656+
int matchCount = 0;
1657+
foreach(var pair in hast.KeyValuePairs)
1658+
{
1659+
var pairKey = pair.Item1 as StringConstantExpressionAst;
1660+
if (pairKey == null)
1661+
{
1662+
break;
1663+
}
1664+
foreach(var key in keys)
1665+
{
1666+
if (key.Equals(pairKey.Value, StringComparison.OrdinalIgnoreCase))
1667+
{
1668+
matchCount++;
1669+
break;
1670+
}
1671+
}
1672+
}
1673+
return matchCount == hast.KeyValuePairs.Count;
1674+
}
16051675
#endregion Methods
16061676
}
16071677

0 commit comments

Comments
 (0)