diff --git a/src/ScriptCs.Hosting/FileConsole.cs b/src/ScriptCs.Hosting/FileConsole.cs new file mode 100644 index 00000000..dbac0953 --- /dev/null +++ b/src/ScriptCs.Hosting/FileConsole.cs @@ -0,0 +1,80 @@ +using System; +using System.IO; +using ScriptCs.Contracts; + +namespace ScriptCs.Hosting +{ + public class FileConsole : IConsole + { + private readonly string _path; + private readonly IConsole _innerConsole; + + public FileConsole(string path, IConsole innerConsole) + { + Guard.AgainstNullArgument("innerConsole", innerConsole); + + _path = path; + _innerConsole = innerConsole; + } + + public void Write(string value) + { + _innerConsole.Write(value); + this.Append(value); + } + + public void WriteLine() + { + _innerConsole.WriteLine(); + this.AppendLine(string.Empty); + } + + public void WriteLine(string value) + { + _innerConsole.WriteLine(value); + this.AppendLine(value); + } + + public string ReadLine() + { + var line = _innerConsole.ReadLine(); + this.AppendLine(line); + return line; + } + + public void Clear() + { + _innerConsole.Clear(); + } + + public void Exit() + { + _innerConsole.Exit(); + } + + public void ResetColor() + { + _innerConsole.ResetColor(); + } + + public ConsoleColor ForegroundColor + { + get { return _innerConsole.ForegroundColor; } + set { _innerConsole.ForegroundColor = value; } + } + + private void Append(string text) + { + using (var writer = new StreamWriter(_path, true)) + { + writer.Write(text); + writer.Flush(); + } + } + + private void AppendLine(string text) + { + this.Append(text + Environment.NewLine); + } + } +} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj b/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj index f3896106..02d58d20 100644 --- a/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj +++ b/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj @@ -1,7 +1,6 @@  - + Debug AnyCPU @@ -79,6 +78,7 @@ Guard.cs + diff --git a/src/ScriptCs/Program.cs b/src/ScriptCs/Program.cs index f9e3e593..4ff2ec14 100644 --- a/src/ScriptCs/Program.cs +++ b/src/ScriptCs/Program.cs @@ -3,6 +3,7 @@ using System.Runtime; using ScriptCs.Argument; using ScriptCs.Command; +using ScriptCs.Contracts; using ScriptCs.Hosting; namespace ScriptCs @@ -19,7 +20,7 @@ private static int Main(string[] args) } #endif - var console = new ScriptConsole(); + IConsole console = new ScriptConsole(); var parser = new ArgumentHandler(new ArgumentParser(console), new ConfigFileParser(console), new FileSystem()); var arguments = parser.Parse(args); @@ -27,6 +28,11 @@ private static int Main(string[] args) var commandArgs = arguments.CommandArguments; var scriptArgs = arguments.ScriptArguments; + if (!string.IsNullOrWhiteSpace(commandArgs.Output)) + { + console = new FileConsole(commandArgs.Output, console); + } + var configurator = new LoggerConfigurator(commandArgs.LogLevel); configurator.Configure(console); var logger = configurator.GetLogger(); diff --git a/src/ScriptCs/ScriptCsArgs.cs b/src/ScriptCs/ScriptCsArgs.cs index 0a4cf011..83c3ba03 100644 --- a/src/ScriptCs/ScriptCsArgs.cs +++ b/src/ScriptCs/ScriptCsArgs.cs @@ -69,5 +69,7 @@ public ScriptCsArgs() [ArgDescription("Defines the version of the package to install. Used in conjunction with -install")] public string PackageVersion { get; set; } + [ArgDescription("Write all console output to the specified file")] + public string Output { get; set; } } } \ No newline at end of file