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

Commit 1e7b9347 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Created resuable DumpApkCommand and added "badger""

parents e0017d64 214846df
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
+254 −186

File changed.

Preview size limit exceeded, changes collapsed.

+154 −64
Original line number Diff line number Diff line
@@ -19,14 +19,62 @@

#include "Command.h"
#include "Debug.h"
#include "LoadedApk.h"
#include "dump/DumpManifest.h"

namespace aapt {

/** Command the contents of files generated from the compilation stage. */
/**
 * The base command for dumping information about apks. When the command is executed, the command
 * performs the DumpApkCommand::Dump() operation on each apk provided as a file argument.
 **/
class DumpApkCommand : public Command {
 public:
  explicit DumpApkCommand(const std::string&& name, text::Printer* printer, IDiagnostics* diag)
      : Command(name), printer_(printer), diag_(diag) {
  }

  text::Printer* GetPrinter() {
    return printer_;
  }

  IDiagnostics* GetDiagnostics() {
    return diag_;
  }

  /** Perform the dump operation on the apk. */
  virtual int Dump(LoadedApk* apk) = 0;

  int Action(const std::vector<std::string>& args) final {
    if (args.size() < 1) {
      diag_->Error(DiagMessage() << "No dump apk specified.");
      return 1;
    }

    bool error = false;
    for (auto apk : args) {
      auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_);
      if (!loaded_apk) {
        error = true;
        continue;
      }

      error |= Dump(loaded_apk.get());
    }

    return error;
  }

 private:
  text::Printer* printer_;
  IDiagnostics* diag_;
};

