From a30dd76df3411b31a4d5e810e95b43a165930875 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 070cda8cdd..26041541b0 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 9dd5e33032..46af0e000a 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 b0fad9be0e..caebc1c0dc 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 ba63413cb9..2aa2c5a2a0 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"); @@ -572,6 +574,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 ede41552f4..2403e99131 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -403,15 +403,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) { @@ -419,6 +417,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);