11using System ;
2- using Roslyn . Scripting . CSharp ;
32using System . Collections . Generic ;
43using System . ComponentModel . Composition ;
54using System . IO ;
@@ -24,31 +23,28 @@ public ScriptExecutor(IFileSystem fileSystem, IFilePreProcessor filePreProcessor
2423 }
2524
2625 public ScriptExecutor ( IFileSystem fileSystem , IFilePreProcessor filePreProcessor , IScriptEngine scriptEngine ) :
27- this ( fileSystem , filePreProcessor , scriptEngine , new ScriptHostFactory ( ) )
28- {
29-
30- }
26+ this ( fileSystem , filePreProcessor , scriptEngine , new ScriptHostFactory ( ) ) { }
3127
3228 public void Execute ( string script , IEnumerable < string > paths , IEnumerable < IScriptPack > scriptPacks )
3329 {
34- _scriptEngine . AddReference ( "System" ) ;
35- _scriptEngine . AddReference ( "System.Core" ) ;
36-
3730 var bin = Path . Combine ( _fileSystem . GetWorkingDirectory ( script ) , "bin" ) ;
31+ var files = PrepareBinFolder ( paths , bin ) ;
32+
33+ var references = new List < string > ( ) ;
34+ references . Add ( "System" ) ;
35+ references . Add ( "System.Core" ) ;
36+ references . AddRange ( files ) ;
3837
3938 _scriptEngine . BaseDirectory = bin ;
39+ _scriptEngine . ScriptHostFactory = _scriptHostFactory ;
4040
41- var files = PrepareBinFolder ( paths , bin ) ;
42- var contexts = GetContexts ( scriptPacks ) ;
43- var host = _scriptHostFactory . CreateScriptHost ( contexts ) ;
44- var session = _scriptEngine . CreateSession ( host ) ;
45- AddReferences ( files , session ) ;
46- var scriptPackSession = new ScriptPackSession ( session ) ;
47- InitializeScriptPacks ( scriptPacks , scriptPackSession ) ;
4841 var path = Path . IsPathRooted ( script ) ? script : Path . Combine ( _fileSystem . CurrentDirectory , script ) ;
49- var csx = _filePreProcessor . ProcessFile ( path ) ;
50- session . Execute ( csx ) ;
51- TerminateScriptPacks ( scriptPacks ) ;
42+ var code = _filePreProcessor . ProcessFile ( path ) ;
43+
44+ _scriptEngine . Execute (
45+ code : code ,
46+ references : references ,
47+ scriptPacks : scriptPacks ) ;
5248 }
5349
5450 private IEnumerable < string > PrepareBinFolder ( IEnumerable < string > paths , string bin )
@@ -70,38 +66,5 @@ private IEnumerable<string> PrepareBinFolder(IEnumerable<string> paths, string b
7066
7167 return files ;
7268 }
73-
74- private void AddReferences ( IEnumerable < string > files , ISession session )
75- {
76- foreach ( var file in files )
77- {
78- session . AddReference ( file ) ;
79- }
80- }
81-
82- private IEnumerable < IScriptPackContext > GetContexts ( IEnumerable < IScriptPack > scriptPacks )
83- {
84- foreach ( var pack in scriptPacks )
85- {
86- yield return pack . GetContext ( ) ;
87- }
88- }
89-
90- private void InitializeScriptPacks ( IEnumerable < IScriptPack > scriptPacks , IScriptPackSession session )
91- {
92- foreach ( var pack in scriptPacks )
93- {
94- pack . Initialize ( session ) ;
95- }
96- }
97-
98- private void TerminateScriptPacks ( IEnumerable < IScriptPack > scriptPacks )
99- {
100- foreach ( var pack in scriptPacks )
101- {
102- pack . Terminate ( ) ;
103- }
104- }
105-
10669 }
10770}
0 commit comments