Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1030)

Unified Diff: tools/gn/bundle_data.cc

Issue 1752033002: Add "create_bundle" target in order to support bundle with gn. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn-bundle-data
Patch Set: Rebase Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/gn/bundle_data.cc
diff --git a/tools/gn/bundle_data.cc b/tools/gn/bundle_data.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7bc260c7cad28206877896abbaedff9b466a7804
--- /dev/null
+++ b/tools/gn/bundle_data.cc
@@ -0,0 +1,114 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "tools/gn/bundle_data.h"
+
+#include "base/logging.h"
+#include "tools/gn/output_file.h"
+#include "tools/gn/settings.h"
+#include "tools/gn/target.h"
+
+namespace {
+
+// Return directory of |path| without the trailing directory separator.
+base::StringPiece FindDirNoTrailingSeparator(const base::StringPiece& path) {
+ base::StringPiece::size_type pos = path.find_last_of("/\\");
+ if (pos == base::StringPiece::npos)
+ return base::StringPiece();
+ return base::StringPiece(path.data(), pos);
+}
+
+} // namespace
+
+// Returns true if |source| correspond to the path of a file in an asset
+// catalog.
+//
+// An asset catalog is a bundle with the ".xcassets" extension. It contains
+// one directory per asset each of them with the ".imageset" extension.
+//
+// As an approximation, this function checks whether source matches:
+// .*\.xcassets/[^/]*\.imageset/[^/]*
+bool IsSourceFileFromAssetCatalog(const SourceFile& source,
+ SourceFile* asset_catalog) {
+ base::StringPiece dir;
+ dir = FindDirNoTrailingSeparator(source.value());
+ if (!dir.ends_with(".imageset"))
+ return false;
+ dir = FindDirNoTrailingSeparator(dir);
+ if (!dir.ends_with(".xcassets"))
+ return false;
+ if (asset_catalog) {
+ std::string asset_catalog_path = dir.as_string();
+ *asset_catalog = SourceFile(SourceFile::SWAP_IN, &asset_catalog_path);
+ }
+ return true;
+}
+
+BundleData::BundleData() {}
+
+BundleData::~BundleData() {}
+
+void BundleData::AddFileRuleFromTarget(const Target* target) {
+ DCHECK_EQ(target->output_type(), Target::BUNDLE_DATA);
+
+ std::vector<SourceFile> file_rule_sources;
+ for (const SourceFile& source_file : target->sources()) {
+ if (IsSourceFileFromAssetCatalog(source_file, nullptr)) {
+ asset_catalog_sources_.push_back(source_file);
+ } else {
+ file_rule_sources.push_back(source_file);
+ }
+ }
+
+ DCHECK_EQ(target->action_values().outputs().list().size(), 1u);
+ file_rules_.push_back(
+ BundleFileRule(file_rule_sources,
+ target->action_values().outputs().list()[0]));
+}
+
+
+void BundleData::GetSourceFiles(std::vector<SourceFile>* sources) const {
+ DCHECK(sources);
+ for (const BundleFileRule& file_rule : file_rules_) {
+ sources->insert(sources->end(),
+ file_rule.sources().begin(),
+ file_rule.sources().end());
+ }
+ sources->insert(sources->end(),
+ asset_catalog_sources_.begin(),
+ asset_catalog_sources_.end());
+}
+
+void BundleData::GetOutputFiles(const Settings* settings,
+ std::vector<OutputFile>* outputs) const {
+ DCHECK(settings);
+ DCHECK(outputs);
+ std::vector<SourceFile> outputs_as_sources;
+ GetOutputsAsSourceFiles(settings, &outputs_as_sources);
+ for (const SourceFile& source_file : outputs_as_sources) {
+ outputs->push_back(OutputFile(settings->build_settings(), source_file));
+ }
+}
+
+void BundleData::GetOutputsAsSourceFiles(
+ const Settings* settings,
+ std::vector<SourceFile>* outputs_as_source) const {
+ DCHECK(settings);
+ DCHECK(outputs_as_source);
+ for (const BundleFileRule& file_rule : file_rules_) {
+ for (const SourceFile& source : file_rule.sources()) {
+ outputs_as_source->push_back(file_rule.ApplyPatternToSource(
+ settings, *this, source));
+ }
+ }
+
+ if (!asset_catalog_sources_.empty())
+ outputs_as_source->push_back(GetCompiledAssetCatalogPath());
+}
+
+SourceFile BundleData::GetCompiledAssetCatalogPath() const {
+ DCHECK(!asset_catalog_sources_.empty());
+ std::string assets_car_path = resources_dir_ + "/Assets.car";
+ return SourceFile(SourceFile::SWAP_IN, &assets_car_path);
+}

Powered by Google App Engine
This is Rietveld 408576698