public ScriptContext CreateScriptContext(ScriptOptions scriptOptions) { var currentDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies(); // explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized var inheritedCompileLibraries = currentDomainAssemblies.Where(x => x.FullName.StartsWith("microsoft.codeanalysis", StringComparison.OrdinalIgnoreCase)).ToList(); // explicitly include System.ValueTuple inheritedCompileLibraries.AddRange(currentDomainAssemblies.Where(x => x.FullName.StartsWith("system.valuetuple", StringComparison.OrdinalIgnoreCase))); CompilationDependency[] compilationDependencies = null; try { _logger.LogInformation($"Searching for compilation dependencies with the fallback framework of '{scriptOptions.DefaultTargetFramework}'."); compilationDependencies = _compilationDependencyResolver.GetDependencies(_env.TargetDirectory, scriptOptions.IsNugetEnabled(), scriptOptions.DefaultTargetFramework).ToArray(); } catch (Exception e) { _logger.LogError("Failed to resolve compilation dependencies", e); compilationDependencies = Array.Empty <CompilationDependency>(); } var metadataReferences = new HashSet <MetadataReference>(MetadataReferenceEqualityComparer.Instance); var assemblyReferences = new HashSet <string>(); var isDesktopClr = true; // if we have no compilation dependencies // we will assume desktop framework // and add default CLR references // same applies for having a context that is not a .NET Core app if (!compilationDependencies.Any()) { _logger.LogInformation("Unable to find dependency context for CSX files. Will default to non-context usage (Desktop CLR scripts)."); AddDefaultClrMetadataReferences(metadataReferences, assemblyReferences); } else { isDesktopClr = false; HashSet <string> loadedFiles = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var compilationAssembly in compilationDependencies.SelectMany(cd => cd.AssemblyPaths).Distinct()) { if (loadedFiles.Add(Path.GetFileName(compilationAssembly))) { _logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly); AddMetadataReference(metadataReferences, assemblyReferences, compilationAssembly); } } } // inject all inherited assemblies foreach (var inheritedCompileLib in inheritedCompileLibraries) { _logger.LogDebug("Adding implicit reference: " + inheritedCompileLib); AddMetadataReference(metadataReferences, assemblyReferences, inheritedCompileLib.Location); } var scriptProjectProvider = new ScriptProjectProvider(scriptOptions, _env, _loggerFactory, isDesktopClr); return(new ScriptContext(scriptProjectProvider, metadataReferences, assemblyReferences, compilationDependencies, _defaultGlobalsType)); }
public void Initalize(IConfiguration configuration) { _scriptOptions = new ScriptOptions(); ConfigurationBinder.Bind(configuration, _scriptOptions); _scriptHelper = new ScriptHelper(_scriptOptions); _logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'."); // Nothing to do if there are no CSX files var allCsxFiles = _fileSystemHelper.GetFiles("**/*.csx").ToArray(); if (allCsxFiles.Length == 0) { _logger.LogInformation("Could not find any CSX files"); return; } _logger.LogInformation($"Found {allCsxFiles.Length} CSX files."); var currentDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies(); // explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized var inheritedCompileLibraries = currentDomainAssemblies.Where(x => x.FullName.StartsWith("microsoft.codeanalysis", StringComparison.OrdinalIgnoreCase)).ToList(); // explicitly include System.ValueTuple inheritedCompileLibraries.AddRange(currentDomainAssemblies.Where(x => x.FullName.StartsWith("system.valuetuple", StringComparison.OrdinalIgnoreCase))); _compilationDependencies = TryGetCompilationDependencies(); // if we have no compilation dependencies // we will assume desktop framework // and add default CLR references // same applies for having a context that is not a .NET Core app if (!_compilationDependencies.Any()) { _logger.LogInformation("Unable to find dependency context for CSX files. Will default to non-context usage (Desktop CLR scripts)."); AddDefaultClrMetadataReferences(_commonReferences); } else { HashSet <string> loadedFiles = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var compilationAssembly in _compilationDependencies.SelectMany(cd => cd.AssemblyPaths).Distinct()) { if (loadedFiles.Add(Path.GetFileName(compilationAssembly))) { _logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly); AddMetadataReference(_commonReferences, compilationAssembly); } } } // inject all inherited assemblies foreach (var inheritedCompileLib in inheritedCompileLibraries) { _logger.LogDebug("Adding implicit reference: " + inheritedCompileLib); AddMetadataReference(_commonReferences, inheritedCompileLib.Location); } // Each .CSX file becomes an entry point for it's own project // Every #loaded file will be part of the project too foreach (var csxPath in allCsxFiles) { AddToWorkspace(csxPath); } // Watch CSX files in order to add/remove them in workspace _fileSystemWatcher.Watch(CsxExtension, OnCsxFileChanged); }