コード例 #1
0
ファイル: Program.cs プロジェクト: xamarin/codeformatter
        private static void RunListRules()
        {
            var rules = FormattingEngine.GetFormattingRules();

            Console.WriteLine("{0,-20} {1}", "Name", "Description");
            Console.WriteLine("==============================================");
            foreach (var rule in rules)
            {
                Console.WriteLine("{0,-20} :{1} {2}", rule.Name, rule.Description, !rule.IsDefaultEnabled ? "(disabled by default)" : string.Empty);
            }
        }
コード例 #2
0
        private static void RunListRules()
        {
            var rules = FormattingEngine.GetFormattingRules();

            Console.WriteLine("{0,-20} {1}", "Name", "Description");
            Console.WriteLine("==============================================");
            foreach (var rule in rules)
            {
                Console.WriteLine("{0,-20} :{1}", rule.Name, rule.Description);
            }
        }
コード例 #3
0
        private static void ListRulesAndAnalyzers(bool listAnalyzers, bool listRules)
        {
            Console.WriteLine("{0,-20} {1}", "Name", "Title");
            Console.WriteLine("==============================================");

            if (listAnalyzers)
            {
                ImmutableArray <DiagnosticDescriptor> diagnosticDescriptors = FormattingEngine.GetSupportedDiagnostics(OptionsHelper.DefaultCompositionAssemblies);
                foreach (var diagnosticDescriptor in diagnosticDescriptors)
                {
                    Console.WriteLine("{0,-20} :{1}", diagnosticDescriptor.Id, diagnosticDescriptor.Title);
                }
            }

            if (listRules)
            {
                var rules = FormattingEngine.GetFormattingRules();
                foreach (var rule in rules)
                {
                    Console.WriteLine("{0,-20} :{1}", rule.Name, rule.Description);
                }
            }
        }
コード例 #4
0
        public static CommandLineParseResult Parse(string[] args)
        {
            var comparer                = StringComparer.OrdinalIgnoreCase;
            var comparison              = StringComparison.OrdinalIgnoreCase;
            var formatTargets           = new List <string>();
            var fileNames               = new List <string>();
            var configBuilder           = ImmutableArray.CreateBuilder <string[]>();
            var copyrightHeader         = ImmutableArray <string> .Empty;
            var ruleMap                 = ImmutableDictionary <string, bool> .Empty;
            var language                = LanguageNames.CSharp;
            var allowTables             = false;
            var verbose                 = false;
            var useEditorConfig         = true;
            var additionalFileItemNames = default(string);
            var msBuildPath             = default(string);

            foreach (var rule in FormattingEngine.GetFormattingRules())
            {
                UpdateRuleMap(ref ruleMap, rule.Name, rule.IsDefaultEnabled);
            }

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg.StartsWith(ConfigSwitch, comparison))
                {
                    var all     = arg.Substring(ConfigSwitch.Length);
                    var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    configBuilder.Add(configs);
                }
                else if (comparer.Equals(arg, "/copyright+") || comparer.Equals(arg, "/copyright"))
                {
                    ruleMap         = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);
                    copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
                }
                else if (arg.StartsWith(CopyrightWithFileSwitch, comparison))
                {
                    ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);

                    var fileName = arg.Substring(CopyrightWithFileSwitch.Length);
                    try
                    {
                        copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
                    }
                    catch (Exception ex)
                    {
                        string error = string.Format("Could not read {0}{1}{2}",
                                                     fileName,
                                                     Environment.NewLine,
                                                     ex.Message);
                        return(CommandLineParseResult.CreateError(error));
                    }
                }
                else if (comparer.Equals(arg, "/copyright-") || comparer.Equals(arg, "/nocopyright"))
                {   // We still check /nocopyright for backwards compat
                    ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
                }
                else if (comparer.Equals(args, "/noEditorConfig"))
                {
                    useEditorConfig = false;
                }
                else if (arg.StartsWith(AdditionalFileItenNamesSwitch, comparison))
                {
                    additionalFileItemNames = arg.Substring(AdditionalFileItenNamesSwitch.Length);
                }
                else if (arg.StartsWith(MsBuildPathSwitch, comparison))
                {
                    msBuildPath = arg.Substring(MsBuildPathSwitch.Length).Trim('"');
                }
                else if (arg.StartsWith(LanguageSwitch, comparison))
                {
                    language = arg.Substring(LanguageSwitch.Length);
                }
                else if (comparer.Equals(arg, "/nounicode"))
                {
                    ruleMap = ruleMap.SetItem(FormattingDefaults.UnicodeLiteralsRuleName, false);
                }
                else if (comparer.Equals(arg, "/verbose"))
                {
                    verbose = true;
                }
                else if (arg.StartsWith(FileSwitch, comparison))
                {
                    fileNames.Add(arg.Substring(FileSwitch.Length));
                }
                else if (arg.StartsWith(RuleEnabledSwitch1, comparison))
                {
                    UpdateRuleMap(ref ruleMap, arg.Substring(RuleEnabledSwitch1.Length), enabled: true);
                }
                else if (arg.StartsWith(RuleEnabledSwitch2, comparison))
                {
                    UpdateRuleMap(ref ruleMap, arg.Substring(RuleEnabledSwitch2.Length), enabled: true);
                }
                else if (arg.StartsWith(RuleDisabledSwitch, comparison))
                {
                    UpdateRuleMap(ref ruleMap, arg.Substring(RuleDisabledSwitch.Length), enabled: false);
                }
                else if (comparer.Equals(arg, "/tables"))
                {
                    allowTables = true;
                }
                else if (comparer.Equals(arg, "/rules"))
                {
                    return(CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules));
                }
                else if (comparer.Equals(arg, "/?") || comparer.Equals(arg, "/help"))
                {
                    return(CommandLineParseResult.CreateSuccess(CommandLineOptions.ShowHelp));
                }
                else if (arg.StartsWith("/", comparison))
                {
                    return(CommandLineParseResult.CreateError($"Unrecognized option \"{arg}\""));
                }
                else
                {
                    formatTargets.Add(arg);
                }
            }

            if (formatTargets.Count == 0)
            {
                return(CommandLineParseResult.CreateError("Must specify at least one project / solution / rsp to format"));
            }

            var options = new CommandLineOptions(
                Operation.Format,
                configBuilder.ToImmutableArray(),
                copyrightHeader,
                ruleMap,
                formatTargets.ToImmutableArray(),
                fileNames.ToImmutableArray(),
                useEditorConfig,
                additionalFileItemNames,
                msBuildPath,
                language,
                allowTables,
                verbose);

            return(CommandLineParseResult.CreateSuccess(options));
        }