Skip to content

Commit

Permalink
Add support to export all installed packages in winget configure expo…
Browse files Browse the repository at this point in the history
…rt (microsoft#5156)

The feature is in parity with `winget export` command. Except the
exported file is in winget configuration yaml format. The exported file
is ready to be used with winget configure commands.

New additions:
- When exporting a specific package (existing functionality), in
addition we'll search the source to find the package before export. If
the package is not from well known source, the source is exported as
well. Supports exporting the version like `winget export` too.
- Added `--all` to export all installed packages. We reuse the workfow
in `winget export` to collect packages to export. For non well known
sources, the sources are exported as well. `--all` cannot be used with
other specific package export arguments.

Manually validated and added e2e tests.
  • Loading branch information
yao-msft authored Jan 28, 2025
1 parent 0daa420 commit 2dc4c07
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 108 deletions.
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/Argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ namespace AppInstaller::CLI
return { type, "module"_liv };
case Execution::Args::Type::ConfigurationExportResource:
return { type, "resource"_liv };
case Execution::Args::Type::ConfigurationExportAll:
return { type, "all"_liv, 'r', "recurse"_liv };
case Execution::Args::Type::ConfigurationHistoryItem:
return { type, "history"_liv, 'h', ArgTypeCategory::ConfigurationSetChoice, ArgTypeExclusiveSet::ConfigurationSetChoice };
case Execution::Args::Type::ConfigurationHistoryRemove:
Expand Down
22 changes: 15 additions & 7 deletions src/AppInstallerCLICore/ConfigureExportCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ namespace AppInstaller::CLI
Argument{ Execution::Args::Type::ConfigurationExportModule, Resource::String::ConfigureExportModule },
Argument{ Execution::Args::Type::ConfigurationExportResource, Resource::String::ConfigureExportResource },
Argument{ Execution::Args::Type::ConfigurationModulePath, Resource::String::ConfigurationModulePath },
Argument{ Execution::Args::Type::Source, Resource::String::ExportSourceArgumentDescription, ArgumentType::Standard },
Argument{ Execution::Args::Type::IncludeVersions, Resource::String::ExportIncludeVersionsArgumentDescription, ArgumentType::Flag },
Argument{ Execution::Args::Type::ConfigurationExportAll, Resource::String::ConfigureExportAll, ArgumentType::Flag },
Argument::ForType(Execution::Args::Type::AcceptSourceAgreements),
};
}

Expand All @@ -39,26 +43,30 @@ namespace AppInstaller::CLI
{
context <<
VerifyIsFullPackage <<
SearchSourceForPackageExport <<
CreateConfigurationProcessor <<
CreateOrOpenConfigurationSet <<
AddWinGetPackageAndResource <<
PopulateConfigurationSetForExport <<
WriteConfigFile;
}

void ConfigureExportCommand::ValidateArgumentsInternal(Execution::Args& execArgs) const
{
Configuration::ValidateCommonArguments(execArgs);

bool validInputArgs = false;
if (execArgs.Contains(Execution::Args::Type::ConfigurationExportModule, Execution::Args::Type::ConfigurationExportResource) ||
execArgs.Contains(Execution::Args::Type::ConfigurationExportPackageId))
if (!execArgs.Contains(Execution::Args::Type::ConfigurationExportModule, Execution::Args::Type::ConfigurationExportResource) &&
!execArgs.Contains(Execution::Args::Type::ConfigurationExportPackageId) &&
!execArgs.Contains(Execution::Args::Type::ConfigurationExportAll))
{
validInputArgs = true;
throw CommandException(Resource::String::ConfigureExportArgumentRequiredError);
}

if (!validInputArgs)
if (execArgs.Contains(Execution::Args::Type::ConfigurationExportAll) &&
(execArgs.Contains(Execution::Args::Type::ConfigurationExportPackageId) ||
execArgs.Contains(Execution::Args::Type::ConfigurationExportModule) ||
execArgs.Contains(Execution::Args::Type::ConfigurationExportResource)))
{
throw CommandException(Resource::String::ConfigureExportArgumentError);
throw CommandException(Resource::String::ConfigureExportArgumentConflictWithAllError);
}
}
}
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/ExecutionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ namespace AppInstaller::CLI::Execution
ConfigurationExportPackageId,
ConfigurationExportModule,
ConfigurationExportResource,
ConfigurationExportAll,
ConfigurationHistoryItem,
ConfigurationHistoryRemove,
ConfigurationStatusWatch,
Expand Down
4 changes: 3 additions & 1 deletion src/AppInstallerCLICore/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ namespace AppInstaller::CLI::Resource
WINGET_DEFINE_RESOURCE_STRINGID(ConfigurationWarningValueTruncated);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureCommandLongDescription);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureCommandShortDescription);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportArgumentError);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportAll);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportArgumentConflictWithAllError);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportArgumentRequiredError);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportCommandLongDescription);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportCommandShortDescription);
WINGET_DEFINE_RESOURCE_STRINGID(ConfigureExportModule);
Expand Down
271 changes: 179 additions & 92 deletions src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/AppInstallerCLICore/Workflows/ConfigurationFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ namespace AppInstaller::CLI::Workflow
// Outputs: None
void ValidateAllGoodMessage(Execution::Context& context);

