public IReplExecutor Create(CShell.Framework.Services.IRepl repl, string workspaceDirectory) { if (scriptServices == null) { scriptServices = builder.Build(repl); } scriptServices.FileSystem.CurrentDirectory = workspaceDirectory; scriptServices.InstallationProvider.Initialize(); var replExecutor = new ReplExecutor( repl, scriptServices.ObjectSerializer, scriptServices.FileSystem, scriptServices.FilePreProcessor, scriptServices.Engine, scriptServices.PackageInstaller, scriptServices.PackageAssemblyResolver, scriptServices.Logger); var assemblies = scriptServices.AssemblyResolver.GetAssemblyPaths(scriptServices.FileSystem.CurrentDirectory); var scriptPacks = scriptServices.ScriptPackResolver.GetPacks(); replExecutor.Initialize(assemblies, scriptPacks); repl.Initialize(replExecutor); return replExecutor; }
public ScriptCsHost() { var logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); var scriptServicesBuilder = new ScriptServicesBuilder(new ScriptConsole(), logger).LogLevel(LogLevel.Info) .Cache(false) .Repl(false) .ScriptEngine<RoslynScriptEngine>(); Root = scriptServicesBuilder.Build(); }
private void InitRepl() { var servicesBuilder = new ReplScriptServicesBuilder(new ScriptConsole(), new Common.Logging.Simple.NoOpLogger()).Repl(true); servicesBuilder.ScriptName("F14N.REPL"); this.scriptServices = servicesBuilder.Build(); this.scriptServices.Executor.Initialize(new string[] { }, this.scriptServices.ScriptPackResolver.GetPacks()); this.scriptServices.Executor.AddReferenceAndImportNamespaces(new[] { typeof(FluentAutomation.FluentTest), typeof(FluentAutomation.SeleniumWebDriver) }); this.repl = this.scriptServices.Executor as Repl; this.settingsManager = new SettingsManager((scriptString) => { return this.repl.Execute(scriptString); }); }
public void Initialize() { var console = new ScriptConsole(); var scriptServicesBuilder = new ScriptServicesBuilder(console, _logger); scriptServicesBuilder.LoadModules("csx", new string[0]); _scriptServiceRoot = scriptServicesBuilder.Build(); _scriptServiceRoot.Executor.AddReferences(ScriptExecutor.DefaultReferences.ToArray()); _scriptServiceRoot.Executor.ImportNamespaces(ScriptExecutor.DefaultNamespaces.Concat(new[] { "MMBot", "Newtonsoft.Json", "Newtonsoft.Json.Linq" }).ToArray()); _scriptServiceRoot.Executor.AddReference<Robot>(); _scriptServiceRoot.Executor.AddReference<JArray>(); _scriptServiceRoot.Executor.AddReference<HttpResponseMessage>(); _scriptServiceRoot.Executor.AddReference<IScriptPackContext>(); _scriptServiceRoot.Executor.Initialize(new string[0], new IScriptPack[] { new MMBot2ScriptPackInternal(_robot), }); }
public void Initalize(IConfiguration configuration) { _logger.LogInformation($"Detecting CSX files in '{_env.Path}'."); var allCsxFiles = Directory.GetFiles(_env.Path, "*.csx", SearchOption.TopDirectoryOnly); if (allCsxFiles.Length == 0) { _logger.LogInformation("Could not find any CSX files"); return; } _scriptCsContext.Path = _env.Path; _logger.LogInformation($"Found {allCsxFiles.Length} CSX files."); //script name is added here as a fake one (dir path not even a real file); this is OK though -> it forces MEF initialization var scriptServicesBuilder = new ScriptServicesBuilder(new ScriptConsole(), LogManager.GetCurrentClassLogger()). LogLevel(LogLevel.Info).Cache(false).Repl(false).ScriptName(_env.Path).ScriptEngine<NullScriptEngine>(); _scriptServices = scriptServicesBuilder.Build(); var mscorlib = MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location); var systemCore = MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location); var scriptcsContracts = MetadataReference.CreateFromFile(typeof(IScriptHost).Assembly.Location); var parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6, DocumentationMode.Parse, SourceCodeKind.Script); var scriptPacks = _scriptServices.ScriptPackResolver.GetPacks().ToList(); var assemblyPaths = _scriptServices.AssemblyResolver.GetAssemblyPaths(_env.Path); foreach (var csxPath in allCsxFiles) { try { _scriptCsContext.CsxFiles.Add(csxPath); var processResult = _scriptServices.FilePreProcessor.ProcessFile(csxPath); var references = new List<MetadataReference> { mscorlib, systemCore, scriptcsContracts }; var usings = new List<string>(ScriptExecutor.DefaultNamespaces); //default references ImportReferences(references, ScriptExecutor.DefaultReferences); //file usings usings.AddRange(processResult.Namespaces); //#r references ImportReferences(references, processResult.References); //nuget references ImportReferences(references, assemblyPaths); //script packs if (scriptPacks != null && scriptPacks.Any()) { var scriptPackSession = new ScriptPackSession(scriptPacks, new string[0]); scriptPackSession.InitializePacks(); //script pack references ImportReferences(references, scriptPackSession.References); //script pack usings usings.AddRange(scriptPackSession.Namespaces); _scriptCsContext.ScriptPacks.UnionWith(scriptPackSession.Contexts.Select(pack => pack.GetType().ToString())); } _scriptCsContext.References.UnionWith(references.Select(x => x.Display)); _scriptCsContext.Usings.UnionWith(usings); var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: usings.Distinct()); var fileName = Path.GetFileName(csxPath); var projectId = ProjectId.CreateNewId(Guid.NewGuid().ToString()); var project = ProjectInfo.Create(projectId, VersionStamp.Create(), fileName, $"{fileName}.dll", LanguageNames.CSharp, null, null, compilationOptions, parseOptions, null, null, references, null, null, true, typeof(IScriptHost)); _workspace.AddProject(project); AddFile(csxPath, projectId); foreach (var filePath in processResult.LoadedScripts.Distinct().Except(new[] { csxPath })) { _scriptCsContext.CsxFiles.Add(filePath); var loadedFileName = Path.GetFileName(filePath); var loadedFileProjectId = ProjectId.CreateNewId(Guid.NewGuid().ToString()); var loadedFileSubmissionProject = ProjectInfo.Create(loadedFileProjectId, VersionStamp.Create(), $"{loadedFileName}-LoadedFrom-{fileName}", $"{loadedFileName}-LoadedFrom-{fileName}.dll", LanguageNames.CSharp, null, null, compilationOptions, parseOptions, null, null, references, null, null, true, typeof(IScriptHost)); _workspace.AddProject(loadedFileSubmissionProject); AddFile(filePath, loadedFileProjectId); _workspace.AddProjectReference(projectId, new ProjectReference(loadedFileProjectId)); } } catch (Exception ex) { _logger.LogError($"{csxPath} will be ignored due to the following error:", ex); } } }
public ScriptController() { _scriptcs = new ScriptCsService().Root; _scriptcs.Executor.Initialize(new[] { "System.Web" }, Enumerable.Empty<IScriptPack>()); _scriptcs.Executor.AddReferences(Assembly.GetExecutingAssembly()); }
public ReplExecutorFactory(ScriptServices scriptServices, IRepl repl) { this.scriptServices = scriptServices; this.repl = repl; }