OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Analyse a directory structure and find packages resolvers for each | 5 /// Analyse a directory structure and find packages resolvers for each |
6 /// sub-directory. | 6 /// sub-directory. |
7 /// | 7 /// |
8 /// The resolvers are generally the same that would be found by using | 8 /// The resolvers are generally the same that would be found by using |
9 /// the `discovery.dart` library on each sub-directory in turn, | 9 /// the `discovery.dart` library on each sub-directory in turn, |
10 /// but more efficiently and with some heuristics for directories that | 10 /// but more efficiently and with some heuristics for directories that |
(...skipping 23 matching lines...) Expand all Loading... |
34 /// | 34 /// |
35 /// Introduced either by a `.packages` file or a `packages/` directory. | 35 /// Introduced either by a `.packages` file or a `packages/` directory. |
36 Packages get packages; | 36 Packages get packages; |
37 | 37 |
38 /// Child contexts that apply to sub-directories of [directory]. | 38 /// Child contexts that apply to sub-directories of [directory]. |
39 List<PackageContext> get children; | 39 List<PackageContext> get children; |
40 | 40 |
41 /// Look up the [PackageContext] that applies to a specific directory. | 41 /// Look up the [PackageContext] that applies to a specific directory. |
42 /// | 42 /// |
43 /// The directory must be inside [directory]. | 43 /// The directory must be inside [directory]. |
44 PackageContext operator[](Directory directory); | 44 PackageContext operator [](Directory directory); |
45 | 45 |
46 /// A map from directory to package resolver. | 46 /// A map from directory to package resolver. |
47 /// | 47 /// |
48 /// Has an entry for this package context and for each child context | 48 /// Has an entry for this package context and for each child context |
49 /// contained in this one. | 49 /// contained in this one. |
50 Map<Directory, Packages> asMap(); | 50 Map<Directory, Packages> asMap(); |
51 | 51 |
52 /// Analyze [directory] and sub-directories for package resolution strategies. | 52 /// Analyze [directory] and sub-directories for package resolution strategies. |
53 /// | 53 /// |
54 /// Returns a mapping from sub-directories to [Packages] objects. | 54 /// Returns a mapping from sub-directories to [Packages] objects. |
55 /// | 55 /// |
56 /// The analysis assumes that there are no `.packages` files in a parent | 56 /// The analysis assumes that there are no `.packages` files in a parent |
57 /// directory of `directory`. If there is, its corresponding `Packages` object | 57 /// directory of `directory`. If there is, its corresponding `Packages` object |
58 /// should be provided as `root`. | 58 /// should be provided as `root`. |
59 static PackageContext findAll(Directory directory, | 59 static PackageContext findAll(Directory directory, |
60 {Packages root: Packages.noPackages}) { | 60 {Packages root: Packages.noPackages}) { |
61 if (!directory.existsSync()) { | 61 if (!directory.existsSync()) { |
62 throw new ArgumentError("Directory not found: $directory"); | 62 throw new ArgumentError("Directory not found: $directory"); |
63 } | 63 } |
64 List contexts = []; | 64 var contexts = <PackageContext>[]; |
65 void findRoots(Directory directory) { | 65 void findRoots(Directory directory) { |
66 Packages packages; | 66 Packages packages; |
67 List oldContexts; | 67 List<PackageContext> oldContexts; |
68 File packagesFile = new File(path.join(directory.path, ".packages")); | 68 File packagesFile = new File(path.join(directory.path, ".packages")); |
69 if (packagesFile.existsSync()) { | 69 if (packagesFile.existsSync()) { |
70 packages = _loadPackagesFile(packagesFile); | 70 packages = _loadPackagesFile(packagesFile); |
71 oldContexts = contexts; | 71 oldContexts = contexts; |
72 contexts = []; | 72 contexts = []; |
73 } else { | 73 } else { |
74 Directory packagesDir = | 74 Directory packagesDir = |
75 new Directory(path.join(directory.path, "packages")); | 75 new Directory(path.join(directory.path, "packages")); |
76 if (packagesDir.existsSync()) { | 76 if (packagesDir.existsSync()) { |
77 packages = new FilePackagesDirectoryPackages(packagesDir); | 77 packages = new FilePackagesDirectoryPackages(packagesDir); |
78 oldContexts = contexts; | 78 oldContexts = contexts; |
79 contexts = []; | 79 contexts = []; |
80 } | 80 } |
81 } | 81 } |
82 for (var entry in directory.listSync()) { | 82 for (var entry in directory.listSync()) { |
83 if (entry is Directory) { | 83 if (entry is Directory) { |
84 if (packages == null || !entry.path.endsWith("/packages")) { | 84 if (packages == null || !entry.path.endsWith("/packages")) { |
85 findRoots(entry); | 85 findRoots(entry); |
86 } | 86 } |
87 } | 87 } |
88 } | 88 } |
89 if (packages != null) { | 89 if (packages != null) { |
90 oldContexts.add(new _PackageContext(directory, packages, contexts)); | 90 oldContexts.add(new _PackageContext(directory, packages, contexts)); |
91 contexts = oldContexts; | 91 contexts = oldContexts; |
92 } | 92 } |
93 } | 93 } |
94 findRoots(directory); | 94 findRoots(directory); |
95 // If the root is not itself context root, add a the wrapper context. | 95 // If the root is not itself context root, add a the wrapper context. |
96 if (contexts.length == 1 && | 96 if (contexts.length == 1 && contexts[0].directory == directory) { |
97 contexts[0].directory == directory) { | |
98 return contexts[0]; | 97 return contexts[0]; |
99 } | 98 } |
100 return new _PackageContext(directory, root, contexts); | 99 return new _PackageContext(directory, root, contexts); |
101 } | 100 } |
102 } | 101 } |
103 | 102 |
104 class _PackageContext implements PackageContext { | 103 class _PackageContext implements PackageContext { |
105 final Directory directory; | 104 final Directory directory; |
106 final Packages packages; | 105 final Packages packages; |
107 final List<PackageContext> children; | 106 final List<PackageContext> children; |
108 _PackageContext(this.directory, this.packages, List<PackageContext> children) | 107 _PackageContext(this.directory, this.packages, List<PackageContext> children) |
109 : children = new List<PackageContext>.unmodifiable(children); | 108 : children = new List<PackageContext>.unmodifiable(children); |
110 | 109 |
111 Map<Directory, Packages> asMap() { | 110 Map<Directory, Packages> asMap() { |
112 var result = new HashMap<Directory, Packages>(); | 111 var result = new HashMap<Directory, Packages>(); |
113 recurse(_PackageContext current) { | 112 recurse(_PackageContext current) { |
114 result[current.directory] = current.packages; | 113 result[current.directory] = current.packages; |
115 for (var child in current.children) { | 114 for (var child in current.children) { |
116 recurse(child); | 115 recurse(child); |
117 } | 116 } |
118 } | 117 } |
119 recurse(this); | 118 recurse(this); |
120 return result; | 119 return result; |
121 } | 120 } |
122 | 121 |
123 PackageContext operator[](Directory directory) { | 122 PackageContext operator [](Directory directory) { |
124 String path = directory.path; | 123 String path = directory.path; |
125 if (!path.startsWith(this.directory.path)) { | 124 if (!path.startsWith(this.directory.path)) { |
126 throw new ArgumentError("Not inside $path: $directory"); | 125 throw new ArgumentError("Not inside $path: $directory"); |
127 } | 126 } |
128 _PackageContext current = this; | 127 _PackageContext current = this; |
129 // The current path is know to agree with directory until deltaIndex. | 128 // The current path is know to agree with directory until deltaIndex. |
130 int deltaIndex = current.directory.path.length; | 129 int deltaIndex = current.directory.path.length; |
131 List children = current.children; | 130 List children = current.children; |
132 int i = 0; | 131 int i = 0; |
133 while (i < children.length) { | 132 while (i < children.length) { |
(...skipping 23 matching lines...) Expand all Loading... |
157 return true; | 156 return true; |
158 } | 157 } |
159 } | 158 } |
160 | 159 |
161 Packages _loadPackagesFile(File file) { | 160 Packages _loadPackagesFile(File file) { |
162 var uri = new Uri.file(file.path); | 161 var uri = new Uri.file(file.path); |
163 var bytes = file.readAsBytesSync(); | 162 var bytes = file.readAsBytesSync(); |
164 var map = pkgfile.parse(bytes, uri); | 163 var map = pkgfile.parse(bytes, uri); |
165 return new MapPackages(map); | 164 return new MapPackages(map); |
166 } | 165 } |
OLD | NEW |