Skip to content

Commit

Permalink
(#2503) Add ability to export remembered arguments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheCakeIsNaOH committed Mar 6, 2022
1 parent d493ac8 commit d2c4fc9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public abstract class ChocolateyExportCommandSpecsBase : TinySpec
protected Mock<INugetService> nugetService = new Mock<INugetService>();
protected Mock<IFileSystem> fileSystem = new Mock<IFileSystem>();
protected ChocolateyConfiguration configuration = new ChocolateyConfiguration();
protected Mock<IChocolateyPackageInformationService> packageInfoService = new Mock<IChocolateyPackageInformationService>();

public override void Context()
{
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object);
command = new ChocolateyExportCommand(nugetService.Object, fileSystem.Object, packageInfoService.Object);
}

public void reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ public class ChocolateyExportCommand : ICommand
{
private readonly INugetService _nugetService;
private readonly IFileSystem _fileSystem;
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly IChocolateyPackageService _packageService;

public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem)
public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem, IChocolateyPackageInformationService packageInfoService)
{
_nugetService = nugetService;
_fileSystem = fileSystem;
_packageInfoService = packageInfoService;
}

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

Expand Down Expand Up @@ -97,12 +103,14 @@ choco export [<options/switches>]
"chocolatey".Log().Info(@"
choco export
choco export --include-version-numbers
choco export --include-version-numbers --include-remembered-arguments
choco export ""'c:\temp\packages.config'""
choco export ""'c:\temp\packages.config'"" --include-version-numbers
choco export -o=""'c:\temp\packages.config'""
choco export -o=""'c:\temp\packages.config'"" --include-version-numbers
choco export --output-file-path=""'c:\temp\packages.config'""
choco export --output-file-path=""'c:\temp\packages.config'"" --include-version-numbers
choco export --output-file-path=""'c:\temp\packages.config'"" --include-remembered-arguments
NOTE: See scripting in the command reference (`choco -?`) for how to
write proper scripts and integrations.
Expand Down Expand Up @@ -140,6 +148,17 @@ public void run(ChocolateyConfiguration configuration)
{
var packageResults = _nugetService.get_all_installed_packages(configuration);
var settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false) };
var originalConfiguration = configuration.deep_copy();

if (configuration.ExportCommand.IncludeRememberedPackageArguments)
{
// The -o argument from the export command options set interferes with the -o argument from the install command options set.
ConfigurationOptions.OptionSet.Remove("o");

//Add the options set from the install command.
var installCommand = new ChocolateyInstallCommand(_packageService);
installCommand.configure_argument_parser(ConfigurationOptions.OptionSet, configuration);
}

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

if (configuration.ExportCommand.IncludeRememberedPackageArguments)
{
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
configuration.Features.UseRememberedArgumentsForUpgrades = true;
_nugetService.set_package_config_for_upgrade(configuration, pkgInfo);

//Mirrors the arguments captured in ChocolateyPackageService.capture_arguments()

if (configuration.Prerelease) xw.WriteAttributeString("prerelease", "true");
if (configuration.IgnoreDependencies) xw.WriteAttributeString("ignoreDependencies", "true");
if (configuration.ForceX86) xw.WriteAttributeString("forceX86", "true");

if (!string.IsNullOrWhiteSpace(configuration.InstallArguments)) xw.WriteAttributeString("installArguments", configuration.InstallArguments);
if (configuration.OverrideArguments) xw.WriteAttributeString("overrideArguments", "true");
if (configuration.ApplyInstallArgumentsToDependencies) xw.WriteAttributeString("applyInstallArgumentsToDependencies", "true");

if (!string.IsNullOrWhiteSpace(configuration.PackageParameters)) xw.WriteAttributeString("packageParameters", configuration.PackageParameters);
if (configuration.ApplyPackageParametersToDependencies) xw.WriteAttributeString("applyPackageParametersToDependencies", "true");

if (configuration.AllowDowngrade) xw.WriteAttributeString("allowDowngrade", "true");
if (configuration.AllowMultipleVersions) xw.WriteAttributeString("allowMultipleVersions", "true");

if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username)) xw.WriteAttributeString("user", configuration.SourceCommand.Username);
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password)) xw.WriteAttributeString("password", configuration.SourceCommand.Password);
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate)) xw.WriteAttributeString("cert", configuration.SourceCommand.Certificate);
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword)) xw.WriteAttributeString("certPassword", configuration.SourceCommand.CertificatePassword);

//Arguments from the global options set
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
{
xw.WriteAttributeString("timeout",configuration.CommandExecutionTimeoutSeconds.to_string());
}

//TODO, should this be exported? It seems rather system specific
//if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) xw.WriteAttributeString("cacheLocation", configuration.CacheLocation);

if (configuration.Features.FailOnStandardError) xw.WriteAttributeString("failOnStderr", "true");
if (!configuration.Features.UsePowerShellHost) xw.WriteAttributeString("useSystemPowershell", "true");

//Make sure to reset the configuration
configuration = originalConfiguration.deep_copy();
}

xw.WriteEndElement();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public sealed class ProxyConfiguration
public sealed class ExportCommandConfiguration
{
public bool IncludeVersionNumbers { get; set; }
public bool IncludeRememberedPackageArguments { get; set; }

public string OutputFilePath { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void RegisterComponents(Container container)
new ChocolateyUnpackSelfCommand(container.GetInstance<IFileSystem>()),
new ChocolateyVersionCommand(container.GetInstance<IChocolateyPackageService>()),
new ChocolateyUpdateCommand(container.GetInstance<IChocolateyPackageService>()),
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>()),
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>(), container.GetInstance<IChocolateyPackageInformationService>())
new ChocolateyTemplateCommand(container.GetInstance<ITemplateService>())
};
return list.AsReadOnly();
Expand Down

0 comments on commit d2c4fc9

Please sign in to comment.