public override bool Execute()
        {
            try
            {
                Log.LogMessage("SolutionCop: Starting analysis for project {0}", ProjectFullPath);
                Log.LogMessage("SolutionCop: Working folder = {0}", Path.GetFullPath("."));
                DirectoryInfo dir = Directory.GetParent(BuildEngine.ProjectFileOfTaskNode);
                string        pathToCofigFile;
                while (true)
                {
                    if (dir == null)
                    {
                        Log.LogError("SolutionCop: Cannot find SolutionCop.xml config file in parent folders.");
                        return(false);
                    }
                    pathToCofigFile = Path.Combine(dir.FullName, "SolutionCop.xml");
                    if (File.Exists(pathToCofigFile))
                    {
                        Log.LogMessage("SolutionCop: Found SolutionCop.xml config file: {0}", pathToCofigFile);
                        break;
                    }
                    dir = dir.Parent;
                }

                var msBuileAnalysisLogger = new MsBuildSolutionCopConsole(Log);
                var projectsVerifier      = new ProjectsVerifier(msBuileAnalysisLogger, new NullBuildServerReporter());
                var verificationResult    = projectsVerifier.VerifyProjects(pathToCofigFile, new[] { ProjectFullPath }, (errors, validationResults) =>
                {
                    if (errors.Any())
                    {
                        errors.ForEach(x => Log.LogError("SolutionCop: " + x));
                    }
                    else
                    {
                        msBuileAnalysisLogger.LogInfo("No errors found!");
                    }
                });
                Log.LogMessage("SolutionCop: Analysis finished!");
                return(verificationResult == VerificationResult.NoErrors);
            }
            catch (Exception e)
            {
                Log.LogError("SolutionCop: Unexpected error: {0}", e.Message);
                return(false);
            }
        }
Beispiel #2
0
        private static void VerifySolution(ICommandLineParameters commandLineParameters)
        {
            var solutionInfo = SolutionParser.LoadFromFile(SolutionCopConsole, commandLineParameters.PathToSolution);

            if (solutionInfo.IsParsed)
            {
                var pathToConfigFile = commandLineParameters.PathToConfigFile;
                if (string.IsNullOrEmpty(pathToConfigFile))
                {
                    pathToConfigFile = Path.Combine(Path.GetDirectoryName(commandLineParameters.PathToSolution), "SolutionCop.xml");
                    SolutionCopConsole.LogInfo("Custom path to config file is not specified, using default one: {0}", pathToConfigFile);
                }

                var reporter = CreateBuildServerReporter(commandLineParameters.ReportFormat);

                var verificationResult = new ProjectsVerifier(SolutionCopConsole, reporter).VerifyProjects(
                    pathToConfigFile,
                    solutionInfo.ProjectFilePaths.ToArray(),
                    (errors, validationResults) =>
                {
                    if (errors.Any())
                    {
                        SolutionCopConsole.LogError("***** Full list of errors: *****");
                        errors.ForEach(x => SolutionCopConsole.LogError(x));
                        reporter.SolutionVerificationFailed(string.Concat("FAILED - ", Path.GetFileName(pathToConfigFile)));
                    }
                    else
                    {
                        SolutionCopConsole.LogInfo("No errors found!");
                        reporter.SolutionVerificationPassed(string.Concat("PASSED - ", Path.GetFileName(pathToConfigFile)));
                    }
                });
                Environment.Exit(verificationResult == VerificationResult.ErrorsFound ? -1 : 0);
            }
            else
            {
                SolutionCopConsole.LogError("Cannot parse solution file.");
                Environment.Exit(-1);
            }
        }