Skip to content

Commit

Permalink
Merge branch 'ticket/stable/GH-1095_nuget-log-always' into stable
Browse files Browse the repository at this point in the history
* ticket/stable/GH-1095_nuget-log-always:
  (maint) formatting
  (GH-917) Option - Do Not Show Download Progress
  (GH-1172) Show web status code in failure
  (GH-1095) Always log Nuget.Core messages
  • Loading branch information
ferventcoder committed Feb 28, 2017
2 parents 6d6489d + 1a9aa99 commit e4c7beb
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down Expand Up @@ -368,6 +369,9 @@ private static void set_global_options(IList<string> 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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace chocolatey.infrastructure.app.nuget
{
using logging;
using NuGet;

// ReSharper disable InconsistentNaming
Expand All @@ -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;
}
}
}
Expand Down
66 changes: 28 additions & 38 deletions src/chocolatey/infrastructure.app/nuget/NugetCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,35 @@ 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)
{
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)
{
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<string> 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<string> sources = configuration.Sources.Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);

IList<IPackageRepository> repositories = new List<IPackageRepository>();

Expand Down Expand Up @@ -126,16 +120,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 });
}
}

Expand All @@ -145,12 +139,12 @@ 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;
}
Expand All @@ -159,24 +153,20 @@ public static IPackageManager GetPackageManager(ChocolateyConfiguration configur
{
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) =>
{
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
Expand Down
21 changes: 19 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -468,7 +469,15 @@ public ConcurrentDictionary<string, PackageResult> 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));
Expand Down Expand Up @@ -748,7 +757,15 @@ public ConcurrentDictionary<string, PackageResult> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e4c7beb

Please sign in to comment.