/// <summary>
        /// Runs the source generation. This code has to be in a method so the AssemblyResolver
        /// can resolve the dependencies in a proper order.
        /// </summary>
        private static int RunGeneration(string generatedFilesOutputPath, string binlogOutputPath, BuildEnvironment environment, bool enableConsole)
        {
            using (var logger = new BinaryLoggerForwarderProvider(binlogOutputPath))
            {
                typeof(Program).Log().Info($"Generating files to path {generatedFilesOutputPath}, logoutput={binlogOutputPath}");

                try
                {
                    var host = new SourceGeneratorEngine(environment, Server.DesktopGenerationServerHost.SharedAssemblyReferenceProvider);

                    var generatedFiles = host.Generate();

                    File.WriteAllText(generatedFilesOutputPath, string.Join(";", generatedFiles));

                    return(0);
                }
                catch (Exception e)
                {
                    if (enableConsole)
                    {
                        Console.WriteLine(e.ToString());
                    }

                    typeof(Program).Log().Error("Generation failed: " + e.ToString());

                    return(3);
                }
            }
        }
Exemplo n.º 2
0
        public GenerationResponse RunGeneration(RunRequest request, CancellationToken cancellationToken)
        {
            Log($"CurrentDirectory = '{request.CurrentDirectory}'");
            for (int i = 0; i < request.Arguments.Length; ++i)
            {
                Log($"Argument[{i}] = '{request.Arguments[i]}'");
            }

            // We're only processing one request at a time, so we can change this path
            // This is required as many msbuild tasks assume that the current work folder
            // is the project being built.
            Environment.CurrentDirectory = request.CurrentDirectory;

            // Compiler server must be provided with a valid temporary directory in order to correctly
            // isolate signing between compilations.
            if (string.IsNullOrEmpty(request.TempDirectory))
            {
                Log($"Rejecting build due to missing temp directory");
                return(new RejectedGenerationResponse());
            }

            var responseFilePath         = request.Arguments[0];
            var generatedFilesOutputPath = request.Arguments[1];
            var binlogOutputPath         = request.Arguments[2];
            var enableConsole            = request.Arguments.ElementAtOrDefault(3)?.Equals("-console", StringComparison.OrdinalIgnoreCase) ?? false;

            using (var responseFile = File.OpenRead(request.Arguments[0]))
            {
                var env = new DataContractSerializer(typeof(BuildEnvironment));

                if (env.ReadObject(responseFile) is BuildEnvironment environment)
                {
                    AssemblyResolver.RegisterAssemblyLoader(environment);

                    using (var logger = new BinaryLoggerForwarderProvider(binlogOutputPath))
                    {
                        typeof(Program).Log().Info($"Generating files to path {generatedFilesOutputPath}, logoutput={binlogOutputPath}");

                        try
                        {
                            string[] generatedFiles = Generate(environment);

                            File.WriteAllText(generatedFilesOutputPath, string.Join(";", generatedFiles));

                            return(new CompletedGenerationResponse(0, true, "empty output"));
                        }
                        catch (Exception e)
                        {
                            if (enableConsole)
                            {
                                Console.WriteLine(e.ToString());
                            }

                            typeof(Program).Log().Error("Generation failed: " + e.ToString());

                            return(new RejectedGenerationResponse());
                        }
                    }
                }
                else
                {
                    return(new RejectedGenerationResponse());
                }
            }
        }