OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tools/gn/bundle_data.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "tools/gn/output_file.h" |
| 9 #include "tools/gn/settings.h" |
| 10 #include "tools/gn/target.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Return directory of |path| without the trailing directory separator. |
| 15 base::StringPiece FindDirNoTrailingSeparator(const base::StringPiece& path) { |
| 16 base::StringPiece::size_type pos = path.find_last_of("/\\"); |
| 17 if (pos == base::StringPiece::npos) |
| 18 return base::StringPiece(); |
| 19 return base::StringPiece(path.data(), pos); |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 bool IsSourceFileFromAssetCatalog(const SourceFile& source, |
| 25 SourceFile* asset_catalog) { |
| 26 // Check that the file matches the following pattern: |
| 27 // .*\.xcassets/[^/]*\.imageset/[^/]* |
| 28 base::StringPiece dir; |
| 29 dir = FindDirNoTrailingSeparator(source.value()); |
| 30 if (!dir.ends_with(".imageset")) |
| 31 return false; |
| 32 dir = FindDirNoTrailingSeparator(dir); |
| 33 if (!dir.ends_with(".xcassets")) |
| 34 return false; |
| 35 if (asset_catalog) { |
| 36 std::string asset_catalog_path = dir.as_string(); |
| 37 *asset_catalog = SourceFile(SourceFile::SWAP_IN, &asset_catalog_path); |
| 38 } |
| 39 return true; |
| 40 } |
| 41 |
| 42 BundleData::BundleData() {} |
| 43 |
| 44 BundleData::~BundleData() {} |
| 45 |
| 46 void BundleData::AddFileRuleFromTarget(const Target* target) { |
| 47 DCHECK_EQ(target->output_type(), Target::BUNDLE_DATA); |
| 48 |
| 49 std::vector<SourceFile> file_rule_sources; |
| 50 for (const SourceFile& source_file : target->sources()) { |
| 51 if (IsSourceFileFromAssetCatalog(source_file, nullptr)) { |
| 52 asset_catalog_sources_.push_back(source_file); |
| 53 } else { |
| 54 file_rule_sources.push_back(source_file); |
| 55 } |
| 56 } |
| 57 |
| 58 if (!file_rule_sources.empty()) { |
| 59 DCHECK_EQ(target->action_values().outputs().list().size(), 1u); |
| 60 file_rules_.push_back(BundleFileRule( |
| 61 file_rule_sources, target->action_values().outputs().list()[0])); |
| 62 } |
| 63 } |
| 64 |
| 65 void BundleData::GetSourceFiles(std::vector<SourceFile>* sources) const { |
| 66 for (const BundleFileRule& file_rule : file_rules_) { |
| 67 sources->insert(sources->end(), file_rule.sources().begin(), |
| 68 file_rule.sources().end()); |
| 69 } |
| 70 sources->insert(sources->end(), asset_catalog_sources_.begin(), |
| 71 asset_catalog_sources_.end()); |
| 72 } |
| 73 |
| 74 void BundleData::GetOutputFiles(const Settings* settings, |
| 75 std::vector<OutputFile>* outputs) const { |
| 76 std::vector<SourceFile> outputs_as_sources; |
| 77 GetOutputsAsSourceFiles(settings, &outputs_as_sources); |
| 78 for (const SourceFile& source_file : outputs_as_sources) |
| 79 outputs->push_back(OutputFile(settings->build_settings(), source_file)); |
| 80 } |
| 81 |
| 82 void BundleData::GetOutputsAsSourceFiles( |
| 83 const Settings* settings, |
| 84 std::vector<SourceFile>* outputs_as_source) const { |
| 85 for (const BundleFileRule& file_rule : file_rules_) { |
| 86 for (const SourceFile& source : file_rule.sources()) { |
| 87 outputs_as_source->push_back( |
| 88 file_rule.ApplyPatternToSource(settings, *this, source)); |
| 89 } |
| 90 } |
| 91 |
| 92 if (!asset_catalog_sources_.empty()) |
| 93 outputs_as_source->push_back(GetCompiledAssetCatalogPath()); |
| 94 } |
| 95 |
| 96 SourceFile BundleData::GetCompiledAssetCatalogPath() const { |
| 97 DCHECK(!asset_catalog_sources_.empty()); |
| 98 std::string assets_car_path = resources_dir_ + "/Assets.car"; |
| 99 return SourceFile(SourceFile::SWAP_IN, &assets_car_path); |
| 100 } |
OLD | NEW |