1616using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
1717using System ;
1818using System . Collections . Generic ;
19+ #if ! CORECLR
1920using System . ComponentModel . Composition ;
2021using System . ComponentModel . Composition . Hosting ;
22+ #endif // !CORECLR
2123using System . IO ;
2224using System . Linq ;
2325using 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 ;
0 commit comments