From 7d485df05a2bc8bcfedafab592dc19f227cf2de9 Mon Sep 17 00:00:00 2001 From: eir Date: Thu, 18 Aug 2016 12:33:34 +0200 Subject: [PATCH] (GH-839) Switch to apply package parameters to dependent packages In version 0.9.9.12 and earlier, install arguments and package-parameters would also be applied to dependent packages, but in choco 0.9.10 this was changed so that it would only be applied to the specified package. Sometimes it will be useful to pass the install arguments and package parameters further to dependent packages. This can be controlled by using cmdline switches. --- .../commands/ChocolateyInstallCommandSpecs.cs | 12 ++++++++++++ .../commands/ChocolateyUninstallCommandSpecs.cs | 12 ++++++++++++ .../commands/ChocolateyUpgradeCommandSpecs.cs | 12 ++++++++++++ .../commands/ChocolateyInstallCommand.cs | 6 ++++++ .../commands/ChocolateyUninstallCommand.cs | 6 ++++++ .../commands/ChocolateyUpgradeCommand.cs | 6 ++++++ .../configuration/ChocolateyConfiguration.cs | 2 ++ .../PackagesConfigFilePackageSetting.cs | 6 ++++++ .../services/ChocolateyPackageService.cs | 4 ++++ .../services/PowershellService.cs | 17 ++++++++++++----- 10 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs index 7cce208bb8..f43a002320 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyInstallCommandSpecs.cs @@ -151,6 +151,18 @@ public void should_add_short_version_of_packageparameters_to_the_option_set() optionSet.Contains("params").ShouldBeTrue(); } + [Fact] + public void should_add_applyPackageParametersToDependencies_to_the_option_set() + { + optionSet.Contains("apply-package-parameters-to-dependencies").ShouldBeTrue(); + } + + [Fact] + public void should_add_applyInstallArgumentsToDependencies_to_the_option_set() + { + optionSet.Contains("apply-install-arguments-to-dependencies").ShouldBeTrue(); + } + [Fact] public void should_add_allowmultipleversions_to_the_option_set() { diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUninstallCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUninstallCommandSpecs.cs index 7a260134ea..3ebd5cce59 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUninstallCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUninstallCommandSpecs.cs @@ -133,6 +133,18 @@ public void should_add_short_version_of_packageparameters_to_the_option_set() optionSet.Contains("params").ShouldBeTrue(); } + [Fact] + public void should_add_applyPackageParametersToDependencies_to_the_option_set() + { + optionSet.Contains("apply-package-parameters-to-dependencies").ShouldBeTrue(); + } + + [Fact] + public void should_add_applyInstallArgumentsToDependencies_to_the_option_set() + { + optionSet.Contains("apply-install-arguments-to-dependencies").ShouldBeTrue(); + } + [Fact] public void should_add_forcedependencies_to_the_option_set() { diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUpgradeCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUpgradeCommandSpecs.cs index a1706a3d00..fad894dd9f 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUpgradeCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateyUpgradeCommandSpecs.cs @@ -145,6 +145,18 @@ public void should_add_short_version_of_packageparameters_to_the_option_set() optionSet.Contains("params").ShouldBeTrue(); } + [Fact] + public void should_add_applyPackageParametersToDependencies_to_the_option_set() + { + optionSet.Contains("apply-package-parameters-to-dependencies").ShouldBeTrue(); + } + + [Fact] + public void should_add_applyInstallArgumentsToDependencies_to_the_option_set() + { + optionSet.Contains("apply-install-arguments-to-dependencies").ShouldBeTrue(); + } + [Fact] public void should_add_allowmultipleversions_to_the_option_set() { diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs index 18c9b162bf..fde190de50 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs @@ -62,6 +62,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon .Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=", "PackageParameters - Parameters to pass to the package. Defaults to unspecified.", option => configuration.PackageParameters = option.remove_surrounding_quotes()) + .Add("apply-install-arguments-to-dependencies", + "Apply Install Arguments To Dependencies - Should install arguments be applied to dependent packages? Defaults to false.", + option => configuration.ApplyInstallArgumentsToDependencies = option != null) + .Add("apply-package-parameters-to-dependencies", + "Apply Package Parameters To Dependencies - Should package parameters be applied to dependent packages? Defaults to false.", + option => configuration.ApplyPackageParametersToDependencies = option != null) .Add("allowdowngrade|allow-downgrade", "AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.", option => configuration.AllowDowngrade = option != null) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs index 460a90ae9d..ab2358c31c 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs @@ -58,6 +58,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon .Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=", "PackageParameters - Parameters to pass to the package. Defaults to unspecified.", option => configuration.PackageParameters = option.remove_surrounding_quotes()) + .Add("apply-install-arguments-to-dependencies", + "Apply Install Arguments To Dependencies - Should install arguments be applied to dependent packages? Defaults to false.", + option => configuration.ApplyInstallArgumentsToDependencies = option != null) + .Add("apply-package-parameters-to-dependencies", + "Apply Package Parameters To Dependencies - Should package parameters be applied to dependent packages? Defaults to false.", + option => configuration.ApplyPackageParametersToDependencies = option != null) .Add("m|sxs|sidebyside|side-by-side|allowmultiple|allow-multiple|allowmultipleversions|allow-multiple-versions", "AllowMultipleVersions - Should multiple versions of a package be installed? Defaults to false.", option => configuration.AllowMultipleVersions = option != null) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs index 67fc5334ea..3c1e7402da 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs @@ -63,6 +63,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon .Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=", "PackageParameters - Parameters to pass to the package. Defaults to unspecified.", option => configuration.PackageParameters = option.remove_surrounding_quotes()) + .Add("apply-install-arguments-to-dependencies", + "Apply Install Arguments To Dependencies - Should install arguments be applied to dependent packages? Defaults to false.", + option => configuration.ApplyInstallArgumentsToDependencies = option != null) + .Add("apply-package-parameters-to-dependencies", + "Apply Package Parameters To Dependencies - Should package parameters be applied to dependent packages? Defaults to false.", + option => configuration.ApplyPackageParametersToDependencies = option != null) .Add("allowdowngrade|allow-downgrade", "AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.", option => configuration.AllowDowngrade = option != null) diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index 95ec785443..a54cca0958 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -203,6 +203,8 @@ private void append_output(StringBuilder propertyValues, string append) public bool OverrideArguments { get; set; } public bool NotSilent { get; set; } public string PackageParameters { get; set; } + public bool ApplyPackageParametersToDependencies { get; set; } + public bool ApplyInstallArgumentsToDependencies { get; set; } public bool IgnoreDependencies { get; set; } public bool AllowMultipleVersions { get; set; } public bool AllowDowngrade { get; set; } diff --git a/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs b/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs index 95f58f137f..da5f2641ea 100644 --- a/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs +++ b/src/chocolatey/infrastructure.app/configuration/PackagesConfigFilePackageSetting.cs @@ -40,6 +40,12 @@ public sealed class PackagesConfigFilePackageSetting [XmlAttribute(AttributeName = "packageParameters")] public string PackageParameters { get; set; } + [XmlAttribute(AttributeName = "applyPackageParametersToDependencies")] + public bool ApplyPackageParametersToDependencies { get; set; } + + [XmlAttribute(AttributeName = "applyInstallArgumentsToDependencies")] + public bool ApplyInstallArgumentsToDependencies { get; set; } + [XmlAttribute(AttributeName = "forceX86")] public bool ForceX86 { get; set; } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 0a78813ec6..323d82cb63 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -398,6 +398,8 @@ private string capture_arguments(ChocolateyConfiguration config, PackageResult p if (!string.IsNullOrWhiteSpace(config.InstallArguments)) arguments.Append(" --install-arguments=\"'{0}'\"".format_with(config.InstallArguments)); if (config.OverrideArguments) arguments.Append(" --override-arguments"); if (!string.IsNullOrWhiteSpace(config.PackageParameters)) arguments.Append(" --package-parameters=\"'{0}'\"".format_with(config.PackageParameters)); + if (config.ApplyPackageParametersToDependencies) arguments.Append(" --apply-package-parameters-to-dependencies"); + if (config.ApplyInstallArgumentsToDependencies) arguments.Append(" --apply-install-arguments-to-dependencies"); if (config.AllowDowngrade) arguments.Append(" --allow-downgrade"); if (config.AllowMultipleVersions) arguments.Append(" --allow-multiple-versions"); if (config.IgnoreDependencies) arguments.Append(" --ignore-dependencies"); @@ -573,6 +575,8 @@ private IEnumerable get_packages_from_config(string pac if (pkgSettings.ForceX86) packageConfig.ForceX86 = true; if (pkgSettings.AllowMultipleVersions) packageConfig.AllowMultipleVersions = true; if (pkgSettings.IgnoreDependencies) packageConfig.IgnoreDependencies = true; + if (pkgSettings.ApplyInstallArgumentsToDependencies) packageConfig.ApplyInstallArgumentsToDependencies = true; + if (pkgSettings.ApplyPackageParametersToDependencies) packageConfig.ApplyPackageParametersToDependencies = true; this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(packageConfig.PackageNames)); packageConfigs.Add(packageConfig); diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 955aa8ecd3..d42d8c357c 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -404,15 +404,13 @@ public void prepare_powershell_environment(IPackage package, ChocolateyConfigura // we only want to pass the following args to packages that would apply. // like choco install git -params '' should pass those params to git.install, - // but not another package - if (!package_is_a_dependency_not_a_virtual(configuration, package.Id)) + // but not another package unless the switch apply-install-arguments-to-dependencies is used + if (!package_is_a_dependency_not_a_virtual(configuration, package.Id) || configuration.ApplyInstallArgumentsToDependencies) { - this.Log().Debug(ChocolateyLoggers.Verbose, "Setting installer args and package parameters for {0}".format_with(package.Id)); + this.Log().Debug(ChocolateyLoggers.Verbose, "Setting installer args for {0}".format_with(package.Id)); Environment.SetEnvironmentVariable("installArguments", configuration.InstallArguments); Environment.SetEnvironmentVariable("installerArguments", configuration.InstallArguments); Environment.SetEnvironmentVariable("chocolateyInstallArguments", configuration.InstallArguments); - Environment.SetEnvironmentVariable("packageParameters", configuration.PackageParameters); - Environment.SetEnvironmentVariable("chocolateyPackageParameters", configuration.PackageParameters); if (configuration.OverrideArguments) { @@ -420,6 +418,15 @@ public void prepare_powershell_environment(IPackage package, ChocolateyConfigura } } + // we only want to pass package parameters to packages that would apply. + // but not another package unless the switch apply-package-parameters-to-dependencies is used + if (!package_is_a_dependency_not_a_virtual(configuration, package.Id) || configuration.ApplyPackageParametersToDependencies) + { + this.Log().Debug(ChocolateyLoggers.Verbose, "Setting package parameters for {0}".format_with(package.Id)); + Environment.SetEnvironmentVariable("packageParameters", configuration.PackageParameters); + Environment.SetEnvironmentVariable("chocolateyPackageParameters", configuration.PackageParameters); + } + if (!string.IsNullOrWhiteSpace(configuration.DownloadChecksum)) { Environment.SetEnvironmentVariable("chocolateyChecksum32", configuration.DownloadChecksum);