GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — scriptcs/src/ScriptCs.Core/ScriptExecutor.cs at dev · scriptcs/scriptcs Expand file tree/ Copy pathScriptExecutor.csMore file actions 264 lines (218 loc) · 9.71 KB / Copy pathScriptExecutor.csFile metadata and controls264 lines (218 loc) · 9.71 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Reflection; using ScriptCs.Contracts; namespace ScriptCs { public class ScriptExecutor : IScriptExecutor { private readonly ILog _log; public static readonly string[] DefaultReferences = { "System", "System.Core", "System.Data", "System.Data.DataSetExtensions", "System.Xml", "System.Xml.Linq", "System.Net.Http", "Microsoft.CSharp", typeof(ScriptExecutor).Assembly.Location, typeof(IScriptEnvironment).Assembly.Location }; public static readonly string[] DefaultNamespaces = { "System", "System.Collections.Generic", "System.Linq", "System.Text", "System.Threading.Tasks", "System.IO", "System.Net.Http", "System.Dynamic" }; private const string ScriptLibrariesInjected = "ScriptLibrariesInjected"; public IFileSystem FileSystem { get; private set; } public IFilePreProcessor FilePreProcessor { get; private set; } public IScriptEngine ScriptEngine { get; private set; } public AssemblyReferences References { get; private set; } public IReadOnlyCollection<string> Namespaces { get; private set; } public ScriptPackSession ScriptPackSession { get; protected set; } public IScriptLibraryComposer ScriptLibraryComposer { get; protected set; } public IScriptInfo ScriptInfo { get; protected set; } public ScriptExecutor( IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILogProvider logProvider, IScriptInfo scriptInfo) : this(fileSystem, filePreProcessor, scriptEngine, logProvider, new NullScriptLibraryComposer(), scriptInfo) { } public ScriptExecutor( IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILogProvider logProvider, IScriptLibraryComposer composer, IScriptInfo scriptInfo) { Guard.AgainstNullArgument("fileSystem", fileSystem); Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder); Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder); Guard.AgainstNullArgument("filePreProcessor", filePreProcessor); Guard.AgainstNullArgument("scriptEngine", scriptEngine); Guard.AgainstNullArgument("logProvider", logProvider); Guard.AgainstNullArgument("composer", composer); Guard.AgainstNullArgument("scriptInfo", scriptInfo); References = new AssemblyReferences(new[] { GetAssemblyFromName("System.Runtime") }, DefaultReferences); Namespaces = new ReadOnlyCollection<string>(DefaultNamespaces); FileSystem = fileSystem; FilePreProcessor = filePreProcessor; ScriptEngine = scriptEngine; _log = logProvider.ForCurrentType(); ScriptLibraryComposer = composer; ScriptInfo = scriptInfo; } public virtual void ImportNamespaces(params string[] namespaces) { Guard.AgainstNullArgument("namespaces", namespaces); Namespaces = new ReadOnlyCollection<string>(Namespaces.Union(namespaces).ToArray()); } public virtual void RemoveNamespaces(params string[] namespaces) { Guard.AgainstNullArgument("namespaces", namespaces); Namespaces = new ReadOnlyCollection<string>(Namespaces.Except(namespaces).ToArray()); } public virtual void AddReferences(params Assembly[] assemblies) { Guard.AgainstNullArgument("assemblies", assemblies); References = References.Union(assemblies); } public virtual void RemoveReferences(params Assembly[] assemblies) { Guard.AgainstNullArgument("assemblies", assemblies); References = References.Except(assemblies); } public virtual void AddReferences(params string[] paths) { Guard.AgainstNullArgument("paths", paths); References = References.Union(paths); } public virtual void RemoveReferences(params string[] paths) { Guard.AgainstNullArgument("paths", paths); References = References.Except(paths); } public virtual void Initialize( IEnumerable<string> paths, IEnumerable<IScriptPack> scriptPacks, params string[] scriptArgs) { AddReferences(paths.ToArray()); var bin = Path.Combine(FileSystem.CurrentDirectory, FileSystem.BinFolder); var cache = Path.Combine(FileSystem.CurrentDirectory, FileSystem.DllCacheFolder); ScriptEngine.BaseDirectory = bin; ScriptEngine.CacheDirectory = cache; _log.Debug("Initializing script packs"); var scriptPackSession = new ScriptPackSession(scriptPacks, scriptArgs); ScriptPackSession = scriptPackSession; scriptPackSession.InitializePacks(); } public virtual void Reset() { References = new AssemblyReferences(DefaultReferences); Namespaces = new ReadOnlyCollection<string>(DefaultNamespaces); ScriptPackSession.State.Clear(); } public virtual void Terminate() { _log.Debug("Terminating packs"); ScriptPackSession.TerminatePacks(); } public virtual ScriptResult Execute(string script, params string[] scriptArgs) { var path = Path.IsPathRooted(script) ? script : Path.Combine(FileSystem.CurrentDirectory, script); var result = FilePreProcessor.ProcessFile(path); ScriptEngine.FileName = Path.GetFileName(path); ScriptInfo.ScriptPath = Path.GetFullPath(path); return EngineExecute(Path.GetDirectoryName(path), scriptArgs, result); } public virtual ScriptResult ExecuteScript(string script, params string[] scriptArgs) { var result = FilePreProcessor.ProcessScript(script); return EngineExecute(FileSystem.CurrentDirectory, scriptArgs, result); } protected internal virtual ScriptResult EngineExecute( string workingDirectory, string[] scriptArgs, FilePreProcessorResult result ) { InjectScriptLibraries(workingDirectory, result, ScriptPackSession.State); var namespaces = Namespaces.Union(result.Namespaces); var references = References.Union(result.References); ScriptInfo.ScriptPath = result.ScriptPath; foreach (var loadedScript in result.LoadedScripts) { ScriptInfo.LoadedScripts.Add(loadedScript); } _log.Debug("Starting execution in engine"); return ScriptEngine.Execute(result.Code, scriptArgs, references, namespaces, ScriptPackSession); } protected internal virtual void InjectScriptLibraries( string workingDirectory, FilePreProcessorResult result, IDictionary<string, object> state ) { Guard.AgainstNullArgument("result", result); Guard.AgainstNullArgument("state", state); if (state.ContainsKey(ScriptLibrariesInjected)) { return; } var scriptLibrariesPreProcessorResult = LoadScriptLibraries(workingDirectory); // script libraries should be injected just before the #line directive // if there is no #line directive, they can be injected at the beginning of code if (result.Code == null) result.Code = string.Empty; var safeInsertIndex = result.Code.IndexOf("#line"); if (safeInsertIndex < 0) safeInsertIndex = 0; if (scriptLibrariesPreProcessorResult != null) { result.Code = result.Code.Insert(safeInsertIndex, scriptLibrariesPreProcessorResult.Code + Environment.NewLine + "Env.Initialize();" + Environment.NewLine); result.References.AddRange(scriptLibrariesPreProcessorResult.References); result.Namespaces.AddRange(scriptLibrariesPreProcessorResult.Namespaces); } else { result.Code = result.Code.Insert(safeInsertIndex, "Env.Initialize();" + Environment.NewLine); } state.Add(ScriptLibrariesInjected, null); } protected internal virtual FilePreProcessorResult LoadScriptLibraries(string workingDirectory) { if (string.IsNullOrWhiteSpace(ScriptLibraryComposer.ScriptLibrariesFile)) { return null; } var scriptLibrariesPath = Path.Combine(workingDirectory, FileSystem.PackagesFolder, ScriptLibraryComposer.ScriptLibrariesFile); if (FileSystem.FileExists(scriptLibrariesPath)) { _log.DebugFormat("Found Script Library at {0}", scriptLibrariesPath); return FilePreProcessor.ProcessFile(scriptLibrariesPath); } return null; } private static Assembly GetAssemblyFromName(string assemblyName) { try { return Assembly.Load(new AssemblyName(assemblyName)); } catch { return null; } } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |