Skip to content

Commit b549b47

Browse files
TheCakeIsNaOHgep13
authored andcommitted
(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 f7c203e commit b549b47

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-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
rule = "--name=''"

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ public abstract class ChocolateyExportCommandSpecsBase : TinySpec
4141
protected Mock<IContainerResolver> ContainerResolver = new Mock<IContainerResolver>();
4242
protected Mock<IRegistryService> RegistryService = new Mock<IRegistryService>();
4343
protected Mock<IChocolateyPackageInformationService> PackageInfoService = new Mock<IChocolateyPackageInformationService>();
44+
protected Mock<IChocolateyPackageService> PackageService = new Mock<IChocolateyPackageService>();
4445

4546
public override void Context()
4647
{
47-
Command = new ChocolateyExportCommand(NugetService.Object, FileSystem.Object, ContainerResolver.Object, RegistryService.Object, PackageInfoService.Object);
48+
Command = new ChocolateyExportCommand(NugetService.Object, FileSystem.Object, ContainerResolver.Object, RegistryService.Object, PackageInfoService.Object, PackageService.Object);
4849
}
4950

5051
public void Reset()
5152
{
5253
NugetService.ResetCalls();
5354
FileSystem.ResetCalls();
55+
PackageInfoService.ResetCalls();
56+
PackageService.ResetCalls();
5457
}
5558
}
5659

@@ -108,6 +111,18 @@ public void Should_add_include_version_to_the_option_set()
108111
{
109112
_optionSet.Contains("include-version").Should().BeTrue();
110113
}
114+
115+
[Fact]
116+
public void Should_add_include_arguments_to_the_option_set()
117+
{
118+
_optionSet.Contains("include-arguments").Should().BeTrue();
119+
}
120+
121+
[Fact]
122+
public void Should_add_include_remembered_arguments_to_the_option_set()
123+
{
124+
_optionSet.Contains("include-remembered-arguments").Should().BeTrue();
125+
}
111126
}
112127

113128
public class When_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase

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

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@ public class ChocolateyExportCommand : ICommand
4646
private readonly IContainerResolver _containerResolver;
4747
private readonly IRegistryService _registryService;
4848
private readonly IChocolateyPackageInformationService _packageInfoService;
49-
50-
public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem, IContainerResolver containerResolver, IRegistryService registryService, IChocolateyPackageInformationService packageInfoService)
49+
private readonly IChocolateyPackageService _packageService;
50+
51+
public ChocolateyExportCommand(
52+
INugetService nugetService,
53+
IFileSystem fileSystem,
54+
IContainerResolver containerResolver,
55+
IRegistryService registryService,
56+
IChocolateyPackageInformationService packageInfoService,
57+
IChocolateyPackageService packageService)
5158
{
5259
_nugetService = nugetService;
5360
_fileSystem = fileSystem;
5461
_containerResolver = containerResolver;
5562
_registryService = registryService;
5663
_packageInfoService = packageInfoService;
64+
_packageService = packageService;
5765
}
5866

5967
public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration configuration)
@@ -71,6 +79,9 @@ public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration
7179
.Add("include-alternative-sources",
7280
"IncludeAlternativeSources - Includes software from alternative sources which are managed by Chocolatey CLI.",
7381
option => configuration.ExportCommand.IncludeAlternativeSources = option != null)
82+
.Add("include-arguments|include-remembered-arguments",
83+
"Include Remembered Arguments - controls whether or not remembered arguments for each package appear in generated file. Defaults to false. Available in 2.3.0+",
84+
option => configuration.ExportCommand.IncludeRememberedPackageArguments = option != null)
7485
;
7586
}
7687

@@ -114,12 +125,14 @@ choco export [<options/switches>]
114125
"chocolatey".Log().Info(@"
115126
choco export
116127
choco export --include-version-numbers
128+
choco export --include-version-numbers --include-remembered-arguments
117129
choco export ""'c:\temp\packages.config'""
118130
choco export ""'c:\temp\packages.config'"" --include-version-numbers
119131
choco export -o=""'c:\temp\packages.config'""
120132
choco export -o=""'c:\temp\packages.config'"" --include-version-numbers
121133
choco export --output-file-path=""'c:\temp\packages.config'""
122134
choco export --output-file-path=""'c:\temp\packages.config'"" --include-version-numbers
135+
choco export --output-file-path=""""'c:\temp\packages.config'"""" --include-remembered-arguments
123136
124137
NOTE: See scripting in the command reference (`choco -?`) for how to
125138
write proper scripts and integrations.
@@ -150,14 +163,16 @@ public bool MayRequireAdminAccess()
150163

151164
public void DryRun(ChocolateyConfiguration configuration)
152165
{
153-
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));
166+
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));
154167
}
155168

