Skip to content

Commit db7ea32

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 GetPackageConfigFromRememberedArguments method in the NugetService to read and parse the remembered arguments.
1 parent 0ebf572 commit db7ea32

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $commandOptions = @{
5656
config = "--name='' --value=''"
5757
feature = "--name=''"
5858
apikey = "--source='' --api-key='' --remove"
59-
export = "--include-version-numbers --output-file-path=''"
59+
export = "--include-version-numbers --output-file-path='' --include-remembered-arguments"
6060
template = "--name=''"
6161
cache = "--expired"
6262
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ public abstract class ChocolateyExportCommandSpecsBase : TinySpec
3737
protected Mock<INugetService> NugetService = new Mock<INugetService>();
3838
protected Mock<IFileSystem> FileSystem = new Mock<IFileSystem>();
3939
protected ChocolateyConfiguration Configuration = new ChocolateyConfiguration();
40+
protected Mock<IChocolateyPackageInformationService> PackageInfoService = new Mock<IChocolateyPackageInformationService>();
41+
protected Mock<IChocolateyPackageService> PackageService = new Mock<IChocolateyPackageService>();
4042

4143
public override void Context()
4244
{
43-
Command = new ChocolateyExportCommand(NugetService.Object, FileSystem.Object);
45+
Command = new ChocolateyExportCommand(NugetService.Object, FileSystem.Object, PackageInfoService.Object, PackageService.Object);
4446
}
4547

4648
public void Reset()
4749
{
4850
NugetService.ResetCalls();
4951
FileSystem.ResetCalls();
52+
PackageInfoService.ResetCalls();
53+
PackageService.ResetCalls();
5054
}
5155
}
5256

@@ -104,6 +108,18 @@ public void Should_add_include_version_to_the_option_set()
104108
{
105109
_optionSet.Contains("include-version").Should().BeTrue();
106110
}
111+
112+
[Fact]
113+
public void Should_add_include_arguments_to_the_option_set()
114+
{
115+
_optionSet.Contains("include-arguments").Should().BeTrue();
116+
}
117+
118+
[Fact]
119+
public void Should_add_include_remembered_arguments_to_the_option_set()
120+
{
121+
_optionSet.Contains("include-remembered-arguments").Should().BeTrue();
122+
}
107123
}
108124

109125
public class When_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase

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

Lines changed: 52 additions & 3 deletions
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 ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration configuration)
@@ -53,6 +61,9 @@ public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration
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 2.3.0+",
66+
option => configuration.ExportCommand.IncludeRememberedPackageArguments = option != null)
5667
;
5768
}
5869

@@ -96,12 +107,14 @@ choco export [<options/switches>]
96107
"chocolatey".Log().Info(@"
97108
choco export
98109
choco export --include-version-numbers
110+
choco export --include-version-numbers --include-remembered-arguments
99111
choco export ""'c:\temp\packages.config'""
100112
choco export ""'c:\temp\packages.config'"" --include-version-numbers
101113
choco export -o=""'c:\temp\packages.config'""
102114
choco export -o=""'c:\temp\packages.config'"" --include-version-numbers
103115
choco export --output-file-path=""'c:\temp\packages.config'""
104116
choco export --output-file-path=""'c:\temp\packages.config'"" --include-version-numbers
117+
choco export --output-file-path=""""'c:\temp\packages.config'"""" --include-remembered-arguments
105118
106119
NOTE: See scripting in the command reference (`choco -?`) for how to
107120
write proper scripts and integrations.
@@ -132,14 +145,16 @@ public bool MayRequireAdminAccess()
132145

133146
public void DryRun(ChocolateyConfiguration configuration)
134147
{
135-
this.Log().Info("Export would have been with options: {0} Output File Path={1}{0} Include Version Numbers:{2}".FormatWith(Environment.NewLine, configuration.ExportCommand.OutputFilePath, configuration.ExportCommand.IncludeVersionNumbers));
148+
this.Log().Info("Export would have been with options: {0} Output File Path={1}{0} Include Version Numbers:{2}{0} Include Remembered Arguments: {3}".FormatWith(Environment.NewLine, configuration.ExportCommand.OutputFilePath, configuration.ExportCommand.IncludeVersionNumbers, configuration.ExportCommand.IncludeRememberedPackageArguments));
136149
}
137150

138151
public void Run(ChocolateyConfiguration configuration)
139152
{
140153
var installedPackages = _nugetService.GetInstalledPackages(configuration);
141154
var xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
142155

156+
configuration.CreateBackup();
157+
143158
FaultTolerance.TryCatchWithLoggingException(
144159
() =>
145160
{
@@ -162,6 +177,40 @@ public void Run(ChocolateyConfiguration configuration)
162177
packageElement.Version = packageResult.PackageMetadata.Version.ToString();
163178
}
164179

180+
if (configuration.ExportCommand.IncludeRememberedPackageArguments)
181+
{
182+
var pkgInfo = _packageInfoService.Get(packageResult.PackageMetadata);
183+
configuration.Features.UseRememberedArgumentsForUpgrades = true;
184+
var rememberedConfig = _nugetService.GetPackageConfigFromRememberedArguments(configuration, pkgInfo);
185+
186+
// Mirrors the arguments captured in ChocolateyPackageService.CaptureArguments()
187+
if (configuration.Prerelease) packageElement.Prerelease = true;
188+
if (configuration.IgnoreDependencies) packageElement.IgnoreDependencies = true;
189+
if (configuration.ForceX86) packageElement.ForceX86 = true;
190+
if (!string.IsNullOrWhiteSpace(configuration.InstallArguments)) packageElement.InstallArguments = configuration.InstallArguments;
191+
if (configuration.OverrideArguments) packageElement.OverrideArguments = true;
192+
if (configuration.ApplyInstallArgumentsToDependencies) packageElement.ApplyInstallArgumentsToDependencies = true;
193+
if (!string.IsNullOrWhiteSpace(configuration.PackageParameters)) packageElement.PackageParameters = configuration.PackageParameters;
194+
if (configuration.ApplyPackageParametersToDependencies) packageElement.ApplyPackageParametersToDependencies = true;
195+
if (configuration.AllowDowngrade) packageElement.AllowDowngrade = true;
196+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username)) packageElement.User = configuration.SourceCommand.Username;
197+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password)) packageElement.Password = configuration.SourceCommand.Password;
198+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate)) packageElement.Cert = configuration.SourceCommand.Certificate;
199+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword)) packageElement.CertPassword = configuration.SourceCommand.CertificatePassword;
200+
// Arguments from the global options set
201+
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
202+
{
203+
packageElement.ExecutionTimeout = configuration.CommandExecutionTimeoutSeconds;
204+
}
205+
// This was discussed in the PR, and because it is potentially system specific, it should not be included in the exported file
206+
// if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) packageElement.CacheLocation = configuration.CacheLocation;
207+
// if (configuration.Features.FailOnStandardError) packageElement.FailOnStderr = true;
208+
// if (!configuration.Features.UsePowerShellHost) packageElement.UseSystemPowershell = true;
209+
210+
// Make sure to reset the configuration so as to be able to parse the next set of remembered arguments
211+
configuration.RevertChanges();
212+
}
213+
165214
packagesConfig.Packages.Add(packageElement);
166215
}
167216

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ public sealed class ProxyConfiguration
720720
public sealed class ExportCommandConfiguration
721721
{
722722
public bool IncludeVersionNumbers { get; set; }
723+
public bool IncludeRememberedPackageArguments { get; set; }
723724

724725
public string OutputFilePath { get; set; }
725726
}

0 commit comments

Comments
 (0)