Skip to content

Commit ebc6c41

Browse files
committed
(chocolatey#2503) Add ability to export remembered arguments
This adds the --include-remembered-arguments option which is used to export any remembered arguments. It reuses the set_package_config_for_upgrade method in the NugetService to read and parse the remembered arguments.
1 parent 68ea3e4 commit ebc6c41

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ $commandOptions = @{
6363
download = "--internalize --internalize-all-urls --ignore-dependencies --installed-packages --ignore-unfound-packages --resources-location='' --download-location='' --outputdirectory='' --source='' --version='' --prerelease --user='' --password='' --cert='' --certpassword='' --append-use-original-location --recompile --disable-package-repository-optimizations -?" + $allcommands
6464
sync = "--output-directory='' --id='' --package-id='' -?" + $allcommands
6565
optimize = "--deflate-nupkg-only --id='' -?" + $allcommands
66-
export = "--include-version-numbers --output-file-path='' -?" + $allcommands
66+
export = "--include-version-numbers --output-file-path='' --include-remembered-arguments -?" + $allcommands
6767
template = "--name=''" + $allcommands
6868
}
6969

src/chocolatey.tests/infrastructure.app/commands/ChocolateyExportCommandSpecs.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,20 @@ public abstract class ChocolateyExportCommandSpecsBase : TinySpec
3636
protected Mock<INugetService> nugetService = new Mock<INugetService>();
3737
protected Mock<IFileSystem> fileSystem = new Mock<IFileSystem>();
3838
protected ChocolateyConfiguration configuration = new ChocolateyConfiguration();
39+
protected Mock<IChocolateyPackageInformationService> packageInfoService = new Mock<IChocolateyPackageInformationService>();
40+
protected Mock<IChocolateyPackageService> packageService = new Mock<IChocolateyPackageService>();
3941

4042
public override void Context()
4143
{
42-
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object);
44+
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object, packageInfoService.Object, packageService.Object);
4345
}
4446

4547
public void reset()
4648
{
4749
nugetService.ResetCalls();
4850
fileSystem.ResetCalls();
51+
packageInfoService.ResetCalls();
52+
packageService.ResetCalls();
4953
}
5054
}
5155

@@ -103,6 +107,18 @@ public void should_add_include_version_to_the_option_set()
103107
{
104108
optionSet.Contains("include-version").ShouldBeTrue();
105109
}
110+
111+
[Fact]
112+
public void should_add_include_arguments_to_the_option_set()
113+
{
114+
optionSet.Contains("include-arguments").ShouldBeTrue();
115+
}
116+
117+
[Fact]
118+
public void should_add_include_remembered_arguments_to_the_option_set()
119+
{
120+
optionSet.Contains("include-remembered-arguments").ShouldBeTrue();
121+
}
106122
}
107123

108124
public class when_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase

src/chocolatey/infrastructure.app/commands/ChocolateyExportCommand.cs

+72-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ public class ChocolateyExportCommand : ICommand
3737
{
3838
private readonly INugetService _nugetService;
3939
private readonly IFileSystem _fileSystem;
40-
41-
public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem)
40+
private readonly IChocolateyPackageInformationService _packageInfoService;
41+
private readonly IChocolateyPackageService _packageService;
42+
43+
public ChocolateyExportCommand(
44+
INugetService nugetService,
45+
IFileSystem fileSystem,
46+
IChocolateyPackageInformationService packageInfoService,
47+
IChocolateyPackageService packageService)
4248
{
4349
_nugetService = nugetService;
4450
_fileSystem = fileSystem;
51+
_packageInfoService = packageInfoService;
52+
_packageService = packageService;
4553
}
4654

4755
public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
@@ -53,6 +61,9 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati
5361
.Add("include-version-numbers|include-version",
5462
"Include Version Numbers - controls whether or not version numbers for each package appear in generated file. Defaults to false.",
5563
option => configuration.ExportCommand.IncludeVersionNumbers = option != null)
64+
.Add("include-arguments|include-remembered-arguments",
65+
"Include Remembered Arguments - controls whether or not remembered arguments for each package appear in generated file. Defaults to false. Available in 1.2.0+",
66+
option => configuration.ExportCommand.IncludeRememberedPackageArguments = option != null)
5667
;
5768
}
5869

