diff --git a/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 b/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 index 5c294c7daa..0b28a349f7 100644 --- a/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 +++ b/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 @@ -78,14 +78,17 @@ function script:chocoCommands($filter) { } function script:chocoLocalPackages($filter) { + if ($filter -ne $null -and $filter.StartsWith(".")) { return; } #file search @(& $script:choco list $filter -lo -r --id-starts-with) | %{ $_.Split('|')[0] } } function script:chocoLocalPackagesUpgrade($filter) { + if ($filter -ne $null -and $filter.StartsWith(".")) { return; } #file search @('all|') + @(& $script:choco list $filter -lo -r --id-starts-with) | where { $_ -like "$filter*" } | %{ $_.Split('|')[0] } } function script:chocoRemotePackages($filter) { + if ($filter -ne $null -and $filter.StartsWith(".")) { return; } #file search @('packages.config|') + @(& $script:choco search $filter --page=0 --page-size=30 -r --id-starts-with --order-by-popularity) | where { $_ -like "$filter*" } | %{ $_.Split('|')[0] } } @@ -99,17 +102,17 @@ function ChocolateyTabExpansion($lastBlock) { switch -regex ($lastBlock -replace "^$(Get-AliasPattern choco) ","") { # Handles uninstall package names - "^uninstall\s+(?[^-\s]*)$" { + "^uninstall\s+(?[^\.][^-\s]*)$" { chocoLocalPackages $matches['package'] } # Handles install package names - "^(install)\s+(?[^-\s]*)$" { + "^(install)\s+(?[^\.][^-\s]+)$" { chocoRemotePackages $matches['package'] } # Handles upgrade / uninstall package names - "^upgrade\s+(?[^-\s]*)$" { + "^upgrade\s+(?[^\.][^-\s]*)$" { chocoLocalPackagesUpgrade $matches['package'] } diff --git a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 index 05ac65fff8..9732fcd934 100644 --- a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 +++ b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 @@ -89,6 +89,13 @@ param( Write-Debug "Running 'Start-ChocolateyProcessAsAdmin' with exeToRun:`'$exeToRun`', statements: `'$statements`' "; + try{ + if ($exeToRun -ne $null) { $exeToRun = $exeToRun -replace "`0", "" } + if ($statements -ne $null) { $statements = $statements -replace "`0", "" } + } catch { + Write-Debug "Removing null characters resulted in an error - $($_.Exception.Message)" + } + $wrappedStatements = $statements if ($wrappedStatements -eq $null) { $wrappedStatements = ''} @@ -126,10 +133,14 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t Write-Debug $dbgMessage - $exeIsTextFile = [System.IO.Path]::GetFullPath($exeToRun) + ".istext" - if (([System.IO.File]::Exists($exeIsTextFile))) { - Set-PowerShellExitCode 4 - throw "The file was a text file but is attempting to be run as an executable - '$exeToRun'" + try { + $exeIsTextFile = [System.IO.Path]::GetFullPath($exeToRun) + ".istext" + if (([System.IO.File]::Exists($exeIsTextFile))) { + Set-PowerShellExitCode 4 + throw "The file was a text file but is attempting to be run as an executable - '$exeToRun'" + } + } catch { + Write-Debug "Unable to detect whether the file is a text file or not - $($_.Exception.Message)" } if ($exeToRun -eq 'msiexec' -or $exeToRun -eq 'msiexec.exe') { diff --git a/src/chocolatey.resources/helpers/functions/UnInstall-ChocolateyZipPackage.ps1 b/src/chocolatey.resources/helpers/functions/UnInstall-ChocolateyZipPackage.ps1 index 89ab57fece..c384c66c2b 100644 --- a/src/chocolatey.resources/helpers/functions/UnInstall-ChocolateyZipPackage.ps1 +++ b/src/chocolatey.resources/helpers/functions/UnInstall-ChocolateyZipPackage.ps1 @@ -65,8 +65,10 @@ param( if ((Test-Path -path $zipContentFile)) { $zipContentFile $zipContents=get-content $zipContentFile - foreach ($fileInZip in $zipContents) { - remove-item -Path "$fileInZip" -ErrorAction SilentlyContinue -Recurse -Force + foreach ($fileInZip in $zipContents) { + if ($fileInZip -ne $null -and $fileInZip.Trim() -ne '') { + Remove-Item -Path "$fileInZip" -ErrorAction SilentlyContinue -Recurse -Force + } } } } diff --git a/src/chocolatey.resources/tools/7z.dll b/src/chocolatey.resources/tools/7z.dll index 9efe1ea619..79bc2008b2 100644 Binary files a/src/chocolatey.resources/tools/7z.dll and b/src/chocolatey.resources/tools/7z.dll differ diff --git a/src/chocolatey.resources/tools/7z.exe b/src/chocolatey.resources/tools/7z.exe index 5029a9e9ba..263111831f 100644 Binary files a/src/chocolatey.resources/tools/7z.exe and b/src/chocolatey.resources/tools/7z.exe differ diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 0866dc8a26..974d7e51c9 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -70,6 +70,7 @@ public static class ApplicationParameters public static readonly string ChocolateyToolsLocationEnvironmentVariableName = "ChocolateyToolsLocation"; public static readonly string ChocolateyPackageInstallLocationEnvironmentVariableName = "ChocolateyPackageInstallLocation"; public static readonly string ChocolateyPackageInstallerTypeEnvironmentVariableName = "ChocolateyInstallerType"; + public static readonly string ChocolateyPackageExitCodeEnvironmentVariableName = "ChocolateyExitCode"; public static class Environment { diff --git a/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs b/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs index 0e9e92246e..b509967415 100644 --- a/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs +++ b/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs @@ -47,6 +47,7 @@ public static void reset_environment_variables(ChocolateyConfiguration config) { Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallLocationEnvironmentVariableName, null); Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallerTypeEnvironmentVariableName, null); + Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageExitCodeEnvironmentVariableName, null); } public static void set_environment_variables(ChocolateyConfiguration config) @@ -198,12 +199,14 @@ public static void update_environment_variables() private static IDictionary convert_to_case_insensitive_dictionary(IDictionary originalDictionary) { - return new Hashtable(originalDictionary, StringComparer.InvariantCultureIgnoreCase); + if (originalDictionary == null) return new Hashtable(new Dictionary(), StringComparer.OrdinalIgnoreCase); + + return new Hashtable(originalDictionary, StringComparer.OrdinalIgnoreCase); } private static void refresh_environment_variables(IDictionary environmentVariables) { - foreach (DictionaryEntry variable in environmentVariables) + foreach (DictionaryEntry variable in environmentVariables.or_empty_list_if_null()) { Environment.SetEnvironmentVariable(variable.Key.to_string(), variable.Value.to_string()); } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 3ff13da7a9..907d28c968 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -809,7 +809,7 @@ private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult pac if (config.Force) { - var packageDirectory = _fileSystem.combine_paths(packageResult.InstallLocation); + var packageDirectory = packageResult.InstallLocation; if (string.IsNullOrWhiteSpace(packageDirectory) || !_fileSystem.directory_exists(packageDirectory)) return; @@ -1010,7 +1010,7 @@ private void move_bad_package_to_failure_location(PackageResult packageResult) { _fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageFailuresLocation); - if (packageResult.InstallLocation != null && _fileSystem.directory_exists(packageResult.InstallLocation)) + if (!string.IsNullOrWhiteSpace(packageResult.InstallLocation) && _fileSystem.directory_exists(packageResult.InstallLocation)) { FaultTolerance.try_catch_with_logging_exception( () => _fileSystem.move_directory(packageResult.InstallLocation, packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation)), diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index d728882e56..52fedb70f5 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -328,7 +328,7 @@ public ConcurrentDictionary install_run(ChocolateyConfigu //todo: handle all - SemanticVersion version = config.Version != null ? new SemanticVersion(config.Version) : null; + SemanticVersion version = !string.IsNullOrWhiteSpace(config.Version) ? new SemanticVersion(config.Version) : null; if (config.Force) config.AllowDowngrade = true; IList packageNames = config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().ToList(); @@ -466,6 +466,7 @@ public ConcurrentDictionary install_run(ChocolateyConfigu 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)); + if (errorResult.ExitCode == 0) errorResult.ExitCode = 1; if (continueAction != null) continueAction.Invoke(errorResult); } } @@ -513,7 +514,7 @@ public ConcurrentDictionary upgrade_run(ChocolateyConfigu _fileSystem.create_directory_if_not_exists(ApplicationParameters.PackagesLocation); var packageInstalls = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); - SemanticVersion version = config.Version != null ? new SemanticVersion(config.Version) : null; + SemanticVersion version = !string.IsNullOrWhiteSpace(config.Version) ? new SemanticVersion(config.Version) : null; if (config.Force) config.AllowDowngrade = true; var packageManager = NugetCommon.GetPackageManager( @@ -744,6 +745,7 @@ public ConcurrentDictionary upgrade_run(ChocolateyConfigu var logMessage = "{0} not upgraded. An error occurred during installation:{1} {2}".format_with(packageName, Environment.NewLine, ex.Message); this.Log().Error(ChocolateyLoggers.Important, logMessage); packageResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage)); + if (packageResult.ExitCode == 0) packageResult.ExitCode = 1; if (continueAction != null) continueAction.Invoke(packageResult); } } @@ -1176,6 +1178,7 @@ public ConcurrentDictionary uninstall_run(ChocolateyConfi this.Log().Error(ChocolateyLoggers.Important, logMessage); var result = packageUninstalls.GetOrAdd(packageVersion.Id.to_lower() + "." + packageVersion.Version.to_string(), new PackageResult(packageVersion, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, packageVersion.Id))); result.Messages.Add(new ResultMessage(ResultType.Error, logMessage)); + if (result.ExitCode == 0) result.ExitCode = 1; // do not call continueAction - will result in multiple passes } } @@ -1280,7 +1283,7 @@ private void set_package_names_if_all_is_specified(ChocolateyConfiguration confi if (!string.IsNullOrWhiteSpace(config.UpgradeCommand.PackageNamesToSkip)) { var packagesToSkip = config.UpgradeCommand.PackageNamesToSkip - .Split(',') + .Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries) .Where(item => !string.IsNullOrWhiteSpace(item)) .Select(p => p.trim_safe()) .ToList(); diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 704e44f0d9..0d6228fe0b 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -442,7 +442,6 @@ public void prepare_powershell_environment(IPackage package, ChocolateyConfigura Environment.SetEnvironmentVariable("CacheChecksumType_{0}".format_with(urlKey), "sha512"); } } - } private ResolveEventHandler _handler = null; @@ -480,7 +479,10 @@ private System.Reflection.Assembly attempt_version_load(AssemblyName requestedAs } catch (Exception ex) { + if (requestedAssembly.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)) return null; + this.Log().Debug(ChocolateyLoggers.Verbose, "Attempting to load assembly {0} failed:{1} {2}".format_with(requestedAssembly.Name, Environment.NewLine, ex.Message.escape_curly_braces())); + return null; } } diff --git a/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs b/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs index d501200255..8f04a53f82 100644 --- a/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs +++ b/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs @@ -51,7 +51,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable c => c.Count() > 0, (name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices"); }); - if (defaultChoice != null) + if (!string.IsNullOrWhiteSpace(defaultChoice)) { Ensure .that(() => choices) @@ -117,7 +117,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable var selection = timeoutInSeconds == 0 ? Console.ReadLine() : Console.ReadLine(timeoutInSeconds * 1000); if (shortPrompt) Console.WriteLine(); - if (string.IsNullOrWhiteSpace(selection) && defaultChoice != null) + if (string.IsNullOrWhiteSpace(selection) && !string.IsNullOrWhiteSpace(defaultChoice)) { "chocolatey".Log().Info(ChocolateyLoggers.LogFileOnly, "Choosing default choice of '{0}'".format_with(defaultChoice.escape_curly_braces())); return defaultChoice; diff --git a/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs b/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs index 89167de4ee..6913313301 100644 --- a/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs +++ b/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs @@ -66,6 +66,8 @@ private static IEnvironment Environment public string combine_paths(string leftItem, params string[] rightItems) { + if (leftItem == null) throw new ApplicationException("Path to combine cannot be empty."); + var combinedPath = Platform.get_platform() == PlatformType.Windows ? leftItem : leftItem.Replace('\\', '/'); foreach (var rightItem in rightItems) { @@ -166,11 +168,15 @@ public string get_current_assembly_path() public IEnumerable get_files(string directoryPath, string pattern = "*.*", SearchOption option = SearchOption.TopDirectoryOnly) { + if (string.IsNullOrWhiteSpace(directoryPath)) return new List(); + return Directory.EnumerateFiles(directoryPath, pattern, option); } public IEnumerable get_files(string directoryPath, string[] extensions, SearchOption option = SearchOption.TopDirectoryOnly) { + if (string.IsNullOrWhiteSpace(directoryPath)) return new List(); + return Directory.EnumerateFiles(directoryPath, "*.*", option) .Where(f => extensions.Any(x => f.EndsWith(x, StringComparison.OrdinalIgnoreCase))); } diff --git a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs index 4ab93ce266..64084d5958 100644 --- a/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs +++ b/src/chocolatey/infrastructure/logging/Log4NetAppenderConfiguration.cs @@ -54,7 +54,7 @@ public static void configure(string outputDirectory = null) XmlConfigurator.Configure(xmlConfigStream); - if (outputDirectory != null) + if (!string.IsNullOrWhiteSpace(outputDirectory)) { set_file_appender(outputDirectory); } @@ -85,8 +85,8 @@ private static void set_file_appender(string outputDirectory) Layout = layout, AppendToFile = true, RollingStyle = RollingFileAppender.RollingMode.Size, - MaxFileSize = 1024 * 1024, - MaxSizeRollBackups = 10, + MaxFileSize = 1024 * 1024 * 10, + MaxSizeRollBackups = 50, LockingModel = new FileAppender.MinimalLock(), }; app.ActivateOptions(); @@ -98,8 +98,8 @@ private static void set_file_appender(string outputDirectory) Layout = layout, AppendToFile = true, RollingStyle = RollingFileAppender.RollingMode.Size, - MaxFileSize = 1024 * 1024, - MaxSizeRollBackups = 10, + MaxFileSize = 1024 * 1024 * 10, + MaxSizeRollBackups = 50, LockingModel = new FileAppender.MinimalLock(), }; diff --git a/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs b/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs index 6b7f6eb844..0dc7fbd8de 100644 --- a/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs +++ b/src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs @@ -186,7 +186,7 @@ public override Dictionary Prompt(string caption, string messa private static string[] get_hotkey_and_label(string input) { var result = new[] { String.Empty, String.Empty }; - string[] fragments = input.Split('&'); + string[] fragments = input.Split(new char[] {'&'}, StringSplitOptions.RemoveEmptyEntries); if (fragments.Length == 2) { if (fragments[1].Length > 0) result[0] = fragments[1][0].to_string().ToUpper(CultureInfo.CurrentCulture); diff --git a/src/chocolatey/infrastructure/tokens/TokenReplacer.cs b/src/chocolatey/infrastructure/tokens/TokenReplacer.cs index ca28274d11..bda724f96c 100644 --- a/src/chocolatey/infrastructure/tokens/TokenReplacer.cs +++ b/src/chocolatey/infrastructure/tokens/TokenReplacer.cs @@ -16,7 +16,6 @@ namespace chocolatey.infrastructure.tokens { using System.Collections.Generic; - using System.Net.Mime; using System.Reflection; using System.Text.RegularExpressions;