private static int RunFormat(CommandLineOptions options) { var cts = new CancellationTokenSource(); var ct = cts.Token; Console.CancelKeyPress += delegate { cts.Cancel(); }; try { RunFormatAsync(options, ct).Wait(ct); Console.WriteLine("Completed formatting."); return 0; } catch (AggregateException ex) { var typeLoadException = ex.InnerExceptions.FirstOrDefault() as ReflectionTypeLoadException; if (typeLoadException == null) throw; Console.WriteLine("ERROR: Type loading error detected. In order to run this tool you need either Visual Studio 2015 or Microsoft Build Tools 2015 tools installed."); var messages = typeLoadException.LoaderExceptions.Select(e => e.Message).Distinct(); foreach (var message in messages) Console.WriteLine("- {0}", message); return 1; } }
private static async Task<int> RunFormatAsync(CommandLineOptions options, CancellationToken cancellationToken) { var engine = FormattingEngine.Create(); engine.PreprocessorConfigurations = options.PreprocessorConfigurations; engine.FileNames = options.FileNames; engine.CopyrightHeader = options.CopyrightHeader; engine.AllowTables = options.AllowTables; engine.Verbose = options.Verbose; if (!SetRuleMap(engine, options.RuleMap)) { return 1; } foreach (var item in options.FormatTargets) { await RunFormatItemAsync(engine, item, options.Language, cancellationToken); } return 0; }
public static CommandLineParseResult CreateSuccess(CommandLineOptions options) { return new CommandLineParseResult(options: options); }
private CommandLineParseResult(CommandLineOptions options = null, string error = null) { _options = options; _error = error; }
public static bool TryParse(string[] args, out CommandLineOptions options) { var result = Parse(args); options = result.IsSuccess ? result.Options : null; return result.IsSuccess; }
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 = FormattingDefaults.DefaultCopyrightHeader; var ruleMap = ImmutableDictionary<string, bool>.Empty; var language = LanguageNames.CSharp; var allowTables = false; var verbose = false; ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false); for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg.StartsWith(ConfigSwitch, StringComparison.OrdinalIgnoreCase)) { var all = arg.Substring(ConfigSwitch.Length); var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); configBuilder.Add(configs); } else if (arg.StartsWith(CopyrightSwitch, StringComparison.OrdinalIgnoreCase)) { var fileName = arg.Substring(CopyrightSwitch.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 (arg.StartsWith(LanguageSwitch, StringComparison.OrdinalIgnoreCase)) { language = arg.Substring(LanguageSwitch.Length); } else if (comparer.Equals(arg, "/withcopyright")) { ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true); } 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 { 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(), language, allowTables, verbose); return CommandLineParseResult.CreateSuccess(options); }