private static int ApplyFormatter(string fileName, AnalyzerAndFixer[] rules) { string text; try { text = File.ReadAllText(fileName); } catch { AnsiConsole.GetError(useConsoleColor: true).WriteLine($"Could not read file {fileName}"); return(ExitFailed); } var tree = SyntaxFactory.ParseSyntaxTree(text, path: fileName); var context = new FormatterAnalysisContext(); foreach (var rule in rules) { rule.Analyzer.Initialize(context); } foreach (var syntaxTreeActions in context.RegisteredSyntaxTreeActions) { var syntaxTreeContext = new SyntaxTreeAnalysisContext( tree, options: null, reportDiagnostic: delegate { }, isSupportedDiagnostic: diag => false, cancellationToken: default(CancellationToken)); } return(0); }
public int Main(string[] args) { #if DEBUG // Add our own debug helper because DNU is usually run from a wrapper script, // making it too late to use the DNX one. Technically it's possible to use // the DNX_OPTIONS environment variable, but that's difficult to do per-command // on Windows if (args.Any(a => string.Equals(a, "--debug", StringComparison.OrdinalIgnoreCase))) { args = args.Where(a => !string.Equals(a, "--debug", StringComparison.OrdinalIgnoreCase)).ToArray(); Console.WriteLine($"Process Id: {Process.GetCurrentProcess().Id}"); Console.WriteLine("Waiting for Debugger to attach..."); SpinWait.SpinUntil(() => Debugger.IsAttached); } #endif var app = new CommandLineApplication(); app.Name = "dnu"; app.FullName = "Microsoft .NET Development Utility"; var optionVerbose = app.Option("-v|--verbose", "Show verbose output", CommandOptionType.NoValue); app.HelpOption("-?|-h|--help"); app.VersionOption("--version", () => _runtimeEnv.GetShortVersion(), () => _runtimeEnv.GetFullVersion()); // Show help information if no subcommand/option was specified app.OnExecute(() => { app.ShowHelp(); return(2); }); // Defer reading option verbose until AFTER execute. var reportsFactory = new ReportsFactory(_runtimeEnv, () => optionVerbose.HasValue()); BuildConsoleCommand.Register(app, reportsFactory, _hostServices); CommandsConsoleCommand.Register(app, reportsFactory, _environment); InstallConsoleCommand.Register(app, reportsFactory, _environment); ListConsoleCommand.Register(app, reportsFactory, _environment); PackConsoleCommand.Register(app, reportsFactory, _hostServices); PackagesConsoleCommand.Register(app, reportsFactory); PublishConsoleCommand.Register(app, reportsFactory, _environment, _hostServices); RestoreConsoleCommand.Register(app, reportsFactory, _environment, _runtimeEnv); WrapConsoleCommand.Register(app, reportsFactory); FeedsConsoleCommand.Register(app, reportsFactory); ClearCacheConsoleCommand.Register(app, reportsFactory); try { return(app.Execute(args)); } catch (CommandParsingException ex) { AnsiConsole.GetError(useConsoleColor: _runtimeEnv.OperatingSystem == "Windows").WriteLine($"Error: {ex.Message}".Red().Bold()); ex.Command.ShowHelp(); return(1); } #if DEBUG catch { throw; }
/// <summary> /// Resets the Reporters to write to the current Console Out/Error. /// </summary> public static void Reset() { lock (_lock) { Output = new Reporter(AnsiConsole.GetOutput()); Error = new Reporter(AnsiConsole.GetError()); } }
/// <summary> /// Resets the Reporters to write to the current Console Out/Error. /// </summary> public static void Reset() { lock (_lock) { Output = new Reporter(AnsiConsole.GetOutput()); Error = new Reporter(AnsiConsole.GetError()); Verbose = IsVerbose ? new Reporter(AnsiConsole.GetOutput()) : NullReporter; } }
public static Reports CreateReports(bool verbose, bool quiet) { bool useConsoleColor = _runtimeEnv.OperatingSystem == "Windows"; IReport report = new Report(AnsiConsole.GetOutput(useConsoleColor)); Reports reports = new Reports { Information = report, Verbose = verbose ? report : Reports.Constants.NullReport, Error = new Report(AnsiConsole.GetError(useConsoleColor)) }; reports.Quiet = (quiet ? reports.Verbose : report); return(reports); }
/// <summary> /// Initializes the specified application. /// </summary> /// <param name="app">The application.</param> /// <returns></returns> public static CommandLineApplication Initialize(this CommandLineApplication app) { Helper.Load(); AnsiConsole.GetError(true); app.HelpOption(HelpFlag); app.VersionOption(VersionFlag, Constants.ShortVersion, Constants.LongVersion); app.Name = Constants.Name; app.Description = Constants.ProgramHelpDescription; app.ExtendedHelpText = Constants.ExtendedHelpText; return(app); }
public Reports CreateReports(bool verbose, bool quiet) { var useConsoleColor = _runtimeEnv.OperatingSystem == "Windows"; IReport output = new Report(AnsiConsole.GetOutput(useConsoleColor)); var reports = new Reports() { Information = output, Verbose = verbose ? output : Reports.Constants.NullReport, Error = new Report(AnsiConsole.GetError(useConsoleColor)) }; // If "--verbose" and "--quiet" are specified together, "--verbose" wins reports.Quiet = quiet ? reports.Verbose : output; return(reports); }
public static int Main(string[] args) { var app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet fmt", FullName = "dotnet fmt", Description = "Format dotnet code according to .NET coding guidelines" }; var lang = app.Argument("[language]", "Programming language, c# by default"); var sources = app.Argument("<source files>", "Source files to format", multipleValues: true); app.OnExecute(() => { var rules = new[] { new AnalyzerAndFixer(new MultipleBlankLines(), new MultipleBlankLinesFix()) }; foreach (var fileName in sources.Values) { var result = ApplyFormatter(fileName, rules); if (result != 0) { return(result); } } return(0); }); try { return(app.Execute(args)); } catch (Exception e) { AnsiConsole.GetError(useConsoleColor: true).WriteLine(e.Message); return(ExitFailed); } }
private static async Task <int> Execute() { try { using (AccountsContext context = new AccountsDesignTimeDbContextFactory().CreateDbContext()) { await context.Database.MigrateAsync().ConfigureAwait(false); } using (ServerContext context = new ServerDesignTimeDbContextFactory().CreateDbContext()) { await context.Database.MigrateAsync().ConfigureAwait(false); } return(0); } catch (Exception e) { AnsiConsole.GetError(true).WriteLine(e.Message); return(-1); } }
/// <summary> /// Initializes the specified application. /// </summary> /// <param name="app">The application.</param> /// <returns></returns> public static CustomCommandLineApplication Initialize(this CustomCommandLineApplication app) { AnsiConsole.GetError(true); app.HelpOption(HelpFlag); app.VersionOption(VersionFlag, Constants.ShortVersion, Constants.LongVersion); app.Name = Constants.Name; app.Description = Constants.ProgramHelpDescription; app.ExtendedHelpText = Constants.ExtendedHelpText; var methods = typeof(Command).GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(c => c.Name != "Initialize").ToList(); methods = methods.Where(c => typeof(CustomCommandLineApplication).IsAssignableFrom(c.ReturnType) && c.GetParameters() is ParameterInfo[] u && u.Length == 1 && typeof(CommandLineApplication).IsAssignableFrom(u[0].ParameterType) ).ToList(); foreach (MethodInfo method in methods) { try { method.Invoke(null, new object[] { app }); } catch (Exception e) { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } throw; } } return(app); }