Skip to content

Commit 265ec90

Browse files
committed
(#2503) Add ability to export remembered arguments
This adds the --include-remembered-arguments option which is used to export any remembered arguments. This reuses the set_package_config_for_upgrade method in the NugetService to read and parse the saved arguments.
1 parent 48ce534 commit 265ec90

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ 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>();
3940

4041
public override void Context()
4142
{
42-
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object);
43+
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object, packageInfoService.Object);
4344
}
4445

4546
public void reset()
4647
{
4748
nugetService.ResetCalls();
4849
fileSystem.ResetCalls();
50+
packageInfoService.ResetCalls();
4951
}
5052
}
5153

@@ -103,6 +105,18 @@ public void should_add_include_version_to_the_option_set()
103105
{
104106
optionSet.Contains("include-version").ShouldBeTrue();
105107
}
108+
109+
[Fact]
110+
public void should_add_include_arguments_to_the_option_set()
111+
{
112+
optionSet.Contains("include-arguments").ShouldBeTrue();
113+
}
114+
115+
[Fact]
116+
public void should_add_include_remembered_arguments_to_the_option_set()
117+
{
118+
optionSet.Contains("include-remembered-arguments").ShouldBeTrue();
119+
}
106120
}
107121

108122
public class when_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase

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

+64-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ public class ChocolateyExportCommand : ICommand
3636
{
3737
private readonly INugetService _nugetService;
3838
private readonly IFileSystem _fileSystem;
39+
private readonly IChocolateyPackageInformationService _packageInfoService;
40+
private readonly IChocolateyPackageService _packageService;
3941

40-
public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem)
42+
public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem, IChocolateyPackageInformationService packageInfoService)
4143
{
4244
_nugetService = nugetService;
4345
_fileSystem = fileSystem;
46+
_packageInfoService = packageInfoService;
4447
}
4548

4649
public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
@@ -52,6 +55,9 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati
5255
.Add("include-version-numbers|include-version",
5356
"Include Version Numbers - controls whether or not version numbers for each package appear in generated file. Defaults to false.",
5457
option => configuration.ExportCommand.IncludeVersionNumbers = option != null)
58+
.Add("include-arguments|include-remembered-arguments",
59+
"Include Remembered Arguments - controls whether or not remembered arguments for each package appear in generated file. Defaults to false.",
60+
option => configuration.ExportCommand.IncludeRememberedPackageArguments = option != null)
5561
;
5662
}
5763

@@ -97,12 +103,14 @@ choco export [<options/switches>]
97103
"chocolatey".Log().Info(@"
98104
choco export
99105
choco export --include-version-numbers
106+
choco export --include-version-numbers --include-remembered-arguments
100107
choco export ""'c:\temp\packages.config'""
101108
choco export ""'c:\temp\packages.config'"" --include-version-numbers
102109
choco export -o=""'c:\temp\packages.config'""
103110
choco export -o=""'c:\temp\packages.config'"" --include-version-numbers
104111
choco export --output-file-path=""'c:\temp\packages.config'""
105112
choco export --output-file-path=""'c:\temp\packages.config'"" --include-version-numbers
113+
choco export --output-file-path=""'c:\temp\packages.config'"" --include-remembered-arguments
106114
107115
NOTE: See scripting in the command reference (`choco -?`) for how to
108116
write proper scripts and integrations.
@@ -133,13 +141,24 @@ public bool may_require_admin_access()
133141

134142
public void noop(ChocolateyConfiguration configuration)
135143
{
136-
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));
144+
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));
137145
}
138146

