| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 part of generate; | 5 part of generate; |
| 6 | 6 |
| 7 class PackageInfo { | 7 class PackageInfo { |
| 8 final String name; | 8 final String name; |
| 9 final Directory packageDir; | 9 final Directory packageDir; |
| 10 final Directory importDir; | 10 final Directory importDir; |
| 11 final List<File> mojomFiles; | 11 final List<File> mojomFiles; |
| 12 PackageInfo(this.name, this.packageDir, this.importDir, this.mojomFiles); | 12 PackageInfo(this.name, this.packageDir, this.importDir, this.mojomFiles); |
| 13 } | 13 } |
| 14 | 14 |
| 15 /// This class finds .mojom files under [_mojomRootDir] that have a | 15 /// This class finds .mojom files under [_mojomRootDir] that have a |
| 16 /// 'DartPackage' annotation. It locates the packages named in the annotations | 16 /// 'DartPackage' annotation. It locates the packages named in the annotations |
| 17 /// and generates a list of [PackageInfo] records. | 17 /// and generates a list of [PackageInfo] records. |
| 18 class MojomFinder { | 18 class MojomFinder { |
| 19 static final Stopwatch _stopwatch = new Stopwatch(); | 19 static final Stopwatch _stopwatch = new Stopwatch(); |
| 20 static dev.Counter _mojomMs; | 20 static dev.Counter _mojomMs; |
| 21 | 21 |
| 22 Directory _mojomRootDir; | 22 Directory _mojomRootDir; |
| 23 Directory _dartRootDir; | 23 Directory _dartRootDir; |
| 24 List<String> _skip; | 24 List<String> _skip; |
| 25 Map<String, String> _packageLocations; | 25 |
| 26 // Cache of mappings from package name to Dart source location. |
| 27 HashMap<String, Directory> _packageLocations; |
| 26 | 28 |
| 27 MojomFinder(this._mojomRootDir, this._dartRootDir, this._skip) { | 29 MojomFinder(this._mojomRootDir, this._dartRootDir, this._skip) { |
| 28 _packageLocations = new Map<String, String>(); | 30 _packageLocations = new HashMap<String, Directory>(); |
| 29 if (_mojomMs == null) { | 31 if (_mojomMs == null) { |
| 30 _mojomMs = new dev.Counter("mojom searching", | 32 _mojomMs = new dev.Counter("mojom searching", |
| 31 "Time(ms) searching for .mojom files with DartPackage annotations."); | 33 "Time(ms) searching for .mojom files with DartPackage annotations."); |
| 32 dev.Metrics.register(_mojomMs); | 34 dev.Metrics.register(_mojomMs); |
| 33 } | 35 } |
| 34 } | 36 } |
| 35 | 37 |
| 36 /// Look for .mojom files in the tree that have a DartPackage annotation. | 38 /// Look for .mojom files in the tree that have a DartPackage annotation. |
| 37 /// Return a list of PackageInfo records describing packages and the .mojom | 39 /// Return a list of PackageInfo records describing packages and the .mojom |
| 38 /// files that they own. | 40 /// files that they own. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 65 var packageInfo = | 67 var packageInfo = |
| 66 new PackageInfo(package, dartSourceDir, _mojomRootDir, [entry]); | 68 new PackageInfo(package, dartSourceDir, _mojomRootDir, [entry]); |
| 67 packageInfos[package] = packageInfo; | 69 packageInfos[package] = packageInfo; |
| 68 } | 70 } |
| 69 _stopwatch.stop(); | 71 _stopwatch.stop(); |
| 70 _mojomMs.value += _stopwatch.elapsedMilliseconds; | 72 _mojomMs.value += _stopwatch.elapsedMilliseconds; |
| 71 _stopwatch.reset(); | 73 _stopwatch.reset(); |
| 72 return packageInfos.values.toList(); | 74 return packageInfos.values.toList(); |
| 73 } | 75 } |
| 74 | 76 |
| 75 /// Extract a DartPackage attribute from a .mojom file. | 77 // Extract a DartPackage attribute from a .mojom file. |
| 76 Future<String> _extractDartPackageAttribute(File mojom) async { | 78 Future<String> _extractDartPackageAttribute(File mojom) async { |
| 77 String contents = await mojom.readAsString(); | 79 String contents = await mojom.readAsString(); |
| 78 int dpIndex = contents.indexOf('DartPackage'); | 80 int dpIndex = contents.indexOf('DartPackage'); |
| 79 if (dpIndex == -1) return null; | 81 if (dpIndex == -1) return null; |
| 80 | 82 |
| 81 // There must be a '[' before 'DartPackage', and there can't be a ']' | 83 // There must be a '[' before 'DartPackage', and there can't be a ']' |
| 82 // in between. | 84 // in between. |
| 83 int openSbIndex = contents.lastIndexOf('[', dpIndex); | 85 int openSbIndex = contents.lastIndexOf('[', dpIndex); |
| 84 if (openSbIndex == -1) return null; | 86 if (openSbIndex == -1) return null; |
| 85 int closeSbIndex = contents.lastIndexOf(']', dpIndex); | 87 int closeSbIndex = contents.lastIndexOf(']', dpIndex); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 96 if (closeQuoteIndex == -1) break; | 98 if (closeQuoteIndex == -1) break; |
| 97 if (contents[closeQuoteIndex - 1] == '\\') { | 99 if (contents[closeQuoteIndex - 1] == '\\') { |
| 98 searchIndex = closeQuoteIndex + 1; | 100 searchIndex = closeQuoteIndex + 1; |
| 99 closeQuoteIndex = -1; | 101 closeQuoteIndex = -1; |
| 100 } | 102 } |
| 101 } | 103 } |
| 102 if (closeQuoteIndex == -1) return null; | 104 if (closeQuoteIndex == -1) return null; |
| 103 return contents.substring(openQuoteIndex + 1, closeQuoteIndex); | 105 return contents.substring(openQuoteIndex + 1, closeQuoteIndex); |
| 104 } | 106 } |
| 105 | 107 |
| 106 /// Finds where the Dart package named [package] lives. Looks immediately | 108 // Finds where the Dart package named [package] lives. Looks immediately |
| 107 /// under [_dartRootDir]. | 109 // under [_dartRootDir]. |
| 108 Future<Directory> _findDartSourceDir(String package) async { | 110 Future<Directory> _findDartSourceDir(String package) async { |
| 111 if (_packageLocations.containsKey(package)) { |
| 112 return _packageLocations[package]; |
| 113 } |
| 109 var packagePath = path.join(_dartRootDir.path, package); | 114 var packagePath = path.join(_dartRootDir.path, package); |
| 110 Directory packageDir = new Directory(packagePath); | 115 Directory packageDir = new Directory(packagePath); |
| 111 log.info("Looking for dart package: $packagePath"); | |
| 112 if (await packageDir.exists()) { | 116 if (await packageDir.exists()) { |
| 117 log.info("Found dart package $package at $packagePath"); |
| 118 _packageLocations[package] = packageDir; |
| 113 return packageDir; | 119 return packageDir; |
| 120 } |
| 121 |
| 122 // If the directory doesn't exist, look for a pubspec.yaml file with a |
| 123 // 'name:' field matching the package name. |
| 124 Directory dir = await _findDartSourceDirByPubspec(package); |
| 125 if (dir != null) { |
| 126 _packageLocations[package] = dir; |
| 127 return dir; |
| 114 } else { | 128 } else { |
| 115 log.info("$packagePath not found"); | 129 log.info("$packagePath not found"); |
| 130 return null; |
| 116 } | 131 } |
| 117 } | 132 } |
| 118 | 133 |
| 134 Future<Directory> _findDartSourceDirByPubspec(String package) async { |
| 135 await for (var entry in _dartRootDir.list(recursive: true)) { |
| 136 if (entry is! File) continue; |
| 137 if (_shouldSkip(entry)) continue; |
| 138 if (!isPubspecYaml(entry.path)) continue; |
| 139 String contents = await entry.readAsString(); |
| 140 var pubspec = yaml.loadYaml(contents); |
| 141 String name = pubspec['name']; |
| 142 if (name == package) { |
| 143 log.info("Found dart package $package at ${entry.parent}"); |
| 144 return entry.parent; |
| 145 } |
| 146 } |
| 147 return null; |
| 148 } |
| 149 |
| 119 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); | 150 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); |
| 120 } | 151 } |
| OLD | NEW |