Exemple #1
0
        public static async Task <ProjectCacheService> FromDescriptorAsync(
            ProjectCacheDescriptor pluginDescriptor,
            BuildManager buildManager,
            ILoggingService loggingService,
            CancellationToken cancellationToken)
        {
            var plugin = await Task.Run(() => GetPluginInstance(pluginDescriptor), cancellationToken)
                         .ConfigureAwait(false);

            // TODO: Detect and use the highest verbosity from all the user defined loggers. That's tricky because right now we can't discern between user set loggers and msbuild's internally added loggers.
            var logger = new LoggingServiceToPluginLoggerAdapter(LoggerVerbosity.Normal, loggingService);

            await plugin.BeginBuildAsync(
                new CacheContext(
                    pluginDescriptor.PluginSettings,
                    new IFileSystemAdapter(FileSystems.Default),
                    pluginDescriptor.ProjectGraph,
                    pluginDescriptor.EntryPoints),
                // TODO: Detect verbosity from logging service.
                logger,
                cancellationToken);

            if (logger.HasLoggedErrors)
            {
                throw new Exception(
                          ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ProjectCacheInitializationFailed"));
            }

            return(new ProjectCacheService(plugin, buildManager, logger, pluginDescriptor, cancellationToken));
        }
        private async Task BeginBuildAsync(ProjectCacheDescriptor?vsWorkaroundOverrideDescriptor = null)
        {
            var logger = new LoggingServiceToPluginLoggerAdapter(
                _loggingService,
                BuildEventContext.Invalid,
                BuildEventFileInfo.Empty);

            try
            {
                SetState(ProjectCacheServiceState.BeginBuildStarted);

                logger.LogMessage("Initializing project cache plugin", MessageImportance.Low);
                var timer = Stopwatch.StartNew();

                if (_projectCacheDescriptor.VsWorkaround)
                {
                    logger.LogMessage("Running project cache with Visual Studio workaround");
                }

                var projectDescriptor = vsWorkaroundOverrideDescriptor ?? _projectCacheDescriptor;
                await _projectCachePlugin.BeginBuildAsync(
                    new CacheContext(
                        projectDescriptor.PluginSettings,
                        new DefaultMSBuildFileSystem(),
                        projectDescriptor.ProjectGraph,
                        projectDescriptor.EntryPoints),
                    logger,
                    _cancellationToken);

                timer.Stop();
                logger.LogMessage($"Finished initializing project cache plugin in {timer.Elapsed.TotalMilliseconds} ms", MessageImportance.Low);

                SetState(ProjectCacheServiceState.BeginBuildFinished);
            }
            catch (Exception e)
            {
                HandlePluginException(e, nameof(ProjectCachePluginBase.BeginBuildAsync));
            }

            if (logger.HasLoggedErrors)
            {
                ProjectCacheException.ThrowForErrorLoggedInsideTheProjectCache("ProjectCacheInitializationFailed");
            }
        }
        // TODO: remove vsWorkaroundOverrideDescriptor after we change VS to set the cache descriptor via build parameters.
        private async Task BeginBuildAsync(ProjectCacheDescriptor?vsWorkaroundOverrideDescriptor = null)
        {
            BuildEventContext  buildEventContext  = BuildEventContext.Invalid;
            BuildEventFileInfo buildEventFileInfo = BuildEventFileInfo.Empty;
            var pluginLogger = new LoggingServiceToPluginLoggerAdapter(
                _loggingService,
                buildEventContext,
                buildEventFileInfo);
            ProjectCacheDescriptor projectDescriptor = vsWorkaroundOverrideDescriptor ?? _projectCacheDescriptor;

            try
            {
                SetState(ProjectCacheServiceState.BeginBuildStarted);
                _loggingService.LogComment(buildEventContext, MessageImportance.Low, "ProjectCacheBeginBuild");
                MSBuildEventSource.Log.ProjectCacheBeginBuildStart(_projectCachePluginTypeName);

                await _projectCachePlugin.BeginBuildAsync(
                    new CacheContext(
                        projectDescriptor.PluginSettings,
                        new DefaultMSBuildFileSystem(),
                        projectDescriptor.ProjectGraph,
                        projectDescriptor.EntryPoints),
                    pluginLogger,
                    _cancellationToken);
            }
            catch (Exception e)
            {
                HandlePluginException(e, nameof(ProjectCachePluginBase.BeginBuildAsync));
            }
            finally
            {
                MSBuildEventSource.Log.ProjectCacheBeginBuildStop(_projectCachePluginTypeName);
                SetState(ProjectCacheServiceState.BeginBuildFinished);
            }

            if (pluginLogger.HasLoggedErrors)
            {
                ProjectCacheException.ThrowForErrorLoggedInsideTheProjectCache("ProjectCacheInitializationFailed");
            }
        }