/** Command that prints contents of files generated from the compilation stage. */
class DumpAPCCommand : public Command {
 public:
  explicit DumpAPCCommand(IDiagnostics* diag) : Command("apc"), diag_(diag) {
  explicit DumpAPCCommand(text::Printer* printer, IDiagnostics* diag)
      : Command("apc"), printer_(printer), diag_(diag) {
    SetDescription("Print the contents of the AAPT2 Container (APC) generated fom compilation.");
    AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.",
                      &no_values_);
@@ -36,115 +84,157 @@ class DumpAPCCommand : public Command {
  int Action(const std::vector<std::string>& args) override;

 private:
  text::Printer* printer_;
  IDiagnostics* diag_;
  bool verbose_ = false;
  bool no_values_ = false;
  bool verbose_ = false;
};

/** Prints every configuration used by a resource in an APK. */
class DumpConfigsCommand : public Command {
/** Easter egg command shown when users enter "badger" instead of "badging". */
class DumpBadgerCommand : public Command {
 public:
  explicit DumpConfigsCommand(IDiagnostics* diag) : Command("configurations"), diag_(diag) {
    SetDescription("Print every configuration used by a resource in the APK.");
  explicit DumpBadgerCommand(text::Printer* printer) : Command("badger"), printer_(printer) {
  }

  int Action(const std::vector<std::string>& args) override;

 private:
  IDiagnostics* diag_;
  text::Printer* printer_;
  const static char kBadgerData[2925];
};

/** Prints the contents of the resource table string pool in the APK. */
class DumpStringsCommand : public Command {
class DumpBadgingCommand : public DumpApkCommand {
 public:
    explicit DumpStringsCommand(IDiagnostics* diag) : Command("strings"), diag_(diag) {
      SetDescription("Print the contents of the resource table string pool in the APK.");
  explicit DumpBadgingCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("badging", printer, diag) {
    SetDescription("Print information extracted from the manifest of the APK.");
    AddOptionalSwitch("--include-meta-data", "Include meta-data information.",
                      &options_.include_meta_data);
  }

    int Action(const std::vector<std::string>& args) override;
  int Dump(LoadedApk* apk) override {
    return DumpManifest(apk, options_, GetPrinter(), GetDiagnostics());
  }

 private:
    IDiagnostics* diag_;
  DumpManifestOptions options_;
};

class DumpConfigsCommand : public DumpApkCommand {
 public:
  explicit DumpConfigsCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("configurations", printer, diag) {
    SetDescription("Print every configuration used by a resource in the APK.");
  }

  int Dump(LoadedApk* apk) override;
};

class DumpPackageNameCommand : public DumpApkCommand {
 public:
  explicit DumpPackageNameCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("packagename", printer, diag) {
    SetDescription("Print the package name of the APK.");
  }

  int Dump(LoadedApk* apk) override;
};

class DumpPermissionsCommand : public DumpApkCommand {
 public:
  explicit DumpPermissionsCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("permissions", printer, diag) {
    SetDescription("Print the permissions extracted from the manifest of the APK.");
  }

  int Dump(LoadedApk* apk) override {
    DumpManifestOptions options;
    options.only_permissions = true;
    return DumpManifest(apk, options, GetPrinter(), GetDiagnostics());
  }
};

/** Prints the contents of the resource table from the APK. */
class DumpTableCommand : public Command {
class DumpStringsCommand : public DumpApkCommand {
 public:
  explicit DumpTableCommand(IDiagnostics* diag) : Command("resources"), diag_(diag) {
  explicit DumpStringsCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("strings", printer, diag) {
    SetDescription("Print the contents of the resource table string pool in the APK.");
  }

  int Dump(LoadedApk* apk) override;
};

class DumpTableCommand : public DumpApkCommand {
 public:
  explicit DumpTableCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("resources", printer, diag) {
    SetDescription("Print the contents of the resource table from the APK.");
    AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.",
                      &no_values_);
    AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
  }

  int Action(const std::vector<std::string>& args) override;
  int Dump(LoadedApk* apk) override;

 private:
  IDiagnostics* diag_;
  bool verbose_ = false;
  bool no_values_ = false;
  bool verbose_ = false;
};

/** Prints the string pool of a compiled xml in an APK. */
class DumpXmlStringsCommand : public Command {
class DumpXmlStringsCommand : public DumpApkCommand {
 public:
    explicit DumpXmlStringsCommand(IDiagnostics* diag) : Command("xmlstrings"), diag_(diag) {
  explicit DumpXmlStringsCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("xmlstrings", printer, diag) {
    SetDescription("Print the string pool of a compiled xml in an APK.");
    AddRequiredFlagList("--file", "A compiled xml file to print", &files_);
  }

    int Action(const std::vector<std::string>& args) override;
  int Dump(LoadedApk* apk) override;

 private:
    IDiagnostics* diag_;
  std::vector<std::string> files_;
};


/** Prints the tree of a compiled xml in an APK. */
class DumpXmlTreeCommand : public Command {
class DumpXmlTreeCommand : public DumpApkCommand {
 public:
  explicit DumpXmlTreeCommand(IDiagnostics* diag) : Command("xmltree"), diag_(diag) {
  explicit DumpXmlTreeCommand(text::Printer* printer, IDiagnostics* diag)
      : DumpApkCommand("xmltree", printer, diag) {
    SetDescription("Print the tree of a compiled xml in an APK.");
    AddRequiredFlagList("--file", "A compiled xml file to print", &files_);
  }

  int Action(const std::vector<std::string>& args) override;
  int Dump(LoadedApk* apk) override;

 private:
  IDiagnostics* diag_;
  std::vector<std::string> files_;
};

/** Prints the contents of the resource table from the APK. */
class DumpPackageNameCommand : public Command {
 public:
  explicit DumpPackageNameCommand(IDiagnostics* diag) : Command("packagename"), diag_(diag) {
    SetDescription("Print the package name of the APK.");
  }

  int Action(const std::vector<std::string>& args) override;

 private:
  IDiagnostics* diag_;
};

/** The default dump command. Performs no action because a subcommand is required. */
class DumpCommand : public Command {
 public:
  explicit DumpCommand(IDiagnostics* diag) : Command("dump", "d"), diag_(diag) {
    AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpTableCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(diag_));
    AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(diag_));
  explicit DumpCommand(text::Printer* printer, IDiagnostics* diag)
      : Command("dump", "d"), diag_(diag) {
    AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpTableCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(printer, diag_));
    AddOptionalSubcommand(util::make_unique<DumpBadgerCommand>(printer), /* hidden */ true);
  }

  int Action(const std::vector<std::string>& args) override;
  int Action(const std::vector<std::string>& args) override {
    if (args.size() == 0) {
      diag_->Error(DiagMessage() << "no subcommand specified");
    } else {
      diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'");
    }
    Usage(&std::cerr);
    return 1;
  }

 private:
  IDiagnostics* diag_;
Loading