156169
public void Run(ChocolateyConfiguration configuration)
157170
{
158171
var installedPackages = _nugetService.GetInstalledPackages(configuration);
159172
var xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
160173

174+
configuration.CreateBackup();
175+
161176
FaultTolerance.TryCatchWithLoggingException(
162177
() =>
163178
{
@@ -182,6 +197,93 @@ public void Run(ChocolateyConfiguration configuration)
182197

183198
xw.WriteAttributeString("sourceType", "nuget");
184199

200+
if (configuration.ExportCommand.IncludeRememberedPackageArguments)
201+
{
202+
var pkgInfo = _packageInfoService.Get(packageResult.PackageMetadata);
203+
configuration.Features.UseRememberedArgumentsForUpgrades = true;
204+
var rememberedConfig = _nugetService.GetPackageConfigFromRememberedArguments(configuration, pkgInfo);
205+
206+
// Mirrors the arguments captured in ChocolateyPackageService.CaptureArguments()
207+
if (configuration.Prerelease)
208+
{
209+
packageElement.Prerelease = true;
210+
}
211+
212+
if (configuration.IgnoreDependencies)
213+
{
214+
packageElement.IgnoreDependencies = true;
215+
}
216+
217+
if (configuration.ForceX86)
218+
{
219+
packageElement.ForceX86 = true;
220+
}
221+
222+
if (!string.IsNullOrWhiteSpace(configuration.InstallArguments))
223+
{
224+
packageElement.InstallArguments = configuration.InstallArguments;
225+
}
226+
227+
if (configuration.OverrideArguments)
228+
{
229+
packageElement.OverrideArguments = true;
230+
}
231+
232+
if (configuration.ApplyInstallArgumentsToDependencies)
233+
{
234+
packageElement.ApplyInstallArgumentsToDependencies = true;
235+
}
236+
237+
if (!string.IsNullOrWhiteSpace(configuration.PackageParameters))
238+
{
239+
packageElement.PackageParameters = configuration.PackageParameters;
240+
}
241+
242+
if (configuration.ApplyPackageParametersToDependencies)
243+
{
244+
packageElement.ApplyPackageParametersToDependencies = true;
245+
}
246+
247+
if (configuration.AllowDowngrade)
248+
{
249+
packageElement.AllowDowngrade = true;
250+
}
251+
252+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username))
253+
{
254+
packageElement.User = configuration.SourceCommand.Username;
255+
}
256+
257+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password))
258+
{
259+
packageElement.Password = configuration.SourceCommand.Password;
260+
}
261+
262+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate))
263+
{
264+
packageElement.Cert = configuration.SourceCommand.Certificate;
265+
}
266+
267+
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword))
268+
{
269+
packageElement.CertPassword = configuration.SourceCommand.CertificatePassword;
270+
}
271+
272+
// Arguments from the global options set
273+
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
274+
{
275+
packageElement.ExecutionTimeout = configuration.CommandExecutionTimeoutSeconds;
276+
}
277+
278+
// This was discussed in the PR, and because it is potentially system specific, it should not be included in the exported file
279+
// if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) packageElement.CacheLocation = configuration.CacheLocation;
280+
// if (configuration.Features.FailOnStandardError) packageElement.FailOnStderr = true;
281+
// if (!configuration.Features.UsePowerShellHost) packageElement.UseSystemPowershell = true;
282+
283+
// Make sure to reset the configuration so as to be able to parse the next set of remembered arguments
284+
configuration.RevertChanges();
285+
}
286+
185287
packagesConfig.Packages.Add(packageElement);
186288
}
187289

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ public sealed class ProxyConfiguration
736736
public sealed class ExportCommandConfiguration
737737
{
738738
public bool IncludeVersionNumbers { get; set; }
739+
public bool IncludeRememberedPackageArguments { get; set; }
739740

740741
public string OutputFilePath { get; set; }
741742

0 commit comments

Comments
 (0)