Ejemplo n.º 1
0
        /// <summary>
        ///     Returns CompilationResultsModel which has the results status and the compilation diagnostics.
        /// </summary>
        /// <param name="codeSnippet">The code snippet to be compiled.</param>
        /// <returns>CompilationResultsModel</returns>
        public CompilationResultsModel CompileSnippet(string codeSnippet, Versions version)
        {
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(codeSnippet);

            string assemblyName          = Path.GetRandomFileName();
            string commonAssemblyPath    = Path.GetDirectoryName(typeof(object).Assembly.Location);
            string graphAssemblyPathV1   = Path.GetDirectoryName(typeof(GraphServiceClient).Assembly.Location);
            string graphAssemblyPathBeta = Path.GetDirectoryName(typeof(beta.Microsoft.Graph.GraphServiceClient).Assembly.Location);

            List <MetadataReference> metadataReferences = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Private.CoreLib.dll")),
                MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Console.dll")),
                MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Runtime.dll")),
                MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "netstandard.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(IAuthenticationProvider).Assembly.Location), "Microsoft.Graph.Core.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(AuthenticationProvider).Assembly.Location), "msgraph-sdk-raptor-compiler-lib.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Task).Assembly.Location), "System.Threading.Tasks.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(JToken).Assembly.Location), "Newtonsoft.Json.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(HttpClient).Assembly.Location), "System.Net.Http.dll")),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Expression).Assembly.Location), "System.Linq.Expressions.dll")),
            };

            //Use the right Microsoft Graph Version
            if (_dllPath != null && _dllPath != string.Empty)
            {
                if (!System.IO.File.Exists(_dllPath))
                {
                    throw new ArgumentException($"Provided dll path {_dllPath} doesn't exist!");
                }

                metadataReferences.Add(MetadataReference.CreateFromFile(_dllPath));
            }
            else if (version == Versions.V1)
            {
                metadataReferences.Add(MetadataReference.CreateFromFile(Path.Combine(graphAssemblyPathV1, "Microsoft.Graph.dll")));
            }
            else
            {
                metadataReferences.Add(MetadataReference.CreateFromFile(Path.Combine(graphAssemblyPathBeta, "Microsoft.Graph.Beta.dll")));
            }

            var compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: metadataReferences,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using var memoryStream = new MemoryStream();
            var emitResult = compilation.Emit(memoryStream);
            CompilationResultsModel results = GetCompilationResults(emitResult);

            return(results);
        }
Ejemplo n.º 2
0
    private static void EvaluateCompilationResult(CompilationResultsModel compilationResult, LanguageTestData testData, string codeSnippetFormatted, CompilationOutputMessage compilationOutputMessage)
    {
        if (!compilationResult.IsSuccess)
        {
            // environment variable for sources directory is defined only for cloud runs
            var config = TestsSetup.Config.Value;
            if (config.IsLocalRun)
            {
                var linqPadQueriesDefaultFolder = Path.Join(
                    Environment.GetEnvironmentVariable("USERPROFILE"),
                    "/OneDrive - Microsoft", // remove this if you are not syncing your Documents to OneDrive
                    "/Documents",
                    "/LINQPad Queries");

                if (Directory.Exists(linqPadQueriesDefaultFolder))
                {
                    WriteLinqFile(testData, linqPadQueriesDefaultFolder, codeSnippetFormatted);
                }
            }

            Assert.Fail($"{compilationOutputMessage}");
        }
    }
    /// <summary>
    ///     Returns CompilationResultsModel which has the results status and the compilation diagnostics.
    /// </summary>
    /// <param name="codeSnippet">The code snippet to be compiled.</param>
    /// <param name="version"></param>
    /// <returns>CompilationResultsModel</returns>
    private (CompilationResultsModel, Assembly) CompileSnippetAndGetAssembly(string codeSnippet, Versions version)
    {
        var buffer = Encoding.UTF8.GetBytes(codeSnippet);
        // Mark Original Source as Embeddable
        var sourceText = SourceText.From(buffer, buffer.Length, Encoding.UTF8, canBeEmbedded: true);
        // Embed Original Source Code in Syntax Tree
        SyntaxTree syntaxTree     = CSharpSyntaxTree.ParseText(codeSnippet, new CSharpParseOptions(), SourceCodePath);
        var        syntaxRootNode = syntaxTree.GetRoot() as CSharpSyntaxNode;
        var        encoded        = CSharpSyntaxTree.Create(syntaxRootNode, null, SourceCodePath, Encoding.UTF8);

        string assemblyName          = Path.GetRandomFileName();
        string commonAssemblyPath    = Path.GetDirectoryName(typeof(object).Assembly.Location);
        string graphAssemblyPathV1   = Path.GetDirectoryName(typeof(GraphServiceClient).Assembly.Location);
        string graphAssemblyPathBeta = Path.GetDirectoryName(typeof(beta.Microsoft.Graph.GraphServiceClient).Assembly.Location);

        List <MetadataReference> metadataReferences = new List <MetadataReference>
        {
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Private.CoreLib.dll")),
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Console.dll")),
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Runtime.dll")),
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Text.Json.dll")),
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "System.Memory.dll")),
            MetadataReference.CreateFromFile(Path.Combine(commonAssemblyPath, "netstandard.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(IAuthenticationProvider).Assembly.Location), "Microsoft.Graph.Core.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(TokenCredential).Assembly.Location), "Azure.Core.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(MicrosoftGraphCSharpCompiler).Assembly.Location), "msgraph-sdk-raptor-compiler-lib.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Task).Assembly.Location), "System.Threading.Tasks.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(HttpClient).Assembly.Location), "System.Net.Http.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Expression).Assembly.Location), "System.Linq.Expressions.dll")),
            MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Uri).Assembly.Location), "System.Private.Uri.dll"))
        };

        //Use the right Microsoft Graph Version
        if (!string.IsNullOrEmpty(TestData.DllPath))
        {
            if (!System.IO.File.Exists(TestData.DllPath))
            {
                throw new ArgumentException($"Provided dll path {TestData.DllPath} doesn't exist!");
            }

            metadataReferences.Add(MetadataReference.CreateFromFile(TestData.DllPath));
        }
        else if (version == Versions.V1)
        {
            metadataReferences.Add(MetadataReference.CreateFromFile(Path.Combine(graphAssemblyPathV1, "Microsoft.Graph.dll")));
        }
        else
        {
            metadataReferences.Add(MetadataReference.CreateFromFile(Path.Combine(graphAssemblyPathBeta, "Microsoft.Graph.Beta.dll")));
        }

        var compilation = CSharpCompilation.Create(
            assemblyName,
            syntaxTrees: new[] { encoded, GlobalUsingsSyntaxTree.Value },
            references: metadataReferences,
            options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, deterministic: true, platform: Platform.AnyCpu)
            .WithConcurrentBuild(true)
            .WithOptimizationLevel(OptimizationLevel.Release));

        var(emitResult, assembly) = GetEmitResult(compilation, sourceText);
        CompilationResultsModel results = GetCompilationResults(emitResult);

        return(results, assembly);
    }