Chromium Code Reviews| 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( | |
| 61 BundleFileRule(file_rule_sources, | |
| 62 target->action_values().outputs().list()[0])); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 | |
| 67 void BundleData::GetSourceFiles(std::vector<SourceFile>* sources) const { | |
| 68 DCHECK(sources); | |
|
brettw
2016/03/07 21:57:30
I don't usually DCHECK these things. In general po
sdefresne
2016/03/08 14:33:30
Ack.
| |
| 69 for (const BundleFileRule& file_rule : file_rules_) { | |
| 70 sources->insert(sources->end(), | |
| 71 file_rule.sources().begin(), | |
| 72 file_rule.sources().end()); | |
| 73 } | |
| 74 sources->insert(sources->end(), | |
| 75 asset_catalog_sources_.begin(), | |
| 76 asset_catalog_sources_.end()); | |
| 77 } | |
| 78 | |
| 79 void BundleData::GetOutputFiles(const Settings* settings, | |
| 80 std::vector<OutputFile>* outputs) const { | |
| 81 DCHECK(settings); | |
| 82 DCHECK(outputs); | |
| 83 std::vector<SourceFile> outputs_as_sources; | |
| 84 GetOutputsAsSourceFiles(settings, &outputs_as_sources); | |
| 85 for (const SourceFile& source_file : outputs_as_sources) { | |
|
brettw
2016/03/07 21:57:30
No {}
sdefresne
2016/03/08 14:33:30
Ack.
| |
| 86 outputs->push_back(OutputFile(settings->build_settings(), source_file)); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void BundleData::GetOutputsAsSourceFiles( | |
| 91 const Settings* settings, | |
| 92 std::vector<SourceFile>* outputs_as_source) const { | |
| 93 DCHECK(settings); | |
| 94 DCHECK(outputs_as_source); | |
| 95 for (const BundleFileRule& file_rule : file_rules_) { | |
| 96 for (const SourceFile& source : file_rule.sources()) { | |
| 97 outputs_as_source->push_back(file_rule.ApplyPatternToSource( | |
| 98 settings, *this, source)); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 if (!asset_catalog_sources_.empty()) | |
| 103 outputs_as_source->push_back(GetCompiledAssetCatalogPath()); | |
| 104 } | |
| 105 | |
| 106 SourceFile BundleData::GetCompiledAssetCatalogPath() const { | |
| 107 DCHECK(!asset_catalog_sources_.empty()); | |
| 108 std::string assets_car_path = resources_dir_ + "/Assets.car"; | |
| 109 return SourceFile(SourceFile::SWAP_IN, &assets_car_path); | |
| 110 } | |
| OLD | NEW |