Пример #1
0
        /// <summary>
        /// Executes the custom MsBuild task. Called by the MsBuild tool.
        /// </summary>
        /// <returns>
        /// True if there was no error and no exception.
        /// </returns>
        public override bool Execute()
        {
            if (_logger == null)
            {
                // This must not be moved to the ctor because BuildEngine is not yet initialized at construction time.
                _logger = new MsBuildLoggerGateway(BuildEngine);
            }

            try
            {
                var runWasSuccessful = true;

                _logger.LogTraceMessage(GetInputParameterDiagnosticMessages());

                var defaultInfoImportance = EnumHelper.ParseNullable <Importance>(InfoImportance.GetValue());
                var analyzerFactory       = new DependencyAnalyzerFactory(_logger.LogTraceMessage).SetDefaultInfoImportance(defaultInfoImportance);
                var analyzer         = analyzerFactory.CreateOutOfProcess(ProjectFolder, ServiceAddressProvider.ServiceAddress);
                var analyzerMessages = analyzer.AnalyzeProject(SourceFilePaths, ReferencedAssemblyPaths);

                _logger.InfoImportance = analyzer.InfoImportance.ToMessageImportance();

                foreach (var analyzerMessage in analyzerMessages)
                {
                    switch (analyzerMessage)
                    {
                    case InfoMessageBase infoMessage:
                        _logger.LogInfo(infoMessage);
                        break;

                    case IssueMessageBase issueMessage:
                        _logger.LogIssue(issueMessage);
                        runWasSuccessful = runWasSuccessful && issueMessage.IssueKind != IssueKind.Error;
                        break;

                    default:
                        throw new Exception($"Unexpected analyzer message type: {analyzerMessage?.GetType().Name}");
                    }
                }

                return(runWasSuccessful);
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception during NsDepCopTask execution: {e}");
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Executes the custom MsBuild task. Called by the MsBuild tool.
        /// </summary>
        /// <returns>
        /// True if there was no error and no exception.
        /// </returns>
        public override bool Execute()
        {
            if (_logger == null)
            {
                // This must not be moved to the ctor because BuildEngine is not yet inicialized at construction time.
                _logger = new MsBuildLoggerGateway(BuildEngine);
            }

            try
            {
                var runWasSuccessful = true;

                _logger.LogTraceMessage(GetInputParameterDiagnosticMessages());

                var defaultInfoImportance = EnumHelper.ParseNullable <Importance>(InfoImportance.GetValue());
                var analyzerFactory       = new DependencyAnalyzerFactory(_logger.LogTraceMessage).SetDefaultInfoImportance(defaultInfoImportance);
                var analyzer         = analyzerFactory.CreateOutOfProcess(ProjectFolder, ServiceAddressProvider.ServiceAddress);
                var analyzerMessages = analyzer.AnalyzeProject(SourceFilePaths, ReferencedAssemblyPaths);

                _logger.InfoImportance = analyzer.InfoImportance.ToMessageImportance();

                foreach (var analyzerMessage in analyzerMessages)
                {
                    switch (analyzerMessage)
                    {
                    case IllegalDependencyMessage illegalDependencyMessage:
                        _logger.LogIssue(
                            IssueDefinitions.IllegalDependencyIssue,
                            illegalDependencyMessage.IllegalDependency,
                            illegalDependencyMessage.IssueKind,
                            illegalDependencyMessage.IllegalDependency.SourceSegment);
                        break;

                    case ConfigErrorMessage configErrorMessage:
                        _logger.LogIssue(IssueDefinitions.ConfigExceptionIssue, configErrorMessage.Exception);
                        break;

                    case TooManyIssuesMessage tooManyIssuesMessage:
                        _logger.LogIssue(IssueDefinitions.TooManyIssuesIssue, tooManyIssuesMessage.IssueKind);
                        break;

                    case NoConfigFileMessage _:
                        _logger.LogIssue(IssueDefinitions.NoConfigFileIssue);
                        break;

                    case ConfigDisabledMessage _:
                        _logger.LogIssue(IssueDefinitions.ConfigDisabledIssue);
                        break;

                    case AnalysisStartedMessage analysisStartedMessage:
                        _logger.LogIssue(IssueDefinitions.AnalysisStartedIssue, analysisStartedMessage.ProjectLocation);
                        break;

                    case AnalysisFinishedMessage analysisFinishedMessage:
                        _logger.LogIssue(IssueDefinitions.AnalysisFinishedIssue, analysisFinishedMessage.AnalysisDuration);
                        break;

                    default:
                        throw new Exception($"Unexpected analyzer message type: {analyzerMessage?.GetType().Name}");
                    }

                    if (analyzerMessage is IssueMessageBase issueMessage)
                    {
                        runWasSuccessful = runWasSuccessful && issueMessage.IssueKind != IssueKind.Error;
                    }
                }

                return(runWasSuccessful);
            }
            catch (Exception e)
            {
                _logger.LogIssue(TaskExceptionIssue, e);
                return(false);
            }
        }