forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdartdoc.dart
297 lines (259 loc) · 9.78 KB
/
dartdoc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io' show Platform, exitCode, stderr;
import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/generator/empty_generator.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/generator/html_generator.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:dartdoc/src/runtime_stats.dart';
import 'package:dartdoc/src/utils.dart';
import 'package:dartdoc/src/validator.dart';
import 'package:dartdoc/src/version.dart';
import 'package:dartdoc/src/warnings.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
const String programName = 'dartdoc';
// Update when pubspec version changes by running `pub run build_runner build`
const String dartdocVersion = packageVersion;
class DartdocFileWriter implements FileWriter {
final String _outputDir;
@override
final ResourceProvider resourceProvider;
final Map<String, Warnable?> _fileElementMap = {};
@override
final Set<String> writtenFiles = {};
final int _maxFileCount;
final int _maxTotalSize;
int _fileCount = 0;
int _totalSize = 0;
DartdocFileWriter(
this._outputDir,
this.resourceProvider, {
int maxFileCount = 0,
int maxTotalSize = 0,
}) : _maxFileCount = maxFileCount,
_maxTotalSize = maxTotalSize;
void _validateMaxWriteStats(String filePath, int size) {
_fileCount++;
_totalSize += size;
if (_maxFileCount > 0 && _maxFileCount < _fileCount) {
throw DartdocFailure(
'Maximum file count reached: $_maxFileCount ($filePath)');
}
if (_maxTotalSize > 0 && _maxTotalSize < _totalSize) {
throw DartdocFailure(
'Maximum total size reached: $_maxTotalSize bytes ($filePath)');
}
}
@override
void writeBytes(
String filePath,
List<int> content, {
bool allowOverwrite = false,
}) {
_validateMaxWriteStats(filePath, content.length);
// Replace '/' separators with proper separators for the platform.
var outFile = path.joinAll(filePath.split('/'));
if (!allowOverwrite) {
_warnAboutOverwrite(outFile, null);
}
_fileElementMap[outFile] = null;
var file = _getFile(outFile);
file.writeAsBytesSync(content);
writtenFiles.add(outFile);
logProgress(outFile);
}
@override
void write(String filePath, String content, {Warnable? element}) {
final bytes = utf8.encode(content);
_validateMaxWriteStats(filePath, bytes.length);
// Replace '/' separators with proper separators for the platform.
var outFile = path.joinAll(filePath.split('/'));
_warnAboutOverwrite(outFile, element);
_fileElementMap[outFile] = element;
var file = _getFile(outFile);
file.writeAsBytesSync(bytes);
writtenFiles.add(outFile);
logProgress(outFile);
}
void _warnAboutOverwrite(String outFile, Warnable? element) {
if (_fileElementMap.containsKey(outFile)) {
assert(element != null,
'Attempted overwrite of $outFile without corresponding element');
var originalElement = _fileElementMap[outFile];
var referredFrom =
originalElement == null ? const <Warnable>[] : [originalElement];
element?.warn(PackageWarning.duplicateFile,
message: outFile, referredFrom: referredFrom);
}
}
/// Returns the file at [outFile] relative to [_outputDir], creating the
/// parent directory if necessary.
File _getFile(String outFile) {
var file = resourceProvider
.getFile(resourceProvider.pathContext.join(_outputDir, outFile));
var parent = file.parent;
if (!parent.exists) {
parent.create();
}
return file;
}
}
/// Generates Dart documentation for all public Dart libraries in the given
/// directory.
class Dartdoc {
Generator _generator;
final PackageBuilder packageBuilder;
final DartdocOptionContext config;
final Folder _outputDir;
// Fires when the self checks make progress.
final StreamController<String> _onCheckProgress =
StreamController(sync: true);
Dartdoc._(this.config, this._outputDir, this._generator, this.packageBuilder);
Generator get generator => _generator;
@visibleForTesting
set generator(Generator newGenerator) => _generator = newGenerator;
/// Factory method that builds Dartdoc with an empty generator.
static Dartdoc withEmptyGenerator(
DartdocOptionContext config,
PackageBuilder packageBuilder,
) {
return Dartdoc._(
config,
config.resourceProvider.getFolder('UNUSED'),
initEmptyGenerator(),
packageBuilder,
);
}
/// Asynchronous factory method that builds Dartdoc with a generator
/// determined by the given context.
static Future<Dartdoc> fromContext(
DartdocGeneratorOptionContext context,
PackageBuilder packageBuilder,
) async {
var resourceProvider = context.resourceProvider;
var outputPath = resourceProvider.pathContext.absolute(context.output);
var outputDir = resourceProvider.getFolder(outputPath)..create();
var writer = DartdocFileWriter(
outputPath,
resourceProvider,
maxFileCount: context.maxFileCount,
maxTotalSize: context.maxTotalSize,
);
return Dartdoc._(
context,
outputDir,
await initHtmlGenerator(context, writer: writer),
packageBuilder,
);
}
Stream<String> get onCheckProgress => _onCheckProgress.stream;
@visibleForTesting
Future<DartdocResults> generateDocsBase() async {
var stopwatch = Stopwatch()..start();
runtimeStats.startPerfTask('buildPackageGraph');
var packageGraph = await packageBuilder.buildPackageGraph();
runtimeStats.endPerfTask();
if (packageBuilder.includeExternalsWasSpecified) {
packageGraph.defaultPackage.warn(
PackageWarning.deprecated,
message:
"The '--include-externals' option is deprecated, and will soon be "
'removed.',
);
}
var libs = packageGraph.libraryCount;
logInfo("Initialized dartdoc with $libs librar${libs == 1 ? 'y' : 'ies'}");
runtimeStats.startPerfTask('generator.generate');
await generator.generate(packageGraph);
runtimeStats.endPerfTask();
var writtenFiles = generator.writtenFiles;
if (config.validateLinks && writtenFiles.isNotEmpty) {
runtimeStats.startPerfTask('validateLinks');
Validator(packageGraph, config, _outputDir.path, writtenFiles,
_onCheckProgress)
.validateLinks();
runtimeStats.endPerfTask();
}
var warnings = packageGraph.packageWarningCounter.warningCount;
var errors = packageGraph.packageWarningCounter.errorCount;
logWarning("Found $warnings ${pluralize('warning', warnings)} "
"and $errors ${pluralize('error', errors)}.");
var seconds = stopwatch.elapsedMilliseconds / 1000.0;
libs = packageGraph.localPublicLibraries.length;
logInfo("Documented $libs public librar${libs == 1 ? 'y' : 'ies'} "
'in ${seconds.toStringAsFixed(1)} seconds');
if (config.showStats) {
logInfo(runtimeStats.buildReport());
}
return DartdocResults(config.topLevelPackageMeta, packageGraph, _outputDir);
}
/// Generates Dartdoc documentation.
///
/// [DartdocResults] is returned if dartdoc succeeds. [DartdocFailure] is
/// thrown if dartdoc fails in an expected way, for example if there is an
/// analysis error in the code.
Future<DartdocResults> generateDocs() async {
DartdocResults? dartdocResults;
try {
logInfo('Documenting ${config.topLevelPackageMeta}...');
dartdocResults = await generateDocsBase();
if (dartdocResults.packageGraph.localPublicLibraries.isEmpty) {
logWarning('dartdoc could not find any libraries to document');
}
final errorCount =
dartdocResults.packageGraph.packageWarningCounter.errorCount;
if (errorCount > 0) {
throw DartdocFailure('encountered $errorCount errors');
}
var outDirPath = config.resourceProvider.pathContext
.absolute(dartdocResults.outDir.path);
logInfo('Success! Docs generated into $outDirPath');
return dartdocResults;
} finally {
dartdocResults?.packageGraph.dispose();
}
}
/// Runs [generateDocs] function and properly handles the errors.
void executeGuarded() {
onCheckProgress.listen(logProgress);
// This function should *never* `await runZonedGuarded` because the errors
// thrown in [generateDocs] are uncaught. We want this because uncaught
// errors cause IDE debugger to automatically stop at the exception.
//
// If you await the zone, the code that comes after the await is not
// executed if the zone dies due to an uncaught error. To avoid this,
// confusion, never `await runZonedGuarded`.
runZonedGuarded(
() async {
runtimeStats.startPerfTask('generateDocs');
await generateDocs();
runtimeStats.endPerfTask();
},
(e, stackTrace) {
stderr.writeln('\n$_dartdocFailedMessage: $e\n$stackTrace');
exitCode = e is DartdocFailure ? 1 : 255;
},
zoneSpecification: ZoneSpecification(
print: (_, __, ___, String line) => logInfo(line),
),
);
}
}
/// The results of a [Dartdoc.generateDocs] call.
class DartdocResults {
final PackageMeta packageMeta;
final PackageGraph packageGraph;
final Folder outDir;
DartdocResults(this.packageMeta, this.packageGraph, this.outDir);
}
String get _dartdocFailedMessage =>
'dartdoc $packageVersion (${Platform.script.path}) failed';