From 49fd3451926de85823996e58df9eacb8b3ca6bba Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 6 Apr 2016 16:23:28 -0700 Subject: [PATCH] Remove Microsoft.DotNet.Cli.Utils --- NuGet.master.config | 7 - .../Internal/AnsiColorExtensions.cs | 18 +++ .../Internal/AnsiConsole.cs | 145 ++++++++++++++++++ .../Internal/Reporter.cs | 46 ++++++ .../Program.cs | 4 +- .../PublishIISCommand.cs | 2 +- .../project.json | 1 - src/dotnet-publish-iis/Program.cs | 4 +- src/dotnet-publish-iis/PublishIISCommand.cs | 2 +- src/dotnet-publish-iis/project.json | 3 +- 10 files changed, 216 insertions(+), 16 deletions(-) delete mode 100644 NuGet.master.config create mode 100644 src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiColorExtensions.cs create mode 100644 src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiConsole.cs create mode 100644 src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/Reporter.cs diff --git a/NuGet.master.config b/NuGet.master.config deleted file mode 100644 index b004e5cc74..0000000000 --- a/NuGet.master.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiColorExtensions.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiColorExtensions.cs new file mode 100644 index 0000000000..28ff11db48 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiColorExtensions.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Extensions.Cli.Utils +{ + public static class AnsiColorExtensions + { + public static string Red(this string text) + { + return "\x1B[31m" + text + "\x1B[39m"; + } + + public static string Yellow(this string text) + { + return "\x1B[33m" + text + "\x1B[39m"; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiConsole.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiConsole.cs new file mode 100644 index 0000000000..1ea22a6cd0 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/AnsiConsole.cs @@ -0,0 +1,145 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IO; + +namespace Microsoft.Extensions.Cli.Utils +{ + public class AnsiConsole + { + private AnsiConsole(TextWriter writer) + { + Writer = writer; + + OriginalForegroundColor = Console.ForegroundColor; + } + + private int _boldRecursion; + + public static AnsiConsole GetOutput() + { + return new AnsiConsole(Console.Out); + } + + public static AnsiConsole GetError() + { + return new AnsiConsole(Console.Error); + } + + public TextWriter Writer { get; } + + public ConsoleColor OriginalForegroundColor { get; } + + private void SetColor(ConsoleColor color) + { + const int Light = 0x08; + int c = (int)color; + + Console.ForegroundColor = + c < 0 ? color : // unknown, just use it + _boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light + (ConsoleColor)(c & ~Light); // ensure color is dark + } + + private void SetBold(bool bold) + { + _boldRecursion += bold ? 1 : -1; + if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold)) + { + return; + } + + // switches on _boldRecursion to handle boldness + SetColor(Console.ForegroundColor); + } + + public void WriteLine(string message) + { + Write(message); + Writer.WriteLine(); + } + + + public void Write(string message) + { + var escapeScan = 0; + for (;;) + { + var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal); + if (escapeIndex == -1) + { + var text = message.Substring(escapeScan); + Writer.Write(text); + break; + } + else + { + var startIndex = escapeIndex + 2; + var endIndex = startIndex; + while (endIndex != message.Length && + message[endIndex] >= 0x20 && + message[endIndex] <= 0x3f) + { + endIndex += 1; + } + + var text = message.Substring(escapeScan, escapeIndex - escapeScan); + Writer.Write(text); + if (endIndex == message.Length) + { + break; + } + + switch (message[endIndex]) + { + case 'm': + int value; + if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value)) + { + switch (value) + { + case 1: + SetBold(true); + break; + case 22: + SetBold(false); + break; + case 30: + SetColor(ConsoleColor.Black); + break; + case 31: + SetColor(ConsoleColor.Red); + break; + case 32: + SetColor(ConsoleColor.Green); + break; + case 33: + SetColor(ConsoleColor.Yellow); + break; + case 34: + SetColor(ConsoleColor.Blue); + break; + case 35: + SetColor(ConsoleColor.Magenta); + break; + case 36: + SetColor(ConsoleColor.Cyan); + break; + case 37: + SetColor(ConsoleColor.Gray); + break; + case 39: + Console.ForegroundColor = OriginalForegroundColor; + break; + } + } + break; + } + + escapeScan = endIndex + 1; + } + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/Reporter.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/Reporter.cs new file mode 100644 index 0000000000..be9e92bec3 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/Reporter.cs @@ -0,0 +1,46 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Extensions.Cli.Utils +{ + // Stupid-simple console manager + public class Reporter + { + private static readonly Reporter NullReporter = new Reporter(console: null); + private static object _lock = new object(); + + private readonly AnsiConsole _console; + + private Reporter(AnsiConsole console) + { + _console = console; + } + + public static Reporter Output { get; } = new Reporter(AnsiConsole.GetOutput()); + public static Reporter Error { get; } = new Reporter(AnsiConsole.GetError()); + + public void WriteLine(string message) + { + lock (_lock) + { + _console?.WriteLine(message); + } + } + + public void WriteLine() + { + lock (_lock) + { + _console?.Writer?.WriteLine(); + } + } + + public void Write(string message) + { + lock (_lock) + { + _console?.Write(message); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs index 665846b1ea..8a341e5f18 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/Program.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.DotNet.Cli.Utils; +using Microsoft.Extensions.Cli.Utils; using Microsoft.Extensions.CommandLineUtils; namespace Microsoft.AspNetCore.Server.IISIntegration.Tools @@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.Tools catch (Exception e) { Reporter.Error.WriteLine(e.Message.Red()); - Reporter.Verbose.WriteLine(e.ToString().Yellow()); + Reporter.Output.WriteLine(e.ToString().Yellow()); } return 1; diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs index 4b24eb5783..56db9baf76 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/PublishIISCommand.cs @@ -5,8 +5,8 @@ using System; using System.IO; using System.Xml; using System.Xml.Linq; -using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.ProjectModel; +using Microsoft.Extensions.Cli.Utils; namespace Microsoft.AspNetCore.Server.IISIntegration.Tools { diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/project.json b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/project.json index 99b1acc3d5..23d25c756f 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/project.json +++ b/src/Microsoft.AspNetCore.Server.IISIntegration.Tools/project.json @@ -12,7 +12,6 @@ "dependencies": { "Microsoft.Extensions.CommandLineUtils": "1.0.0-*", "Microsoft.DotNet.ProjectModel": "1.0.0-*", - "Microsoft.DotNet.Cli.Utils": "1.0.0-*", "System.Diagnostics.Process": "4.1.0-*" }, diff --git a/src/dotnet-publish-iis/Program.cs b/src/dotnet-publish-iis/Program.cs index 9d00b0104b..b61c8668fa 100644 --- a/src/dotnet-publish-iis/Program.cs +++ b/src/dotnet-publish-iis/Program.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.DotNet.Cli.Utils; +using Microsoft.Extensions.Cli.Utils; using Microsoft.Extensions.CommandLineUtils; namespace Microsoft.AspNetCore.Tools.PublishIIS @@ -49,7 +49,7 @@ namespace Microsoft.AspNetCore.Tools.PublishIIS catch (Exception e) { Reporter.Error.WriteLine(e.Message.Red()); - Reporter.Verbose.WriteLine(e.ToString().Yellow()); + Reporter.Output.WriteLine(e.ToString().Yellow()); } return 1; diff --git a/src/dotnet-publish-iis/PublishIISCommand.cs b/src/dotnet-publish-iis/PublishIISCommand.cs index 6bb3f66d76..70fdc77857 100644 --- a/src/dotnet-publish-iis/PublishIISCommand.cs +++ b/src/dotnet-publish-iis/PublishIISCommand.cs @@ -5,8 +5,8 @@ using System; using System.IO; using System.Xml; using System.Xml.Linq; -using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.ProjectModel; +using Microsoft.Extensions.Cli.Utils; using Microsoft.Extensions.Configuration; namespace Microsoft.AspNetCore.Tools.PublishIIS diff --git a/src/dotnet-publish-iis/project.json b/src/dotnet-publish-iis/project.json index a57737ff80..c785477fda 100644 --- a/src/dotnet-publish-iis/project.json +++ b/src/dotnet-publish-iis/project.json @@ -8,12 +8,11 @@ "nowarn": [ "CS1591" ], "xmlDoc": true }, - + "compile": "../Microsoft.AspNetCore.Server.IISIntegration.Tools/Internal/*.cs", "dependencies": { "Microsoft.Extensions.CommandLineUtils": "1.0.0-*", "Microsoft.Extensions.Configuration.Json": "1.0.0-*", "Microsoft.DotNet.ProjectModel": "1.0.0-*", - "Microsoft.DotNet.Cli.Utils": "1.0.0-*", "System.Diagnostics.Process": "4.1.0-*" },