private string GetPath(string file)
 {
     return(ResultPrinter.PadToSize(file.Substring(this.BaseDirectoryPath.Length)));
 }
        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));
        }
示例#3
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));
        }