Skip to content

Commit abb1e06

Browse files
author
Kapil Borle
committed
Compile engine against CoreCLR binaries
1 parent 3512e6b commit abb1e06

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

Engine/Loggers/WriteObjectsLogger.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using System;
1414
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1515
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands;
16-
using System.ComponentModel.Composition;
16+
//using System.ComponentModel.Composition;
1717
using System.Globalization;
1818
using System.Reflection;
1919
using System.Resources;
@@ -24,14 +24,21 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Loggers
2424
/// <summary>
2525
/// WriteObjectsLogger: Logs Diagnostics though WriteObject.
2626
/// </summary>
27-
[Export(typeof(ILogger))]
27+
// [Export(typeof(ILogger))]
2828
public class WriteObjectsLogger : ILogger
2929
{
3030
#region Private members
3131

32+
#if CORECLR
33+
private CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
34+
private ResourceManager rm = new ResourceManager(
35+
"Microsoft.Windows.PowerShell.ScriptAnalyzer.Strings",
36+
typeof(WriteObjectsLogger).GetTypeInfo().Assembly);
37+
#else
3238
private CultureInfo cul = Thread.CurrentThread.CurrentCulture;
3339
private ResourceManager rm = new ResourceManager("Microsoft.Windows.PowerShell.ScriptAnalyzer.Strings",
3440
Assembly.GetExecutingAssembly());
41+
#endif
3542

3643
#endregion
3744

Engine/ScriptAnalyzer.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1717
using System;
1818
using System.Collections.Generic;
19+
#if !CORECLR
1920
using System.ComponentModel.Composition;
2021
using System.ComponentModel.Composition.Hosting;
22+
#endif // !CORECLR
2123
using System.IO;
2224
using System.Linq;
2325
using System.Management.Automation;
@@ -38,7 +40,9 @@ public sealed class ScriptAnalyzer
3840
#region Private members
3941

4042
private IOutputWriter outputWriter;
43+
#if !CORECLR
4144
private CompositionContainer container;
45+
#endif // !CORECLR
4246
Dictionary<string, List<string>> validationResults = new Dictionary<string, List<string>>();
4347
string[] includeRule;
4448
string[] excludeRule;
@@ -71,11 +75,16 @@ public static ScriptAnalyzer Instance
7175
}
7276
}
7377

74-
#endregion
78+
#endregion
7579

76-
#region Properties
80+
#region Properties
7781

78-
// Initializes via ImportMany
82+
#if CORECLR
83+
public IEnumerable<IScriptRule> ScriptRules { get; private set; }
84+
public IEnumerable<ITokenRule> TokenRules { get; private set; }
85+
public IEnumerable<ILogger> Loggers { get; private set; }
86+
public IEnumerable<IDSCResourceRule> DSCResourceRules { get; private set; }
87+
#else
7988
[ImportMany]
8089
public IEnumerable<IScriptRule> ScriptRules { get; private set; }
8190

@@ -88,6 +97,9 @@ public static ScriptAnalyzer Instance
8897
[ImportMany]
8998
public IEnumerable<IDSCResourceRule> DSCResourceRules { get; private set; }
9099

100+
#endif // !CORECLR
101+
// Initializes via ImportMany
102+
91103
internal List<ExternalRule> ExternalRules { get; set; }
92104

93105
#if !PSV3
@@ -106,7 +118,7 @@ internal set
106118

107119
#endregion
108120

109-
#region Methods
121+
#region Methods
110122

111123
/// <summary>
112124
/// Initialize : Initializes default rules, loggers and helper.
@@ -656,6 +668,35 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
656668
return paths;
657669
}
658670

