< 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_22190969454
Line coverage
91%
Covered lines: 54
Uncovered lines: 5
Coverable lines: 59
Total lines: 95
Line coverage: 91.5%
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.Dynamic;
 6using System.Text;
 7
 8namespace pva.SuperV.Engine
 9{
 10    /// <summary>
 11    /// Class for building a <see cref="WipProject"/> and convert it to a <see cref="RunnableProject"/> by generating an
 12    /// </summary>
 13    public static class ProjectBuilder
 14    {
 15        /// <summary>
 16        /// Builds the specified <see cref="WipProject"/>.
 17        /// </summary>
 18        /// <param name="wipProject">The WIP project.</param>
 19        /// <returns>a <see cref="RunnableProject"/></returns>
 20        /// <exception cref="pva.SuperV.Engine.Exceptions.ProjectBuildException"></exception>
 21        public static async Task<RunnableProject> BuildAsync(WipProject wipProject)
 13422        {
 13423            RunnableProject runnableProject = wipProject.CloneAsRunnable();
 13224            wipProject.Dispose();
 13225            await BuildAsync(runnableProject);
 13226            return runnableProject;
 13227        }
 28
 29        public static async ValueTask BuildAsync(RunnableProject runnableProject)
 27630        {
 27631            if (!File.Exists(runnableProject.GetAssemblyFileName()))
 13232            {
 13233                string projectAssemblyFileName = runnableProject.GetAssemblyFileName();
 13234                string projectCode = runnableProject.GetCode();
 13235                var compilation = CreateCompilation(CSharpSyntaxTree.ParseText(projectCode), $"{runnableProject.Name}-V{
 13236                await using MemoryStream dllStream = new();
 13237                await using MemoryStream pdbStream = new();
 13238                await using Stream win32ResStream = compilation.CreateDefaultWin32Resources(
 13239                    versionResource: true, // Important!
 13240                    noManifest: false,
 13241                    manifestContents: null,
 13242                    iconInIcoFormat: null);
 13243                var compilationResult = compilation.Emit(
 13244                    peStream: dllStream,
 13245                    pdbStream: pdbStream,
 13246                    win32Resources: win32ResStream);
 47
 13248                if (!compilationResult.Success)
 049                {
 050                    StringBuilder diagnostics = new();
 051                    compilationResult.Diagnostics
 052                        .ForEach(diagnostic => diagnostics.AppendLine(diagnostic.ToString()));
 053                    throw new ProjectBuildException(runnableProject, diagnostics.ToString());
 54                }
 13255                await File.WriteAllBytesAsync(projectAssemblyFileName, dllStream.ToArray());
 13256            }
 27657        }
 58
 59        /// <summary>
 60        /// Creates a <see cref="CSharpCompilation"/> to generate an assembly for the project.
 61        /// </summary>
 62        /// <param name="tree">The tree.</param>
 63        /// <param name="name">The name.</param>
 64        /// <returns>A <see cref="CSharpCompilation"/></returns>
 65        private static CSharpCompilation CreateCompilation(SyntaxTree tree, string name)
 13266        {
 13267            var systemAssembliesPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
 13268            List<MetadataReference> refs =
 13269            [
 13270                /*
 13271                * Adding some necessary .NET assemblies
 13272                * These assemblies couldn't be loaded correctly via the same construction as above,
 13273                * in specific the System.Runtime.
 13274                */
 13275                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "mscorlib.dll")),
 13276                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "System.dll")),
 13277                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "System.Core.dll")),
 13278                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "System.Runtime.dll")),
 13279                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "System.Collections.dll")),
 13280                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "System.Dynamic.Runtime.dll")),
 13281                MetadataReference.CreateFromFile(Path.Combine(systemAssembliesPath!, "Microsoft.CSharp.dll")),
 13282                MetadataReference.CreateFromFile(typeof(ExpandoObject).Assembly.Location),
 13283                // Basic types assembly
 13284                MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
 13285                // SuperV Project assembly
 13286                MetadataReference.CreateFromFile(typeof(Project).Assembly.Location)
 13287            ];
 88
 13289            return CSharpCompilation
 13290                .Create(name, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
 13291                .AddReferences(refs)
 13292                .AddSyntaxTrees(tree);
 13293        }
 94    }
 95}