Skip to content

Centralize all file writing in HtmlGeneratorInstance #1538

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 1 commit into from
Nov 6, 2017
Merged
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
47 changes: 25 additions & 22 deletions lib/src/html/html_generator_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'dart:async' show Future, StreamController;
import 'dart:convert' show JsonEncoder;
import 'dart:io' show Directory, File;
import 'dart:typed_data' show Uint8List;

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

await _copyResources();
if (faviconPath != null) {
File file = new File(path.join(out.path, 'static-assets', 'favicon.png'));
file.writeAsBytesSync(new File(faviconPath).readAsBytesSync());
var bytes = new File(faviconPath).readAsBytesSync();
_writeFile(path.join(out.path, 'static-assets', 'favicon.png'), bytes);
}
}

Expand Down Expand Up @@ -292,11 +291,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
'encountered $resourcePath');
}
String destFileName = resourcePath.substring(prefix.length);
File destFile =
new File(path.join(out.path, 'static-assets', destFileName))
..createSync(recursive: true);
Uint8List resourceBytes = await loader.loadAsBytes(resourcePath);
destFile.writeAsBytesSync(resourceBytes);
_writeFile(path.join(out.path, 'static-assets', destFileName),
await loader.loadAsBytes(resourcePath));
}
}

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

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

void _writeFile(String filename, String content) {
File file = _createOutputFile(filename);
file.writeAsStringSync(content);
/// [content] must be either [String] or [List<int>].
void _writeFile(String filename, Object content) {
// If you see this assert, we're probably being called to build non-canonical
// docs somehow. Check data.self.isCanonical and callers for bugs.
assert(!writtenFiles.contains(filename));

File file = new File(filename);
Directory parent = file.parent;
if (!parent.existsSync()) {
parent.createSync(recursive: true);
}

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

File _createOutputFile(String filename) {
File file = new File(filename);
Directory parent = file.parent;
if (!parent.existsSync()) parent.createSync(recursive: true);
return file;
}