16
16
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
17
17
using System ;
18
18
using System . Collections . Generic ;
19
+ #if ! CORECLR
19
20
using System . ComponentModel . Composition ;
20
21
using System . ComponentModel . Composition . Hosting ;
22
+ #endif // !CORECLR
21
23
using System . IO ;
22
24
using System . Linq ;
23
25
using System . Management . Automation ;
@@ -38,7 +40,9 @@ public sealed class ScriptAnalyzer
38
40
#region Private members
39
41
40
42
private IOutputWriter outputWriter ;
43
+ #if ! CORECLR
41
44
private CompositionContainer container ;
45
+ #endif // !CORECLR
42
46
Dictionary < string , List < string > > validationResults = new Dictionary < string , List < string > > ( ) ;
43
47
string [ ] includeRule ;
44
48
string [ ] excludeRule ;
@@ -71,11 +75,16 @@ public static ScriptAnalyzer Instance
71
75
}
72
76
}
73
77
74
- #endregion
78
+ #endregion
75
79
76
- #region Properties
80
+ #region Properties
77
81
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
79
88
[ ImportMany ]
80
89
public IEnumerable < IScriptRule > ScriptRules { get ; private set ; }
81
90
@@ -88,6 +97,9 @@ public static ScriptAnalyzer Instance
88
97
[ ImportMany ]
89
98
public IEnumerable < IDSCResourceRule > DSCResourceRules { get ; private set ; }
90
99
100
+ #endif // !CORECLR
101
+ // Initializes via ImportMany
102
+
91
103
internal List < ExternalRule > ExternalRules { get ; set ; }
92
104
93
105
#if ! PSV3
@@ -106,7 +118,7 @@ internal set
106
118
107
119
#endregion
108
120
109
- #region Methods
121
+ #region Methods
110
122
111
123
/// <summary>
112
124
/// Initialize : Initializes default rules, loggers and helper.
@@ -656,6 +668,35 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
656
668
return paths ;
657
669
}
658
670
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
+
659
700
private void LoadRules ( Dictionary < string , List < string > > result , CommandInvocationIntrinsics invokeCommand , bool loadBuiltInRules )
660
701
{
661
702
List < string > paths = new List < string > ( ) ;
@@ -669,6 +710,9 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
669
710
this . TokenRules = null ;
670
711
this . ExternalRules = null ;
671
712
713
+ #if CORECLR
714
+ this . ScriptRules = GetRulesFromDLL ( ) ;
715
+ #else
672
716
// An aggregate catalog that combines multiple catalogs.
673
717
using ( AggregateCatalog catalog = new AggregateCatalog ( ) )
674
718
{
@@ -709,7 +753,7 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
709
753
this . outputWriter . WriteWarning ( compositionException . ToString ( ) ) ;
710
754
}
711
755
}
712
-
756
+ #endif // CORECLR
713
757
if ( ! loadBuiltInRules )
714
758
{
715
759
this . ScriptRules = null ;
0 commit comments