< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.ProjectBuilder
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/ProjectBuilder.cs
Tag: dotnet-ubuntu_18869653307
Line coverage
91%
Covered lines: 51
Uncovered lines: 5
Coverable lines: 56
Total lines: 91
Line coverage: 91%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildAsync()100%11100%
BuildAsync()75%4484%
CreateCompilation(...)100%11100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/ProjectBuilder.cs

#LineLine coverage
 1using Microsoft.CodeAnalysis;
 2using Microsoft.CodeAnalysis.CSharp;
 3using pva.Helpers.Extensions;
 4using pva.SuperV.Engine.Exceptions;
 5using System.Text;
 6
 7namespace pva.SuperV.Engine
 8{
 9    /// <summary>
 10    /// Class for building a <see cref="WipProject"/> and convert it to a <see cref="RunnableProject"/> by generating an
 11    /// </summary>
 12    public static class ProjectBuilder
 13    {
 14        /// <summary>
 15        /// Builds the specified <see cref="WipProject"/>.
 16        /// </summary>
 17        /// <param name="wipProject">The WIP project.</param>
 18        /// <returns>a <see cref="RunnableProject"/></returns>
 19        /// <exception cref="pva.SuperV.Engine.Exceptions.ProjectBuildException"></exception>
 20        public static async Task<RunnableProject> BuildAsync(WipProject wipProject)
 12621        {
 12622            RunnableProject runnableProject = wipProject.CloneAsRunnable();
 12423            wipProject.Dispose();
 12424            await BuildAsync(runnableProject);
 12425            return runnableProject;
 12426        }
 27
 28        public static async ValueTask BuildAsync(RunnableProject runnableProject)
 32729        {
 32730            if (!File.Exists(runnableProject.GetAssemblyFileName()))
 12431            {
 12432                string projectAssemblyFileName = runnableProject.GetAssemblyFileName();
 12433                string projectCode = runnableProject.GetCode();
 12434                var compilation = CreateCompilation(CSharpSyntaxTree.ParseText(projectCode), $"{runnableProject.Name}-V{
 12435                using MemoryStream dllStream = new();
 12436                using MemoryStream pdbStream = new();
 12437                using Stream win32ResStream = compilation.CreateDefaultWin32Resources(
 12438                    versionResource: true, // Important!
 12439                    noManifest: false,
 12440                    manifestContents: null,
 12441                    iconInIcoFormat: null);
 12442                var compilationResult = compilation.Emit(
 12443                    peStream: dllStream,
 12444                    pdbStream: pdbStream,
 12445                    win32Resources: win32ResStream);
 46
 12447                if (!compilationResult.Success)
 048                {
 049                    StringBuilder diagnostics = new();
 050                    compilationResult.Diagnostics
 051                        .ForEach(diagnostic => diagnostics.AppendLine(diagnostic.ToString()));
 052                    throw new ProjectBuildException(runnableProject, diagnostics.ToString());
 53                }
 12454                await File.WriteAllBytesAsync(projectAssemblyFileName, dllStream.ToArray());
 12455            }
 32756        }
 57
 58        /// <summary>
 59        /// Creates a <see cref="CSharpCompilation"/> to generate an assembly for the project.
 60        /// </summary>
 61        /// <param name="tree">The tree.</param>
 62        /// <param name="name">The name.</param>
 63        /// <returns>A <see cref="CSharpCompilation"/></returns>
 64        private static CSharpCompilation CreateCompilation(SyntaxTree tree, string name)
 12465        {
 12466            var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
 12467            List<MetadataReference> refs =
 12468            [
 12469                /*
 12470                * Adding some necessary .NET assemblies
 12471                * These assemblies couldn't be loaded correctly via the same construction as above,
 12472                * in specific the System.Runtime.
 12473                */
 12474                MetadataReference.CreateFromFile(Path.Combine(assemblyPath!, "mscorlib.dll")),
 12475                MetadataReference.CreateFromFile(Path.Combine(assemblyPath!, "System.dll")),
 12476                MetadataReference.CreateFromFile(Path.Combine(assemblyPath!, "System.Core.dll")),
 12477                MetadataReference.CreateFromFile(Path.Combine(assemblyPath!, "System.Runtime.dll")),
 12478                MetadataReference.CreateFromFile(Path.Combine(assemblyPath!, "System.Collections.dll")),
 12479                // Basic types assembly
 12480                MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
 12481                // SuperV Project assembly
 12482                MetadataReference.CreateFromFile(typeof(Project).Assembly.Location)
 12483            ];
 84
 12485            return CSharpCompilation
 12486                .Create(name, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
 12487                .AddReferences(refs)
 12488                .AddSyntaxTrees(tree);
 12489        }
 90    }
 91}