@@ -44,79 +44,40 @@ class DartdocGeneratorOptionContext extends DartdocOptionContext
44
44
: super (optionSet, dir);
45
45
}
46
46
47
- class DartdocFileWriter implements FileWriter {
48
- final String outputDir;
49
- final Map <String , Warnable > _fileElementMap = {};
50
- @override
51
- final Set <String > writtenFiles = Set ();
52
-
53
- DartdocFileWriter (this .outputDir);
54
-
55
- @override
56
- void write (String filePath, Object content,
57
- {bool allowOverwrite, Warnable element}) {
58
- // Replace '/' separators with proper separators for the platform.
59
- String outFile = path.joinAll (filePath.split ('/' ));
60
-
61
- allowOverwrite ?? = false ;
62
- if (! allowOverwrite) {
63
- if (_fileElementMap.containsKey (outFile)) {
64
- assert (element != null ,
65
- 'Attempted overwrite of ${outFile } without corresponding element' );
66
- Warnable originalElement = _fileElementMap[outFile];
67
- Iterable <Warnable > referredFrom =
68
- originalElement != null ? [originalElement] : null ;
69
- element? .warn (PackageWarning .duplicateFile,
70
- message: outFile, referredFrom: referredFrom);
71
- }
72
- }
73
- _fileElementMap[outFile] = element;
74
-
75
- var file = File (path.join (outputDir, outFile));
76
- var parent = file.parent;
77
- if (! parent.existsSync ()) {
78
- parent.createSync (recursive: true );
79
- }
80
-
81
- if (content is String ) {
82
- file.writeAsStringSync (content);
83
- } else if (content is List <int >) {
84
- file.writeAsBytesSync (content);
85
- } else {
86
- throw ArgumentError .value (
87
- content, 'content' , '`content` must be `String` or `List<int>`.' );
88
- }
89
-
90
- writtenFiles.add (outFile);
91
- logProgress (outFile);
92
- }
93
- }
94
-
95
47
/// Generates Dart documentation for all public Dart libraries in the given
96
48
/// directory.
97
49
class Dartdoc extends PackageBuilder {
98
- final Generator generator ;
50
+ final List < Generator > generators ;
99
51
final Set <String > writtenFiles = Set ();
100
52
Directory outputDir;
101
53
102
54
// Fires when the self checks make progress.
103
55
final StreamController <String > _onCheckProgress =
104
56
StreamController (sync : true );
105
57
106
- Dartdoc ._(DartdocOptionContext config, this .generator ) : super (config) {
58
+ Dartdoc ._(DartdocOptionContext config, this .generators ) : super (config) {
107
59
outputDir = Directory (config.output)..createSync (recursive: true );
60
+ generators.forEach ((g) => g.onFileCreated.listen (logProgress));
108
61
}
109
62
110
63
/// An asynchronous factory method that builds Dartdoc's file writers
111
64
/// and returns a Dartdoc object with them.
112
65
static Future <Dartdoc > withDefaultGenerators (
113
66
DartdocGeneratorOptionContext config) async {
114
- return Dartdoc ._(config, await initHtmlGenerator (config));
67
+ List <Generator > generators = await initHtmlGenerators (config);
68
+ return Dartdoc ._(config, generators);
115
69
}
116
70
117
71
/// An asynchronous factory method that builds
118
72
static Future <Dartdoc > withEmptyGenerator (DartdocOptionContext config) async {
119
- return Dartdoc ._(config, await initEmptyGenerator (config));
73
+ List <Generator > generators = await initEmptyGenerators (config);
74
+ return Dartdoc ._(config, generators);
75
+ }
76
+
77
+ /// Basic synchronous factory that gives a stripped down Dartdoc that won't
78
+ /// use generators. Useful for testing.
79
+ factory Dartdoc .withoutGenerators (DartdocOptionContext config) {
80
+ return Dartdoc ._(config, []);
120
81
}
121
82
122
83
Stream <String > get onCheckProgress => _onCheckProgress.stream;
@@ -133,20 +94,19 @@ class Dartdoc extends PackageBuilder {
133
94
double seconds;
134
95
packageGraph = await buildPackageGraph ();
135
96
seconds = _stopwatch.elapsedMilliseconds / 1000.0 ;
136
- int libs = packageGraph.libraries.length;
137
- logInfo ( "Initialized dartdoc with ${libs } librar${libs == 1 ? 'y' : 'ies' } "
97
+ logInfo (
98
+ "Initialized dartdoc with ${packageGraph . libraries . length } librar${packageGraph . libraries . length == 1 ? 'y' : 'ies' } "
138
99
"in ${seconds .toStringAsFixed (1 )} seconds" );
139
100
_stopwatch.reset ();
140
101
141
- final generator = this .generator;
142
- if (generator != null ) {
102
+ if (generators.isNotEmpty) {
143
103
// Create the out directory.
144
104
if (! outputDir.existsSync ()) outputDir.createSync (recursive: true );
145
105
146
- DartdocFileWriter writer = DartdocFileWriter (outputDir.path);
147
- await generator.generate (packageGraph, writer );
148
-
149
- writtenFiles. addAll (writer.writtenFiles);
106
+ for ( var generator in generators) {
107
+ await generator.generate (packageGraph, outputDir.path );
108
+ writtenFiles. addAll (generator.writtenFiles.keys. map (path.normalize));
109
+ }
150
110
if (config.validateLinks && writtenFiles.isNotEmpty) {
151
111
validateLinks (packageGraph, outputDir.path);
152
112
}
@@ -162,8 +122,8 @@ class Dartdoc extends PackageBuilder {
162
122
}
163
123
164
124
seconds = _stopwatch.elapsedMilliseconds / 1000.0 ;
165
- libs = packageGraph.localPublicLibraries.length;
166
- logInfo ( "Documented ${libs } public librar${libs == 1 ? 'y' : 'ies' } "
125
+ logInfo (
126
+ "Documented ${packageGraph . localPublicLibraries . length } public librar${packageGraph . localPublicLibraries . length == 1 ? 'y' : 'ies' } "
167
127
"in ${seconds .toStringAsFixed (1 )} seconds" );
168
128
return DartdocResults (config.topLevelPackageMeta, packageGraph, outputDir);
169
129
}
0 commit comments