Skip to content

Split FileWriter.writeBytes out from FileWriter.write #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,60 @@ class DartdocFileWriter implements FileWriter {
DartdocFileWriter(this.outputDir, this.resourceProvider);

@override
void write(String filePath, Object content,
{bool allowOverwrite, Warnable element}) {
void writeBytes(
String filePath,
List<int> content, {
bool allowOverwrite = false,
}) {
// Replace '/' separators with proper separators for the platform.
var outFile = path.joinAll(filePath.split('/'));

allowOverwrite ??= false;
if (!allowOverwrite) {
if (_fileElementMap.containsKey(outFile)) {
assert(element != null,
'Attempted overwrite of $outFile without corresponding element');
var originalElement = _fileElementMap[outFile];
Iterable<Warnable> referredFrom =
originalElement != null ? [originalElement] : null;
element?.warn(PackageWarning.duplicateFile,
message: outFile, referredFrom: referredFrom);
}
_warnAboutOverwrite(outFile, null);
}
_fileElementMap[outFile] = null;

var file = _getFile(outFile);
file.writeAsBytesSync(content);
writtenFiles.add(outFile);
logProgress(outFile);
}

@override
void write(String filePath, String content, {Warnable element}) {
// Replace '/' separators with proper separators for the platform.
var outFile = path.joinAll(filePath.split('/'));

_warnAboutOverwrite(outFile, element);
_fileElementMap[outFile] = element;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if the test failures might be due to this being removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ha, good catch!


var file = _getFile(outFile);
file.writeAsStringSync(content);
writtenFiles.add(outFile);
logProgress(outFile);
}

void _warnAboutOverwrite(String outFile, Warnable element) {
if (_fileElementMap.containsKey(outFile)) {
assert(element != null,
'Attempted overwrite of $outFile without corresponding element');
var originalElement = _fileElementMap[outFile];
var referredFrom = originalElement != null ? [originalElement] : null;
element?.warn(PackageWarning.duplicateFile,
message: outFile, referredFrom: referredFrom);
}
}

/// Returns the file at [outFile] relative to [outputDir], creating the parent
/// directory if necessary.
File _getFile(String outFile) {
var file = resourceProvider
.getFile(resourceProvider.pathContext.join(outputDir, outFile));
var parent = file.parent2;
if (!parent.exists) {
parent.create();
}

if (content is String) {
file.writeAsStringSync(content);
} else if (content is List<int>) {
file.writeAsBytesSync(content);
} else {
throw ArgumentError.value(
content, 'content', '`content` must be `String` or `List<int>`.');
}

writtenFiles.add(outFile);
logProgress(outFile);
return file;
}
}

Expand Down
17 changes: 14 additions & 3 deletions lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@ abstract class FileWriter {
/// All filenames written by this generator.
Set<String> get writtenFiles;

/// Write [content] to a file at [filePath].
void write(String filePath, Object content,
{bool allowOverwrite, Warnable element});
/// Writes [content] to a file at [filePath].
///
/// If a file is to be overwritten, a warning will be reported on [element].
void write(String filePath, String content, {Warnable element});

/// Writes [content] to a file at [filePath].
///
/// If a file is to be overwritten, a warning will be reported, unless
/// [allowOverwrite] is `true`.
void writeBytes(
String filePath,
List<int> content, {
bool allowOverwrite = false,
});
}

/// An abstract class that defines a generator that generates documentation for
Expand Down
14 changes: 7 additions & 7 deletions lib/src/generator/html_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
if (options.favicon != null) {
// Allow overwrite of favicon.
var bytes =
graph.resourceProvider.getFile(options.favicon).readAsBytesSync();
writer.write(
graph.resourceProvider.pathContext
.join('static-assets', 'favicon.png'),
bytes,
allowOverwrite: true);
writer.resourceProvider.getFile(options.favicon).readAsBytesSync();
writer.writeBytes(
graph.resourceProvider.pathContext.join('static-assets', 'favicon.png'),
bytes,
allowOverwrite: true,
);
}
}

Expand All @@ -66,7 +66,7 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
var destFileName = resourcePath.substring(_dartdocResourcePrefix.length);
var destFilePath = writer.resourceProvider.pathContext
.join('static-assets', destFileName);
writer.write(destFilePath,
writer.writeBytes(destFilePath,
await writer.resourceProvider.loadResourceAsBytes(resourcePath));
}
}
Expand Down