From fcbf9016d58b1ed2289de2989227ba5f31b481d4 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sun, 16 May 2021 11:15:48 -0700 Subject: [PATCH] Adds uwptool install command (flutter/engine#26183) Installs the package pointed to by a URI, as well as its dependencies. --- .../shell/platform/windows/uwptool_main.cc | 35 ++++++++++++++++--- .../shell/platform/windows/uwptool_utils.cc | 33 +++++++++++++++++ .../shell/platform/windows/uwptool_utils.h | 7 ++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/uwptool_main.cc b/engine/src/flutter/shell/platform/windows/uwptool_main.cc index 8a18711855..0183716b90 100644 --- a/engine/src/flutter/shell/platform/windows/uwptool_main.cc +++ b/engine/src/flutter/shell/platform/windows/uwptool_main.cc @@ -42,6 +42,19 @@ int LaunchApp(const std::wstring_view package_family, return -1; } +// Installs the app in the specified build output directory. +// +// Returns true on success. +bool InstallApp(const std::wstring_view package_uri, + const std::vector& dependency_uris) { + flutter::ApplicationStore app_store; + if (app_store.InstallApp(package_uri, dependency_uris)) { + std::wcerr << L"Installed application " << package_uri << std::endl; + return true; + } + return false; +} + // Uninstalls the app with the specified package. // // Returns true on success. @@ -63,11 +76,12 @@ bool UninstallApp(const std::wstring_view package_family) { // Prints the command usage to stderr. void PrintUsage() { - std::cerr << "usage: uwptool COMMAND [ARGUMENTS]" << std::endl; - std::cerr << "commands:" << std::endl; - std::cerr << " listapps list installed apps" << std::endl; - std::cerr << " launch PACKAGE_FAMILY launch an app" << std::endl; - std::cerr << " uninstall PACKAGE_FAMILY uninstall an app" << std::endl; + std::cerr << "usage: uwptool COMMAND [ARGUMENTS]" << std::endl + << "commands:" << std::endl + << " listapps list all apps" << std::endl + << " launch PACKAGE_FAMILY launch an app" << std::endl + << " install PACKAGE_URI DEP_URI... install an app" << std::endl + << " uninstall PACKAGE_FAMILY uninstall an app" << std::endl; } } // namespace @@ -117,6 +131,17 @@ int main(int argc, char** argv) { // Write the PID to stdout. The flutter tool reads this value in. std::cout << process_id << std::endl; return 0; + } else if (command == "install") { + if (args.size() < 2) { + PrintUsage(); + return 1; + } + std::wstring package_uri = flutter::Utf16FromUtf8(args[1]); + std::vector dependency_uris; + for (int i = 2; i < args.size(); ++i) { + dependency_uris.push_back(flutter::Utf16FromUtf8(args[i])); + } + return InstallApp(package_uri, dependency_uris) ? 0 : 1; } else if (command == "uninstall") { if (args.size() < 2) { PrintUsage(); diff --git a/engine/src/flutter/shell/platform/windows/uwptool_utils.cc b/engine/src/flutter/shell/platform/windows/uwptool_utils.cc index ebb0bfa268..95e936adf9 100644 --- a/engine/src/flutter/shell/platform/windows/uwptool_utils.cc +++ b/engine/src/flutter/shell/platform/windows/uwptool_utils.cc @@ -112,4 +112,37 @@ std::vector ApplicationStore::GetApps( return apps; } +bool ApplicationStore::InstallApp( + const std::wstring_view package_uri, + const std::vector& dependency_uris) { + using winrt::Windows::Foundation::AsyncStatus; + using winrt::Windows::Foundation::Uri; + using winrt::Windows::Foundation::Collections::IVector; + using winrt::Windows::Management::Deployment::DeploymentOptions; + using winrt::Windows::Management::Deployment::PackageManager; + + Uri package(package_uri); + IVector dependencies = winrt::single_threaded_vector(); + for (const auto& dependency_uri : dependency_uris) { + dependencies.Append(Uri(dependency_uri)); + } + PackageManager package_manager; + auto operation = package_manager.AddPackageAsync(package, dependencies, + DeploymentOptions::None); + operation.get(); + + if (operation.Status() == AsyncStatus::Completed) { + return true; + } else if (operation.Status() == AsyncStatus::Canceled) { + return false; + } else if (operation.Status() == AsyncStatus::Error) { + auto result = operation.GetResults(); + std::wcerr << L"error: install failed for package " << package_uri + << L" with error: " << result.ErrorText().c_str() << std::endl; + return false; + } + + return false; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/uwptool_utils.h b/engine/src/flutter/shell/platform/windows/uwptool_utils.h index 08691d6ab1..4ccfe90368 100644 --- a/engine/src/flutter/shell/platform/windows/uwptool_utils.h +++ b/engine/src/flutter/shell/platform/windows/uwptool_utils.h @@ -70,6 +70,13 @@ class ApplicationStore { // Returns all installed applications with the specified family name. std::vector GetApps( const std::wstring_view package_family) const; + + // Installs the specified application. + // + // Installs the application located at package_uri with the specified + // dependencies. + bool InstallApp(const std::wstring_view package_uri, + const std::vector& dependency_uris); }; } // namespace flutter