diff --git a/.build.ps1 b/.build.ps1 index 96ace5991..dc525f13c 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -173,13 +173,16 @@ task createModule { elseif ($Configuration -match 'PSv3') { $destinationDirBinaries = "$destinationDir\PSv3" } - } + else { + $destinationDirBinaries = $destinationDir + } - CopyToDestinationDir $itemsToCopyBinaries $destinationDirBinaries + CopyToDestinationDir $itemsToCopyBinaries $destinationDirBinaries - # copy newtonsoft dll if net451 framework - if ($Framework -eq "net451") { - copy-item -path "$solutionDir\Rules\bin\$Configuration\$Framework\Newtonsoft.Json.dll" -Destination $destinationDirBinaries + # copy newtonsoft dll if net451 framework + if ($Framework -eq "net451") { + copy-item -path "$solutionDir\Rules\bin\$Configuration\$Framework\Newtonsoft.Json.dll" -Destination $destinationDirBinaries + } } } @@ -229,6 +232,27 @@ task newSession { Start-Process "powershell" -ArgumentList @('-noexit', "-command import-module $modulePath -verbose") } +$localPSModulePath = $env:PSMODULEPATH -split ";" | Where-Object {$_.StartsWith($HOME)} +$pssaDestModulePath = '' +if ($null -ne $localPSModulePath -and $localPSModulePath.Count -eq 1) { + $pssaDestModulePath = Join-Path $localPSModulePath 'PSScriptAnalyzer' +} + +function Test-PSSADestModulePath { + ($pssaDestModulePath -ne '') -and (Test-Path $pssaDestModulePath) +} + +task uninstall -if {Test-PSSADestModulePath} { + Remove-Item -Force -Recurse $pssaDestModulePath +} + +task install -if {Test-Path $modulePath} uninstall, { + Copy-Item ` + -Recurse ` + -Path $modulePath ` + -Destination $pssaDestModulePath +} + # TODO fix building psv3 task release cleanModule, clean, build, createModule, buildDocs task . build, createModule, newSession diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 60172a58b..c4a71f5d7 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; #if !CORECLR using System.ComponentModel.Composition; #endif @@ -19,6 +20,7 @@ using System.IO; using System.Linq; using System.Management.Automation.Language; +using System.Reflection; using Microsoft.Management.Infrastructure; using Microsoft.PowerShell.DesiredStateConfiguration.Internal; using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions; @@ -40,6 +42,44 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule private string fileName; private IDictionary propAttrDict; private IEnumerable resourceFunctions; + private Func, Collection, List> dscClassImporter; + + /// + /// Constructs an object of type UseIdenticalMandatoryParametersDSC + /// + public UseIdenticalMandatoryParametersDSC() + { + var importClassesMethod = typeof(DscClassCache).GetMethod( + "ImportClasses", + BindingFlags.Public | BindingFlags.Static); + if (importClassesMethod != null) + { + // In some version of S.M.A DscClassCache.ImportClasses method takes 4 parameters + // while in others it takes 3. + if (importClassesMethod.GetParameters().Count() == 4) + { + dscClassImporter = (path, moduleInfo, errors) => + { + return importClassesMethod.Invoke( + null, + new object[] { path, moduleInfo, errors, false }) as List; + }; + } + else + { + dscClassImporter = (path, moduleInfo, errors) => + { + return importClassesMethod.Invoke( + null, + new object[] { path, moduleInfo, errors}) as List; + }; + } + } + else + { + dscClassImporter = (path, moduleInfo, errors) => null; + } + } /// /// AnalyzeDSCResource: Analyzes given DSC Resource @@ -215,7 +255,7 @@ private IDictionary GetKeys(string fileName) isDSCClassCacheInitialized = true; } - cimClasses = DscClassCache.ImportClasses(mofFilepath, moduleInfo, errors); + cimClasses = dscClassImporter(mofFilepath, moduleInfo, errors); } catch { diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 4a12eff16..d815f9467 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -1,4 +1,3 @@ -Import-Module PSScriptAnalyzer $directory = Split-Path -Parent $MyInvocation.MyCommand.Path $ruleName = 'PSDSCUseIdenticalMandatoryParametersForDSC' $resourceBasepath = "$directory\DSCResourceModule\DSCResources"