1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Collections . ObjectModel ;
24using System . IO ;
35using System . Linq ;
46using Common . Logging ;
@@ -15,20 +17,90 @@ public class ScriptExecutor : IScriptExecutor
1517 public IFilePreProcessor FilePreProcessor { get ; private set ; }
1618 public IScriptEngine ScriptEngine { get ; private set ; }
1719 public ILog Logger { get ; private set ; }
18- public IEnumerable < string > References { get ; protected set ; }
20+ public Collection < string > References { get ; private set ; }
21+ public Collection < string > Namespaces { get ; private set ; }
1922 public ScriptPackSession ScriptPackSession { get ; protected set ; }
2023
2124 public ScriptExecutor ( IFileSystem fileSystem , IFilePreProcessor filePreProcessor , IScriptEngine scriptEngine , ILog logger )
2225 {
26+ References = new Collection < string > ( ) ;
27+ AddReferences ( DefaultReferences ) ;
28+ Namespaces = new Collection < string > ( ) ;
29+ AddNamespaces ( DefaultNamespaces ) ;
2330 FileSystem = fileSystem ;
2431 FilePreProcessor = filePreProcessor ;
2532 ScriptEngine = scriptEngine ;
2633 Logger = logger ;
2734 }
2835
36+ public void AddNamespaces ( IEnumerable < string > namespaces )
37+ {
38+ Guard . AgainstNullArgument ( "namespaces" , namespaces ) ;
39+
40+ foreach ( var @namespace in namespaces )
41+ {
42+ AddNamespace ( @namespace ) ;
43+ }
44+ }
45+
46+ public void AddNamespace ( string @namespace )
47+ {
48+ Guard . AgainstNullArgument ( "namespace" , @namespace ) ;
49+
50+ Namespaces . Add ( @namespace ) ;
51+ }
52+
53+ public void AddNamespaceByType ( Type typeFromReferencedAssembly )
54+ {
55+ Guard . AgainstNullArgument ( "typeFromReferencedAssembly" , typeFromReferencedAssembly ) ;
56+
57+ AddNamespace ( typeFromReferencedAssembly . Namespace ) ;
58+ }
59+
60+ public void AddNamespaceByType < T > ( )
61+ {
62+ AddNamespaceByType ( typeof ( T ) ) ;
63+ }
64+
65+ public void AddReferences ( IEnumerable < string > paths )
66+ {
67+ Guard . AgainstNullArgument ( "paths" , paths ) ;
68+
69+ foreach ( var path in paths )
70+ {
71+ AddReference ( path ) ;
72+ }
73+ }
74+
75+ public void AddReferenceByType ( Type typeFromReferencedAssembly )
76+ {
77+ Guard . AgainstNullArgument ( "typeFromReferencedAssembly" , typeFromReferencedAssembly ) ;
78+
79+ AddReference ( typeFromReferencedAssembly . Assembly . Location ) ;
80+ }
81+
82+ public void AddReferenceByType < T > ( )
83+ {
84+ AddReferenceByType ( typeof ( T ) ) ;
85+ }
86+
87+ public void AddReference ( string path )
88+ {
89+ Guard . AgainstNullArgument ( "path" , path ) ;
90+
91+ References . Add ( path ) ;
92+ }
93+
94+ public void RemoveReference ( string path )
95+ {
96+ Guard . AgainstNullArgument ( "path" , path ) ;
97+
98+ References . Remove ( path ) ;
99+ }
100+
29101 public virtual void Initialize ( IEnumerable < string > paths , IEnumerable < IScriptPack > scriptPacks )
30102 {
31- References = DefaultReferences . Union ( paths ) ;
103+ AddReferences ( paths ) ;
32104 var bin = Path . Combine ( FileSystem . CurrentDirectory , "bin" ) ;
33105
34106 ScriptEngine . BaseDirectory = bin ;
@@ -56,9 +128,10 @@ public virtual ScriptResult Execute(string script, string[] scriptArgs)
56128 var path = Path . IsPathRooted ( script ) ? script : Path . Combine ( FileSystem . CurrentDirectory , script ) ;
57129 var result = FilePreProcessor . ProcessFile ( path ) ;
58130 var references = References . Union ( result . References ) ;
131+ var namespaces = Namespaces . Union ( result . UsingStatements ) ;
59132
60133 Logger . Debug ( "Starting execution in engine" ) ;
61- return ScriptEngine . Execute ( result . Code , scriptArgs , references , DefaultNamespaces , ScriptPackSession ) ;
134+ return ScriptEngine . Execute ( result . Code , scriptArgs , references , namespaces , ScriptPackSession ) ;
62135 }
63136 }
64137}
0 commit comments