Skip to content

Commit 431314e

Browse files
author
Kapil Borle
committed
Load builtin rules on Core CLR
1 parent 378159d commit 431314e

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,28 +669,57 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
669669
}
670670

671671

672-
private IEnumerable<IScriptRule> GetRulesFromDLL()
672+
private IEnumerable<T> GetRulesFromDLL<T>() where T : class, IRule
673673
{
674674
string dirName = Path.GetDirectoryName(typeof(ScriptAnalyzer).GetTypeInfo().Assembly.Location);
675675
var dllPaths = Directory.EnumerateFiles(dirName, "*.dll", SearchOption.TopDirectoryOnly);
676-
var rules = new List<IScriptRule>();
676+
var rules = new List<T>();
677677
foreach (var dllPath in dllPaths)
678678
{
679-
var rulesFromOneFile = GetRulesFromDLL(dllPath);
679+
outputWriter.WriteVerbose(string.Format("Found Assembly: {0}", dllPath));
680+
var rulesFromOneFile = GetRulesFromDLL<T>(dllPath);
680681
rules.AddRange(rulesFromOneFile);
681682
}
682683
return rules;
683684
}
684685

685-
private IEnumerable<IScriptRule> GetRulesFromDLL(string ruleDllPath)
686+
private IEnumerable<T> GetRulesFromDLL<T>(string ruleDllPath) where T : class, IRule
686687
{
687-
var dll = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(ruleDllPath)));
688-
var rules = new List<IScriptRule>();
688+
var fileName = Path.GetFileNameWithoutExtension(ruleDllPath);
689+
var assName = new AssemblyName(fileName);
690+
outputWriter.WriteVerbose(string.Format("Loading Assembly:{0}", assName.FullName));
691+
692+
var dll = Assembly.Load(assName);
693+
var rules = new List<T>();
694+
if (dll == null)
695+
{
696+
outputWriter.WriteVerbose(string.Format("Cannot load {0}", ruleDllPath));
697+
return rules;
698+
}
689699
foreach (var type in dll.ExportedTypes)
690700
{
691-
if (type == typeof(IScriptRule))
701+
var typeInfo = type.GetTypeInfo();
702+
if (!typeInfo.IsInterface
703+
&& !typeInfo.IsAbstract
704+
&& typeInfo.ImplementedInterfaces.Contains(typeof(T)))
692705
{
693-
IScriptRule rule = Activator.CreateInstance(type) as IScriptRule;
706+
outputWriter.WriteVerbose(
707+
string.Format(
708+
"Creating Instance of {0}", type.Name));
709+
710+
var ruleObj = Activator.CreateInstance(type);
711+
outputWriter.WriteVerbose(
712+
string.Format(
713+
"Created Instance of {0}", type.Name));
714+
715+
T rule = ruleObj as T;
716+
if (rule == null)
717+
{
718+
outputWriter.WriteVerbose(
719+
string.Format(
720+
"Cannot cast instance of type {0} to {1}", type.Name, typeof(T).GetTypeInfo().Name));
721+
continue;
722+
}
694723
rules.Add(rule);
695724
}
696725
}
@@ -711,7 +740,9 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
711740
this.ExternalRules = null;
712741

713742
#if CORECLR
714-
this.ScriptRules = GetRulesFromDLL();
743+
this.ScriptRules = GetRulesFromDLL<IScriptRule>();
744+
this.TokenRules = GetRulesFromDLL<ITokenRule>();
745+
this.DSCResourceRules = GetRulesFromDLL<IDSCResourceRule>();
715746
#else
716747
// An aggregate catalog that combines multiple catalogs.
717748
using (AggregateCatalog catalog = new AggregateCatalog())

buildCoreClr.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
param(
2+
[switch]$build,
3+
[switch]$uninstall,
4+
[switch]$install
5+
)
6+
7+
$solutionDir = "C:\Users\kborle\Source\Repos\PSScriptAnalyzer"
8+
9+
10+
$itemsToCopy = @("$solutionDir\Engine\bin\debug\netcoreapp1.0\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
11+
"$solutionDir\Rules\bin\debug\netcoreapp1.0\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll",
12+
"$solutionDir\Engine\PSScriptAnalyzer.psd1",
13+
"$solutionDir\Engine\PSScriptAnalyzer.psm1",
14+
"$solutionDir\Engine\ScriptAnalyzer.format.ps1xml",
15+
"$solutionDir\Engine\ScriptAnalyzer.types.ps1xml")
16+
17+
$destinationDir = "$solutionDir/out/coreclr/PSScriptAnalyzer"
18+
19+
if ($build)
20+
{
21+
Push-Location Engine\
22+
dotnet build
23+
Pop-Location
24+
25+
Push-Location Rules\
26+
dotnet build
27+
Pop-Location
28+
29+
if (-not (Test-Path $destinationDir))
30+
{
31+
New-Item -ItemType Directory $destinationDir -Force
32+
}
33+
else
34+
{
35+
Remove-Item "$destinationDir\*" -Recurse
36+
}
37+
38+
foreach ($file in $itemsToCopy)
39+
{
40+
Copy-Item -Path $file -Destination (Join-Path $destinationDir (Split-Path $file -Leaf)) -Verbose
41+
}
42+
(Get-Content "$solutionDir\Engine\PSScriptAnalyzer.psd1") -replace "ModuleVersion = '1.6.0'","ModuleVersion = '0.0.1'" | Out-File "$solutionDir\Engine\PSScriptAnalyzer.psd1"
43+
}
44+
45+
$modulePath = "C:\Users\kborle\Documents\WindowsPowerShell\Modules";
46+
$pssaModulePath = Join-Path $modulePath PSScriptAnalyzer
47+
48+
49+
if ($uninstall)
50+
{
51+
if ((Test-Path $pssaModulePath))
52+
{
53+
Remove-Item -Recurse $pssaModulePath -Verbose
54+
}
55+
56+
}
57+
58+
if ($install)
59+
{
60+
Copy-Item -Recurse -Path "$destinationDir" -Destination "$modulePath\." -Verbose -Force
61+
}
62+

0 commit comments

Comments
 (0)