// Adds a configuration unit with the winget package and/or exports resource given.
// Search source for package(s) to be exported in configuration file.
// Required Args: None
// Inputs: None
// Outputs: PackageCollection
void SearchSourceForPackageExport(Execution::Context& context);

// Adds configuration unit(s) with the winget package and/or exports resource given to configuration set.
// Required Args: None
// Inputs: ConfigurationProcessor, ConfigurationSet
// Outputs: None
void AddWinGetPackageAndResource(Execution::Context& context);
void PopulateConfigurationSetForExport(Execution::Context& context);

// Write the configuration file.
// Required Args: OutputFile
Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/Workflows/WorkflowBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ namespace AppInstaller::CLI::Workflow
{
Logging::Telemetry().LogMultiAppMatch();

if (m_operationType == OperationType::Upgrade || m_operationType == OperationType::Uninstall || m_operationType == OperationType::Repair)
if (m_operationType == OperationType::Upgrade || m_operationType == OperationType::Uninstall || m_operationType == OperationType::Repair || m_operationType == OperationType::Export)
{
context.Reporter.Warn() << Resource::String::MultipleInstalledPackagesFound << std::endl;
context << ReportMultiplePackageFoundResult;
Expand Down
154 changes: 154 additions & 0 deletions src/AppInstallerCLIE2ETests/ConfigureExportCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// -----------------------------------------------------------------------------
// <copyright file="ConfigureExportCommand.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace AppInstallerCLIE2ETests
{
using System.IO;
using AppInstallerCLIE2ETests.Helpers;
using NUnit.Framework;
using NUnit.Framework.Internal;

/// <summary>
/// `Configure export` command tests.
/// </summary>
public class ConfigureExportCommand
{
private const string Command = "configure export";
private const string ShowCommand = "configure show";

/// <summary>
/// Set up.
/// </summary>
[OneTimeSetUp]
public void BaseSetup()
{
TestCommon.SetupTestSource(false);
WinGetSettingsHelper.ConfigureFeature("configureExport", true);
var installDir = TestCommon.GetRandomTestDir();
TestCommon.RunAICLICommand("install", $"AppInstallerTest.TestPackageExport -v 1.0.0.0 --silent -l {installDir}");
}

/// <summary>
/// Tear down.
/// </summary>
[OneTimeTearDown]
public void BaseTeardown()
{
TestCommon.TearDownTestSource();
WinGetSettingsHelper.ConfigureFeature("configureExport", false);
TestCommon.RunAICLICommand("uninstall", "AppInstallerTest.TestPackageExport");
}

/// <summary>
/// Export a specific package.
/// </summary>
[Test]
public void ExportTestPackage()
{
var exportDir = TestCommon.GetRandomTestDir();
var exportFile = Path.Combine(exportDir, "exported.yml");
var result = TestCommon.RunAICLICommand(Command, $"--package-id AppInstallerTest.TestPackageExport -o {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(File.Exists(exportFile));

// Check exported file is readable and validate content
var showResult = TestCommon.RunAICLICommand(ShowCommand, $"-f {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, showResult.ExitCode);
Assert.True(showResult.StdOut.Contains("WinGetSource"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_{Constants.TestSourceType}]"));
Assert.True(showResult.StdOut.Contains($"type: {Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains($"argument: {Constants.TestSourceUrl}"));
Assert.True(showResult.StdOut.Contains($"name: {Constants.TestSourceName}"));

Assert.True(showResult.StdOut.Contains("WinGetPackage"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_AppInstallerTest.TestPackageExport]"));
Assert.True(showResult.StdOut.Contains($"Dependencies: {Constants.TestSourceName}_{Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains("id: AppInstallerTest.TestPackageExport"));
Assert.True(showResult.StdOut.Contains($"source: {Constants.TestSourceName}"));
}

/// <summary>
/// Export a specific package with version.
/// </summary>
[Test]
public void ExportTestPackageWithVersion()
{
var exportDir = TestCommon.GetRandomTestDir();
var exportFile = Path.Combine(exportDir, "exported.yml");
var result = TestCommon.RunAICLICommand(Command, $"--package-id AppInstallerTest.TestPackageExport --include-versions -o {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(File.Exists(exportFile));

// Check exported file is readable and validate content
var showResult = TestCommon.RunAICLICommand(ShowCommand, $"-f {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, showResult.ExitCode);
Assert.True(showResult.StdOut.Contains("WinGetSource"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_{Constants.TestSourceType}]"));
Assert.True(showResult.StdOut.Contains($"type: {Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains($"argument: {Constants.TestSourceUrl}"));
Assert.True(showResult.StdOut.Contains($"name: {Constants.TestSourceName}"));

Assert.True(showResult.StdOut.Contains("WinGetPackage"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_AppInstallerTest.TestPackageExport]"));
Assert.True(showResult.StdOut.Contains($"Dependencies: {Constants.TestSourceName}_{Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains("id: AppInstallerTest.TestPackageExport"));
Assert.True(showResult.StdOut.Contains($"source: {Constants.TestSourceName}"));
Assert.True(showResult.StdOut.Contains("version: 1.0.0.0"));
}

/// <summary>
/// Export all.
/// </summary>
[Test]
public void ExportAll()
{
var exportDir = TestCommon.GetRandomTestDir();
var exportFile = Path.Combine(exportDir, "exported.yml");
var result = TestCommon.RunAICLICommand(Command, $"--all -o {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(File.Exists(exportFile));

// Check exported file is readable and validate content
var showResult = TestCommon.RunAICLICommand(ShowCommand, $"-f {exportFile}");
Assert.AreEqual(Constants.ErrorCode.S_OK, showResult.ExitCode);
Assert.True(showResult.StdOut.Contains("WinGetSource"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_{Constants.TestSourceType}]"));
Assert.True(showResult.StdOut.Contains($"type: {Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains($"argument: {Constants.TestSourceUrl}"));
Assert.True(showResult.StdOut.Contains($"name: {Constants.TestSourceName}"));

Assert.True(showResult.StdOut.Contains("WinGetPackage"));
Assert.True(showResult.StdOut.Contains($"[{Constants.TestSourceName}_AppInstallerTest.TestPackageExport]"));
Assert.True(showResult.StdOut.Contains($"Dependencies: {Constants.TestSourceName}_{Constants.TestSourceType}"));
Assert.True(showResult.StdOut.Contains("id: AppInstallerTest.TestPackageExport"));
Assert.True(showResult.StdOut.Contains($"source: {Constants.TestSourceName}"));
}

/// <summary>
/// Export a specific package that's not installed.
/// </summary>
[Test]
public void ExportFailedWithNotFoundPackage()
{
var exportDir = TestCommon.GetRandomTestDir();
var exportFile = Path.Combine(exportDir, "exported.yml");
var result = TestCommon.RunAICLICommand(Command, $"--package-id NotFound.NotFound -o {exportFile}");
Assert.AreEqual(Constants.ErrorCode.ERROR_NO_APPLICATIONS_FOUND, result.ExitCode);
}

/// <summary>
/// Export all with specific package id.
/// </summary>
[Test]
public void ExportFailedWithAllAndSpecificPackage()
{
var exportDir = TestCommon.GetRandomTestDir();
var exportFile = Path.Combine(exportDir, "exported.yml");
var result = TestCommon.RunAICLICommand(Command, $"--all --package-id AppInstallerTest.TestPackageExport -o {exportFile}");
Assert.AreEqual(Constants.ErrorCode.ERROR_INVALID_CL_ARGUMENTS, result.ExitCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Id: AppInstallerTest.TestPackageExport
Name: TestPackageExport
Version: 1.0.0.0
Publisher: AppInstallerTest
License: Test
Installers:
- Arch: x86
Url: https://localhost:5001/TestKit/AppInstallerTestExeInstaller/AppInstallerTestExeInstaller.exe
Sha256: <EXEHASH>
InstallerType: exe
ProductCode: '{92e3d4e5-6e3d-4ae4-b9f0-b7e0a5f25b91}'
Switches:
Custom: '/ProductID {92e3d4e5-6e3d-4ae4-b9f0-b7e0a5f25b91} /DisplayName TestPackageExport'
SilentWithProgress: /exeswp
Silent: /exesilent
Interactive: /exeinteractive
Language: /exeenus
Log: /LogFile <LOGPATH>
InstallLocation: /InstallDir <INSTALLPATH>
ManifestVersion: 0.1.0
17 changes: 12 additions & 5 deletions src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ They can be configured through the settings file 'winget settings'.</value>
<value>Ignore unavailable packages</value>
</data>
<data name="ExportIncludeVersionsArgumentDescription" xml:space="preserve">
<value>Include package versions in produced file</value>
<value>Include package versions in export file</value>
</data>
<data name="ImportIgnoreVersionsArgumentDescription" xml:space="preserve">
<value>Ignore package versions from import file</value>
Expand Down Expand Up @@ -2976,12 +2976,16 @@ Please specify one of them using the --source option to proceed.</value>
<data name="ConfigurationGettingResourceSettings" xml:space="preserve">
<value>Getting configuration settings...</value>
</data>
<data name="ConfigureExportArgumentError" xml:space="preserve">
<value>At least --packageId and/or --module with --resource must be provided</value>
<comment>{Locked="--packageId,--module, --resource"}</comment>
<data name="ConfigureExportArgumentRequiredError" xml:space="preserve">
<value>At least --packageId and/or --module with --resource must be provided. Or use --all to export all package configurations.</value>
<comment>{Locked="--packageId,--module, --resource, --all"}</comment>
</data>
<data name="ConfigureExportArgumentConflictWithAllError" xml:space="preserve">
<value>Arguments --packageId, --module and --resource cannot be used with --all.</value>
<comment>{Locked="--packageId,--module, --resource, --all"}</comment>
</data>
<data name="ConfigureExportCommandLongDescription" xml:space="preserve">
<value>Exports configuration resources to a configuration file. When used with --packageId, exports a WinGetPackage resource of the given package id. When used with --module and --resource, gets the settings of the resource and exports it to the configuration file. If the output configuration file already exists, appends the exported configuration resources.</value>
<value>Exports configuration resources to a configuration file. When used with --all, exports all package configurations. When used with --packageId, exports a WinGetPackage resource of the given package id. When used with --module and --resource, gets the settings of the resource and exports it to the configuration file. If the output configuration file already exists, appends the exported configuration resources.</value>
<comment>{Locked="WinGetPackage,--packageId,--module, --resource"}</comment>
</data>
<data name="ConfigureExportCommandShortDescription" xml:space="preserve">
Expand All @@ -2996,6 +3000,9 @@ Please specify one of them using the --source option to proceed.</value>
<data name="ConfigureExportResource" xml:space="preserve">
<value>The configuration resource to export.</value>
</data>
<data name="ConfigureExportAll" xml:space="preserve">
<value>Exports all package configurations.</value>
</data>
<data name="WINGET_CONFIG_ERROR_GET_FAILED" xml:space="preserve">
<value>The configuration unit failed getting its properties.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ namespace AppInstaller::Repository
bool Explicit = false;
};

// Check if a source matches a well known source
std::optional<WellKnownSource> CheckForWellKnownSource(const SourceDetails& sourceDetails);

// Individual source agreement entry. Label will be highlighted in the display as the key of the agreement entry.
struct SourceAgreement
{
Expand Down
5 changes: 5 additions & 0 deletions src/AppInstallerRepositoryCore/RepositorySource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ namespace AppInstaller::Repository
}
}

std::optional<WellKnownSource> CheckForWellKnownSource(const SourceDetails& sourceDetails)
{
return CheckForWellKnownSourceMatch(sourceDetails.Name, sourceDetails.Arg, sourceDetails.Type);
}

Source::Source() {}

Source::Source(std::string_view name)
Expand Down

0 comments on commit 2dc4c07

Please sign in to comment.