OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * To generate docs for a library, run this script with the path to an | 6 * To generate docs for a library, run this script with the path to an |
7 * entrypoint .dart file, like: | 7 * entrypoint .dart file, like: |
8 * | 8 * |
9 * $ dart dartdoc.dart foo.dart | 9 * $ dart dartdoc.dart foo.dart |
10 * | 10 * |
11 * This will create a "docs" directory with the docs for your libraries. To | 11 * This will create a "docs" directory with the docs for your libraries. To |
12 * create these beautiful docs, dartdoc parses your library and every library | 12 * create these beautiful docs, dartdoc parses your library and every library |
13 * it imports (recursively). From each library, it parses all classes and | 13 * it imports (recursively). From each library, it parses all classes and |
14 * members, finds the associated doc comments and builds crosslinked docs from | 14 * members, finds the associated doc comments and builds crosslinked docs from |
15 * them. | 15 * them. |
16 */ | 16 */ |
17 library dartdoc; | 17 library dartdoc; |
18 | 18 |
19 import 'dart:async'; | 19 import 'dart:async'; |
20 import 'dart:io'; | 20 import 'dart:io'; |
| 21 import 'dart:uri'; |
21 | 22 |
22 // TODO(rnystrom): Use "package:" URL (#4968). | 23 // TODO(rnystrom): Use "package:" URL (#4968). |
23 import '../lib/dartdoc.dart'; | 24 import '../lib/dartdoc.dart'; |
| 25 import '../lib/src/dartdoc/utils.dart'; |
24 import 'package:args/args.dart'; | 26 import 'package:args/args.dart'; |
25 import 'package:pathos/path.dart' as path; | 27 import 'package:pathos/path.dart' as path; |
26 | 28 |
27 /** | 29 /** |
28 * Run this from the `lib/_internal/dartdoc` directory. | 30 * Run this from the `lib/_internal/dartdoc` directory. |
29 */ | 31 */ |
30 main() { | 32 main() { |
31 // Need this because ArgParser.getUsage doesn't show command invocation. | 33 // Need this because ArgParser.getUsage doesn't show command invocation. |
32 final USAGE = 'Usage dartdoc [options] <entrypoint(s)>\n[options] include:'; | 34 final USAGE = 'Usage dartdoc [options] <entrypoint(s)>\n[options] include:'; |
33 | 35 |
34 final args = new Options().arguments; | 36 final args = new Options().arguments; |
35 | 37 |
36 final dartdoc = new Dartdoc(); | 38 final dartdoc = new Dartdoc(); |
37 | 39 |
38 final argParser = new ArgParser(); | 40 final argParser = new ArgParser(); |
39 | 41 |
40 final Path libPath = scriptDir.append('../../../../'); | 42 final Path libPath = scriptDir.append('../../../../'); |
41 | 43 |
42 Path packageRoot; | 44 String packageRoot; |
43 | 45 |
44 argParser.addFlag('no-code', | 46 argParser.addFlag('no-code', |
45 help: 'Do not include source code in the documentation.', | 47 help: 'Do not include source code in the documentation.', |
46 defaultsTo: false, negatable: false, | 48 defaultsTo: false, negatable: false, |
47 callback: (noCode) => dartdoc.includeSource = !noCode); | 49 callback: (noCode) => dartdoc.includeSource = !noCode); |
48 | 50 |
49 argParser.addOption('mode', abbr: 'm', | 51 argParser.addOption('mode', abbr: 'm', |
50 help: 'Define how HTML pages are generated.', | 52 help: 'Define how HTML pages are generated.', |
51 allowed: ['static', 'live-nav'], allowedHelp: { | 53 allowed: ['static', 'live-nav'], allowedHelp: { |
52 'static': 'Generates completely static HTML containing\n' | 54 'static': 'Generates completely static HTML containing\n' |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 dartdoc.excludedLibraries = allLibs; | 161 dartdoc.excludedLibraries = allLibs; |
160 } | 162 } |
161 }, allowMultiple: true); | 163 }, allowMultiple: true); |
162 | 164 |
163 argParser.addOption('package-root', | 165 argParser.addOption('package-root', |
164 help: 'Sets the package directory to the specified directory.\n' | 166 help: 'Sets the package directory to the specified directory.\n' |
165 'If omitted the package directory is the closest packages directory to' | 167 'If omitted the package directory is the closest packages directory to' |
166 ' the entrypoint.', | 168 ' the entrypoint.', |
167 callback: (packageDir) { | 169 callback: (packageDir) { |
168 if(packageDir != null) { | 170 if(packageDir != null) { |
169 packageRoot = new Path(packageDir); | 171 packageRoot = packageDir; |
170 } | 172 } |
171 }); | 173 }); |
172 | 174 |
173 // TODO(amouravski): This method is deprecated. Remove on April 22. | 175 // TODO(amouravski): This method is deprecated. Remove on April 22. |
174 argParser.addOption('pkg', | 176 argParser.addOption('pkg', |
175 help: 'Deprecated: same as --package-root.', | 177 help: 'Deprecated: same as --package-root.', |
176 callback: (packageDir) { | 178 callback: (packageDir) { |
177 if(packageDir != null) { | 179 if(packageDir != null) { |
178 packageRoot = new Path(packageDir); | 180 packageRoot = packageDir; |
179 } | 181 } |
180 }); | 182 }); |
181 | 183 |
182 dartdoc.dartdocPath = libPath.append('lib/_internal/dartdoc'); | 184 dartdoc.dartdocPath = libPath.append('lib/_internal/dartdoc'); |
183 | 185 |
184 if (args.isEmpty) { | 186 if (args.isEmpty) { |
185 print('No arguments provided.'); | 187 print('No arguments provided.'); |
186 print(USAGE); | 188 print(USAGE); |
187 print(argParser.getUsage()); | 189 print(argParser.getUsage()); |
188 exit(1); | 190 exit(1); |
189 } | 191 } |
190 | 192 |
191 final entrypoints = <Path>[]; | 193 final entrypoints = <Uri>[]; |
192 try { | 194 try { |
193 final option = argParser.parse(args); | 195 final option = argParser.parse(args); |
194 | 196 |
195 // This checks to see if the root of all entrypoints is the same. | 197 // This checks to see if the root of all entrypoints is the same. |
196 // If it is not, then we display a warning, as package imports might fail. | 198 // If it is not, then we display a warning, as package imports might fail. |
197 var entrypointRoot; | 199 var entrypointRoot; |
198 for(final arg in option.rest) { | 200 for (final entrypoint in option.rest) { |
199 var entrypoint = new Path(arg); | 201 var uri = Uri.parse(entrypoint); |
200 entrypoints.add(entrypoint); | 202 if (uri.scheme == '') uri = pathToFileUri(entrypoint); |
| 203 entrypoints.add(uri); |
201 | 204 |
| 205 if (uri.scheme != 'file') continue; |
202 if (entrypointRoot == null) { | 206 if (entrypointRoot == null) { |
203 entrypointRoot = entrypoint.directoryPath; | 207 entrypointRoot = path.dirname(entrypoint); |
204 } else if (entrypointRoot.toNativePath() != | 208 } else if (entrypointRoot != path.dirname(entrypoint)) { |
205 entrypoint.directoryPath.toNativePath()) { | |
206 print('Warning: entrypoints are at different directories. "package:"' | 209 print('Warning: entrypoints are at different directories. "package:"' |
207 ' imports may fail.'); | 210 ' imports may fail.'); |
208 } | 211 } |
209 } | 212 } |
210 } on FormatException catch (e) { | 213 } on FormatException catch (e) { |
211 print(e.message); | 214 print(e.message); |
212 print(USAGE); | 215 print(USAGE); |
213 print(argParser.getUsage()); | 216 print(argParser.getUsage()); |
214 exit(1); | 217 exit(1); |
215 } | 218 } |
216 | 219 |
217 if (entrypoints.isEmpty) { | 220 if (entrypoints.isEmpty) { |
218 print('No entrypoints provided.'); | 221 print('No entrypoints provided.'); |
219 print(argParser.getUsage()); | 222 print(argParser.getUsage()); |
220 exit(1); | 223 exit(1); |
221 } | 224 } |
222 | 225 |
223 if (packageRoot == null) { | 226 if (packageRoot == null) packageRoot = _getPackageRoot(entrypoints); |
224 // Check if there's a `packages` directory in the entry point directory. | |
225 var script = path.normalize(path.absolute(entrypoints[0].toNativePath())); | |
226 var dir = path.join(path.dirname(script), 'packages/'); | |
227 if (new Directory(dir).existsSync()) { | |
228 // TODO(amouravski): convert all of dartdoc to use pathos. | |
229 packageRoot = new Path(dir); | |
230 } else { | |
231 // If there is not, then check if the entrypoint is somewhere in a `lib` | |
232 // directory. | |
233 dir = path.dirname(script); | |
234 var parts = path.split(dir); | |
235 var libDir = parts.lastIndexOf('lib'); | |
236 if (libDir > 0) { | |
237 packageRoot = new Path(path.join(path.joinAll(parts.take(libDir)), | |
238 'packages')); | |
239 } | |
240 } | |
241 } | |
242 | 227 |
243 cleanOutputDirectory(dartdoc.outputDir); | 228 cleanOutputDirectory(dartdoc.outputDir); |
244 | 229 |
245 // Start the analysis and documentation. | 230 // Start the analysis and documentation. |
246 dartdoc.documentLibraries(entrypoints, libPath, packageRoot) | 231 dartdoc.documentLibraries(entrypoints, libPath, packageRoot) |
247 .then((_) { | 232 // Prepare the dart2js script code and copy static resources. |
248 print('Copying static files...'); | 233 // TODO(amouravski): move compileScript out and pre-generate the client |
249 Future.wait([ | 234 // scripts. This takes a long time and the js hardly ever changes. |
250 // Prepare the dart2js script code and copy static resources. | 235 .then((_) => compileScript(dartdoc.mode, dartdoc.outputDir, libPath)) |
251 // TODO(amouravski): move compileScript out and pre-generate the client | 236 .then((_) => copyDirectory(scriptDir.append('../static'), |
252 // scripts. This takes a long time and the js hardly ever changes. | 237 dartdoc.outputDir)) |
253 compileScript(dartdoc.mode, dartdoc.outputDir, libPath), | |
254 copyDirectory(scriptDir.append('../static'), dartdoc.outputDir) | |
255 ]); | |
256 }) | |
257 .then((_) { | 238 .then((_) { |
258 print(dartdoc.status); | 239 print(dartdoc.status); |
259 if (dartdoc.totals == 0) { | 240 if (dartdoc.totals == 0) { |
260 exit(1); | 241 exit(1); |
261 } | 242 } |
262 }) | 243 }) |
263 .catchError((e) { | 244 .catchError((e) { |
264 print('Error: generation failed: ${e}'); | 245 print('Error: generation failed: ${e}'); |
265 dartdoc.cleanup(); | 246 dartdoc.cleanup(); |
266 exit(1); | 247 exit(1); |
267 }) | 248 }) |
268 .whenComplete(() => dartdoc.cleanup()); | 249 .whenComplete(() => dartdoc.cleanup()); |
269 } | 250 } |
| 251 |
| 252 String _getPackageRoot(List<Uri> entrypoints) { |
| 253 // Check if there's a `packages` directory in the entry point directory. |
| 254 var fileEntrypoint = entrypoints.firstWhere( |
| 255 (entrypoint) => entrypoint.scheme == 'file', |
| 256 orElse: () => null); |
| 257 if (fileEntrypoint == null) return; |
| 258 |
| 259 var script = path.normalize(path.absolute(fileUriToPath(fileEntrypoint))); |
| 260 var dir = path.join(path.dirname(script), 'packages/'); |
| 261 if (new Directory(dir).existsSync()) return dir; |
| 262 |
| 263 // If there is not, then check if the entrypoint is somewhere in a `lib` |
| 264 // directory. |
| 265 var parts = path.split(path.dirname(script)); |
| 266 var libDir = parts.lastIndexOf('lib'); |
| 267 if (libDir > 0) { |
| 268 return path.join(path.joinAll(parts.take(libDir)), 'packages'); |
| 269 } else { |
| 270 return null; |
| 271 } |
| 272 } |
OLD | NEW |