Skip to content

Commit 4645500

Browse files
authored
refactor: begin extracting per-package data out from Package (#1622)
* Begin extracting per-package data out from Package * Template update * pubspec.lock change * remove a bunch of autogenerated snapshot files?
1 parent 975466a commit 4645500

17 files changed

+469
-426
lines changed

lib/dartdoc.dart

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class DartDoc extends PackageBuilder {
147147
}
148148
}
149149

150-
Package package;
150+
PackageGraph packageGraph;
151151

152152
/// Generate DartDoc documentation.
153153
///
@@ -157,23 +157,23 @@ class DartDoc extends PackageBuilder {
157157
Future<DartDocResults> generateDocs() async {
158158
Stopwatch _stopwatch = new Stopwatch()..start();
159159
double seconds;
160-
package = await buildPackage();
160+
packageGraph = await buildPackageGraph();
161161
seconds = _stopwatch.elapsedMilliseconds / 1000.0;
162162
logInfo(
163-
"Initialized dartdoc with ${package.libraries.length} librar${package.libraries.length == 1 ? 'y' : 'ies'} "
163+
"Initialized dartdoc with ${packageGraph.libraries.length} librar${packageGraph.libraries.length == 1 ? 'y' : 'ies'} "
164164
"in ${seconds.toStringAsFixed(1)} seconds");
165165
_stopwatch.reset();
166166

167167
// Create the out directory.
168168
if (!outputDir.existsSync()) outputDir.createSync(recursive: true);
169169

170170
for (var generator in generators) {
171-
await generator.generate(package, outputDir.path);
171+
await generator.generate(packageGraph, outputDir.path);
172172
writtenFiles.addAll(generator.writtenFiles.map(path.normalize));
173173
}
174-
if (config.validateLinks) validateLinks(package, outputDir.path);
175-
int warnings = package.packageWarningCounter.warningCount;
176-
int errors = package.packageWarningCounter.errorCount;
174+
if (config.validateLinks) validateLinks(packageGraph, outputDir.path);
175+
int warnings = packageGraph.packageWarningCounter.warningCount;
176+
int errors = packageGraph.packageWarningCounter.errorCount;
177177
if (warnings == 0 && errors == 0) {
178178
logInfo("no issues found");
179179
} else {
@@ -183,23 +183,24 @@ class DartDoc extends PackageBuilder {
183183

184184
seconds = _stopwatch.elapsedMilliseconds / 1000.0;
185185
logInfo(
186-
"Documented ${package.publicLibraries.length} public librar${package.publicLibraries.length == 1 ? 'y' : 'ies'} "
186+
"Documented ${packageGraph.publicLibraries.length} public librar${packageGraph.publicLibraries.length == 1 ? 'y' : 'ies'} "
187187
"in ${seconds.toStringAsFixed(1)} seconds");
188188

189-
if (package.publicLibraries.isEmpty) {
189+
if (packageGraph.publicLibraries.isEmpty) {
190190
throw new DartDocFailure(
191191
"dartdoc could not find any libraries to document. Run `pub get` and try again.");
192192
}
193193

194-
if (package.packageWarningCounter.errorCount > 0) {
194+
if (packageGraph.packageWarningCounter.errorCount > 0) {
195195
throw new DartDocFailure("dartdoc encountered errors while processing");
196196
}
197197

198-
return new DartDocResults(packageMeta, package, outputDir);
198+
return new DartDocResults(packageMeta, packageGraph, outputDir);
199199
}
200200

201201
/// Warn on file paths.
202-
void _warn(Package package, PackageWarning kind, String warnOn, String origin,
202+
void _warn(PackageGraph packageGraph, PackageWarning kind, String warnOn,
203+
String origin,
203204
{String referredFrom}) {
204205
// Ordinarily this would go in [Package.warn], but we don't actually know what
205206
// ModelElement to warn on yet.
@@ -235,14 +236,15 @@ class DartDoc extends PackageBuilder {
235236
}
236237

237238
if (referredFromElements.isEmpty && referredFrom == 'index.html')
238-
referredFromElements.add(package);
239+
referredFromElements.add(packageGraph);
239240
String message = warnOn;
240241
if (referredFrom == 'index.json') message = '$warnOn (from index.json)';
241-
package.warnOnElement(warnOnElement, kind,
242+
packageGraph.warnOnElement(warnOnElement, kind,
242243
message: message, referredFrom: referredFromElements);
243244
}
244245

245-
void _doOrphanCheck(Package package, String origin, Set<String> visited) {
246+
void _doOrphanCheck(
247+
PackageGraph packageGraph, String origin, Set<String> visited) {
246248
String normalOrigin = path.normalize(origin);
247249
String staticAssets = path.joinAll([normalOrigin, 'static-assets', '']);
248250
String indexJson = path.joinAll([normalOrigin, 'index.json']);
@@ -264,15 +266,16 @@ class DartDoc extends PackageBuilder {
264266
if (visited.contains(fullPath)) continue;
265267
if (!writtenFiles.contains(fullPath)) {
266268
// This isn't a file we wrote (this time); don't claim we did.
267-
_warn(package, PackageWarning.unknownFile, fullPath, normalOrigin);
269+
_warn(packageGraph, PackageWarning.unknownFile, fullPath, normalOrigin);
268270
} else {
269-
_warn(package, PackageWarning.orphanedFile, fullPath, normalOrigin);
271+
_warn(
272+
packageGraph, PackageWarning.orphanedFile, fullPath, normalOrigin);
270273
}
271274
_onCheckProgress.add(fullPath);
272275
}
273276

274277
if (!foundIndexJson) {
275-
_warn(package, PackageWarning.brokenLink, indexJson, normalOrigin);
278+
_warn(packageGraph, PackageWarning.brokenLink, indexJson, normalOrigin);
276279
_onCheckProgress.add(indexJson);
277280
}
278281
}
@@ -300,7 +303,7 @@ class DartDoc extends PackageBuilder {
300303
}
301304

302305
void _doSearchIndexCheck(
303-
Package package, String origin, Set<String> visited) {
306+
PackageGraph packageGraph, String origin, Set<String> visited) {
304307
String fullPath = path.joinAll([origin, 'index.json']);
305308
String indexPath = path.joinAll([origin, 'index.html']);
306309
File file = new File("$fullPath");
@@ -319,7 +322,7 @@ class DartDoc extends PackageBuilder {
319322
if (entry.containsKey('href')) {
320323
String entryPath = path.joinAll([origin, entry['href']]);
321324
if (!visited.contains(entryPath)) {
322-
_warn(package, PackageWarning.brokenLink, entryPath,
325+
_warn(packageGraph, PackageWarning.brokenLink, entryPath,
323326
path.normalize(origin),
324327
referredFrom: fullPath);
325328
}
@@ -329,14 +332,14 @@ class DartDoc extends PackageBuilder {
329332
// Missing from search index
330333
Set<String> missing_from_search = visited.difference(found);
331334
for (String s in missing_from_search) {
332-
_warn(package, PackageWarning.missingFromSearchIndex, s,
335+
_warn(packageGraph, PackageWarning.missingFromSearchIndex, s,
333336
path.normalize(origin),
334337
referredFrom: fullPath);
335338
}
336339
}
337340

338-
void _doCheck(
339-
Package package, String origin, Set<String> visited, String pathToCheck,
341+
void _doCheck(PackageGraph packageGraph, String origin, Set<String> visited,
342+
String pathToCheck,
340343
[String source, String fullPath]) {
341344
if (fullPath == null) {
342345
fullPath = path.joinAll([origin, pathToCheck]);
@@ -345,7 +348,7 @@ class DartDoc extends PackageBuilder {
345348

346349
Tuple2 stringLinksAndHref = _getStringLinksAndHref(fullPath);
347350
if (stringLinksAndHref == null) {
348-
_warn(package, PackageWarning.brokenLink, pathToCheck,
351+
_warn(packageGraph, PackageWarning.brokenLink, pathToCheck,
349352
path.normalize(origin),
350353
referredFrom: source);
351354
_onCheckProgress.add(pathToCheck);
@@ -387,7 +390,7 @@ class DartDoc extends PackageBuilder {
387390
}
388391
}
389392
for (Tuple2 visitPaths in toVisit) {
390-
_doCheck(package, origin, visited, visitPaths.item1, pathToCheck,
393+
_doCheck(packageGraph, origin, visited, visitPaths.item1, pathToCheck,
391394
visitPaths.item2);
392395
}
393396
_onCheckProgress.add(pathToCheck);
@@ -397,16 +400,16 @@ class DartDoc extends PackageBuilder {
397400

398401
/// Don't call this method more than once, and only after you've
399402
/// generated all docs for the Package.
400-
void validateLinks(Package package, String origin) {
403+
void validateLinks(PackageGraph packageGraph, String origin) {
401404
assert(_hrefs == null);
402-
_hrefs = package.allHrefs;
405+
_hrefs = packageGraph.allHrefs;
403406

404407
final Set<String> visited = new Set();
405408
final String start = 'index.html';
406409
logInfo('Validating docs...');
407-
_doCheck(package, origin, visited, start);
408-
_doOrphanCheck(package, origin, visited);
409-
_doSearchIndexCheck(package, origin, visited);
410+
_doCheck(packageGraph, origin, visited, start);
411+
_doOrphanCheck(packageGraph, origin, visited);
412+
_doSearchIndexCheck(packageGraph, origin, visited);
410413
}
411414
}
412415

@@ -424,10 +427,10 @@ class DartDocFailure {
424427
/// The results of a [DartDoc.generateDocs] call.
425428
class DartDocResults {
426429
final PackageMeta packageMeta;
427-
final Package package;
430+
final PackageGraph packageGraph;
428431
final Directory outDir;
429432

430-
DartDocResults(this.packageMeta, this.package, this.outDir);
433+
DartDocResults(this.packageMeta, this.packageGraph, this.outDir);
431434
}
432435

433436
class _Error implements Comparable<_Error> {

lib/src/element_type.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ElementType extends Privacy {
3737
@override
3838
bool get isPublic {
3939
Class canonicalClass =
40-
element.package.findCanonicalModelElementFor(element.element) ??
40+
element.packageGraph.findCanonicalModelElementFor(element.element) ??
4141
element;
4242
return canonicalClass.isPublic;
4343
}
@@ -139,7 +139,7 @@ class ElementType extends Privacy {
139139

140140
ElementType get _returnType {
141141
var rt = _returnTypeCore;
142-
Library lib = element.package.findLibraryFor(rt.element);
142+
Library lib = element.packageGraph.findLibraryFor(rt.element);
143143
if (lib == null) {
144144
lib = new ModelElement.from(rt.element.library, element.library);
145145
}

lib/src/generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library dartdoc.generator;
88
import 'dart:async' show Stream, Future;
99
import 'dart:io' show File;
1010

11-
import 'model.dart' show Package;
11+
import 'model.dart' show PackageGraph;
1212

1313
/// An abstract class that defines a generator that generates documentation for
1414
/// a given package.
@@ -17,7 +17,7 @@ import 'model.dart' show Package;
1717
abstract class Generator {
1818
/// Generate the documentation for the given package in the specified
1919
/// directory. Completes the returned future when done.
20-
Future generate(Package package, String outputDirectoryPath);
20+
Future generate(PackageGraph packageGraph, String outputDirectoryPath);
2121

2222
/// Fires when a file is created.
2323
Stream<File> get onFileCreated;

lib/src/html/html_generator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ class HtmlGenerator extends Generator {
6767

6868
@override
6969

70-
/// Actually write out the documentation for [package].
70+
/// Actually write out the documentation for [packageGraph].
7171
/// Stores the HtmlGeneratorInstance so we can access it in [writtenFiles].
72-
Future generate(Package package, String outputDirectoryPath) async {
72+
Future generate(PackageGraph packageGraph, String outputDirectoryPath) async {
7373
assert(_instance == null);
7474

7575
var enabled = true;
@@ -102,7 +102,7 @@ class HtmlGenerator extends Generator {
102102

103103
try {
104104
_instance =
105-
new HtmlGeneratorInstance(_options, _templates, package, write);
105+
new HtmlGeneratorInstance(_options, _templates, packageGraph, write);
106106
await _instance.generate();
107107
} finally {
108108
enabled = false;

0 commit comments

Comments
 (0)