| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:front_end/compiler_options.dart'; | 8 import 'package:front_end/compiler_options.dart'; |
| 9 import 'package:front_end/dependency_grapher.dart'; | 9 import 'package:front_end/dependency_grapher.dart'; |
| 10 import 'package:path/path.dart' as pathos; | 10 import 'package:path/path.dart' as pathos; |
| 11 | 11 |
| 12 main() async { | 12 main() async { |
| 13 exit(await new _SubpackageRelationshipsTest().run()); | 13 exit(await new _SubpackageRelationshipsTest().run()); |
| 14 } | 14 } |
| 15 | 15 |
| 16 /// Map from subpackage name to the rules for what the subpackage is allowed to | 16 /// Map from subpackage name to the rules for what the subpackage is allowed to |
| 17 /// depend directly on. | 17 /// depend directly on. |
| 18 /// | 18 /// |
| 19 /// Each subdirectory of `lib/src` is considered a subpackage. Files in | 19 /// Each listed directory is considered a subpackage. Each package contains all |
| 20 /// `lib/src` but not in a subdirectory are considered to be in the `lib/src` | 20 /// of its descendant files that are not in a more deeply nested subpackage. |
| 21 /// subpackage. Files outside of `lib/src` (but still in `lib`) are considered | |
| 22 /// to be in the `lib` subpackage. | |
| 23 /// | 21 /// |
| 24 /// TODO(paulberry): stuff in lib/src shouldn't depend on lib; lib should just | 22 /// TODO(paulberry): stuff in lib/src shouldn't depend on lib; lib should just |
| 25 /// re-export stuff in lib/src. | 23 /// re-export stuff in lib/src. |
| 26 /// TODO(paulberry): remove dependencies on analyzer. | 24 /// TODO(paulberry): remove dependencies on analyzer. |
| 27 final subpackageRules = { | 25 final subpackageRules = { |
| 28 'lib': new SubpackageRules( | 26 'lib': new SubpackageRules( |
| 29 mayImportAnalyzer: true, | 27 mayImportAnalyzer: true, |
| 30 allowedDependencies: ['lib/src', 'lib/src/base']), | 28 allowedDependencies: ['lib/src', 'lib/src/base']), |
| 31 'lib/src': new SubpackageRules( | 29 'lib/src': new SubpackageRules( |
| 32 mayImportAnalyzer: true, | 30 mayImportAnalyzer: true, |
| 33 allowedDependencies: ['lib', 'lib/src/base', 'lib/src/scanner']), | 31 allowedDependencies: ['lib', 'lib/src/base', 'lib/src/scanner']), |
| 34 'lib/src/base': new SubpackageRules( | 32 'lib/src/base': new SubpackageRules( |
| 35 mayImportAnalyzer: true, allowedDependencies: ['lib']), | 33 mayImportAnalyzer: true, allowedDependencies: ['lib']), |
| 36 'lib/src/scanner': new SubpackageRules(allowedDependencies: ['lib/src/base']), | 34 'lib/src/fasta': |
| 37 'lib/src/fasta': new SubpackageRules(mayImportAnalyzer: true), | 35 new SubpackageRules(mayImportAnalyzer: true, allowedDependencies: [ |
| 36 'lib/src/fasta/builder', |
| 37 'lib/src/fasta/dill', |
| 38 'lib/src/fasta/kernel', |
| 39 'lib/src/fasta/parser', |
| 40 'lib/src/fasta/scanner', |
| 41 'lib/src/fasta/testing', |
| 42 ]), |
| 43 'lib/src/fasta/analyzer': |
| 44 new SubpackageRules(mayImportAnalyzer: true, allowedDependencies: [ |
| 45 'lib/src/fasta', |
| 46 'lib/src/fasta/builder', |
| 47 'lib/src/fasta/dill', |
| 48 'lib/src/fasta/kernel', |
| 49 'lib/src/fasta/scanner', |
| 50 'lib/src/fasta/source', |
| 51 ]), |
| 52 'lib/src/fasta/bin': new SubpackageRules(allowedDependencies: [ |
| 53 'lib/src/fasta', |
| 54 ]), |
| 55 'lib/src/fasta/builder': new SubpackageRules(allowedDependencies: [ |
| 56 'lib/src/fasta', |
| 57 'lib/src/fasta/dill', |
| 58 'lib/src/fasta/parser', |
| 59 'lib/src/fasta/source', |
| 60 ]), |
| 61 'lib/src/fasta/dill': new SubpackageRules(allowedDependencies: [ |
| 62 'lib/src/fasta', |
| 63 'lib/src/fasta/kernel', |
| 64 ]), |
| 65 'lib/src/fasta/kernel': new SubpackageRules(allowedDependencies: [ |
| 66 'lib/src/fasta', |
| 67 'lib/src/fasta/builder', |
| 68 'lib/src/fasta/dill', |
| 69 'lib/src/fasta/parser', |
| 70 'lib/src/fasta/scanner', |
| 71 'lib/src/fasta/source', |
| 72 ]), |
| 73 'lib/src/fasta/parser': |
| 74 new SubpackageRules(allowSubdirs: true, allowedDependencies: [ |
| 75 'lib/src/fasta', |
| 76 'lib/src/fasta/scanner', |
| 77 'lib/src/fasta/util', |
| 78 ]), |
| 79 'lib/src/fasta/scanner': |
| 80 new SubpackageRules(allowSubdirs: true, allowedDependencies: [ |
| 81 'lib/src/fasta', |
| 82 'lib/src/fasta/parser', |
| 83 'lib/src/fasta/util', |
| 84 ]), |
| 85 'lib/src/fasta/source': new SubpackageRules(allowedDependencies: [ |
| 86 'lib/src/fasta', |
| 87 'lib/src/fasta/analyzer', |
| 88 'lib/src/fasta/builder', |
| 89 'lib/src/fasta/dill', |
| 90 'lib/src/fasta/kernel', |
| 91 'lib/src/fasta/parser', |
| 92 'lib/src/fasta/scanner', |
| 93 ]), |
| 94 'lib/src/fasta/testing': |
| 95 new SubpackageRules(mayImportAnalyzer: true, allowedDependencies: [ |
| 96 'lib/src/fasta', |
| 97 'lib/src/fasta/dill', |
| 98 'lib/src/fasta/kernel', |
| 99 ]), |
| 100 'lib/src/fasta/util': new SubpackageRules(), |
| 101 'lib/src/scanner': new SubpackageRules(allowedDependencies: [ |
| 102 'lib/src/base', |
| 103 ]), |
| 38 }; | 104 }; |
| 39 | 105 |
| 40 /// Rules for what a subpackage may depend directly on. | 106 /// Rules for what a subpackage may depend directly on. |
| 41 class SubpackageRules { | 107 class SubpackageRules { |
| 42 /// Indicates whether the subpackage may directly depend on analyzer. | 108 /// Indicates whether the subpackage may directly depend on analyzer. |
| 43 final bool mayImportAnalyzer; | 109 final bool mayImportAnalyzer; |
| 44 | 110 |
| 111 /// Indicates whether dart files may exist in subdirectories of this |
| 112 /// subpackage. |
| 113 /// |
| 114 /// If `false`, any subdirectory of this subpackage must be a separate |
| 115 /// subpackage. |
| 116 final bool allowSubdirs; |
| 117 |
| 45 /// Indicates which other subpackages a given subpackage may directly depend | 118 /// Indicates which other subpackages a given subpackage may directly depend |
| 46 /// on. | 119 /// on. |
| 47 final List<String> allowedDependencies; | 120 final List<String> allowedDependencies; |
| 48 | 121 |
| 49 SubpackageRules( | 122 SubpackageRules( |
| 50 {this.mayImportAnalyzer: false, this.allowedDependencies: const []}); | 123 {this.mayImportAnalyzer: false, |
| 124 this.allowSubdirs: false, |
| 125 this.allowedDependencies: const []}); |
| 51 } | 126 } |
| 52 | 127 |
| 53 class _SubpackageRelationshipsTest { | 128 class _SubpackageRelationshipsTest { |
| 54 /// File uri of the front_end package's "lib" directory. | 129 /// File uri of the front_end package's "lib" directory. |
| 55 final frontEndLibUri = Platform.script.resolve('../lib/'); | 130 final frontEndLibUri = Platform.script.resolve('../lib/'); |
| 56 | 131 |
| 57 /// Indicates whether any problems have been reported yet. | 132 /// Indicates whether any problems have been reported yet. |
| 58 bool problemsReported = false; | 133 bool problemsReported = false; |
| 59 | 134 |
| 60 /// Check for problems resulting from URI [src] having a direct dependency on | 135 /// Check for problems resulting from URI [src] having a direct dependency on |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 203 } |
| 129 return problemsReported ? 1 : 0; | 204 return problemsReported ? 1 : 0; |
| 130 } | 205 } |
| 131 | 206 |
| 132 /// Determines which subpackage [src] is in. | 207 /// Determines which subpackage [src] is in. |
| 133 /// | 208 /// |
| 134 /// If [src] is not part of the front end, `null` is returned. | 209 /// If [src] is not part of the front end, `null` is returned. |
| 135 String subpackageForUri(Uri src) { | 210 String subpackageForUri(Uri src) { |
| 136 if (src.scheme != 'package') return null; | 211 if (src.scheme != 'package') return null; |
| 137 if (src.pathSegments[0] != 'front_end') return null; | 212 if (src.pathSegments[0] != 'front_end') return null; |
| 138 if (src.pathSegments[1] != 'src') return 'lib'; | 213 var pathWithLib = 'lib/${src.pathSegments.skip(1).join('/')}'; |
| 139 if (src.pathSegments.length == 3) return 'lib/src'; | 214 String subpackage; |
| 140 return 'lib/src/${src.pathSegments[2]}'; | 215 String pathWithinSubpackage; |
| 216 for (var subpackagePath in subpackageRules.keys) { |
| 217 var subpackagePathWithSlash = '$subpackagePath/'; |
| 218 if (pathWithLib.startsWith(subpackagePathWithSlash) && |
| 219 (subpackage == null || subpackage.length < subpackagePath.length)) { |
| 220 subpackage = subpackagePath; |
| 221 pathWithinSubpackage = |
| 222 pathWithLib.substring(subpackagePathWithSlash.length); |
| 223 } |
| 224 } |
| 225 if (subpackage == null) { |
| 226 problem('Uri $src is inside package:front_end but is not in any known ' |
| 227 'subpackage'); |
| 228 } else if (!subpackageRules[subpackage].allowSubdirs && |
| 229 pathWithinSubpackage.contains('/')) { |
| 230 problem('Uri $src is in a subfolder of $subpackage, but that ' |
| 231 'subpackage does not allow dart files in subdirectories.'); |
| 232 } |
| 233 return subpackage; |
| 141 } | 234 } |
| 142 } | 235 } |
| OLD | NEW |