Skip to content

Commit

Permalink
[uwptool] Refactor command-handling to a map (flutter#26309)
Browse files Browse the repository at this point in the history
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
cbracken authored May 21, 2021
1 parent d0cb12d commit 37c5a95
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 124 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,8 @@ FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h
FILE: ../../../flutter/shell/platform/windows/text_input_plugin_delegate.h
FILE: ../../../flutter/shell/platform/windows/text_input_plugin_unittest.cc
FILE: ../../../flutter/shell/platform/windows/uwptool_commands.cc
FILE: ../../../flutter/shell/platform/windows/uwptool_commands.h
FILE: ../../../flutter/shell/platform/windows/uwptool_main.cc
FILE: ../../../flutter/shell/platform/windows/uwptool_utils.cc
FILE: ../../../flutter/shell/platform/windows/uwptool_utils.h
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,13 @@ source_set("uwptool_utils") {
}

sources = [
"uwptool_commands.cc",
"uwptool_commands.h",
"uwptool_utils.cc",
"uwptool_utils.h",
]

deps = [ ":string_conversion" ]
}

# Command-line tool used by the flutter tool that serves a similar purpose to
Expand Down
112 changes: 112 additions & 0 deletions shell/platform/windows/uwptool_commands.cc
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
94 changes: 94 additions & 0 deletions shell/platform/windows/uwptool_commands.h
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_
Loading

0 comments on commit 37c5a95

Please sign in to comment.