Skip to content

Commit b9a6cff

Browse files
committed
Split FileWriter.writeBytes out from FileWriter.write
1 parent f9a875d commit b9a6cff

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

lib/dartdoc.dart

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,58 @@ class DartdocFileWriter implements FileWriter {
5353
DartdocFileWriter(this.outputDir, this.resourceProvider);
5454

5555
@override
56-
void write(String filePath, Object content,
57-
{bool allowOverwrite, Warnable element}) {
56+
void writeBytes(
57+
String filePath,
58+
List<int> content, {
59+
bool allowOverwrite = false,
60+
}) {
5861
// Replace '/' separators with proper separators for the platform.
5962
var outFile = path.joinAll(filePath.split('/'));
6063

61-
allowOverwrite ??= false;
6264
if (!allowOverwrite) {
63-
if (_fileElementMap.containsKey(outFile)) {
64-
assert(element != null,
65-
'Attempted overwrite of $outFile without corresponding element');
66-
var originalElement = _fileElementMap[outFile];
67-
Iterable<Warnable> referredFrom =
68-
originalElement != null ? [originalElement] : null;
69-
element?.warn(PackageWarning.duplicateFile,
70-
message: outFile, referredFrom: referredFrom);
71-
}
65+
_warnAboutOverwrite(outFile, null);
7266
}
73-
_fileElementMap[outFile] = element;
7467

68+
var file = _getFile(outFile);
69+
file.writeAsBytesSync(content);
70+
writtenFiles.add(outFile);
71+
logProgress(outFile);
72+
}
73+
74+
@override
75+
void write(String filePath, String content, {Warnable element}) {
76+
// Replace '/' separators with proper separators for the platform.
77+
var outFile = path.joinAll(filePath.split('/'));
78+
79+
_warnAboutOverwrite(outFile, element);
80+
81+
var file = _getFile(outFile);
82+
file.writeAsStringSync(content);
83+
writtenFiles.add(outFile);
84+
logProgress(outFile);
85+
}
86+
87+
void _warnAboutOverwrite(String outFile, Warnable element) {
88+
if (_fileElementMap.containsKey(outFile)) {
89+
assert(element != null,
90+
'Attempted overwrite of $outFile without corresponding element');
91+
var originalElement = _fileElementMap[outFile];
92+
var referredFrom = originalElement != null ? [originalElement] : null;
93+
element?.warn(PackageWarning.duplicateFile,
94+
message: outFile, referredFrom: referredFrom);
95+
}
96+
}
97+
98+
/// Returns the file at [outFile] relative to [outputDir], creating the parent
99+
/// directory if necessary.
100+
File _getFile(String outFile) {
75101
var file = resourceProvider
76102
.getFile(resourceProvider.pathContext.join(outputDir, outFile));
77103
var parent = file.parent2;
78104
if (!parent.exists) {
79105
parent.create();
80106
}
81-
82-
if (content is String) {
83-
file.writeAsStringSync(content);
84-
} else if (content is List<int>) {
85-
file.writeAsBytesSync(content);
86-
} else {
87-
throw ArgumentError.value(
88-
content, 'content', '`content` must be `String` or `List<int>`.');
89-
}
90-
91-
writtenFiles.add(outFile);
92-
logProgress(outFile);
107+
return file;
93108
}
94109
}
95110

lib/src/generator/generator.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@ abstract class FileWriter {
1717
/// All filenames written by this generator.
1818
Set<String> get writtenFiles;
1919

20-
/// Write [content] to a file at [filePath].
21-
void write(String filePath, Object content,
22-
{bool allowOverwrite, Warnable element});
20+
/// Writes [content] to a file at [filePath].
21+
///
22+
/// If a file is to be overwritten, a warning will be reported on [element].
23+
void write(String filePath, String content, {Warnable element});
24+
25+
/// Writes [content] to a file at [filePath].
26+
///
27+
/// If a file is to be overwritten, a warning will be reported, unless
28+
/// [allowOverwrite] is `true`.
29+
void writeBytes(
30+
String filePath,
31+
List<int> content, {
32+
bool allowOverwrite = false,
33+
});
2334
}
2435

2536
/// An abstract class that defines a generator that generates documentation for

lib/src/generator/html_generator.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
4848
if (options.favicon != null) {
4949
// Allow overwrite of favicon.
5050
var bytes =
51-
graph.resourceProvider.getFile(options.favicon).readAsBytesSync();
52-
writer.write(
53-
graph.resourceProvider.pathContext
54-
.join('static-assets', 'favicon.png'),
55-
bytes,
56-
allowOverwrite: true);
51+
writer.resourceProvider.getFile(options.favicon).readAsBytesSync();
52+
writer.writeBytes(
53+
graph.resourceProvider.pathContext.join('static-assets', 'favicon.png'),
54+
bytes,
55+
allowOverwrite: true,
56+
);
5757
}
5858
}
5959

@@ -66,7 +66,7 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
6666
var destFileName = resourcePath.substring(_dartdocResourcePrefix.length);
6767
var destFilePath = writer.resourceProvider.pathContext
6868
.join('static-assets', destFileName);
69-
writer.write(destFilePath,
69+
writer.writeBytes(destFilePath,
7070
await writer.resourceProvider.loadResourceAsBytes(resourcePath));
7171
}
7272
}

0 commit comments

Comments
 (0)