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

Commit b1027277 authored by Shane Farmer's avatar Shane Farmer
Browse files

AAPT2: Read config from disk

Implement the todo left from last change to read the contents of the
configuration file from disk. Since this is an operation that may fail
the API was changed to take return a Maybe to indicate errors reading
the file.

Test: unit test for error condition
Test: ran aapt2 optimize with the new code path wired in

Change-Id: I93d532b4a57af9520231225eee4fc5f2b1a046b9
parent 71d3509c
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -21,10 +21,13 @@
#include <memory>
#include <utility>

#include <android-base/file.h>
#include <android-base/logging.h>

#include "ConfigDescription.h"
#include "Diagnostics.h"
#include "io/File.h"
#include "io/FileSystem.h"
#include "util/Util.h"
#include "xml/XmlActionExecutor.h"
#include "xml/XmlDom.h"
@@ -42,6 +45,8 @@ using ::aapt::configuration::Configuration;
using ::aapt::configuration::GlTexture;
using ::aapt::configuration::Group;
using ::aapt::configuration::Locale;
using ::aapt::io::IFile;
using ::aapt::io::RegularFile;
using ::aapt::util::TrimWhitespace;
using ::aapt::xml::Element;
using ::aapt::xml::FindRootElement;
@@ -49,6 +54,7 @@ using ::aapt::xml::NodeCast;
using ::aapt::xml::XmlActionExecutor;
using ::aapt::xml::XmlActionExecutorPolicy;
using ::aapt::xml::XmlNodeAction;
using ::android::base::ReadFileToString;

const std::unordered_map<std::string, Abi> kAbiMap = {
    {"armeabi", Abi::kArmeV6},
@@ -96,6 +102,17 @@ class NamespaceVisitor : public xml::Visitor {

}  // namespace



/** Returns a ConfigurationParser for the file located at the provided path. */
Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) {
  std::string contents;
  if (!ReadFileToString(path, &contents, true)) {
    return {};
  }
  return ConfigurationParser(contents);
}

ConfigurationParser::ConfigurationParser(std::string contents)
    : contents_(std::move(contents)),
      diag_(&noop_) {
+4 −6
Original line number Diff line number Diff line
@@ -142,18 +142,16 @@ class Element;
 */
class ConfigurationParser {
 public:

  /** Returns a ConfigurationParser for the file located at the provided path. */
  static Maybe<ConfigurationParser> ForPath(const std::string& path);

  /** Returns a ConfigurationParser for the configuration in the provided file contents. */
  static ConfigurationParser ForContents(const std::string& contents) {
    ConfigurationParser parser{contents};
    return parser;
  }

  /** Returns a ConfigurationParser for the file located at the provided path. */
  static ConfigurationParser ForPath(const std::string& path) {
    // TODO: Read XML file into memory.
    return ForContents(path);
  }

  /** Sets the diagnostics context to use when parsing. */
  ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
    diag_ = diagnostics;
+5 −0
Original line number Diff line number Diff line
@@ -130,6 +130,11 @@ class ConfigurationParserTest : public ConfigurationParser, public ::testing::Te
  StdErrDiagnostics diag_;
};

TEST_F(ConfigurationParserTest, ForPath_NoFile) {
  auto result = ConfigurationParser::ForPath("./does_not_exist.xml");
  EXPECT_FALSE(result);
}

TEST_F(ConfigurationParserTest, ValidateFile) {
  auto parser = ConfigurationParser::ForContents(kValidConfig).WithDiagnostics(&diag_);
  auto result = parser.Parse();
+1 −1
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8" ?>
<post-process xmlns="http://schemas.android.com/tools/aapt2">
<post-process xmlns="http://schemas.android.com/tools/aapt">
  <groups>
    <abi-group label="arm">
      <abi>armeabi-v7a</abi>