139147
public void run(ChocolateyConfiguration configuration)
140148
{
141149
var packageResults = _nugetService.get_all_installed_packages(configuration);
142150
var settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
151+
var originalConfiguration = configuration.deep_copy();
152+
153+
if (configuration.ExportCommand.IncludeRememberedPackageArguments)
154+
{
155+
// The -o argument from the export command options set interferes with the -o argument from the install command options set.
156+
ConfigurationOptions.OptionSet.Remove("o");
157+
158+
//Add the options set from the install command.
159+
var installCommand = new ChocolateyInstallCommand(_packageService);
160+
installCommand.configure_argument_parser(ConfigurationOptions.OptionSet, configuration);
161+
}
143162

144163
FaultTolerance.try_catch_with_logging_exception(
145164
() =>
@@ -161,6 +180,49 @@ public void run(ChocolateyConfiguration configuration)
161180
xw.WriteAttributeString("version", packageResult.Package.Version.ToString());
162181
}
163182

183+
if (configuration.ExportCommand.IncludeRememberedPackageArguments)
184+
{
185+
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
186+
configuration.Features.UseRememberedArgumentsForUpgrades = true;
187+
_nugetService.set_package_config_for_upgrade(configuration, pkgInfo);
188+
189+
//Mirrors the arguments captured in ChocolateyPackageService.capture_arguments()
190+
191+
if (configuration.Prerelease) xw.WriteAttributeString("prerelease", "true");
192+
if (configuration.IgnoreDependencies) xw.WriteAttributeString("ignoreDependencies", "true");
193+
if (configuration.ForceX86) xw.WriteAttributeString("forceX86", "true");
194+
195+
if (!string.IsNullOrWhiteSpace(configuration.InstallArguments)) xw.WriteAttributeString("installArguments", configuration.InstallArguments);
196+
if (configuration.OverrideArguments) xw.WriteAttributeString("overrideArguments", "true");
197+
if (configuration.ApplyInstallArgumentsToDependencies) xw.WriteAttributeString("applyInstallArgumentsToDependencies", "true");
198+
199+
if (!string.IsNullOrWhiteSpace(configuration.PackageParameters)) xw.WriteAttributeString("packageParameters", configuration.PackageParameters);
200+
if (configuration.ApplyPackageParametersToDependencies) xw.WriteAttributeString("applyPackageParametersToDependencies", "true");
201+
202+
if (configuration.AllowDowngrade) xw.WriteAttributeString("allowDowngrade", "true");
203+
if (configuration.AllowMultipleVersions) xw.WriteAttributeString("allowMultipleVersions", "true");
204+
205+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username)) xw.WriteAttributeString("user", configuration.SourceCommand.Username);
206+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password)) xw.WriteAttributeString("password", configuration.SourceCommand.Password);
207+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate)) xw.WriteAttributeString("cert", configuration.SourceCommand.Certificate);
208+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword)) xw.WriteAttributeString("certPassword", configuration.SourceCommand.CertificatePassword);
209+
210+
//Arguments from the global options set
211+
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
212+
{
213+
xw.WriteAttributeString("timeout",configuration.CommandExecutionTimeoutSeconds.to_string());
214+
}
215+
216+
//TODO, should this be exported? It seems rather system specific
217+
//if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) xw.WriteAttributeString("cacheLocation", configuration.CacheLocation);
218+
219+
if (configuration.Features.FailOnStandardError) xw.WriteAttributeString("failOnStderr", "true");
220+
if (!configuration.Features.UsePowerShellHost) xw.WriteAttributeString("useSystemPowershell", "true");
221+
222+
//Make sure to reset the configuration
223+
configuration = originalConfiguration.deep_copy();
224+
}
225+
164226
xw.WriteEndElement();
165227
}
166228

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

+1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ public sealed class ProxyConfiguration
560560
public sealed class ExportCommandConfiguration
561561
{
562562
public bool IncludeVersionNumbers { get; set; }
563+
public bool IncludeRememberedPackageArguments { get; set; }
563564

564565
public string OutputFilePath { get; set; }
565566
}

src/chocolatey/infrastructure.app/registration/ContainerBinding.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void RegisterComponents(Container container)
9797
new ChocolateyUnpackSelfCommand(container.GetInstance<IFileSystem>()),
9898
new ChocolateyVersionCommand(container.GetInstance<IChocolateyPackageService>()),
9999
new ChocolateyUpdateCommand(container.GetInstance<IChocolateyPackageService>()),
100-
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>()),
100+
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>(), container.GetInstance<IChocolateyPackageInformationService>()),
101101
new ChocolateyTemplateCommand(container.GetInstance<ITemplateService>())
102102
};
103103
return list.AsReadOnly();

0 commit comments

Comments
 (0)