Skip to content
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

CPLAT-5152: Optimize builder's use of asset reads to avoid unnecessary rebuilds #280

Merged
merged 7 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion example/builder/abstract_inheritance.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/basic.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/basic_library.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/basic_with_state.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/basic_with_type_params.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/generic_inheritance_sub.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/private_component.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/props_mixin.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/builder/state_mixin.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 69 additions & 77 deletions lib/src/builder/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@ import './util.dart';
Builder overReactBuilder(BuilderOptions options) => new OverReactBuilder();

class OverReactBuilder extends Builder {
static final _headerLine = '// '.padRight(77, '*');

static final _formatter = new DartFormatter();

static final RegExp _overReactPartDirective = new RegExp(
r'''['"].*\.over_react\.g\.dart['"]''',
);

static bool _mightContainDeclarations(String source) {
return ParsedDeclarations.key_any_annotation.hasMatch(source) ||
_overReactPartDirective.hasMatch(source);
}
@override
Map<String, List<String>> get buildExtensions => {
'.dart': [outputExtension],
};

@override
FutureOr<void> build(BuildStep buildStep) async {
Expand All @@ -33,88 +25,93 @@ class OverReactBuilder extends Builder {
return;
aaronlademann-wf marked this conversation as resolved.
Show resolved Hide resolved
}

CompilationUnit libraryUnit;
try {
libraryUnit = parseCompilationUnit(
source,
name: buildStep.inputId.path,
suppressErrors: false,
parseFunctionBodies: true);
} catch (error, stackTrace) {
log.severe('There was an error parsing the compilation unit for file: ${buildStep.inputId}');
log.fine(error);
log.fine(stackTrace);
final libraryUnit = _tryParseCompilationUnit(source, buildStep.inputId);
if (libraryUnit == null) {
return;
}

if (isPart(libraryUnit)) {
return;
}

final librarySourceFile = new SourceFile.fromString(
source,
url: idToPackageUri(buildStep.inputId));
final libraryDeclarations = new ParsedDeclarations(
libraryUnit,
librarySourceFile,
log);
if (libraryDeclarations.hasErrors) {
log.severe('There was an error parsing the file declarations for file: ${buildStep.inputId}');
return;
}

final outputs = <String>[];
final libraryGenerator = new ImplGenerator(log, librarySourceFile)
..generate(libraryDeclarations);
outputs.add(libraryGenerator.outputContentsBuffer.toString());
void generateForFile(String source, AssetId id, CompilationUnit unit) {
if (!ParsedDeclarations.mightContainDeclarations(source)) {
return;
}
final sourceFile = new SourceFile.fromString(
source, url: idToPackageUri(id));
final declarations = new ParsedDeclarations(unit, sourceFile, log);
if (declarations.hasErrors) {
log.severe('There was an error parsing the file declarations for file: $id');
return;
}
final generator = new ImplGenerator(log, sourceFile)
..generate(declarations);
final generatedOutput = generator.outputContentsBuffer.toString().trim();
if (generatedOutput.isNotEmpty) {
outputs.add(generatedOutput);
}
}

// Collect all of the part files for this library.
final parts = libraryUnit.directives
.whereType<PartDirective>()
// Ignore `.over_react.g.dart` parts - that's what we're generating.
.where((part) => !part.uri.stringValue.endsWith(outputExtension));
// Ignore all generated `.g.dart` parts.
.where((part) => !part.uri.stringValue.endsWith('.g.dart'));

// Generate over_react code for the input library.
generateForFile(source, buildStep.inputId, libraryUnit);

// Generate over_react code for each part file of the input library.
for (final part in parts) {
final partId = new AssetId.resolve(
part.uri.stringValue,
from: buildStep.inputId);
final partSource = await buildStep.readAsString(partId);
final partSourceFile = new SourceFile.fromString(
partSource,
url: idToPackageUri(partId));
CompilationUnit partUnit;
try {
partUnit = parseCompilationUnit(
partSource,
name: partId.path,
suppressErrors: false,
parseFunctionBodies: true);
} catch (error, stackTrace) {
log.severe('There was an error parsing the compilation unit for file: $partId');
log.fine(error);
log.fine(stackTrace);
return;
if (!await buildStep.canRead(partId)) {
continue;
}
final partDeclarations = new ParsedDeclarations(partUnit, partSourceFile, log);
if (partDeclarations.hasErrors) {
log.severe('There was an error parsing the file declarations for file: ${buildStep.inputId}');
return;
final partSource = await buildStep.readAsString(partId);
final partUnit = _tryParseCompilationUnit(partSource, partId);
if (partUnit == null) {
continue;
}
final partGenerator = new ImplGenerator(log, partSourceFile)
..generate(partDeclarations);
outputs.add(partGenerator.outputContentsBuffer.toString());
generateForFile(partSource, partId, partUnit);
}

final nonEmptyOutputs = outputs
.map((s) => s.trim())
.where((s) => s.isNotEmpty);
if (nonEmptyOutputs.isEmpty) {
return;
if (outputs.isNotEmpty) {
await _writePart(buildStep, outputs);
}
}

static final _headerLine = '// '.padRight(77, '*');

static final _formatter = new DartFormatter();

static final RegExp _overReactPartDirective = new RegExp(
r'''['"].*\.over_react\.g\.dart['"]''',
);

static bool _mightContainDeclarations(String source) {
return ParsedDeclarations.mightContainDeclarations(source) ||
_overReactPartDirective.hasMatch(source);
}

await _writePart(buildStep, nonEmptyOutputs);
static CompilationUnit _tryParseCompilationUnit(String source, AssetId id) {
try {
return parseCompilationUnit(
source,
name: id.path,
suppressErrors: false,
parseFunctionBodies: true);
} catch (error, stackTrace) {
log.severe('There was an error parsing the compilation unit for file: $id');
log.fine(error);
log.fine(stackTrace);
return null;
}
}

FutureOr<void> _writePart(BuildStep buildStep, Iterable<String> outputs) async {
static FutureOr<void> _writePart(BuildStep buildStep, Iterable<String> outputs) async {
final outputId = buildStep.inputId.changeExtension(outputExtension);
final partOf = "'${p.basename(buildStep.inputId.uri.toString())}'";

Expand All @@ -124,7 +121,7 @@ class OverReactBuilder extends Builder {
..writeln('part of $partOf;')
..writeln()
..writeln(_headerLine)
..writeln('// OverReactGenerator')
..writeln('// OverReactBuilder (package:over_react/src/builder.dart)')
..writeln(_headerLine);

for (final item in outputs) {
Expand All @@ -136,9 +133,4 @@ class OverReactBuilder extends Builder {
final output = _formatter.format(buffer.toString());
await buildStep.writeAsString(outputId, output);
}

@override
Map<String, List<String>> get buildExtensions => {
'.dart': [outputExtension],
};
}
2 changes: 1 addition & 1 deletion lib/src/component/abstract_transition.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/component/aria_mixin.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/component/error_boundary.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/component/prop_mixins.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/component/resize_sensor.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/util/class_names.over_react.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading