예제 #1
0
        public async ValueTask <bool> Build(string solutionFilePath, string outputDirectory, bool outputCSharp, bool test, CancellationToken cancellationToken = default)
        {
            _logger.LogDebug("Loading solution from {0}", solutionFilePath);
            var solution = await _solutionFactory.ParseFromFileAsync(solutionFilePath).ConfigureAwait(false);

            _logger.LogDebug("Found solution");

            _logger.LogDebug("Creating build order");
            var buildOrder = ProjectDependencyOrganizer.CreateBuildOrder(solution.Projects).ToList();

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"Build order is {string.Join(",", buildOrder.Select(x => x.Name))}");
            }

            _logger.LogDebug("Loading dependencies");
            var assemblyLoadContext = new AssemblyLoadContext(name: null, isCollectible: true);

            try
            {
                var projects = await LoadProjects(
                    solution,
                    buildOrder,
                    assemblyLoadContext,
                    cancellationToken);

                _logger.LogDebug("Compiling projects");
                var assemblies = new List <Assembly>();
                foreach (var project in projects)
                {
                    var assembly = await TryCompileProject(
                        outputDirectory,
                        outputCSharp,
                        assemblyLoadContext,
                        project,
                        cancellationToken);

                    if (assembly is null)
                    {
                        return(false);
                    }
                    assemblies.Add(assembly);
                }

                if (test)
                {
                    _logger.LogDebug("Running tests");
                    TestResult?testResults = null;
                    foreach (var(project, assembly) in buildOrder.Zip(assemblies))
                    {
                        if (project.IsTest)
                        {
                            var results = TestRunner.RunTests(assembly);
                            testResults = testResults is null ? results : results.Merge(testResults);
                        }
                    }

                    _logger.LogInformation(testResults?.GetMessage() ?? "No tests found to run");

                    if (testResults is TestResult.Failure)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            finally
            {
                assemblyLoadContext.Unload();
            }
        }