private static int ReturnExitCode(
            CommandLineOptions commandLineOptions,
            CommandLineFormatterResult result
            )
        {
            if (
                (commandLineOptions.Check && result.UnformattedFiles > 0) ||
                result.FailedSyntaxTreeValidation > 0 ||
                result.ExceptionsFormatting > 0 ||
                result.ExceptionsValidatingSource > 0
                )
            {
                return(1);
            }

            return(0);
        }
 protected CommandLineFormatter(
     string baseDirectoryPath,
     string path,
     CommandLineOptions commandLineOptions,
     PrinterOptions printerOptions,
     IFileSystem fileSystem,
     IConsole console,
     IgnoreFile ignoreFile,
     CommandLineFormatterResult result
     )
 {
     this.BaseDirectoryPath = baseDirectoryPath;
     this.Path               = path;
     this.PrinterOptions     = printerOptions;
     this.CommandLineOptions = commandLineOptions;
     this.FileSystem         = fileSystem;
     this.Console            = console;
     this.IgnoreFile         = ignoreFile;
     this.Result             = result;
 }
        public static async Task <int> Format(
            CommandLineOptions commandLineOptions,
            IFileSystem fileSystem,
            IConsole console,
            CancellationToken cancellationToken
            )
        {
            var stopwatch = Stopwatch.StartNew();
            var result    = new CommandLineFormatterResult();

            foreach (var path in commandLineOptions.DirectoryOrFilePaths)
            {
                var normalizedPath    = path.Replace('\\', '/');
                var baseDirectoryPath = fileSystem.File.Exists(normalizedPath)
                    ? fileSystem.Path.GetDirectoryName(normalizedPath)
                    : path;

                if (baseDirectoryPath == null)
                {
                    throw new Exception(
                              $"The path of {normalizedPath} does not appear to point to a directory or a file."
                              );
                }

                var configurationFileOptions = ConfigurationFileOptions.Create(
                    baseDirectoryPath,
                    fileSystem
                    );

                var ignoreFile =
                    await IgnoreFile.Create(
                        baseDirectoryPath,
                        fileSystem,
                        console,
                        cancellationToken
                        );

                if (ignoreFile is null)
                {
                    return(1);
                }

                var printerOptions = new PrinterOptions
                {
                    TabWidth  = configurationFileOptions.TabWidth,
                    UseTabs   = configurationFileOptions.UseTabs,
                    Width     = configurationFileOptions.PrintWidth,
                    EndOfLine = configurationFileOptions.EndOfLine
                };

                var commandLineFormatter = new CommandLineFormatter(
                    baseDirectoryPath,
                    normalizedPath,
                    commandLineOptions,
                    printerOptions,
                    fileSystem,
                    console,
                    ignoreFile,
                    result
                    );

                await commandLineFormatter.FormatFiles(cancellationToken);
            }

            result.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
            ResultPrinter.PrintResults(result, console, commandLineOptions);
            return(ReturnExitCode(commandLineOptions, result));
        }
Esempio n. 4
0
        public static async Task <int> Format(
            CommandLineOptions commandLineOptions,
            IFileSystem fileSystem,
            IConsole console,
            CancellationToken cancellationToken
            )
        {
            var stopwatch = Stopwatch.StartNew();
            var result    = new CommandLineFormatterResult();

            async Task <CommandLineFormatter?> CreateFormatter(string path)
            {
                var normalizedPath    = path.Replace('\\', '/');
                var baseDirectoryPath = fileSystem.File.Exists(normalizedPath)
                    ? fileSystem.Path.GetDirectoryName(normalizedPath)
                    : path;

                if (baseDirectoryPath == null)
                {
                    throw new Exception(
                              $"The path of {normalizedPath} does not appear to point to a directory or a file."
                              );
                }

                var printerOptions = ConfigurationFileOptions.CreatePrinterOptions(
                    baseDirectoryPath,
                    fileSystem
                    );

                var ignoreFile = await IgnoreFile.Create(
                    baseDirectoryPath,
                    fileSystem,
                    console,
                    cancellationToken
                    );

                if (ignoreFile is null)
                {
                    return(null);
                }

                return(new CommandLineFormatter(
                           baseDirectoryPath,
                           normalizedPath,
                           commandLineOptions,
                           printerOptions,
                           fileSystem,
                           console,
                           ignoreFile,
                           result
                           ));
            }

            if (commandLineOptions.StandardInFileContents != null)
            {
                var path = commandLineOptions.DirectoryOrFilePaths[0];

                var commandLineFormatter = await CreateFormatter(path);

                if (commandLineFormatter == null)
                {
                    return(1);
                }

                await commandLineFormatter.FormatFile(
                    commandLineOptions.StandardInFileContents,
                    path,
                    console.InputEncoding,
                    cancellationToken
                    );
            }
            else
            {
                foreach (var path in commandLineOptions.DirectoryOrFilePaths)
                {
                    var commandLineFormatter = await CreateFormatter(path);

                    if (commandLineFormatter == null)
                    {
                        return(1);
                    }

                    await commandLineFormatter.FormatFiles(cancellationToken);
                }
            }

            result.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
            if (!commandLineOptions.ShouldWriteStandardOut)
            {
                ResultPrinter.PrintResults(result, console, commandLineOptions);
            }
            return(ReturnExitCode(commandLineOptions, result));
        }