OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 import 'dart:io'; | |
6 | |
7 import 'package:args/args.dart'; | |
8 import 'package:logging/logging.dart'; | |
9 import 'package:path/path.dart' as path; | |
10 | |
11 // Must use relative paths because library imports mirrors via relative paths | |
12 import '../lib/docgen.dart'; | |
13 | |
14 /** | |
15 * Analyzes Dart files and generates a representation of included libraries, | |
16 * classes, and members. | |
17 */ | |
18 void main(List<String> arguments) { | |
19 var options = _initArgParser().parse(arguments); | |
20 var files = options.rest.map(path.normalize).toList(); | |
21 if (files.isEmpty) _printHelpAndExit(); | |
22 var startPage = options['start-page']; | |
23 if (_singlePackage(files) && startPage == null) { | |
24 startPage = _defaultStartPageFor(files); | |
25 _printDeprecatedMessage(); | |
26 print("Using default options for documenting a single package: " | |
27 "--start-page=$startPage"); | |
28 } | |
29 var includeSdk = options['parse-sdk'] || options['include-sdk']; | |
30 var scriptDir = path.dirname(Platform.script.toFilePath()); | |
31 var introduction = includeSdk ? '' : options['introduction']; | |
32 | |
33 var pubScript = | |
34 options['sdk'] != null ? path.join(options['sdk'], 'bin', 'pub') : 'pub'; | |
35 | |
36 var dartBinary = options['sdk'] != null | |
37 ? path.join(options['sdk'], 'bin', 'dart') | |
38 : 'dart'; | |
39 | |
40 var excludedLibraries = options['exclude-lib']; | |
41 if (excludedLibraries == null) excludedLibraries = []; | |
42 | |
43 var indentJSON = options['indent-json'] as bool; | |
44 | |
45 docgen(files, | |
46 packageRoot: options['package-root'], | |
47 includePrivate: options['include-private'], | |
48 includeSdk: includeSdk, | |
49 parseSdk: options['parse-sdk'], | |
50 introFileName: introduction, | |
51 out: options['out'], | |
52 excludeLibraries: excludedLibraries, | |
53 includeDependentPackages: options['include-dependent-packages'], | |
54 compile: options['compile'], | |
55 serve: options['serve'], | |
56 dartBinary: dartBinary, | |
57 pubScript: pubScript, | |
58 noDocs: options['no-docs'], | |
59 startPage: startPage, | |
60 indentJSON: indentJSON, | |
61 sdk: options['sdk']); | |
62 } | |
63 | |
64 /** | |
65 * Print help if we are passed the help option or invalid arguments. | |
66 */ | |
67 void _printHelpAndExit() { | |
68 _printDeprecatedMessage(); | |
69 print(_initArgParser().usage); | |
70 print('Usage: dartdocgen [OPTIONS] fooDir/barFile'); | |
71 exit(0); | |
72 } | |
73 | |
74 void _printDeprecatedMessage() { | |
75 print( | |
76 '\nDeprecated: please use https://pub.dartlang.org/packages/dartdoc instea
d.'); | |
77 print('Dart SDK 1.12 will be the last release to ship with docgen.\n'); | |
78 } | |
79 | |
80 /** | |
81 * If the user seems to have given us a single package to document, use some | |
82 * reasonable arguments for what they probably meant. | |
83 */ | |
84 bool _singlePackage(List files) { | |
85 if (files.length != 1) return false; | |
86 var pubspec = new File(path.join(files.first, 'pubspec.yaml')); | |
87 if (!pubspec.existsSync()) return false; | |
88 return true; | |
89 } | |
90 | |
91 /** | |
92 * If we've specified just a package and no other command-line options, | |
93 * use the single package name as the start page. | |
94 */ | |
95 String _defaultStartPageFor(files) { | |
96 var pubspec = new File(path.join(files.first, 'pubspec.yaml')); | |
97 if (!pubspec.existsSync()) return null; | |
98 return packageNameFor(files.first); | |
99 } | |
100 | |
101 /** | |
102 * Creates parser for docgen command line arguments. | |
103 */ | |
104 ArgParser _initArgParser() { | |
105 var parser = new ArgParser(); | |
106 parser.addFlag('help', | |
107 abbr: 'h', | |
108 help: 'Prints help and usage information.', | |
109 negatable: false, callback: (help) { | |
110 if (help) _printHelpAndExit(); | |
111 }); | |
112 parser.addFlag('verbose', | |
113 abbr: 'v', | |
114 help: 'Output more logging information.', | |
115 negatable: false, callback: (verbose) { | |
116 if (verbose) Logger.root.level = Level.FINEST; | |
117 }); | |
118 parser.addFlag('include-private', | |
119 help: 'Flag to include private declarations.', negatable: false); | |
120 parser.addFlag('include-sdk', | |
121 help: 'Flag to parse SDK Library files.', | |
122 defaultsTo: false, | |
123 negatable: true); | |
124 parser.addFlag('parse-sdk', | |
125 help: 'Parses the SDK libraries only.', | |
126 defaultsTo: false, | |
127 negatable: false); | |
128 parser.addOption('package-root', | |
129 help: 'Sets the package root of the library being analyzed.'); | |
130 parser.addFlag('compile', | |
131 help: 'Clone the documentation viewer repo locally ' | |
132 '(if not already present) and compile with dart2js', | |
133 defaultsTo: false, | |
134 negatable: false); | |
135 parser.addFlag('serve', | |
136 help: 'Clone the documentation viewer repo locally ' | |
137 '(if not already present), compile with dart2js, ' | |
138 'and start a simple server', | |
139 defaultsTo: false, | |
140 negatable: false); | |
141 parser.addFlag('no-docs', | |
142 help: 'Do not generate any new documentation', | |
143 defaultsTo: false, | |
144 negatable: false); | |
145 parser.addOption('introduction', | |
146 help: 'Adds the provided markdown text file as the introduction' | |
147 ' for the generated documentation.', | |
148 defaultsTo: ''); | |
149 parser.addOption('out', | |
150 help: 'The name of the output directory.', defaultsTo: 'docs'); | |
151 parser.addOption('exclude-lib', | |
152 help: 'Exclude the library by this name from the documentation', | |
153 allowMultiple: true); | |
154 parser.addFlag('include-dependent-packages', | |
155 help: 'Assumes we are documenting a single package and are running ' | |
156 'in the directory with its pubspec. Includes documentation for all ' | |
157 'of its dependent packages.', | |
158 defaultsTo: true, | |
159 negatable: true); | |
160 parser.addOption('sdk', help: 'SDK directory', defaultsTo: null); | |
161 parser.addOption('start-page', | |
162 help: 'By default the viewer will start at the SDK introduction page. ' | |
163 'To start at some other page, e.g. for a package, provide the name ' | |
164 'of the package in this argument, e.g. --start-page=intl will make ' | |
165 'the start page of the viewer be the intl package.', | |
166 defaultsTo: null); | |
167 parser.addFlag('indent-json', | |
168 help: 'Indents each level of JSON output by two spaces', | |
169 defaultsTo: false, | |
170 negatable: true); | |
171 | |
172 return parser; | |
173 } | |
OLD | NEW |