From 4e43c96b56a93724e101b040c7c004907f78bed9 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Mon, 27 Feb 2017 12:16:30 -0600 Subject: [PATCH 1/4] (GH-1095) Always log Nuget.Core messages Some messages coming from NuGet.Core are error messages, so it is important to show those messages without having NuGet logging only coming through during debugging calls. Updating NuGet Core to only log to Info on important messages and adding both a verbose and fatal level for NuGet messages helps reduce the amount of output from NuGet. --- .../nuget/ChocolateyNugetLogger.cs | 9 +++- .../infrastructure.app/nuget/NugetCommon.cs | 42 +++++++------------ 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetLogger.cs b/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetLogger.cs index 66318573c5..a0232f4e82 100644 --- a/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetLogger.cs +++ b/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetLogger.cs @@ -15,6 +15,7 @@ namespace chocolatey.infrastructure.app.nuget { + using logging; using NuGet; // ReSharper disable InconsistentNaming @@ -41,7 +42,13 @@ public void Log(MessageLevel level, string message, params object[] args) break; case MessageLevel.Error: this.Log().Error("[NuGet] " + message, args); - break; + break; + //todo: case MessageLevel.Fatal: + // this.Log().Fatal("[NuGet] " + message, args); + // break; + //case MessageLevel.Verbose: + // this.Log().Info(ChocolateyLoggers.Verbose,"[NuGet] " + message, args); + // break; } } } diff --git a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs index 595ffdad37..8487d63fff 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs @@ -31,13 +31,7 @@ public sealed class NugetCommon { public static IFileSystem GetNuGetFileSystem(ChocolateyConfiguration configuration, ILogger nugetLogger) { - var fileSystem = new ChocolateyPhysicalFileSystem(ApplicationParameters.PackagesLocation); - if (configuration.Debug) - { - fileSystem.Logger = nugetLogger; - } - - return fileSystem; + return new ChocolateyPhysicalFileSystem(ApplicationParameters.PackagesLocation) { Logger = nugetLogger }; } public static IPackagePathResolver GetPathResolver(ChocolateyConfiguration configuration, IFileSystem nugetPackagesFileSystem) @@ -45,12 +39,10 @@ public static IPackagePathResolver GetPathResolver(ChocolateyConfiguration confi return new ChocolateyPackagePathResolver(nugetPackagesFileSystem, configuration.AllowMultipleVersions); } - public static IPackageRepository GetLocalRepository(IPackagePathResolver pathResolver, IFileSystem nugetPackagesFileSystem) + public static IPackageRepository GetLocalRepository(IPackagePathResolver pathResolver, IFileSystem nugetPackagesFileSystem, ILogger nugetLogger) { - IPackageRepository localRepository = new ChocolateyLocalPackageRepository(pathResolver, nugetPackagesFileSystem); - localRepository.PackageSaveMode = PackageSaveModes.Nupkg | PackageSaveModes.Nuspec; - - return localRepository; + return new ChocolateyLocalPackageRepository(pathResolver, nugetPackagesFileSystem) + { Logger = nugetLogger, PackageSaveMode = PackageSaveModes.Nupkg | PackageSaveModes.Nuspec }; } public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader) @@ -126,16 +118,16 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con var uri = new Uri(source); if (uri.IsFile || uri.IsUnc) { - repositories.Add(new ChocolateyLocalPackageRepository(uri.LocalPath)); + repositories.Add(new ChocolateyLocalPackageRepository(uri.LocalPath){ Logger = nugetLogger }); } else { - repositories.Add(new DataServicePackageRepository(new RedirectedHttpClient(uri, bypassProxy), packageDownloader)); + repositories.Add(new DataServicePackageRepository(new RedirectedHttpClient(uri, bypassProxy) { UserAgent = "Chocolatey Core" }, packageDownloader) { Logger = nugetLogger }); } } catch (Exception) { - repositories.Add(new ChocolateyLocalPackageRepository(source)); + repositories.Add(new ChocolateyLocalPackageRepository(source){ Logger = nugetLogger }); } } @@ -145,30 +137,26 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con } //todo well that didn't work on failing repos... grrr - var repository = new AggregateRepository(repositories) {IgnoreFailingRepositories = true}; - repository.ResolveDependenciesVertically = true; - if (configuration.Debug) + var repository = new AggregateRepository(repositories) { - repository.Logger = nugetLogger; - } + IgnoreFailingRepositories = true, + Logger = nugetLogger, + ResolveDependenciesVertically = true + }; return repository; } - + public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action installSuccessAction, Action uninstallSuccessAction, bool addUninstallHandler) { IFileSystem nugetPackagesFileSystem = GetNuGetFileSystem(configuration, nugetLogger); IPackagePathResolver pathResolver = GetPathResolver(configuration, nugetPackagesFileSystem); - var packageManager = new PackageManager(GetRemoteRepository(configuration, nugetLogger, packageDownloader), pathResolver, nugetPackagesFileSystem, GetLocalRepository(pathResolver, nugetPackagesFileSystem)) + var packageManager = new PackageManager(GetRemoteRepository(configuration, nugetLogger, packageDownloader), pathResolver, nugetPackagesFileSystem, GetLocalRepository(pathResolver, nugetPackagesFileSystem, nugetLogger)) { DependencyVersion = DependencyVersion.Highest, + Logger = nugetLogger, }; - if (configuration.Debug) - { - packageManager.Logger = nugetLogger; - } - //NOTE DO NOT EVER use this method - packageManager.PackageInstalling += (s, e) => packageManager.PackageInstalled += (s, e) => { From 96d52c5c911f70a1ab1fa26e546f8677caea570a Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Mon, 27 Feb 2017 13:00:54 -0600 Subject: [PATCH 2/4] (GH-1172) Show web status code in failure If a package is not installed due to web errors, note the status code as part of the failure. --- .../services/NugetService.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 3ce6ee2337..ec208679f5 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.services using System.Collections.Generic; using System.IO; using System.Linq; + using System.Net; using NuGet; using adapters; using commandline; @@ -468,7 +469,15 @@ public ConcurrentDictionary install_run(ChocolateyConfigu } catch (Exception ex) { - var logMessage = "{0} not installed. An error occurred during installation:{1} {2}".format_with(packageName, Environment.NewLine, ex.Message); + var message = ex.Message; + var webException = ex as System.Net.WebException; + if (webException != null) + { + var response = webException.Response as HttpWebResponse; + if (response != null && !string.IsNullOrWhiteSpace(response.StatusDescription)) message += " {0}".format_with(response.StatusDescription); + } + + var logMessage = "{0} not installed. An error occurred during installation:{1} {2}".format_with(packageName, Environment.NewLine, message); this.Log().Error(ChocolateyLoggers.Important, logMessage); var errorResult = packageInstalls.GetOrAdd(packageName, new PackageResult(packageName, version.to_string(), null)); errorResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage)); @@ -748,7 +757,15 @@ public ConcurrentDictionary upgrade_run(ChocolateyConfigu } catch (Exception ex) { - var logMessage = "{0} not upgraded. An error occurred during installation:{1} {2}".format_with(packageName, Environment.NewLine, ex.Message); + var message = ex.Message; + var webException = ex as System.Net.WebException; + if (webException != null) + { + var response = webException.Response as HttpWebResponse; + if (response != null && !string.IsNullOrWhiteSpace(response.StatusDescription)) message += " {0}".format_with(response.StatusDescription); + } + + var logMessage = "{0} not upgraded. An error occurred during installation:{1} {2}".format_with(packageName, Environment.NewLine, message); this.Log().Error(ChocolateyLoggers.Important, logMessage); packageResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage)); if (packageResult.ExitCode == 0) packageResult.ExitCode = 1; From 3003eaebb6c1f60e561761c7de56910fc0175cf6 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Mon, 27 Feb 2017 18:48:10 -0600 Subject: [PATCH 3/4] (GH-917) Option - Do Not Show Download Progress When using things like Vagrant and having them log information, it is best to ensure that it doesn't show each percentage on its own line and produce a lot of output for a progress bar. Allow flipping a feature to shut off progress (`showDownloadProgress`) or by the switch `--no-progress`. --- .../ApplicationParameters.cs | 1 + .../builders/ConfigurationBuilder.cs | 4 ++++ .../configuration/ChocolateyConfiguration.cs | 1 + .../infrastructure.app/nuget/NugetCommon.cs | 21 +++++++++++-------- .../powershell/PoshHostUserInterface.cs | 7 +++++-- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 5d0b52a763..87ed233422 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -159,6 +159,7 @@ public static class Features public static readonly string UseFipsCompliantChecksums = "useFipsCompliantChecksums"; public static readonly string ScriptsCheckLastExitCode = "scriptsCheckLastExitCode"; public static readonly string ShowNonElevatedWarnings = "showNonElevatedWarnings"; + public static readonly string ShowDownloadProgress = "showDownloadProgress"; } public static class Messages diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 36df59286c..346a1797b1 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -280,6 +280,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile config.Features.UsePackageExitCodes = set_feature_flag(ApplicationParameters.Features.UsePackageExitCodes, configFileSettings, defaultEnabled: true, description: "Use Package Exit Codes - Package scripts can provide exit codes. With this on, package exit codes will be what choco uses for exit when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. With this feature off, choco will exit with a 0 or a 1 (matching previous behavior). Available in 0.9.10+."); config.Features.UseFipsCompliantChecksums = set_feature_flag(ApplicationParameters.Features.UseFipsCompliantChecksums, configFileSettings, defaultEnabled: false, description: "Use FIPS Compliant Checksums - Ensure checksumming done by choco uses FIPS compliant algorithms. Not recommended unless required by FIPS Mode. Enabling on an existing installation could have unintended consequences related to upgrades/uninstalls. Available in 0.9.10+."); config.Features.ShowNonElevatedWarnings = set_feature_flag(ApplicationParameters.Features.ShowNonElevatedWarnings, configFileSettings, defaultEnabled: true, description: "Show Non-Elevated Warnings - Display non-elevated warnings. Available in 0.10.4+."); + config.Features.ShowDownloadProgress = set_feature_flag(ApplicationParameters.Features.ShowDownloadProgress, configFileSettings, defaultEnabled: true, description: "Show Download Progress - Show download progress percentages in the CLI. Available in 0.10.4+."); config.Features.ScriptsCheckLastExitCode = set_feature_flag(ApplicationParameters.Features.ScriptsCheckLastExitCode, configFileSettings, defaultEnabled: false, description: "Scripts Check $LastExitCode (external commands) - Leave this off unless you absolutely need it while you fix your package scripts to use `throw 'error message'` or `Set-PowerShellExitCode #` instead of `exit #`. This behavior started in 0.9.10 and produced hard to find bugs. If the last external process exits successfully but with an exit code of not zero, this could cause hard to detect package failures. Available in 0.10.3+. Will be removed in 0.11.0."); config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass."); } @@ -368,6 +369,9 @@ private static void set_global_options(IList args, ChocolateyConfigurati .Add("use-system-powershell", "UseSystemPowerShell - Execute PowerShell using an external process instead of the built-in PowerShell host. Should only be used when internal host is failing. Available in 0.9.10+.", option => config.Features.UsePowerShellHost = option == null) + .Add("no-progress", + "Do Not Show Progress - Do not show download progress percentages. Available in 0.10.4+.", + option => config.Features.ShowDownloadProgress = option == null) ; }, (unparsedArgs) => diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index c546e4649f..8771dfcd9e 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -353,6 +353,7 @@ public sealed class FeaturesConfiguration public bool UsePackageExitCodes { get; set; } public bool UseFipsCompliantChecksums { get; set; } public bool ShowNonElevatedWarnings { get; set; } + public bool ShowDownloadProgress { get; set; } //todo remove in 0.11.0 public bool ScriptsCheckLastExitCode { get; set; } } diff --git a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs index 8487d63fff..a487ce61cf 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs @@ -47,17 +47,20 @@ public static IPackageRepository GetLocalRepository(IPackagePathResolver pathRes public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader) { - packageDownloader.ProgressAvailable += (sender, e) => + if (configuration.Features.ShowDownloadProgress) { - // http://stackoverflow.com/a/888569/18475 - Console.Write("\rProgress: {0} {1}%".format_with(e.Operation, e.PercentComplete.to_string()).PadRight(Console.WindowWidth)); - if (e.PercentComplete == 100) + packageDownloader.ProgressAvailable += (sender, e) => { - Console.WriteLine(""); - } - }; - - IEnumerable sources = configuration.Sources.Split(new[] {";", ","}, StringSplitOptions.RemoveEmptyEntries); + // http://stackoverflow.com/a/888569/18475 + Console.Write("\rProgress: {0} {1}%".format_with(e.Operation, e.PercentComplete.to_string()).PadRight(Console.WindowWidth)); + if (e.PercentComplete == 100) + { + Console.WriteLine(""); + } + }; + } + + IEnumerable sources = configuration.Sources.Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries); IList repositories = new List(); diff --git a/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs b/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs index 2f3f8241a8..ef8bcefebb 100644 --- a/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs +++ b/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs @@ -128,8 +128,11 @@ public override void WriteProgress(long sourceId, ProgressRecord record) this.Log().Debug(record.Activity.escape_curly_braces()); } - // http://stackoverflow.com/a/888569/18475 - Console.Write("\rProgress: {0}% - {1}".format_with(record.PercentComplete.to_string(), record.StatusDescription).PadRight(Console.WindowWidth)); + if (_configuration.Features.ShowDownloadProgress) + { + // http://stackoverflow.com/a/888569/18475 + Console.Write("\rProgress: {0}% - {1}".format_with(record.PercentComplete.to_string(), record.StatusDescription).PadRight(Console.WindowWidth)); + } } public override void WriteVerboseLine(string message) From 1a9aa99f2c34c2ef4c9eed4a37ad213f75a92965 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Mon, 27 Feb 2017 18:48:28 -0600 Subject: [PATCH 4/4] (maint) formatting --- .../infrastructure.app/nuget/NugetCommon.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs index a487ce61cf..1ff94b6310 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs @@ -41,8 +41,7 @@ public static IPackagePathResolver GetPathResolver(ChocolateyConfiguration confi public static IPackageRepository GetLocalRepository(IPackagePathResolver pathResolver, IFileSystem nugetPackagesFileSystem, ILogger nugetLogger) { - return new ChocolateyLocalPackageRepository(pathResolver, nugetPackagesFileSystem) - { Logger = nugetLogger, PackageSaveMode = PackageSaveModes.Nupkg | PackageSaveModes.Nuspec }; + return new ChocolateyLocalPackageRepository(pathResolver, nugetPackagesFileSystem) { Logger = nugetLogger, PackageSaveMode = PackageSaveModes.Nupkg | PackageSaveModes.Nuspec }; } public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader) @@ -121,7 +120,7 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con var uri = new Uri(source); if (uri.IsFile || uri.IsUnc) { - repositories.Add(new ChocolateyLocalPackageRepository(uri.LocalPath){ Logger = nugetLogger }); + repositories.Add(new ChocolateyLocalPackageRepository(uri.LocalPath) { Logger = nugetLogger }); } else { @@ -130,7 +129,7 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con } catch (Exception) { - repositories.Add(new ChocolateyLocalPackageRepository(source){ Logger = nugetLogger }); + repositories.Add(new ChocolateyLocalPackageRepository(source) { Logger = nugetLogger }); } } @@ -149,7 +148,7 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con return repository; } - + public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action installSuccessAction, Action uninstallSuccessAction, bool addUninstallHandler) { IFileSystem nugetPackagesFileSystem = GetNuGetFileSystem(configuration, nugetLogger); @@ -165,9 +164,9 @@ public static IPackageManager GetPackageManager(ChocolateyConfiguration configur { var pkg = e.Package; "chocolatey".Log().Info(ChocolateyLoggers.Important, "{0}{1} v{2}{3}{4}{5}".format_with( - Environment.NewLine, - pkg.Id, - pkg.Version.to_string(), + Environment.NewLine, + pkg.Id, + pkg.Version.to_string(), configuration.Force ? " (forced)" : string.Empty, pkg.IsApproved ? " [Approved]" : string.Empty, pkg.PackageTestResultStatus == "Failing" && pkg.IsDownloadCacheAvailable ? " - Likely broken for FOSS users (due to download location changes)" : pkg.PackageTestResultStatus == "Failing" ? " - Possibly broken" : string.Empty