Skip to content

Commit

Permalink
Merge 381354f into d283722
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins committed Apr 28, 2021
2 parents d283722 + 381354f commit 7f731ba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
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;

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

0 comments on commit 7f731ba

Please sign in to comment.