| 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 // Returns true if |source| correspond to the path of a file in an asset | 
|  | 25 // catalog. | 
|  | 26 // | 
|  | 27 // An asset catalog is a bundle with the ".xcassets" extension. It contains | 
|  | 28 // one directory per asset each of them with the ".imageset" extension. | 
|  | 29 // | 
|  | 30 // As an approximation, this function checks whether source matches: | 
|  | 31 //    .*\.xcassets/[^/]*\.imageset/[^/]* | 
|  | 32 bool IsSourceFileFromAssetCatalog(const SourceFile& source, | 
|  | 33                                   SourceFile* asset_catalog) { | 
|  | 34   base::StringPiece dir; | 
|  | 35   dir = FindDirNoTrailingSeparator(source.value()); | 
|  | 36   if (!dir.ends_with(".imageset")) | 
|  | 37     return false; | 
|  | 38   dir = FindDirNoTrailingSeparator(dir); | 
|  | 39   if (!dir.ends_with(".xcassets")) | 
|  | 40     return false; | 
|  | 41   if (asset_catalog) { | 
|  | 42     std::string asset_catalog_path = dir.as_string(); | 
|  | 43     *asset_catalog = SourceFile(SourceFile::SWAP_IN, &asset_catalog_path); | 
|  | 44   } | 
|  | 45   return true; | 
|  | 46 } | 
|  | 47 | 
|  | 48 BundleData::BundleData() {} | 
|  | 49 | 
|  | 50 BundleData::~BundleData() {} | 
|  | 51 | 
|  | 52 void BundleData::AddFileRuleFromTarget(const Target* target) { | 
|  | 53   DCHECK_EQ(target->output_type(), Target::BUNDLE_DATA); | 
|  | 54 | 
|  | 55   std::vector<SourceFile> file_rule_sources; | 
|  | 56   for (const SourceFile& source_file : target->sources()) { | 
|  | 57     if (IsSourceFileFromAssetCatalog(source_file, nullptr)) { | 
|  | 58       asset_catalog_sources_.push_back(source_file); | 
|  | 59     } else { | 
|  | 60       file_rule_sources.push_back(source_file); | 
|  | 61     } | 
|  | 62   } | 
|  | 63 | 
|  | 64   DCHECK_EQ(target->action_values().outputs().list().size(), 1u); | 
|  | 65   file_rules_.push_back( | 
|  | 66       BundleFileRule(file_rule_sources, | 
|  | 67                      target->action_values().outputs().list()[0])); | 
|  | 68 } | 
|  | 69 | 
|  | 70 | 
|  | 71 void BundleData::GetSourceFiles(std::vector<SourceFile>* sources) const { | 
|  | 72   DCHECK(sources); | 
|  | 73   for (const BundleFileRule& file_rule : file_rules_) { | 
|  | 74     sources->insert(sources->end(), | 
|  | 75                     file_rule.sources().begin(), | 
|  | 76                     file_rule.sources().end()); | 
|  | 77   } | 
|  | 78   sources->insert(sources->end(), | 
|  | 79                   asset_catalog_sources_.begin(), | 
|  | 80                   asset_catalog_sources_.end()); | 
|  | 81 } | 
|  | 82 | 
|  | 83 void BundleData::GetOutputFiles(const Settings* settings, | 
|  | 84                                 std::vector<OutputFile>* outputs) const { | 
|  | 85   DCHECK(settings); | 
|  | 86   DCHECK(outputs); | 
|  | 87   std::vector<SourceFile> outputs_as_sources; | 
|  | 88   GetOutputsAsSourceFiles(settings, &outputs_as_sources); | 
|  | 89   for (const SourceFile& source_file : outputs_as_sources) { | 
|  | 90     outputs->push_back(OutputFile(settings->build_settings(), source_file)); | 
|  | 91   } | 
|  | 92 } | 
|  | 93 | 
|  | 94 void BundleData::GetOutputsAsSourceFiles( | 
|  | 95     const Settings* settings, | 
|  | 96     std::vector<SourceFile>* outputs_as_source) const { | 
|  | 97   DCHECK(settings); | 
|  | 98   DCHECK(outputs_as_source); | 
|  | 99   for (const BundleFileRule& file_rule : file_rules_) { | 
|  | 100     for (const SourceFile& source : file_rule.sources()) { | 
|  | 101       outputs_as_source->push_back(file_rule.ApplyPatternToSource( | 
|  | 102           settings, *this, source)); | 
|  | 103     } | 
|  | 104   } | 
|  | 105 | 
|  | 106   if (!asset_catalog_sources_.empty()) | 
|  | 107     outputs_as_source->push_back(GetCompiledAssetCatalogPath()); | 
|  | 108 } | 
|  | 109 | 
|  | 110 SourceFile BundleData::GetCompiledAssetCatalogPath() const { | 
|  | 111   DCHECK(!asset_catalog_sources_.empty()); | 
|  | 112   std::string assets_car_path = resources_dir_ + "/Assets.car"; | 
|  | 113   return SourceFile(SourceFile::SWAP_IN, &assets_car_path); | 
|  | 114 } | 
| OLD | NEW | 
|---|