Beispiel #1
0
        public override TaskStatu Job(TfsVariable tfsVariables, UserVariable <CodeMetricSetting> usrVariables)
        {
            var setting = usrVariables.Config;

            if (setting == null)
            {
                return(new TaskStatu("CM01", "No setting found"));
            }

            string buildPath     = tfsVariables.BuildSourceDirectory;
            var    solutionsPath = SolutionHelper.FindSolutionFiles(buildPath);

            solutionsPath = !string.IsNullOrEmpty(tfsVariables.SolutiontoBuild) ?
                            solutionsPath.Where(x => x.Contains(tfsVariables.SolutiontoBuild)).ToArray()
                : solutionsPath;

            if (!solutionsPath.Any())
            {
                return(new TaskStatu("CM02", "No solution file found"));
            }

            foreach (var slnPath in solutionsPath)
            {
                if (setting.ExcludedSolutions.Any(x => x == slnPath))
                {
                    continue;
                }

                using (var slnProvider = new SolutionProvider())
                {
                    var solution = slnProvider.Get(slnPath).Result;
                    var slnName  = Path.GetFileName(slnPath);
                    var projects = solution.Projects.ToList();
                    if (!projects.Any())
                    {
                        return(new TaskStatu("CM03", string.Format("No solution found in [{0}] solution", slnName)));
                    }
                    WriteDetail(string.Format("Calculating metrics for [{0}] solution", slnName));
                    var metricBag = GetSolutionMetric(projects, solution, setting);
                    if (metricBag == null)
                    {
                        return(new TaskStatu("CM04", "CodeMetric failed due to an project build or style error."));
                    }

                    var checkResult = CheckMetric(metricBag, setting, slnName);
                    if (!string.IsNullOrEmpty(checkResult))
                    {
                        return(new TaskStatu("CM05", checkResult));
                    }
                }
            }

            return(new TaskStatu("CodeMetrics check finished successfully."));
        }
Beispiel #2
0
        private Solution GetSolutionInfo(TfsVariable tfsVariables, PartialUnitTestSetting setting, TFSFileState itmChanged)
        {
            bool   isSolutionSelected = !string.IsNullOrEmpty(tfsVariables.SolutiontoBuild);
            string slnPath            = null;

            string[] slnFiles = isSolutionSelected
                ? SolutionHelper.FindSolutionFiles(tfsVariables.BuildSourceDirectory, tfsVariables.SolutiontoBuild)
                : SolutionHelper.FindSolutionFiles(tfsVariables.BuildSourceDirectory);

            if (isSolutionSelected)
            {
                slnPath = slnFiles.FirstOrDefault();
            }
            else
            {
                foreach (var slnFullPath in slnFiles)
                {
                    string mainFolderName = Path.GetDirectoryName(slnFullPath);
                    var    mainFolder     = new DirectoryInfo(mainFolderName);
                    var    repoFolder     = mainFolder.Parent.FullName;
                    string tmpFile        = repoFolder + itmChanged.FilePath;
                    if (File.Exists(tmpFile))
                    {
                        slnPath = slnFullPath;
                        break;
                    }
                }

                if (string.IsNullOrEmpty(slnPath))
                {
                    return(null);
                }
            }
            MSBuildWorkspace workSpace = MSBuildWorkspace.Create();
            var solution = workSpace.OpenSolutionAsync(slnPath).Result;

            return(solution);
        }
Beispiel #3
0
        public override TaskStatu Job(TfsVariable tfsVariables, UserVariable <AnalyzerEnforcerSetting> usrVariables)
        {
            string settingOkMsg = CheckSetting(usrVariables);
            var    setting      = usrVariables.Config;

            if (!string.IsNullOrEmpty(settingOkMsg))
            {
                return(new TaskStatu("RET01", settingOkMsg));
            }
            string buildPath = tfsVariables.BuildSourceDirectory;

            string[] slnFiles = SolutionHelper.FindSolutionFiles(buildPath);
            slnFiles = !string.IsNullOrEmpty(tfsVariables.SolutiontoBuild) ?
                       slnFiles.Where(x => x.Contains(tfsVariables.SolutiontoBuild)).ToArray()
                : slnFiles;

            if ((slnFiles == null) || (!slnFiles.Any()))
            {
                return(new TaskStatu("RET02", "No solution file found."));
            }

            var workspace = MSBuildWorkspace.Create();

            foreach (var slnItem in slnFiles)
            {
                var slnName = Path.GetFileNameWithoutExtension(slnItem);
                if (IsSolutionExcluded(setting, slnName))
                {
                    continue;
                }

                var solution = workspace.OpenSolutionAsync(slnItem).Result;


                foreach (var libItem in setting.References)
                {
                    var analyzer = new AnalyzerFileReference(libItem.DLLPath, FromFileLoader.Instance);

                    foreach (var project in solution.Projects)
                    {
                        if (IsProjectExcluded(setting, project.Name))
                        {
                            continue;
                        }

                        bool analyzerExists = false;
                        foreach (var analyzerReference in project.AnalyzerReferences)
                        {
                            if (analyzerReference.FullPath.EndsWith(libItem.DLLName))
                            {
                                analyzerExists = true;
                                break;
                            }
                        }

                        if (!analyzerExists)
                        {
                            WriteDetail(string.Format("{0} project is missing {1} library. Adding...", project.Name, libItem.DLLName));
                            solution = solution.AddAnalyzerReference(project.Id, analyzer);
                        }
                    }
                }

                workspace.TryApplyChanges(solution);
            }

            return(new TaskStatu("Enforce finished successfully."));
        }