Exemplo n.º 1
0
        /// <summary>
        /// Main program entry.
        /// </summary>
        public static void Main(string[] args)
        {
            StyleCopConsole console = new StyleCopConsole(
                null,
                false,
                null,
                new List<string>(new[] { basePath }),
                true);

            Configuration configuration = new Configuration(null);
            CodeProject project = new CodeProject(0, basePath, configuration);

            console.Core.Environment.AddSourceCode(project, sourceFile, null);

            List<CodeProject> projects = new List<CodeProject>();
            projects.Add(project);

            console.OutputGenerated += OnOutputGenerated;
            console.ViolationEncountered += OnViolationEncountered;
            console.Start(projects, true);
            console.OutputGenerated -= OnOutputGenerated;
            console.ViolationEncountered -= OnViolationEncountered;
            console.Dispose();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Main program entry.
        /// </summary>
        public static void Main(string[] args)
        {
            string projectPath = @"C:\Users\Public\GIT\GitHub\My\StyleCop\Shuruev.StyleCop\Shuruev.StyleCop.Run";
            string filePath = @"C:\Users\Public\GIT\GitHub\My\StyleCop\Shuruev.StyleCop\Shuruev.StyleCop.Run\Class1.cs";

            StyleCopConsole console = new StyleCopConsole(null, false, null, null, true);
            CodeProject project = new CodeProject(0, projectPath, new Configuration(null));

            console.Core.Environment.AddSourceCode(project, filePath, null);

            console.OutputGenerated += OnOutputGenerated;
            console.ViolationEncountered += OnViolationEncountered;
            console.Start(new[] { project }, true);

            foreach (SourceParser parser in console.Core.Parsers)
            {
                Console.WriteLine("Parser: {0}", parser.Name);
                foreach (SourceAnalyzer analyzer in parser.Analyzers)
                {
                    Console.WriteLine("Analyzer: {0}", analyzer.Name);
                }
            }

            console.OutputGenerated -= OnOutputGenerated;
            console.ViolationEncountered -= OnViolationEncountered;
            console.Dispose();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
 /// <summary>
 /// Adds the source code files.
 /// </summary>
 /// <param name="files">The files.</param>
 /// <param name="console">The console.</param>
 /// <param name="project">The project.</param>
 private static void AddSourceCodeFiles(
     IEnumerable<string> files,
     StyleCopConsole console,
     CodeProject project)
 {
     foreach (var file in files)
     {
         console.Core.Environment.AddSourceCode(project, file, null);
     }
 }
        /// <summary>
        /// Give list of files and projects to be treated by Stylecop
        /// </summary>
        /// <param name="console">
        /// A <see cref="StyleCopConsole"/> which define the console which will treat the files
        /// </param>
        /// <returns>
        /// A <see cref="List<CodeProject>"/> which contains all the project to be treated by stylecop
        /// </returns>
        protected override List<CodeProject> GetCodeProjectList(StyleCopConsole console)
        {
            Configuration configuration = new Configuration(new string[] { "DEBUG" });
            List<CodeProject> projects = new List<CodeProject>();
            CodeProject project = new CodeProject(IdeApp.ProjectOperations.CurrentSelectedProject.BaseDirectory.GetHashCode(), IdeApp.ProjectOperations.CurrentSelectedProject.BaseDirectory, configuration);

            // Add each source file to this project.
            console.Core.Environment.AddSourceCode(project, IdeApp.Workbench.ActiveDocument.FileName, null);
            projects.Add(project);

            return projects;
        }
        /// <summary>
        /// Mains entry point.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main(string[] args)
        {
            if (ValidateCommandLineParameters(args))
            {
                return;
            }

            string projectPath = args[0];
            IEnumerable<string> files = GetAllFileNames(projectPath);
            var console = new StyleCopConsole(null, false, null, null, true);
            var project = new CodeProject(0, projectPath, new Configuration(null));

            AddSourceCodeFiles(files, console, project);
            SubscribeStyleCopConsoleEvents(console);
            StartStyleCopAnalysis(console, project);
            UnsubscribeStyleCopConsoleEvents(console);
            DisposeStyleCopConsole(console);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Main program entry.
        /// </summary>
        public static void Main(string[] args)
        {
            string projectPath = @"C:\Users\Public\GIT\GitHub\My\StyleCop\Source\4.4.0.14\ConsoleApplication1";
            string filePath = @"C:\Users\Public\GIT\GitHub\My\StyleCop\Source\4.4.0.14\ConsoleApplication1\Class1.cs";

            StyleCopConsole console = new StyleCopConsole(null, false, null, null, true);
            CodeProject project = new CodeProject(0, projectPath, new Configuration(null));

            console.Core.Environment.AddSourceCode(project, filePath, null);

            console.OutputGenerated += OnOutputGenerated;
            console.ViolationEncountered += OnViolationEncountered;
            console.Start(new[] { project }, true);
            console.OutputGenerated -= OnOutputGenerated;
            console.ViolationEncountered -= OnViolationEncountered;
            console.Dispose();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        /// <summary>
        /// Run the stylecop analyse on the file
        /// </summary>
        protected override void Run()
        {
            base.Run();
            IdeApp.Workbench.ActiveDocument.Save();

            TaskService.Errors.ClearByOwner(RunStyleCopHandler.styleCopAddinOwner);
            this.errorPad.ShowResults(this, new EventArgs());

            StyleCopConsole console = new StyleCopConsole(null, true, null, null, true);

            List<CodeProject> projects = this.GetCodeProjectList(console);

            console.OutputGenerated += this.OnOutputGenerated;
            console.ViolationEncountered += this.OnViolationEncountered;
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            console.Start(projects, false);
            console.OutputGenerated -= this.OnOutputGenerated;
            console.ViolationEncountered -= this.OnViolationEncountered;
            console.Dispose();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Runs StyleCop+ for specified file.
        /// </summary>
        public void Run(string sourceFile, SpecialRunningParameters specialRunningParameters)
        {
            Violations.Clear();
            Output.Length = 0;

            string basePath = AppDomain.CurrentDomain.BaseDirectory;

            StyleCopConsole console = new StyleCopConsole(
                null,
                false,
                null,
                new List<string>(new[] { basePath }),
                true);

            StyleCopPlus styleCopPlus = ExtractStyleCopPlus(console);
            if (styleCopPlus == null)
            {
                throw new InvalidOperationException("StyleCopPlus was not found.");
            }

            styleCopPlus.SpecialRunningParameters = specialRunningParameters;

            CodeProject project = new CodeProject(
                0,
                basePath,
                new Configuration(null));

            console.Core.Environment.AddSourceCode(project, sourceFile, null);

            console.ViolationEncountered += OnViolationEncountered;
            console.OutputGenerated += OnOutputGenerated;
            console.Start(new[] { project }, true);

            console.OutputGenerated -= OnOutputGenerated;
            console.ViolationEncountered -= OnViolationEncountered;
            console.Dispose();
        }
Exemplo n.º 9
0
        /// <summary>
        /// Main program entry.
        /// </summary>
        public static void Main(string[] args)
        {
            StyleCopConsole console = new StyleCopConsole(
                null,
                false,
                null,
                new List<string>(new[] { s_basePath }),
                true);

            /*xxxStyleCopPlus styleCopPlus = null;
            foreach (SourceParser parser in console.Core.Parsers)
            {
                List<SourceAnalyzer> analyzersToRemove = new List<SourceAnalyzer>();
                foreach (SourceAnalyzer analyzer in parser.Analyzers)
                {
                    if (analyzer.GetType() == typeof(StyleCopPlus))
                    {
                        styleCopPlus = (StyleCopPlus)analyzer;
                        break;
                    }

                    analyzersToRemove.Add(analyzer);
                }

                foreach (SourceAnalyzer analyzer in analyzersToRemove)
                {
                    parser.Analyzers.Remove(analyzer);
                }
            }

            if (styleCopPlus == null)
            {
                throw new InvalidOperationException("StyleCopPlus was not found.");
            }*/

            /*xxxstyleCopPlus.DisableAllRulesExcept.Clear();
            styleCopPlus.DisableAllRulesExcept.Add("AdvancedNamingRules");*/

            CodeProject project = new CodeProject(
                0,
                s_basePath,
                new Configuration(null));

            console.Core.Environment.AddSourceCode(project, s_sourceFile, null);

            console.OutputGenerated += OnOutputGenerated;
            console.ViolationEncountered += OnViolationEncountered;
            console.Start(new[] { project }, true);

            foreach (SourceParser parser in console.Core.Parsers)
            {
                Console.WriteLine("Parser: {0}", parser.Name);
                foreach (SourceAnalyzer analyzer in parser.Analyzers)
                {
                    Console.WriteLine("Analyzer: {0}", analyzer.Name);
                }
            }

            console.OutputGenerated -= OnOutputGenerated;
            console.ViolationEncountered -= OnViolationEncountered;
            console.Dispose();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
 /// <summary>
 /// Give list of files and projects to be treated by Stylecop
 /// </summary>
 /// <param name="console">
 /// A <see cref="StyleCopConsole"/> which defines the console which will treat the files
 /// </param>
 /// <returns>
 /// A <see cref="List<CodeProject>"/> which contains all the project to be treated by stylecop
 /// </returns>
 protected abstract List<CodeProject> GetCodeProjectList(StyleCopConsole console);
 /// <summary>
 /// Disposes the style cop console.
 /// </summary>
 /// <param name="console">The console.</param>
 private static void DisposeStyleCopConsole(StyleCopConsole console)
 {
     console.Dispose();
 }
        /// <summary>
        /// Run the stylecop analyse on the file
        /// </summary>
        protected override void Run()
        {
            base.Run();
            IdeApp.Workbench.ActiveDocument.Save();

            TaskService.Errors.ClearByOwner(RunStyleCopHandler.styleCopAddinOwner);
            this.errorPad.ShowResults(this, new EventArgs());

            StyleCopConsole console = new StyleCopConsole(
                null,
                false,
                null,
                null,
                true);

            List<CodeProject> projects = this.GetCodeProjectList(console);

            console.OutputGenerated += this.OnOutputGenerated;
            console.ViolationEncountered += this.OnViolationEncountered;
            try
            {
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                console.Start(projects, false);
            }
            catch (Exception e)
            {
                this.logger.WriteLine("Error: {0}\nStackTrace:{1}", e.Message, e.StackTrace);
            }
            finally
            {
                console.OutputGenerated -= this.OnOutputGenerated;
                console.ViolationEncountered -= this.OnViolationEncountered;
                console.Dispose();
            }
        }
 /// <summary>
 /// Starts the style cop analysis.
 /// </summary>
 /// <param name="console">The console.</param>
 /// <param name="project">The project.</param>
 private static void StartStyleCopAnalysis(StyleCopConsole console, CodeProject project)
 {
     console.Start(new[] { project }, true);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Finds StyleCop+ analyzer and removes all other analyzers.
        /// </summary>
        private static StyleCopPlus ExtractStyleCopPlus(StyleCopConsole console)
        {
            StyleCopPlus styleCopPlus = null;
            foreach (SourceParser parser in console.Core.Parsers)
            {
                List<SourceAnalyzer> analyzersToRemove = new List<SourceAnalyzer>();
                foreach (SourceAnalyzer analyzer in parser.Analyzers)
                {
                    if (typeof(StyleCopPlus) == analyzer.GetType())
                    {
                        styleCopPlus = (StyleCopPlus)analyzer;
                        break;
                    }

                    analyzersToRemove.Add(analyzer);
                }

                foreach (SourceAnalyzer analyzer in analyzersToRemove)
                {
                    parser.Analyzers.Remove(analyzer);
                }
            }

            return styleCopPlus;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Creates a StyleCop report.
        /// </summary>
        /// <param name="outputXmlFile">
        /// The fully-qualified path to write the output of the report to.
        /// </param>
        public void Create(string outputXmlFile)
        {
            // Create a StyleCop configuration specifying the configuration
            // symbols to use for this report.
            var cfg = new Configuration(
                this.ProcessorSymbols != null
                    ?
                        this.ProcessorSymbols.ToArray()
                    :
                        null);

            // Create a new StyleCop console used to do the check.
            var scc = new StyleCopConsole(
                this.StyleCopSettingsFile,
                true,
                GetViolationsFile(outputXmlFile),
                this.AddInDirectories,
                true);

            // Process solution files
            if (this.SolutionFiles != null)
            {
                foreach (var i in this.SolutionFiles)
                {
                    this.AddSolutionFile(i);
                }
            }

            // Process project files
            if (this.ProjectFiles != null)
            {
                foreach (var i in this.ProjectFiles)
                {
                    this.AddProjectFile(
                        i,
                        null);
                }
            }

            // Process directories
            if (this.Directories != null)
            {
                foreach (var i in this.Directories)
                {
                    this.AddDirectory(i);
                }
            }

            // Process files
            if (this.Files != null)
            {
                foreach (var i in this.Files)
                {
                    this.AddFile(
                        i,
                        null,
                        null);
                }
            }

            // Create a list of code projects from the data set.
            var cps = this.Report.Projects.Select(
                r => new CodeProject(
                         r.ID,
                         r.Location,
                         cfg)).ToList();

            // Add the source code files to the style cop checker
            foreach (var f in this.Report.SourceCodeFiles)
            {
                // ReSharper disable AccessToModifiedClosure
                var cp = cps.SingleOrDefault(i => i.Key == f.CodeProjectID);
                scc.Core.Environment.AddSourceCode(
                    cp,
                    f.Path,
                    null);

                // ReSharper restore AccessToModifiedClosure
            }

            if (this.OutputGenerated != null)
            {
                scc.OutputGenerated += this.OutputGenerated;
            }

            scc.ViolationEncountered += this.ViolationEncountered;

            scc.Start(
                cps,
                true);

            if (this.OutputGenerated != null)
            {
                scc.OutputGenerated -= this.OutputGenerated;
            }

            scc.ViolationEncountered -= this.ViolationEncountered;
            scc.Dispose();

            // Write the report to the output XML file.
            this.Report.WriteXml(outputXmlFile);

            if (!string.IsNullOrEmpty(this.TransformFile))
            {
                this.Transform(outputXmlFile);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Executes this MSBuild task, based on the input values passed in by the MSBuild engine.
        /// </summary>
        /// <returns>Returns true if there were no errors, false otherwise.</returns>
        public override bool Execute()
        {
            // Clear the violation count and set the violation limit for the project.
            this.violationCount = 0;
            this.violationLimit = 0;

            if (this.maxViolationCount != null)
            {
                if (!int.TryParse(this.maxViolationCount.ItemSpec, out this.violationLimit))
                {
                    this.violationLimit = 0;
                }
            }

            if (this.violationLimit == 0)
            {
                this.violationLimit = DefaultViolationLimit;
            }

            // Get settings files (if null or empty use null filename so it uses right default).
            string overrideSettingsFileName = null;
            if (this.inputOverrideSettingsFile != null && this.inputOverrideSettingsFile.ItemSpec.Length > 0)
            {
                overrideSettingsFileName = this.inputOverrideSettingsFile.ItemSpec;
            }

            // Get addin paths.
            List<string> addinPaths = new List<string>();
            foreach (ITaskItem addinPath in this.inputAdditionalAddinPaths)
            {
                addinPaths.Add(addinPath.GetMetadata("FullPath"));
            }

            // Create the StyleCop console.
            using (StyleCopConsole console = new StyleCopConsole(
                overrideSettingsFileName,
                this.inputCacheResults,
                this.outputFile == null ? null : this.outputFile.ItemSpec,
                addinPaths,
                true))
            {
                // Create the configuration.
                Configuration configuration = new Configuration(this.inputDefineConstants);

                string projectFullPath = null;
                if (this.inputProjectFullPath != null)
                {
                    projectFullPath = this.inputProjectFullPath.GetMetadata("FullPath");
                }

                if (!string.IsNullOrEmpty(projectFullPath))
                {
                    // Create a CodeProject object for these files.
                    CodeProject project = new CodeProject(
                        projectFullPath.GetHashCode(),
                        projectFullPath,
                        configuration);

                    // Add each source file to this project.
                    foreach (ITaskItem inputSourceFile in this.inputSourceFiles)
                    {
                        console.Core.Environment.AddSourceCode(project, inputSourceFile.ItemSpec, null);
                    }

                    try
                    {
                        // Subscribe to events
                        console.OutputGenerated += this.OnOutputGenerated;
                        console.ViolationEncountered += this.OnViolationEncountered;

                        // Analyze the source files
                        CodeProject[] projects = new CodeProject[] { project };
                        console.Start(projects, this.inputForceFullAnalysis);
                    }
                    finally
                    {
                        // Unsubscribe from events
                        console.OutputGenerated -= this.OnOutputGenerated;
                        console.ViolationEncountered -= this.OnViolationEncountered;
                    }
                }
            }

            return this.succeeded;
        }
 /// <summary>
 /// Unsubscribe style cop console events.
 /// </summary>
 /// <param name="console">The console.</param>
 private static void UnsubscribeStyleCopConsoleEvents(StyleCopConsole console)
 {
     console.OutputGenerated -= OnOutputGenerated;
     console.ViolationEncountered -= OnViolationEncountered;
 }