protected IAssembly CreateAssembly(IEnumerable <string> sources) { return(new SourceAssembly( QualifiedName("Test"), version: new Version(1, 0, 0), ImmutableArray <IAssembly> .Empty, sources.Select(x => DocumentFactory.FromString(x)).ToImmutableArray(), _assemblyCompiler)); }
public async Task <Result> CompileAndRun(string source, CancellationToken token) { var result = new Result(); using var assemblyStream = new MemoryStream(); using var csharpStream = new MemoryStream(); var compilationResult = _assemblyCompiler.CompileAssembly( _assemblyFactory.FromSource( QualifiedName.Parse("WebIde"), (0, 0, null), ImmutableArray <IAssembly> .Empty, ImmutableArray.Create(DocumentFactory.FromString(source))), assemblyStream, csharpStream, cancellationToken: token); await Task.Yield(); token.ThrowIfCancellationRequested(); if (compilationResult.AssemblyDiagnostics.Any()) { result.Diagnostics = compilationResult .AssemblyDiagnostics; } else { csharpStream.Position = 0; result.EmittedCSharp = CSharpSyntaxTree.ParseText(SourceText.From(csharpStream)).GetRoot().NormalizeWhitespace().ToFullString(); await Task.Yield(); token.ThrowIfCancellationRequested(); try { var assembly = Assembly.Load(assemblyStream.ToArray()); await Task.Yield(); token.ThrowIfCancellationRequested(); var entryPoint = assembly.EntryPoint; if (entryPoint != null) { result.RunResult = entryPoint.Invoke(null, null)?.ToString() ?? ""; } } catch (Exception e) { result.RuntimeError = e.ToString(); } } return(result); }
protected IAssembly CreateAssembly(string source, string?name = null, Version?version = null, params IAssembly[] references) { var document = DocumentFactory.FromString(source); return(new SourceAssembly( QualifiedName(name ?? "Test"), version: version ?? new Version(1, 0, 0), references.ToImmutableArray(), ImmutableArray.Create(document), _assemblyCompiler)); }
public async ValueTask <IAssembly> LoadProjectAsync( ProjectInfo projectInfo, AssemblyLoadContext assemblyLoadContext, IEnumerable <IAssembly> alreadyLoadedProjects, CancellationToken cancellationToken = default) { var dependencies = await _dependencyLoader.LoadDependenciesAsync( projectInfo, assemblyLoadContext, alreadyLoadedProjects, cancellationToken); ImmutableArray <IDocument> documents; try { var includedFilePaths = projectInfo .IncludedFilesAndFolders .Select(x => ( path: NormalizePath(x), isDirectory: IsDirectory(x) )) .SelectMany <(string path, bool isDirectory), string>(x => { if (x.isDirectory) { return(_fileSystem.Directory.GetFiles(x.path, "*.fl", SearchOption.AllDirectories)); } return(new[] { x.path }); }) .Distinct(); var excludedFilesAndFolders = projectInfo .ExcludedFilesAndFolders .Select(x => ( path: NormalizePath(x), isDirectory: IsDirectory(x) )).ToList(); documents = await includedFilePaths .Where(x => excludedFilesAndFolders.All(y => !IsOrContains(y.path, x))) .ToAsyncEnumerable() .SelectAwait(x => new ValueTask <string>(_fileSystem.File.ReadAllTextAsync(x, cancellationToken))) .Select(x => DocumentFactory.FromString(x)) .ToImmutableArrayAsync(); } catch (IOException e) { throw new FlcException($"failed to load included files and folders for {projectInfo.Name}", e); } return(_assemblyFactory.FromSource( QualifiedName.Parse(projectInfo.Name), (projectInfo.Version.Major, projectInfo.Version.Minor, projectInfo.Version.Suffix), dependencies, documents)); }