| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 part of generate; | |
| 6 | |
| 7 class PackageInfo { | |
| 8 final String name; | |
| 9 final Directory packageDir; | |
| 10 final Directory importDir; | |
| 11 final List<File> mojomFiles; | |
| 12 PackageInfo(this.name, this.packageDir, this.importDir, this.mojomFiles); | |
| 13 } | |
| 14 | |
| 15 /// This class finds .mojom files under [_mojomRootDir] that have a | |
| 16 /// 'DartPackage' annotation. It locates the packages named in the annotations | |
| 17 /// and generates a list of [PackageInfo] records. | |
| 18 class MojomFinder { | |
| 19 static final Stopwatch _stopwatch = new Stopwatch(); | |
| 20 static dev.Counter _mojomMs; | |
| 21 | |
| 22 Directory _mojomRootDir; | |
| 23 Directory _dartRootDir; | |
| 24 List<String> _skip; | |
| 25 Map<String, String> _packageLocations; | |
| 26 | |
| 27 MojomFinder(this._mojomRootDir, this._dartRootDir, this._skip) { | |
| 28 _packageLocations = new Map<String, String>(); | |
| 29 if (_mojomMs == null) { | |
| 30 _mojomMs = new dev.Counter("mojom searching", | |
| 31 "Time(ms) searching for .mojom files with DartPackage annotations."); | |
| 32 dev.Metrics.register(_mojomMs); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 /// Look for .mojom files in the tree that have a DartPackage annotation. | |
| 37 /// Return a list of PackageInfo records describing packages and the .mojom | |
| 38 /// files that they own. | |
| 39 Future<List<PackageInfo>> find() async { | |
| 40 _stopwatch.start(); | |
| 41 var packageInfos = new Map<String, PackageInfo>(); | |
| 42 var mojomRootList = _mojomRootDir.list(recursive: true, followLinks: false); | |
| 43 await for (var entry in mojomRootList) { | |
| 44 if (entry is! File) continue; | |
| 45 if (_shouldSkip(entry)) continue; | |
| 46 if (!isMojom(entry.path)) continue; | |
| 47 | |
| 48 String package = await _extractDartPackageAttribute(entry); | |
| 49 if (package == null) continue; | |
| 50 | |
| 51 log.info("MojomFinder: found package $package"); | |
| 52 if (packageInfos.containsKey(package)) { | |
| 53 packageInfos[package].mojomFiles.add(entry); | |
| 54 continue; | |
| 55 } | |
| 56 | |
| 57 Directory dartSourceDir = await _findDartSourceDir(package); | |
| 58 if (dartSourceDir == null) { | |
| 59 log.warning(".mojom had annotation '$package', but no package by that " | |
| 60 "name under $_dartRootDir could be found."); | |
| 61 continue; | |
| 62 } | |
| 63 log.info("MojomFinder: found $package at $dartSourceDir"); | |
| 64 | |
| 65 var packageInfo = | |
| 66 new PackageInfo(package, dartSourceDir, _mojomRootDir, [entry]); | |
| 67 packageInfos[package] = packageInfo; | |
| 68 } | |
| 69 _stopwatch.stop(); | |
| 70 _mojomMs.value += _stopwatch.elapsedMilliseconds; | |
| 71 _stopwatch.reset(); | |
| 72 return packageInfos.values.toList(); | |
| 73 } | |
| 74 | |
| 75 /// Extract a DartPackage attribute from a .mojom file. | |
| 76 Future<String> _extractDartPackageAttribute(File mojom) async { | |
| 77 String contents = await mojom.readAsString(); | |
| 78 int dpIndex = contents.indexOf('DartPackage'); | |
| 79 if (dpIndex == -1) return null; | |
| 80 | |
| 81 // There must be a '[' before 'DartPackage', and there can't be a ']' | |
| 82 // in between. | |
| 83 int openSbIndex = contents.lastIndexOf('[', dpIndex); | |
| 84 if (openSbIndex == -1) return null; | |
| 85 int closeSbIndex = contents.lastIndexOf(']', dpIndex); | |
| 86 if (closeSbIndex > openSbIndex) return null; | |
| 87 | |
| 88 int eqIndex = contents.indexOf('=', dpIndex); | |
| 89 if (eqIndex == -1) return null; | |
| 90 int openQuoteIndex = contents.indexOf('"', eqIndex); | |
| 91 if (openQuoteIndex == -1) return null; | |
| 92 int closeQuoteIndex = -1; | |
| 93 int searchIndex = openQuoteIndex + 1; | |
| 94 while (closeQuoteIndex == -1) { | |
| 95 closeQuoteIndex = contents.indexOf('"', searchIndex); | |
| 96 if (closeQuoteIndex == -1) break; | |
| 97 if (contents[closeQuoteIndex - 1] == '\\') { | |
| 98 searchIndex = closeQuoteIndex + 1; | |
| 99 closeQuoteIndex = -1; | |
| 100 } | |
| 101 } | |
| 102 if (closeQuoteIndex == -1) return null; | |
| 103 return contents.substring(openQuoteIndex + 1, closeQuoteIndex); | |
| 104 } | |
| 105 | |
| 106 /// Finds where the Dart package named [package] lives. Looks immediately | |
| 107 /// under [_dartRootDir]. | |
| 108 Future<Directory> _findDartSourceDir(String package) async { | |
| 109 var packagePath = path.join(_dartRootDir.path, package); | |
| 110 Directory packageDir = new Directory(packagePath); | |
| 111 log.info("Looking for dart package: $packagePath"); | |
| 112 if (await packageDir.exists()) { | |
| 113 return packageDir; | |
| 114 } else { | |
| 115 log.info("$packagePath not found"); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool _shouldSkip(File f) => containsPrefix(f.path, _skip); | |
| 120 } | |
| OLD | NEW |