Skip to content

Commit

Permalink
(chocolatey#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. It reuses the
set_package_config_for_upgrade method in the NugetService to read and
parse the remembered arguments.
  • Loading branch information
TheCakeIsNaOH committed Jan 7, 2023
1 parent cac3302 commit d94da07
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ 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>();
protected Mock<IChocolateyPackageService> packageService = new Mock<IChocolateyPackageService>();

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

public void reset()
{
nugetService.ResetCalls();
fileSystem.ResetCalls();
packageInfoService.ResetCalls();
packageService.ResetCalls();
}
}

Expand Down Expand Up @@ -104,6 +108,18 @@ public void should_add_include_version_to_the_option_set()
{
optionSet.Contains("include-version").ShouldBeTrue();
}

[Fact]
public void should_add_include_arguments_to_the_option_set()
{
optionSet.Contains("include-arguments").ShouldBeTrue();
}

[Fact]
public void should_add_include_remembered_arguments_to_the_option_set()
{
optionSet.Contains("include-remembered-arguments").ShouldBeTrue();
}
}

public class when_handling_additional_argument_parsing : ChocolateyExportCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ public class ChocolateyExportCommand : ICommand
{
private readonly INugetService _nugetService;
private readonly IFileSystem _fileSystem;

public ChocolateyExportCommand(INugetService nugetService, IFileSystem fileSystem)
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly IChocolateyPackageService _packageService;

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

public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
Expand All @@ -53,6 +61,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. Available in 1.2.0+",
option => configuration.ExportCommand.IncludeRememberedPackageArguments = option != null)
;
}

Expand Down Expand Up @@ -98,12 +109,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 @@ -134,14 +147,26 @@ public bool may_require_admin_access()

public void noop(ChocolateyConfiguration configuration)
{
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));
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));
}

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

// Add the options set from the install command.
ConfigurationOptions.OptionSet.Remove("o");
var installCommand = new ChocolateyInstallCommand(_packageService);
// If the --source option is available, assume that the install command arguments have already been added.
// This is required for when this method multiple times in a row, such as in the unit tests.
if (!ConfigurationOptions.OptionSet.Contains("source"))
{
installCommand.configure_argument_parser(ConfigurationOptions.OptionSet, configuration);
}
// Make sure we have a clean configuration to revert to after parsing remembered arguments.
configuration.start_backup();

FaultTolerance.try_catch_with_logging_exception(
() =>
{
Expand All @@ -164,6 +189,50 @@ public void run(ChocolateyConfiguration configuration)
packageElement.Version = packageResult.PackageMetadata.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) packageElement.Prerelease = true;
if (configuration.IgnoreDependencies) packageElement.IgnoreDependencies = true;
if (configuration.ForceX86) packageElement.ForceX86 = true;
if (!string.IsNullOrWhiteSpace(configuration.InstallArguments)) packageElement.InstallArguments = configuration.InstallArguments;
if (configuration.OverrideArguments) packageElement.OverrideArguments = true;
if (configuration.ApplyInstallArgumentsToDependencies) packageElement.ApplyInstallArgumentsToDependencies = true;
if (!string.IsNullOrWhiteSpace(configuration.PackageParameters)) packageElement.PackageParameters = configuration.PackageParameters;
if (configuration.ApplyPackageParametersToDependencies) packageElement.ApplyPackageParametersToDependencies = true;
if (configuration.AllowDowngrade) packageElement.AllowDowngrade = true;
if (configuration.AllowMultipleVersions) packageElement.AllowMultipleVersions = true;
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Username)) packageElement.User = configuration.SourceCommand.Username;
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Password)) packageElement.Password = configuration.SourceCommand.Password;
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate)) packageElement.Cert = configuration.SourceCommand.Certificate;
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.CertificatePassword)) packageElement.CertPassword = configuration.SourceCommand.CertificatePassword;
// Arguments from the global options set
if (configuration.CommandExecutionTimeoutSeconds != ApplicationParameters.DefaultWaitForExitInSeconds)
{
packageElement.ExecutionTimeout = configuration.CommandExecutionTimeoutSeconds;
}
// This was discussed in the PR, and because it is potentially system specific, it should not be included in the exported file
// if (!string.IsNullOrWhiteSpace(configuration.CacheLocation)) packageElement.CacheLocation = configuration.CacheLocation;
// if (configuration.Features.FailOnStandardError) packageElement.FailOnStderr = true;
// if (!configuration.Features.UsePowerShellHost) packageElement.UseSystemPowershell = true;
// Make sure to reset the configuration so as to be able to parse the next set of remembered arguments
configuration.reset_config();
}
packagesConfig.Packages.Add(packageElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,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

0 comments on commit d94da07

Please sign in to comment.