Skip to content

Commit e623f57

Browse files
author
Kapil Borle
committed
Add methods to get keys from a mof file
1 parent dc1f5b9 commit e623f57

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
using System.Linq;
1616
using System.Management.Automation.Language;
1717
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
18+
using Microsoft.PowerShell.DesiredStateConfiguration.Internal;
19+
using System.IO;
20+
using Microsoft.Management.Infrastructure;
1821
#if !CORECLR
1922
using System.ComponentModel.Composition;
2023
#endif
@@ -46,6 +49,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
4649

4750
var functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast).Cast<FunctionDefinitionAst>();
4851

52+
var keys = GetKeys(fileName);
4953
// Dictionary to keep track of Mandatory parameters and their presence in Get/Test/Set TargetResource cmdlets
5054
var mandatoryParameters = new Dictionary<string, List<FunctionDefinitionAst>>(StringComparer.OrdinalIgnoreCase);
5155

@@ -119,6 +123,102 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
119123
}
120124
}
121125

126+
private string[] GetKeys(string fileName)
127+
{
128+
var moduleInfo = GetModuleInfo(fileName);
129+
if (moduleInfo == null)
130+
{
131+
return null;
132+
}
133+
134+
var mofFilepath = GetMofFilepath(fileName);
135+
if (mofFilepath == null)
136+
{
137+
return null;
138+
}
139+
140+
var errors = new System.Collections.ObjectModel.Collection<Exception>();
141+
var keys = new List<string>();
142+
List<CimClass> cimClasses = null;
143+
try
144+
{
145+
DscClassCache.Initialize();
146+
cimClasses = DscClassCache.ImportClasses(mofFilepath, moduleInfo, errors);
147+
}
148+
catch
149+
{
150+
// todo log the error
151+
}
152+
153+
return cimClasses?
154+
.FirstOrDefault()?
155+
.CimClassProperties?
156+
.Where(p => p.Flags.HasFlag(CimFlags.Key))
157+
.Select(p => p.Name)
158+
.ToArray();
159+
}
160+
161+
private string GetMofFilepath(string filePath)
162+
{
163+
var mofFilePath = Path.GetFileNameWithoutExtension(filePath) + ".schema.mof";
164+
return File.Exists(mofFilePath) ? mofFilePath : null;
165+
}
166+
167+
private Tuple<string, Version> GetModuleInfo(string fileName)
168+
{
169+
var moduleManifest = GetModuleManifest(fileName);
170+
if (moduleManifest == null)
171+
{
172+
return null;
173+
}
174+
175+
var moduleName = moduleManifest.Name;
176+
Token[] tokens;
177+
ParseError[] parseErrors;
178+
var ast = Parser.ParseFile(moduleManifest.FullName, out tokens, out parseErrors);
179+
if ((parseErrors != null && parseErrors.Length > 0) || ast == null)
180+
{
181+
return null;
182+
}
183+
184+
var foundAst = ast.Find(x => x is HashtableAst, false);
185+
if (foundAst == null)
186+
{
187+
return null;
188+
}
189+
190+
var moduleVersionKvp = ((HashtableAst)foundAst).KeyValuePairs.FirstOrDefault(t =>
191+
{
192+
var keyAst = t.Item1 as StringConstantExpressionAst;
193+
return keyAst != null &&
194+
keyAst.Value.Equals("ModuleVersion", StringComparison.OrdinalIgnoreCase);
195+
});
196+
197+
if (moduleVersionKvp == null)
198+
{
199+
return null;
200+
}
201+
202+
Version version;
203+
Version.TryParse(moduleVersionKvp.Item2.Extent.Text, out version);
204+
return version == null ? null : Tuple.Create(moduleName, version);
205+
}
206+
207+
private FileInfo GetModuleManifest(string fileName)
208+
{
209+
var moduleRoot = Directory.GetParent(fileName)?.Parent?.Parent;
210+
if (moduleRoot != null)
211+
{
212+
var files = moduleRoot.GetFiles("*.psd1");
213+
if (files != null && files.Length == 1)
214+
{
215+
return files[0];
216+
}
217+
}
218+
219+
return null;
220+
}
221+
122222
/// <summary>
123223
/// AnalyzeDSCClass: This function returns nothing in the case of dsc class.
124224
/// </summary>

0 commit comments

Comments
 (0)