コード例 #1
0
        private static void Main(string[] args)
        {
            string attribute       = "";
            string csvOutputFile   = "";
            string directoryToScan = "";
            string negativeSearch  = "";
            Dictionary <String, List <Result> > results = new Dictionary <string, List <Result> >();

            try
            {
                var  options     = new Options();
                bool isFlagValid = options.OptionParser(args, out csvOutputFile, out attribute, out directoryToScan, out negativeSearch);

                // If Command-line options not set correctly, show default message and exit
                if (!isFlagValid)
                {
                    System.Environment.Exit(0);
                }

                if (String.IsNullOrEmpty(csvOutputFile))
                {
                    DateTime dt        = DateTime.Now;
                    String   timestamp = dt.ToString("yyyyMMddHHmmss");
                    csvOutputFile = "enumerated_controllers_" + timestamp + ".csv";
                }

                string curDir = Directory.GetCurrentDirectory();
                if (String.IsNullOrEmpty(directoryToScan))
                {
                    directoryToScan = curDir;
                }
                string[] paths = Directory.GetFiles(directoryToScan, "*.cs", SearchOption.AllDirectories);

                if (paths.Length > 0)
                {
                    foreach (var path in paths)
                    {
                        using (var stream = File.OpenRead(path))
                        {
                            var        tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
                            SyntaxNode root = tree.GetRoot();

                            // Check if the Class inherits Apicontroller or Controller and print out all the public entry points
                            ControllerChecker controllerchk = new ControllerChecker();
                            if (controllerchk.inheritsFromController(root, attribute))
                            {
                                controllerchk.enumerateEntrypoints(root, attribute, negativeSearch, path, results);
                            }
                        }
                    }

                    string[] controllerPaths = results.Keys.ToArray();
                    String   pathToTrim      = getPathToTrim(controllerPaths);

                    if (!String.IsNullOrEmpty(attribute) || !String.IsNullOrEmpty(negativeSearch))
                    {
                        printCommandLineResults(results, pathToTrim);
                    }

                    printCSVResults(results, csvOutputFile, pathToTrim);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("Invalid Path");
            }
            catch (IndexOutOfRangeException)
            {
                //Shoudn't Reach this, but in case
                Console.WriteLine("No Arguments passed");
            }
            catch (UnauthorizedAccessException e)
            {
                e.GetBaseException();
                Console.WriteLine("You do not seem to have appropiate Permissions on this direcctory");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("The operating system is Windows CE, which does not have current directory functionality.");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Illegal characters passed as arguments! ");
            }
            catch (Exception)
            {
                Console.WriteLine("Unexpected error");
            }
        }
コード例 #2
0
        private static void Processor(Options o)
        {
            try
            {
                var results = new Dictionary <string, List <Result> >();

                if (string.IsNullOrEmpty(o.CsvOutput))
                {
                    DateTime dt        = DateTime.Now;
                    string   timestamp = dt.ToString("yyyyMMddHHmmss");
                    o.CsvOutput = "enumerated_controllers_" + timestamp + ".csv";
                }

                //var documents = LoadSolution(o.SolutionPath);

                string[] paths = Directory.GetFiles(o.Directory, "*.cs", SearchOption.AllDirectories);

                if (paths.Any())
                {
                    foreach (var path in paths)
                    {
                        using (var stream = File.OpenRead(path))
                        {
                            var        tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
                            SyntaxNode root = tree.GetRoot();

                            // Check if the Class inherits Apicontroller or Controller and print out all the public entry points
                            ControllerChecker controllerchk = new ControllerChecker();
                            if (controllerchk.inheritsFromController(root, o.AttributeSearch))
                            {
                                controllerchk.enumerateEntrypoints(root, o.AttributeSearch, o.NegativeSearch, path, results);
                            }
                        }
                    }

                    string[] controllerPaths = results.Keys.ToArray();
                    string   pathToTrim      = getPathToTrim(controllerPaths);

                    if (!string.IsNullOrEmpty(o.AttributeSearch) || !string.IsNullOrEmpty(o.NegativeSearch))
                    {
                        printCommandLineResults(results, pathToTrim);
                    }

                    printCSVResults(results, o.CsvOutput, pathToTrim);
                }
                else
                {
                    Console.WriteLine("Unable to find any document from solution line");
                }
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("Invalid Path");
            }
            catch (IndexOutOfRangeException)
            {
                //Shoudn't Reach this, but in case
                Console.WriteLine("No Arguments passed");
            }
            catch (UnauthorizedAccessException e)
            {
                e.GetBaseException();
                Console.WriteLine("You do not seem to have appropiate Permissions on this direcctory");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("The operating system is Windows CE, which does not have current directory functionality.");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Illegal characters passed as arguments! ");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unexpected error {e}");
            }
        }