From dca1fb754e59e080469240c53c138aba1e5339a6 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Thu, 29 Jun 2017 18:37:49 -0700 Subject: [PATCH 1/8] Remove explicit import of PSScriptAnalyzer module --- Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 | 1 - 1 file changed, 1 deletion(-) 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" From 6f02e615cd2f841190e8b017615d9b416bc4b04f Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Wed, 5 Jul 2017 14:27:45 -0700 Subject: [PATCH 2/8] Add install/uninstall tasks to build script --- .build.ps1 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.build.ps1 b/.build.ps1 index 96ace5991..b84561ee9 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -229,6 +229,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 From 602a70685edf6b15f4ced7af4b201ce245796824 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Wed, 5 Jul 2017 15:18:25 -0700 Subject: [PATCH 3/8] Fix createModule task in build script --- .build.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.build.ps1 b/.build.ps1 index b84561ee9..4fd15daf3 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -173,9 +173,12 @@ 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") { From 3036d40a8279e1518aa98bd660b9cb9744fac1cd Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Wed, 5 Jul 2017 15:19:10 -0700 Subject: [PATCH 4/8] Fix invoking DscClassCache.ImportClasses method In most version of PowerShell the method has 3 parameters but in some it has 4. This commit checks the number of parameters before invoking the method. --- Rules/UseIdenticalMandatoryParametersDSC.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 60172a58b..b61286667 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -19,6 +19,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; @@ -215,7 +216,19 @@ private IDictionary GetKeys(string fileName) isDSCClassCacheInitialized = true; } - cimClasses = DscClassCache.ImportClasses(mofFilepath, moduleInfo, errors); + var importClassesMethod = typeof(DscClassCache).GetMethod("ImportClasses", BindingFlags.Public | BindingFlags.Static); + if (importClassesMethod != null) + { + var parameters = new List(new object[] { mofFilepath, moduleInfo, errors }); + + // In some version of S.M.A ImportClasses classes method takes 4 parameters + // while in others it takes 3. + if (importClassesMethod.GetParameters().Count() == 4) + { + parameters.Add(false); + } + cimClasses = importClassesMethod.Invoke(null, parameters.ToArray()) as List; + } } catch { From d8cc876ae558800d32a3c2f65e27e38abbcf5222 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Wed, 5 Jul 2017 17:24:08 -0700 Subject: [PATCH 5/8] Fix formatting issues with UseIdenticalMandatoryParametersDSC --- Rules/UseIdenticalMandatoryParametersDSC.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index b61286667..59703470f 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -216,7 +216,9 @@ private IDictionary GetKeys(string fileName) isDSCClassCacheInitialized = true; } - var importClassesMethod = typeof(DscClassCache).GetMethod("ImportClasses", BindingFlags.Public | BindingFlags.Static); + var importClassesMethod = typeof(DscClassCache).GetMethod( + "ImportClasses", + BindingFlags.Public | BindingFlags.Static); if (importClassesMethod != null) { var parameters = new List(new object[] { mofFilepath, moduleInfo, errors }); From 3fbf4d97e5de47003a83a0125d6b88fe14b0cbd8 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Fri, 7 Jul 2017 13:35:42 -0700 Subject: [PATCH 6/8] Move method checking to constructor --- Rules/UseIdenticalMandatoryParametersDSC.cs | 52 +++++++++++++++------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 59703470f..9770ba20a 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 @@ -41,6 +42,41 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule private string fileName; private IDictionary propAttrDict; private IEnumerable resourceFunctions; + private Func, Collection, List> dscClassImporter; + + public UseIdenticalMandatoryParametersDSC() + { + var importClassesMethod = typeof(DscClassCache).GetMethod( + "ImportClasses", + BindingFlags.Public | BindingFlags.Static); + if (importClassesMethod != null) + { + // In some version of S.M.A ImportClasses classes 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 @@ -216,21 +252,7 @@ private IDictionary GetKeys(string fileName) isDSCClassCacheInitialized = true; } - var importClassesMethod = typeof(DscClassCache).GetMethod( - "ImportClasses", - BindingFlags.Public | BindingFlags.Static); - if (importClassesMethod != null) - { - var parameters = new List(new object[] { mofFilepath, moduleInfo, errors }); - - // In some version of S.M.A ImportClasses classes method takes 4 parameters - // while in others it takes 3. - if (importClassesMethod.GetParameters().Count() == 4) - { - parameters.Add(false); - } - cimClasses = importClassesMethod.Invoke(null, parameters.ToArray()) as List; - } + cimClasses = dscClassImporter(mofFilepath, moduleInfo, errors); } catch { From 15006c288ebe2c09447339c3a104894a4abd5ec3 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Fri, 7 Jul 2017 13:36:07 -0700 Subject: [PATCH 7/8] Fix createModule task in build script --- .build.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.build.ps1 b/.build.ps1 index 4fd15daf3..dc525f13c 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -178,11 +178,11 @@ task createModule { } 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 + } } } @@ -246,7 +246,7 @@ task uninstall -if {Test-PSSADestModulePath} { Remove-Item -Force -Recurse $pssaDestModulePath } -task install -if {Test-Path $modulePath} uninstall,{ +task install -if {Test-Path $modulePath} uninstall, { Copy-Item ` -Recurse ` -Path $modulePath ` From b489eaf1291dde98f062df4f7aaa5c2736071d0b Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Fri, 7 Jul 2017 13:40:03 -0700 Subject: [PATCH 8/8] Add xml documentation --- Rules/UseIdenticalMandatoryParametersDSC.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 9770ba20a..c4a71f5d7 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -44,6 +44,9 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule private IEnumerable resourceFunctions; private Func, Collection, List> dscClassImporter; + /// + /// Constructs an object of type UseIdenticalMandatoryParametersDSC + /// public UseIdenticalMandatoryParametersDSC() { var importClassesMethod = typeof(DscClassCache).GetMethod( @@ -51,7 +54,7 @@ public UseIdenticalMandatoryParametersDSC() BindingFlags.Public | BindingFlags.Static); if (importClassesMethod != null) { - // In some version of S.M.A ImportClasses classes method takes 4 parameters + // In some version of S.M.A DscClassCache.ImportClasses method takes 4 parameters // while in others it takes 3. if (importClassesMethod.GetParameters().Count() == 4) {