Example #1
0
        private static int RunFormatCommand(FormatOptions 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;
            }
        }
Example #2
0
        private static async Task<int> RunFormatAsync(FormatOptions options, CancellationToken cancellationToken)
        {
            var engine = FormattingEngine.Create(s_defaultCompositionAssemblies);

            var configBuilder = ImmutableArray.CreateBuilder<string[]>();
            configBuilder.Add(options.PreprocessorConfigurations.ToArray());            
            engine.PreprocessorConfigurations = configBuilder.ToImmutableArray();

            engine.Verbose = options.Verbose;
            engine.AllowTables = options.DefineDotNetFormatter;
            engine.FileNames = options.Files.ToImmutableArray();
            engine.CopyrightHeader = options.CopyrightHeaderText;

            if (options.UseAnalyzers)
            {
                if (!SetDiagnosticsEnabledMap(engine, options.RuleMap))
                {
                    return 1;
                }
            }
            else
            {
                if (!SetRuleMap(engine, options.RuleMap))
                {
                    return 1;
                }
            }

            foreach (var item in options.FormatTargets)
            {
                await RunFormatItemAsync(engine, item, options.Language, options.UseAnalyzers, cancellationToken);
            }

            return 0;
        }