private void AddToWorkspace(string csxPath) { try { var csxFileName = Path.GetFileName(csxPath); var project = _scriptHelper.CreateProject(csxFileName, _commonReferences); if (_enableScriptNuGetReferences) { var scriptMap = _compilationDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts); var options = project.CompilationOptions.WithSourceReferenceResolver( new NuGetSourceReferenceResolver(ScriptSourceResolver.Default, scriptMap)); project = project.WithCompilationOptions(options); } // add CSX project to workspace _workspace.AddProject(project); _workspace.AddDocument(project.Id, csxPath, SourceCodeKind.Script); _projects[csxPath] = project; _logger.LogInformation($"Added CSX project '{csxPath}' to the workspace."); } catch (Exception ex) { _logger.LogError(ex, $"{csxPath} will be ignored due to an following error"); } }
public void Initalize(IConfiguration configuration) { var scriptHelper = new ScriptHelper(configuration); _logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'."); // Nothing to do if there are no CSX files var allCsxFiles = Directory.GetFiles(_env.TargetDirectory, "*.csx", SearchOption.AllDirectories); if (allCsxFiles.Length == 0) { _logger.LogInformation("Could not find any CSX files"); return; } _logger.LogInformation($"Found {allCsxFiles.Length} CSX files."); // explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis")).ToList(); // explicitly include System.ValueTuple inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("system.valuetuple"))); if (!bool.TryParse(configuration["enableScriptNuGetReferences"], out var enableScriptNuGetReferences)) { enableScriptNuGetReferences = false; } var commonReferences = new HashSet <MetadataReference>(MetadataReferenceEqualityComparer.Instance); var compilationDependencies = TryGetCompilationDependencies(enableScriptNuGetReferences); // 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 (Destkop CLR scripts)."); AddDefaultClrMetadataReferences(commonReferences); } else { foreach (var compilationAssembly in compilationDependencies) { _logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly); AddMetadataReference(commonReferences, compilationAssembly); } } // inject all inherited assemblies foreach (var inheritedCompileLib in inheritedCompileLibraries.SelectMany(x => x.ResolveReferencePaths())) { _logger.LogDebug("Adding implicit reference: " + inheritedCompileLib); AddMetadataReference(commonReferences, inheritedCompileLib); } // 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) { try { var csxFileName = Path.GetFileName(csxPath); var project = scriptHelper.CreateProject(csxFileName, commonReferences); // add CSX project to workspace _workspace.AddProject(project); _workspace.AddDocument(project.Id, csxPath, SourceCodeKind.Script); _projects[csxPath] = project; _logger.LogInformation($"Added CSX project '{csxPath}' to the workspace."); } catch (Exception ex) { _logger.LogError(ex, $"{csxPath} will be ignored due to an following error"); } } }
public void Initalize(IConfiguration configuration) { var scriptHelper = new ScriptHelper(configuration); _logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'."); // Nothing to do if there are no CSX files var allCsxFiles = Directory.GetFiles(_env.TargetDirectory, "*.csx", SearchOption.AllDirectories); if (allCsxFiles.Length == 0) { _logger.LogInformation("Could not find any CSX files"); return; } _logger.LogInformation($"Found {allCsxFiles.Length} CSX files."); // explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis")).ToList(); // explicitly include System.ValueTuple inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("system.valuetuple"))); var runtimeContexts = File.Exists(Path.Combine(_env.TargetDirectory, "project.json")) ? ProjectContext.CreateContextForEachTarget(_env.TargetDirectory) : null; if (!bool.TryParse(configuration["enableScriptNuGetReferences"], out var enableScriptNuGetReferences)) { enableScriptNuGetReferences = false; } if (enableScriptNuGetReferences && (runtimeContexts == null || runtimeContexts.Any() == false)) { runtimeContexts = TryCreateRuntimeContextsFromScriptFiles(); } var runtimeContext = runtimeContexts?.FirstOrDefault(); var commonReferences = new HashSet <MetadataReference>(); // if we have no context, then we also have no dependencies // we will assume desktop framework // and add default CLR references // same applies for having a context that is not a .NET Core app AddDefaultClrMetadataReferences(runtimeContext, commonReferences); if (runtimeContext == null) { _logger.LogInformation("Unable to find project context for CSX files. Will default to non-context usage (Destkop CLR scripts)."); } // otherwise we will grab dependencies for the script from the runtime context else { // assume the first one _logger.LogInformation($"Found script runtime context '{runtimeContext.TargetFramework.Framework}' for '{runtimeContext.ProjectFile.ProjectFilePath}'."); var projectExporter = runtimeContext.CreateExporter("Release"); var projectDependencies = projectExporter.GetDependencies(); // let's inject all compilation assemblies needed var compilationAssemblies = projectDependencies.SelectMany(x => x.CompilationAssemblies); foreach (var compilationAssembly in compilationAssemblies) { _logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly.ResolvedPath); AddMetadataReference(commonReferences, compilationAssembly.ResolvedPath); } } // inject all inherited assemblies foreach (var inheritedCompileLib in inheritedCompileLibraries.SelectMany(x => x.ResolveReferencePaths())) { _logger.LogDebug("Adding implicit reference: " + inheritedCompileLib); AddMetadataReference(commonReferences, inheritedCompileLib); } // 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) { try { var csxFileName = Path.GetFileName(csxPath); var project = scriptHelper.CreateProject(csxFileName, commonReferences); // add CSX project to workspace _workspace.AddProject(project); _workspace.AddDocument(project.Id, csxPath, SourceCodeKind.Script); _projects[csxPath] = project; _logger.LogInformation($"Added CSX project '{csxPath}' to the workspace."); } catch (Exception ex) { _logger.LogError(ex, $"{csxPath} will be ignored due to an following error"); } } }