Skip to content

Commit bf7eb56

Browse files
authored
Split FileWriter.writeBytes out from FileWriter.write (#2627)
1 parent 55034c5 commit bf7eb56

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

lib/dartdoc.dart

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,60 @@ 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
}
67+
_fileElementMap[outFile] = null;
68+
69+
var file = _getFile(outFile);
70+
file.writeAsBytesSync(content);
71+
writtenFiles.add(outFile);
72+
logProgress(outFile);
73+
}
74+
75+
@override
76+
void write(String filePath, String content, {Warnable element}) {
77+
// Replace '/' separators with proper separators for the platform.
78+
var outFile = path.joinAll(filePath.split('/'));
79+
80+
_warnAboutOverwrite(outFile, element);
7381
_fileElementMap[outFile] = element;
7482

83+
var file = _getFile(outFile);
84+
file.writeAsStringSync(content);
85+
writtenFiles.add(outFile);
86+
logProgress(outFile);
87+
}
88+
89+
void _warnAboutOverwrite(String outFile, Warnable element) {
90+
if (_fileElementMap.containsKey(outFile)) {
91+
assert(element != null,
92+
'Attempted overwrite of $outFile without corresponding element');
93+
var originalElement = _fileElementMap[outFile];
94+
var referredFrom = originalElement != null ? [originalElement] : null;
95+
element?.warn(PackageWarning.duplicateFile,
96+
message: outFile, referredFrom: referredFrom);
97+
}
98+
}
99+
100+
/// Returns the file at [outFile] relative to [outputDir], creating the parent
101+
/// directory if necessary.
102+
File _getFile(String outFile) {
75103
var file = resourceProvider
76104
.getFile(resourceProvider.pathContext.join(outputDir, outFile));
77105
var parent = file.parent2;
78106
if (!parent.exists) {
79107
parent.create();
80108
}
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);
109+
return file;
93110
}
94111
}
95112

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)