Skip to content

Commit 447eca6

Browse files
author
Kapil Borle
authored
Merge pull request #801 from PowerShell/kapilmb/fix-dsc-rule
Fix importing DSC classes in UseIdenticalMandatorParametersForDSC rule
2 parents 633a950 + b489eaf commit 447eca6

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

.build.ps1

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ task createModule {
173173
elseif ($Configuration -match 'PSv3') {
174174
$destinationDirBinaries = "$destinationDir\PSv3"
175175
}
176-
}
176+
else {
177+
$destinationDirBinaries = $destinationDir
178+
}
177179

178-
CopyToDestinationDir $itemsToCopyBinaries $destinationDirBinaries
180+
CopyToDestinationDir $itemsToCopyBinaries $destinationDirBinaries
179181

180-
# copy newtonsoft dll if net451 framework
181-
if ($Framework -eq "net451") {
182-
copy-item -path "$solutionDir\Rules\bin\$Configuration\$Framework\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
182+
# copy newtonsoft dll if net451 framework
183+
if ($Framework -eq "net451") {
184+
copy-item -path "$solutionDir\Rules\bin\$Configuration\$Framework\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
185+
}
183186
}
184187
}
185188

@@ -229,6 +232,27 @@ task newSession {
229232
Start-Process "powershell" -ArgumentList @('-noexit', "-command import-module $modulePath -verbose")
230233
}
231234

235+
$localPSModulePath = $env:PSMODULEPATH -split ";" | Where-Object {$_.StartsWith($HOME)}
236+
$pssaDestModulePath = ''
237+
if ($null -ne $localPSModulePath -and $localPSModulePath.Count -eq 1) {
238+
$pssaDestModulePath = Join-Path $localPSModulePath 'PSScriptAnalyzer'
239+
}
240+
241+
function Test-PSSADestModulePath {
242+
($pssaDestModulePath -ne '') -and (Test-Path $pssaDestModulePath)
243+
}
244+
245+
task uninstall -if {Test-PSSADestModulePath} {
246+
Remove-Item -Force -Recurse $pssaDestModulePath
247+
}
248+
249+
task install -if {Test-Path $modulePath} uninstall, {
250+
Copy-Item `
251+
-Recurse `
252+
-Path $modulePath `
253+
-Destination $pssaDestModulePath
254+
}
255+
232256
# TODO fix building psv3
233257
task release cleanModule, clean, build, createModule, buildDocs
234258
task . build, createModule, newSession

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Collections.ObjectModel;
1516
#if !CORECLR
1617
using System.ComponentModel.Composition;
1718
#endif
1819
using System.Globalization;
1920
using System.IO;
2021
using System.Linq;
2122
using System.Management.Automation.Language;
23+
using System.Reflection;
2224
using Microsoft.Management.Infrastructure;
2325
using Microsoft.PowerShell.DesiredStateConfiguration.Internal;
2426
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
@@ -40,6 +42,44 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
4042
private string fileName;
4143
private IDictionary<string, string> propAttrDict;
4244
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
45+
private Func<string, Tuple<string, Version>, Collection<Exception>, List<CimClass>> dscClassImporter;
46+
47+
/// <summary>
48+
/// Constructs an object of type UseIdenticalMandatoryParametersDSC
49+
/// </summary>
50+
public UseIdenticalMandatoryParametersDSC()
51+
{
52+
var importClassesMethod = typeof(DscClassCache).GetMethod(
53+
"ImportClasses",
54+
BindingFlags.Public | BindingFlags.Static);
55+
if (importClassesMethod != null)
56+
{
57+
// In some version of S.M.A DscClassCache.ImportClasses method takes 4 parameters
58+
// while in others it takes 3.
59+
if (importClassesMethod.GetParameters().Count() == 4)
60+
{
61+
dscClassImporter = (path, moduleInfo, errors) =>
62+
{
63+
return importClassesMethod.Invoke(
64+
null,
65+
new object[] { path, moduleInfo, errors, false }) as List<CimClass>;
66+
};
67+
}
68+
else
69+
{
70+
dscClassImporter = (path, moduleInfo, errors) =>
71+
{
72+
return importClassesMethod.Invoke(
73+
null,
74+
new object[] { path, moduleInfo, errors}) as List<CimClass>;
75+
};
76+
}
77+
}
78+
else
79+
{
80+
dscClassImporter = (path, moduleInfo, errors) => null;
81+
}
82+
}
4383

4484
/// <summary>
4585
/// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -215,7 +255,7 @@ private IDictionary<string, string> GetKeys(string fileName)
215255
isDSCClassCacheInitialized = true;
216256
}
217257

218-
cimClasses = DscClassCache.ImportClasses(mofFilepath, moduleInfo, errors);
258+
cimClasses = dscClassImporter(mofFilepath, moduleInfo, errors);
219259
}
220260
catch
221261
{

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Import-Module PSScriptAnalyzer
21
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
32
$ruleName = 'PSDSCUseIdenticalMandatoryParametersForDSC'
43
$resourceBasepath = "$directory\DSCResourceModule\DSCResources"

0 commit comments

Comments
 (0)