|
15 | 15 | using System.Linq; |
16 | 16 | using System.Management.Automation.Language; |
17 | 17 | using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 18 | +using Microsoft.PowerShell.DesiredStateConfiguration.Internal; |
| 19 | +using System.IO; |
| 20 | +using Microsoft.Management.Infrastructure; |
18 | 21 | #if !CORECLR |
19 | 22 | using System.ComponentModel.Composition; |
20 | 23 | #endif |
@@ -46,6 +49,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName |
46 | 49 |
|
47 | 50 | var functionDefinitionAsts = Helper.Instance.DscResourceFunctions(ast).Cast<FunctionDefinitionAst>(); |
48 | 51 |
|
| 52 | + var keys = GetKeys(fileName); |
49 | 53 | // Dictionary to keep track of Mandatory parameters and their presence in Get/Test/Set TargetResource cmdlets |
50 | 54 | var mandatoryParameters = new Dictionary<string, List<FunctionDefinitionAst>>(StringComparer.OrdinalIgnoreCase); |
51 | 55 |
|
@@ -119,6 +123,102 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName |
119 | 123 | } |
120 | 124 | } |
121 | 125 |
|
| 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 | + |
122 | 222 | /// <summary> |
123 | 223 | /// AnalyzeDSCClass: This function returns nothing in the case of dsc class. |
124 | 224 | /// </summary> |
|
0 commit comments