forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[uwptool] Refactor command-handling to a map (flutter#26309)
Extracts each of the commands (listapps, install, uninstall, launch) into a command class that is registered at startup. This simplifies adding further commands, and cleans up the code a bit. This is a refactoring that introduces no changes to existing functionality.
- Loading branch information
Showing
5 changed files
with
254 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include "flutter/shell/platform/windows/uwptool_commands.h" | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
|
||
#include "flutter/shell/platform/windows/string_conversion.h" | ||
#include "flutter/shell/platform/windows/uwptool_utils.h" | ||
|
||
namespace flutter { | ||
|
||
bool ListAppsCommand::ValidateArgs(const std::vector<std::string>& args) const { | ||
return true; | ||
} | ||
|
||
int ListAppsCommand::Run(const std::vector<std::string>& args) const { | ||
flutter::ApplicationStore app_store; | ||
for (const flutter::Application& app : app_store.GetApps()) { | ||
std::wcout << app.GetPackageFamily() << std::endl; | ||
} | ||
return 0; | ||
} | ||
|
||
bool InstallCommand::ValidateArgs(const std::vector<std::string>& args) const { | ||
return args.size() >= 1; | ||
} | ||
|
||
int InstallCommand::Run(const std::vector<std::string>& args) const { | ||
std::wstring package_uri = flutter::Utf16FromUtf8(args[0]); | ||
std::vector<std::wstring> dependency_uris; | ||
for (int i = 1; i < args.size(); ++i) { | ||
dependency_uris.push_back(flutter::Utf16FromUtf8(args[i])); | ||
} | ||
flutter::ApplicationStore app_store; | ||
if (app_store.InstallApp(package_uri, dependency_uris)) { | ||
std::wcerr << L"Installed application " << package_uri << std::endl; | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
bool UninstallCommand::ValidateArgs( | ||
const std::vector<std::string>& args) const { | ||
return args.size() >= 1; | ||
} | ||
|
||
int UninstallCommand::Run(const std::vector<std::string>& args) const { | ||
std::wstring package_family = flutter::Utf16FromUtf8(args[0]); | ||
bool success = true; | ||
flutter::ApplicationStore app_store; | ||
for (flutter::Application& app : app_store.GetApps(package_family)) { | ||
if (app.Uninstall()) { | ||
std::wcerr << L"Uninstalled application " << app.GetPackageFullName() | ||
<< std::endl; | ||
} else { | ||
std::wcerr << L"error: Failed to uninstall application " | ||
<< app.GetPackageFullName() << std::endl; | ||
success = false; | ||
} | ||
} | ||
return success ? 0 : 1; | ||
} | ||
|
||
bool LaunchCommand::ValidateArgs(const std::vector<std::string>& args) const { | ||
return args.size() >= 1; | ||
} | ||
|
||
int LaunchCommand::Run(const std::vector<std::string>& args) const { | ||
// Get the package family name. | ||
std::string package_family = args[0]; | ||
|
||
// Concatenate the remaining args, comma-separated. | ||
std::ostringstream app_args; | ||
for (int i = 1; i < args.size(); ++i) { | ||
app_args << args[i]; | ||
if (i < args.size() - 1) { | ||
app_args << ","; | ||
} | ||
} | ||
int process_id = LaunchApp(flutter::Utf16FromUtf8(package_family), | ||
flutter::Utf16FromUtf8(app_args.str())); | ||
if (process_id == -1) { | ||
std::cerr << "error: Failed to launch app with package family " | ||
<< package_family << std::endl; | ||
return 1; | ||
} | ||
|
||
// Write an informative message for the user to stderr. | ||
std::cerr << "Launched app with package family " << package_family | ||
<< ". PID: " << std::endl; | ||
// Write the PID to stdout. The flutter tool reads this value in. | ||
std::cout << process_id << std::endl; | ||
return 0; | ||
} | ||
|
||
// Launches the app installed on the system with the specified package. | ||
// | ||
// Returns -1 if no matching app, or multiple matching apps are found, or if | ||
// the app fails to launch. Otherwise, the process ID of the launched app is | ||
// returned. | ||
int LaunchCommand::LaunchApp(const std::wstring_view package_family, | ||
const std::wstring_view args) const { | ||
flutter::ApplicationStore app_store; | ||
for (flutter::Application& app : app_store.GetApps(package_family)) { | ||
int process_id = app.Launch(args); | ||
if (process_id != -1) { | ||
return process_id; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_UWPTOOL_COMMANDS_H_ | ||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_UWPTOOL_COMMANDS_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace flutter { | ||
|
||
// A uwptool command that can be invoked as the first argument of the uwptool | ||
// arguments list. | ||
class Command { | ||
public: | ||
Command(const std::string_view name, | ||
const std::string_view usage, | ||
const std::string_view description) | ||
: name_(name), usage_(usage), description_(description) {} | ||
virtual ~Command() {} | ||
|
||
std::string GetCommandName() const { return name_; } | ||
std::string GetUsage() const { return usage_; } | ||
std::string GetDescription() const { return description_; } | ||
|
||
// Returns true if the arguments list constitute valid arguments for this | ||
// command. | ||
virtual bool ValidateArgs(const std::vector<std::string>& args) const = 0; | ||
|
||
// Invokes the command with the specified arguments list. | ||
virtual int Run(const std::vector<std::string>& args) const = 0; | ||
|
||
private: | ||
std::string name_; | ||
std::string usage_; | ||
std::string description_; | ||
}; | ||
|
||
// Command that prints a list of all installed applications on the system. | ||
class ListAppsCommand : public Command { | ||
public: | ||
ListAppsCommand() | ||
: Command("listapps", | ||
"listapps", | ||
"List installed apps by package family name") {} | ||
|
||
bool ValidateArgs(const std::vector<std::string>& args) const override; | ||
int Run(const std::vector<std::string>& args) const override; | ||
}; | ||
|
||
// Command that installs the specified package and dependencies. | ||
class InstallCommand : public Command { | ||
public: | ||
InstallCommand() | ||
: Command("install", | ||
"install PACKAGE_URI DEPENDENCY_URI...", | ||
"Install the specified package with all listed dependencies") {} | ||
|
||
bool ValidateArgs(const std::vector<std::string>& args) const override; | ||
int Run(const std::vector<std::string>& args) const override; | ||
}; | ||
|
||
// Command that uninstalls the specified package. | ||
class UninstallCommand : public Command { | ||
public: | ||
UninstallCommand() | ||
: Command("uninstall", | ||
"uninstall PACKAGE_FAMILY_NAME", | ||
"Uninstall the specified package") {} | ||
|
||
bool ValidateArgs(const std::vector<std::string>& args) const override; | ||
int Run(const std::vector<std::string>& args) const override; | ||
}; | ||
|
||
// Command that launches the specified application package. | ||
class LaunchCommand : public Command { | ||
public: | ||
LaunchCommand() | ||
: Command("launch", | ||
"launch PACKAGE_FAMILY_NAME", | ||
"Launch the specified package") {} | ||
|
||
bool ValidateArgs(const std::vector<std::string>& args) const override; | ||
int Run(const std::vector<std::string>& args) const override; | ||
|
||
private: | ||
int LaunchApp(const std::wstring_view package_family, | ||
const std::wstring_view args) const; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_UWPTOOL_COMMANDS_H_ |
Oops, something went wrong.