Skip to content

Commit

Permalink
Console: Add option to write formatted output to stdout instead of mo…
Browse files Browse the repository at this point in the history
…difying the file (#441)

* Add console option to write to stdout instead of modifying the file

* --write-to-stdout requires exactly one file

* Cannot combine --passive and --write-to-stdout

* refactor options checking

* move to separate function
* print all issues instead of stopping after the first discovered error

* errors in Program are always printed to stderr

* make separate Logger class to make it available in Program

---------

Co-authored-by: Lysann Schlegel <[email protected]>
Co-authored-by: Dave Grochocki <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2023
1 parent 2f7fa4f commit 7bfd446
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 30 deletions.
24 changes: 24 additions & 0 deletions src/XamlStyler.Console/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// © Xavalon. All rights reserved.

namespace Xavalon.XamlStyler.Console
{
public sealed class Logger
{
private readonly System.IO.TextWriter writer;
private readonly LogLevel level;

public Logger(System.IO.TextWriter writer, LogLevel level)
{
this.writer = writer;
this.level = level;
}

public void Log(string line, LogLevel level = LogLevel.Default)
{
if (level <= this.level)
{
this.writer.WriteLine(line);
}
}
}
}
4 changes: 4 additions & 0 deletions src/XamlStyler.Console/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public sealed partial class CommandLineOptions
[Option('p', "passive", Default = false, HelpText = "Check files follow proper formatting without making any modifications. Returns error status if files fail the check.")]
public bool IsPassive { get; set; }

[Option("write-to-stdout", Default = false,
HelpText = "Instead of modifying the file, write to stdout. In this mode, logs are printed to stderr. Must specify exactly one file. Cannot be compbined with --passive.")]
public bool WriteToStdout { get; set; }

[Option('l', "loglevel", Default = LogLevel.Default,
HelpText = "Levels in order of increasing detail: None, Minimal, Default, Verbose, Debug")]
public LogLevel LogLevel { get; set; }
Expand Down
69 changes: 46 additions & 23 deletions src/XamlStyler.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,62 @@ public static int Main(string[] args)

result.WithNotParsed(_ =>
{
System.Console.WriteLine(writer.ToString());
System.Console.Error.WriteLine(writer.ToString());
Environment.Exit(1);
})
.WithParsed(options =>
{
if (options.LogLevel >= LogLevel.Debug)
{
System.Console.WriteLine($"File Parameter: '{options.File}'");
System.Console.WriteLine($"File Count: {options.File?.Count ?? -1}");
System.Console.WriteLine($"File Directory: '{options.Directory}'");
}

bool isFileOptionSpecified = (options.File?.Count ?? 0) != 0;
bool isDirectoryOptionSpecified = !String.IsNullOrEmpty(options.Directory);
Logger logger = new Logger(options.WriteToStdout ? System.Console.Error : System.Console.Out, options.LogLevel);

if (isFileOptionSpecified ^ isDirectoryOptionSpecified)
{
var xamlStylerConsole = new XamlStylerConsole(options);
xamlStylerConsole.Process(isFileOptionSpecified ? ProcessType.File : ProcessType.Directory);
}
else
ProcessType processType;
if (!CheckOptions(options, logger, out processType))
{
string errorString = (isFileOptionSpecified && isDirectoryOptionSpecified)
? "Cannot specify both file(s) and directory"
: "Must specify file(s) or directory";

System.Console.WriteLine($"\nError: {errorString}\n");

Environment.Exit(1);
}

var xamlStylerConsole = new XamlStylerConsole(options, logger);
xamlStylerConsole.Process(processType);
});

return 0;
}

private static bool CheckOptions(CommandLineOptions options, Logger logger, out ProcessType processType)
{
logger.Log($"File Parameter: '{options.File}'", LogLevel.Debug);
logger.Log($"File Count: {options.File?.Count ?? -1}", LogLevel.Debug);
logger.Log($"File Directory: '{options.Directory}'", LogLevel.Debug);

bool result = true;

int numFilesSpecified = options.File?.Count ?? 0;
bool isFileOptionSpecified = numFilesSpecified != 0;
bool isDirectoryOptionSpecified = !String.IsNullOrEmpty(options.Directory);
if (isFileOptionSpecified && isDirectoryOptionSpecified)
{
System.Console.Error.WriteLine($"\nError: Cannot specify both file(s) and directory\n");
result = false;
}
else if (!isFileOptionSpecified && !isDirectoryOptionSpecified)
{
System.Console.Error.WriteLine($"\nError: Must specify file(s) or directory\n");
result = false;
}

if (options.WriteToStdout && (isDirectoryOptionSpecified || numFilesSpecified != 1))
{
System.Console.Error.WriteLine($"\nError: When using --write-to-stdout you must specify exactly one file\n");
result = false;
}

if (options.WriteToStdout && options.IsPassive)
{
System.Console.Error.WriteLine($"\nError: Cannot specify both --passive and --write-to-stdout\n");
result = false;
}

processType = isFileOptionSpecified ? ProcessType.File : ProcessType.Directory;
return result;
}
}
}
}
27 changes: 20 additions & 7 deletions src/XamlStyler.Console/XamlStylerConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace Xavalon.XamlStyler.Console
public sealed class XamlStylerConsole
{
private readonly CommandLineOptions options;
private readonly Logger logger;
private readonly StylerService stylerService;

public XamlStylerConsole(CommandLineOptions options)
public XamlStylerConsole(CommandLineOptions options, Logger logger)
{
this.options = options;
this.logger = logger;

IStylerOptions stylerOptions = new StylerOptions();

Expand Down Expand Up @@ -263,6 +265,20 @@ private bool TryProcessFile(string file)
return false;
}
}
else if (this.options.WriteToStdout)
{
var prevEncoding = System.Console.OutputEncoding;
try
{
System.Console.OutputEncoding = encoding;
System.Console.Write(encoding.GetString(encoding.GetPreamble()));
System.Console.Write(formattedOutput);
}
finally
{
System.Console.OutputEncoding = prevEncoding;
}
}
else
{
this.Log($"\nFormatted Output:\n\n{formattedOutput}\n", LogLevel.Insanity);
Expand Down Expand Up @@ -332,12 +348,9 @@ private string GetConfigurationFromPath(string path)
return null;
}

private void Log(string value, LogLevel logLevel = LogLevel.Default)
private void Log(string line, LogLevel logLevel = LogLevel.Default)
{
if (logLevel <= this.options.LogLevel)
{
System.Console.WriteLine(value);
}
this.logger.Log(line, logLevel);
}
}
}
}

0 comments on commit 7bfd446

Please sign in to comment.