671+
672+
private IEnumerable<IScriptRule> GetRulesFromDLL()
673+
{
674+
string dirName = Path.GetDirectoryName(typeof(ScriptAnalyzer).GetTypeInfo().Assembly.Location);
675+
var dllPaths = Directory.EnumerateFiles(dirName, "*.dll", SearchOption.TopDirectoryOnly);
676+
var rules = new List<IScriptRule>();
677+
foreach (var dllPath in dllPaths)
678+
{
679+
var rulesFromOneFile = GetRulesFromDLL(dllPath);
680+
rules.AddRange(rulesFromOneFile);
681+
}
682+
return rules;
683+
}
684+
685+
private IEnumerable<IScriptRule> GetRulesFromDLL(string ruleDllPath)
686+
{
687+
var dll = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(ruleDllPath)));
688+
var rules = new List<IScriptRule>();
689+
foreach (var type in dll.ExportedTypes)
690+
{
691+
if (type == typeof(IScriptRule))
692+
{
693+
IScriptRule rule = Activator.CreateInstance(type) as IScriptRule;
694+
rules.Add(rule);
695+
}
696+
}
697+
return rules;
698+
}
699+
659700
private void LoadRules(Dictionary<string, List<string>> result, CommandInvocationIntrinsics invokeCommand, bool loadBuiltInRules)
660701
{
661702
List<string> paths = new List<string>();
@@ -669,6 +710,9 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
669710
this.TokenRules = null;
670711
this.ExternalRules = null;
671712

713+
#if CORECLR
714+
this.ScriptRules = GetRulesFromDLL();
715+
#else
672716
// An aggregate catalog that combines multiple catalogs.
673717
using (AggregateCatalog catalog = new AggregateCatalog())
674718
{
@@ -709,7 +753,7 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
709753
this.outputWriter.WriteWarning(compositionException.ToString());
710754
}
711755
}
712-
756+
#endif // CORECLR
713757
if (!loadBuiltInRules)
714758
{
715759
this.ScriptRules = null;

Engine/ScriptAnalyzerEngine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<Compile Include="Helper.cs" />
107107
<Compile Include="IOutputWriter.cs" />
108108
<Compile Include="Loggers\WriteObjectsLogger.cs" />
109-
<Compile Include="SafeDirectoryCatalog.cs" />
109+
<Compile Include="SafeDirectoryCatalog.cs" Condition="'$(Configuration)|$(Platform)' != 'CoreCLR Debug|AnyCPU'"/>
110110
<Compile Include="ScriptAnalyzer.cs" />
111111
<Compile Include="SpecialVars.cs" />
112112
<Compile Include="Strings.Designer.cs">

PSScriptAnalyzer.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.23107.0
4+
VisualStudioVersion = 14.0.25123.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScriptAnalyzerEngine", "Engine\ScriptAnalyzerEngine.csproj", "{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScriptAnalyzerBuiltinRules", "Rules\ScriptAnalyzerBuiltinRules.csproj", "{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
CoreCLR Debug|Any CPU = CoreCLR Debug|Any CPU
1213
Debug|Any CPU = Debug|Any CPU
1314
PSV3 Debug|Any CPU = PSV3 Debug|Any CPU
1415
PSV3 Release|Any CPU = PSV3 Release|Any CPU
1516
Release|Any CPU = Release|Any CPU
16-
CoreCLR Debug|Any CPU = CoreCLR Debug|Any CPU
1717
EndGlobalSection
1818
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.CoreCLR Debug|Any CPU.ActiveCfg = CoreCLR Debug|Any CPU
20+
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.CoreCLR Debug|Any CPU.Build.0 = CoreCLR Debug|Any CPU
1921
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2022
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Debug|Any CPU.Build.0 = Debug|Any CPU
2123
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.PSV3 Debug|Any CPU.ActiveCfg = PSV3 Debug|Any CPU
@@ -24,8 +26,8 @@ Global
2426
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.PSV3 Release|Any CPU.Build.0 = PSV3 Release|Any CPU
2527
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Release|Any CPU.ActiveCfg = Release|Any CPU
2628
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.Release|Any CPU.Build.0 = Release|Any CPU
27-
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.CoreCLR Debug|Any CPU.ActiveCfg = CoreCLR Debug|Any CPU
28-
{F4BDE3D0-3EEF-4157-8A3E-722DF7ADEF60}.CoreCLR Debug|Any CPU.Build.0 = CoreCLR Debug|Any CPU
29+
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.CoreCLR Debug|Any CPU.ActiveCfg = CoreCLR Debug|Any CPU
30+
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.CoreCLR Debug|Any CPU.Build.0 = CoreCLR Debug|Any CPU
2931
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3032
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
3133
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Debug|Any CPU.ActiveCfg = PSV3 Debug|Any CPU
@@ -34,8 +36,6 @@ Global
3436
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.PSV3 Release|Any CPU.Build.0 = PSV3 Release|Any CPU
3537
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
3638
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.Release|Any CPU.Build.0 = Release|Any CPU
37-
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.CoreCLR Debug|Any CPU.ActiveCfg = CoreCLR Debug|Any CPU
38-
{C33B6B9D-E61C-45A3-9103-895FD82A5C1E}.CoreCLR Debug|Any CPU.Build.0 = CoreCLR Debug|Any CPU
3939
EndGlobalSection
4040
GlobalSection(SolutionProperties) = preSolution
4141
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)