Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 214846df authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Created resuable DumpApkCommand and added "badger"

This change refactors the dump commands to inherit from a base
DumpApkCommand and adds a command that prints out an ASCII
image of a badger if the user wrote "badger" instead of
"badging". The command is hidden from the help menu.

Bug: 73535002
Test: manual
Change-Id: I9bdd8a7bbf6a4282c4933e5c478f6d1d8e32d99e
parent 99ecc121
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include "cmd/Dump.h"
#include "cmd/Link.h"
#include "cmd/Optimize.h"
#include "io/FileStream.h"
#include "util/Files.h"
#include "util/Util.h"

@@ -68,10 +69,11 @@ class VersionCommand : public Command {
/** The main entry point of AAPT. */
class MainCommand : public Command {
 public:
  explicit MainCommand(IDiagnostics* diagnostics) : Command("aapt2"), diagnostics_(diagnostics) {
  explicit MainCommand(text::Printer* printer, IDiagnostics* diagnostics)
      : Command("aapt2"), diagnostics_(diagnostics) {
    AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<DumpCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<DumpCommand>(printer, diagnostics));
    AddOptionalSubcommand(util::make_unique<DiffCommand>());
    AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
    AddOptionalSubcommand(util::make_unique<ConvertCommand>());
@@ -101,13 +103,14 @@ class MainCommand : public Command {
 */
class DaemonCommand : public Command {
 public:
  explicit DaemonCommand(IDiagnostics* diagnostics) : Command("daemon", "m"),
                                                      diagnostics_(diagnostics) {
  explicit DaemonCommand(io::FileOutputStream* out, IDiagnostics* diagnostics)
      : Command("daemon", "m"), out_(out), diagnostics_(diagnostics) {
    SetDescription("Runs aapt in daemon mode. Each subsequent line is a single parameter to the\n"
        "command. The end of an invocation is signaled by providing an empty line.");
  }

  int Action(const std::vector<std::string>& /* args */) override {
    text::Printer printer(out_);
    std::cout << "Ready" << std::endl;

    while (true) {
@@ -132,7 +135,9 @@ class DaemonCommand : public Command {

      std::vector<StringPiece> args;
      args.insert(args.end(), raw_args.begin(), raw_args.end());
      if (MainCommand(diagnostics_).Execute(args, &std::cerr) != 0) {
      int result = MainCommand(&printer, diagnostics_).Execute(args, &std::cerr);
      out_->Flush();
      if (result != 0) {
        std::cerr << "Error" << std::endl;
      }
      std::cerr << "Done" << std::endl;
@@ -143,6 +148,7 @@ class DaemonCommand : public Command {
  }

 private:
  io::FileOutputStream* out_;
  IDiagnostics* diagnostics_;
};

@@ -159,11 +165,17 @@ int MainImpl(int argc, char** argv) {
    args.push_back(argv[i]);
  }

  // Add the daemon subcommand here so it cannot be called while executing the daemon
  // Use a smaller buffer so that there is less latency for printing to stdout.
  constexpr size_t kStdOutBufferSize = 1024u;
  aapt::io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize);
  aapt::text::Printer printer(&fout);

  aapt::StdErrDiagnostics diagnostics;
  auto main_command = new aapt::MainCommand(&diagnostics);
  main_command->AddOptionalSubcommand(aapt::util::make_unique<aapt::DaemonCommand>(&diagnostics));
  auto main_command = new aapt::MainCommand(&printer, &diagnostics);

  // Add the daemon subcommand here so it cannot be called while executing the daemon
  main_command->AddOptionalSubcommand(
      aapt::util::make_unique<aapt::DaemonCommand>(&fout, &diagnostics));
  return main_command->Execute(args, &std::cerr);
}

+13 −3
Original line number Diff line number Diff line
@@ -93,10 +93,14 @@ void Command::AddOptionalSwitch(const StringPiece& name,
  flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 0, false});
}

void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand) {
void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) {
  subcommand->fullname_ = name_ + " " + subcommand->name_;
  if (experimental) {
    experimental_subcommands_.push_back(std::move(subcommand));
  } else {
    subcommands_.push_back(std::move(subcommand));
  }
}

void Command::SetDescription(const android::StringPiece& description) {
  description_ = description.to_string();
@@ -170,6 +174,12 @@ int Command::Execute(const std::vector<android::StringPiece>& args, std::ostream
                std::vector<android::StringPiece>(args.begin() + 1, args.end()), out_error);
          }
        }
        for (auto& subcommand : experimental_subcommands_) {
          if (arg == subcommand->name_ || arg==subcommand->short_name_) {
            return subcommand->Execute(
              std::vector<android::StringPiece>(args.begin() + 1, args.end()), out_error);
          }
        }
      }

      file_args.push_back(arg.to_string());
+2 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ class Command {
      const android::StringPiece& description, std::unordered_set<std::string>* value);
  void AddOptionalSwitch(const android::StringPiece& name, const android::StringPiece& description,
      bool* value);
  void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand);
  void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental = false);

  void SetDescription(const android::StringPiece& name);

@@ -83,6 +83,7 @@ class Command {
  std::string fullname_;
  std::vector<Flag> flags_;
  std::vector<std::unique_ptr<Command>> subcommands_;
  std::vector<std::unique_ptr<Command>> experimental_subcommands_;
};

}  // namespace aapt
Loading