@@ -98,12 +109,14 @@ choco export [<options/switches>]
98109
"chocolatey".Log().Info(@"
99110
choco export
100111
choco export --include-version-numbers
112+
choco export --include-version-numbers --include-remembered-arguments
101113
choco export ""'c:\temp\packages.config'""
102114
choco export ""'c:\temp\packages.config'"" --include-version-numbers
103115
choco export -o=""'c:\temp\packages.config'""
104116
choco export -o=""'c:\temp\packages.config'"" --include-version-numbers
105117
choco export --output-file-path=""'c:\temp\packages.config'""
106118
choco export --output-file-path=""'c:\temp\packages.config'"" --include-version-numbers
119+
choco export --output-file-path=""'c:\temp\packages.config'"" --include-remembered-arguments
107120
108121
NOTE: See scripting in the command reference (`choco -?`) for how to
109122
write proper scripts and integrations.
@@ -134,14 +147,26 @@ public bool may_require_admin_access()
134147

135148
public void noop(ChocolateyConfiguration configuration)
136149
{
137-
this.Log().Info("Export would have been with options: {0} Output File Path={1}{0} Include Version Numbers:{2}".format_with(Environment.NewLine, configuration.ExportCommand.OutputFilePath, configuration.ExportCommand.IncludeVersionNumbers));
150+
this.Log().Info("Export would have been with options: {0} Output File Path={1}{0} Include Version Numbers:{2}{0} Include Remembered Arguments: {3}".format_with(Environment.NewLine, configuration.ExportCommand.OutputFilePath, configuration.ExportCommand.IncludeVersionNumbers, configuration.ExportCommand.IncludeRememberedPackageArguments));
138151
}
139152

140153
public void run(ChocolateyConfiguration configuration)
141154
{
142155
var installedPackages = _nugetService.get_all_installed_packages(configuration);
143156
var xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
144157

158+
// Add the options set from the install command.
159+
ConfigurationOptions.OptionSet.Remove("o");
160+
var installCommand = new ChocolateyInstallCommand(_packageService);
161+
// If the --source option is available, assume that the install command arguments have already been added.
162+
// This is required for when this method multiple times in a row, such as in the unit tests.
163+
if (!ConfigurationOptions.OptionSet.Contains("source"))
164+
{
165+
installCommand.configure_argument_parser(ConfigurationOptions.OptionSet, configuration);
166+
}
167+
// Make sure we have a clean configuration to revert to after parsing remembered arguments.
168+
configuration.start_backup();
169+
145170
FaultTolerance.try_catch_with_logging_exception(
146171
() =>
147172
{
@@ -164,6 +189,50 @@ public void run(ChocolateyConfiguration configuration)
164189
packageElement.Version = packageResult.Package.Version.ToString();
165190
}
166191

192+
if (configuration.ExportCommand.IncludeRememberedPackageArguments)
193+
{
194+
195+
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
196+
configuration.Features.UseRememberedArgumentsForUpgrades = true;
197+
_nugetService.set_package_config_for_upgrade(configuration, pkgInfo);
198+
199+
// Mirrors the arguments captured in ChocolateyPackageService.capture_arguments()
200+
201+
if (configuration.Prerelease) packageElement.Prerelease = true;
202+
if (configuration.IgnoreDependencies) packageElement.IgnoreDependencies = true;
203+
if (configuration.ForceX86) packageElement.ForceX86 = true;
204+
205+
if (!string.IsNullOrWhiteSpace(configuration.InstallArguments)) packageElement.InstallArguments = configuration.InstallArguments;
206+
if (configuration.OverrideArguments) packageElement.OverrideArguments = true;
207+
if (configuration.ApplyInstallArgumentsToDependencies) packageElement.ApplyInstallArgumentsToDependencies = true;
208+
209+
if (!string.IsNullOrWhiteSpace(configuration.PackageParameters)) packageElement.PackageParameters = configuration.PackageParameters;
210+
if (configuration.ApplyPackageParametersToDependencies) packageElement.ApplyPackageParametersToDependencies = true;
211+
212+
if (configuration.AllowDowngrade) packageElement.AllowDowngrade = true;
213+
if (configuration.AllowMultipleVersions) packageElement.AllowMultipleVersions = true;
214+
215+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username)) packageElement.User = configuration.SourceCommand.Username;
216+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password)) packageElement.Password = configuration.SourceCommand.Password;
217+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate)) packageElement.Cert = configuration.SourceCommand.Certificate;
218+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword)) packageElement.CertPassword = configuration.SourceCommand.CertificatePassword;
219+
220+
// Arguments from the global options set
221+
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
222+
{
223+
packageElement.ExecutionTimeout = configuration.CommandExecutionTimeoutSeconds;
224+
}
225+
226+
// This was discussed in the PR, and because it is potentially system specific, it should not be included in the exported file
227+
// if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) packageElement.CacheLocation = configuration.CacheLocation;
228+
229+
// if (configuration.Features.FailOnStandardError) packageElement.FailOnStderr = true;
230+
// if (!configuration.Features.UsePowerShellHost) packageElement.UseSystemPowershell = true;
231+
232+
// Make sure to reset the configuration so as to be able to parse the next set of remembered arguments
233+
configuration.reset_config();
234+
}
235+
167236
packagesConfig.Packages.Add(packageElement);
168237
}
169238

src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ public sealed class ProxyConfiguration
666666
public sealed class ExportCommandConfiguration
667667
{
668668
public bool IncludeVersionNumbers { get; set; }
669+
public bool IncludeRememberedPackageArguments { get; set; }
669670

670671
public string OutputFilePath { get; set; }
671672
}

0 commit comments

Comments
 (0)