diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-08-31 19:45:15 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-08-31 19:45:15 +0000 |
commit | 44bc284d96eb551117564ca004a6f2f7bc6aeddd (patch) | |
tree | 5e9057bb268355c62bcf403ce237b8509708b81f /tools/aapt2/configuration/ConfigurationParser.cpp | |
parent | 381855b8a5bd506ee6b8299ab69ccd054c571d6b (diff) | |
parent | 9ecc0751e30086c0668c8fe5758f14fc5acf5ae8 (diff) |
Merge "AAPT2: Add flag to print multi APK artifact names."
Diffstat (limited to 'tools/aapt2/configuration/ConfigurationParser.cpp')
-rw-r--r-- | tools/aapt2/configuration/ConfigurationParser.cpp | 78 |
1 files changed, 56 insertions, 22 deletions
diff --git a/tools/aapt2/configuration/ConfigurationParser.cpp b/tools/aapt2/configuration/ConfigurationParser.cpp index 424e9be3ef09..1735a504e553 100644 --- a/tools/aapt2/configuration/ConfigurationParser.cpp +++ b/tools/aapt2/configuration/ConfigurationParser.cpp @@ -30,6 +30,7 @@ #include "io/File.h" #include "io/FileSystem.h" #include "io/StringInputStream.h" +#include "util/Files.h" #include "util/Maybe.h" #include "util/Util.h" #include "xml/XmlActionExecutor.h" @@ -149,23 +150,48 @@ static bool ReplacePlaceholder(const StringPiece& placeholder, const Maybe<Strin return true; } -Maybe<std::string> Artifact::ToArtifactName(const StringPiece& format, IDiagnostics* diag, - const StringPiece& base_name, - const StringPiece& ext) const { - std::string result = format.to_string(); +/** + * Returns the common artifact base name from a template string. + */ +Maybe<std::string> ToBaseName(std::string result, const StringPiece& apk_name, IDiagnostics* diag) { + const StringPiece ext = file::GetExtension(apk_name); + size_t end_index = apk_name.to_string().rfind(ext.to_string()); + const std::string base_name = + (end_index != std::string::npos) ? std::string{apk_name.begin(), end_index} : ""; - Maybe<StringPiece> maybe_base_name = - base_name.empty() ? Maybe<StringPiece>{} : Maybe<StringPiece>{base_name}; - if (!ReplacePlaceholder("${basename}", maybe_base_name, &result, diag)) { - return {}; + // Base name is optional. + if (result.find("${basename}") != std::string::npos) { + Maybe<StringPiece> maybe_base_name = + base_name.empty() ? Maybe<StringPiece>{} : Maybe<StringPiece>{base_name}; + if (!ReplacePlaceholder("${basename}", maybe_base_name, &result, diag)) { + return {}; + } } // Extension is optional. if (result.find("${ext}") != std::string::npos) { - if (!ReplacePlaceholder("${ext}", {ext}, &result, diag)) { + // Make sure we disregard the '.' in the extension when replacing the placeholder. + if (!ReplacePlaceholder("${ext}", {ext.substr(1)}, &result, diag)) { return {}; } + } else { + // If no extension is specified, and the name template does not end in the current extension, + // add the existing extension. + if (!util::EndsWith(result, ext)) { + result.append(ext.to_string()); + } + } + + return result; +} + +Maybe<std::string> Artifact::ToArtifactName(const StringPiece& format, const StringPiece& apk_name, + IDiagnostics* diag) const { + Maybe<std::string> base = ToBaseName(format.to_string(), apk_name, diag); + if (!base) { + return {}; } + std::string result = std::move(base.value()); if (!ReplacePlaceholder("${abi}", abi_group, &result, diag)) { return {}; @@ -194,29 +220,37 @@ Maybe<std::string> Artifact::ToArtifactName(const StringPiece& format, IDiagnost return result; } -Maybe<std::string> Artifact::Name(const StringPiece& base_name, const StringPiece& ext, - IDiagnostics* diag) const { +Maybe<std::string> Artifact::Name(const StringPiece& apk_name, IDiagnostics* diag) const { if (!name) { return {}; } - std::string result = name.value(); + return ToBaseName(name.value(), apk_name, diag); +} - // Base name is optional. - if (result.find("${basename}") != std::string::npos) { - if (!ReplacePlaceholder("${basename}", {base_name}, &result, diag)) { - return {}; +bool PostProcessingConfiguration::AllArtifactNames(const StringPiece& apk_name, + std::vector<std::string>* artifact_names, + IDiagnostics* diag) const { + for (const auto& artifact : artifacts) { + Maybe<std::string> name; + if (artifact.name) { + name = artifact.Name(apk_name, diag); + } else { + if (!artifact_format) { + diag->Error(DiagMessage() << "No global artifact template and an artifact name is missing"); + return false; + } + name = artifact.ToArtifactName(artifact_format.value(), apk_name, diag); } - } - // Extension is optional. - if (result.find("${ext}") != std::string::npos) { - if (!ReplacePlaceholder("${ext}", {ext}, &result, diag)) { - return {}; + if (!name) { + return false; } + + artifact_names->push_back(std::move(name.value())); } - return result; + return true; } } // namespace configuration |