Skip to content

Commit b299577

Browse files
authored
Centralize all file writing in HtmlGeneratorInstance (#1538)
Everything gets logged. Everything gets checked for dupes. Much easier to add 'no-op' support to validate code structure without writing the output
1 parent b159cdc commit b299577

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

lib/src/html/html_generator_instance.dart

+25-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:async' show Future, StreamController;
66
import 'dart:convert' show JsonEncoder;
77
import 'dart:io' show Directory, File;
8-
import 'dart:typed_data' show Uint8List;
98

109
import 'package:collection/collection.dart' show compareNatural;
1110
import 'package:path/path.dart' as path;
@@ -53,8 +52,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
5352

5453
await _copyResources();
5554
if (faviconPath != null) {
56-
File file = new File(path.join(out.path, 'static-assets', 'favicon.png'));
57-
file.writeAsBytesSync(new File(faviconPath).readAsBytesSync());
55+
var bytes = new File(faviconPath).readAsBytesSync();
56+
_writeFile(path.join(out.path, 'static-assets', 'favicon.png'), bytes);
5857
}
5958
}
6059

@@ -292,11 +291,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
292291
'encountered $resourcePath');
293292
}
294293
String destFileName = resourcePath.substring(prefix.length);
295-
File destFile =
296-
new File(path.join(out.path, 'static-assets', destFileName))
297-
..createSync(recursive: true);
298-
Uint8List resourceBytes = await loader.loadAsBytes(resourcePath);
299-
destFile.writeAsBytesSync(resourceBytes);
294+
_writeFile(path.join(out.path, 'static-assets', destFileName),
295+
await loader.loadAsBytes(resourcePath));
300296
}
301297
}
302298

@@ -306,24 +302,31 @@ class HtmlGeneratorInstance implements HtmlOptions {
306302
String content = template(data,
307303
assumeNullNonExistingProperty: false, errorOnMissingProperty: true);
308304

309-
// If you see this assert, we're probably being called to build non-canonical
310-
// docs somehow. Check data.self.isCanonical and callers for bugs.
311-
assert(!writtenFiles.contains(fullName));
312305
_writeFile(fullName, content);
313-
writtenFiles.add(fullName);
314306
if (data.self is ModelElement) documentedElements.add(data.self);
315307
}
316308

317-
void _writeFile(String filename, String content) {
318-
File file = _createOutputFile(filename);
319-
file.writeAsStringSync(content);
309+
/// [content] must be either [String] or [List<int>].
310+
void _writeFile(String filename, Object content) {
311+
// If you see this assert, we're probably being called to build non-canonical
312+
// docs somehow. Check data.self.isCanonical and callers for bugs.
313+
assert(!writtenFiles.contains(filename));
314+
315+
File file = new File(filename);
316+
Directory parent = file.parent;
317+
if (!parent.existsSync()) {
318+
parent.createSync(recursive: true);
319+
}
320+
321+
if (content is String) {
322+
file.writeAsStringSync(content);
323+
} else if (content is List<int>) {
324+
file.writeAsBytesSync(content);
325+
} else {
326+
throw new ArgumentError.value(
327+
content, 'content', '`content` must be `String` or `List<int>`.');
328+
}
320329
_onFileCreated.add(file);
330+
writtenFiles.add(filename);
321331
}
322332
}
323-
324-
File _createOutputFile(String filename) {
325-
File file = new File(filename);
326-
Directory parent = file.parent;
327-
if (!parent.existsSync()) parent.createSync(recursive: true);
328-
return file;
329-
}

0 commit comments

Comments
 (0)