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-5211 Add @Component2 annotation #289

Merged
merged 12 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 38 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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# OverReact Changelog

## 2.4.1

> Complete `2.4.1` Changsets:
>
> - [Dart 2](https://github.com/Workiva/over_react/compare/2.4.0+dart2...2.4.1+dart2)
> - Dart 1 (no changes)

* [#281] Upgrade to `analyzer ^0.35.0`.

## 2.4.0

> Complete `2.4.0` Changsets:
>
> - [Dart 2](https://github.com/Workiva/over_react/compare/2.3.1+dart2...2.4.0+dart2)
> - Dart 1 (no changes)

* [#280] Optimize the over_react builder to avoid unnecessary asset reads.
Informal profiling in a large codebase that consumes over_react has shown in
the best case a speed up on rebuilds from ~1 minute to ~2 seconds, and in the
worst case no difference at all.

## 2.3.1

> Complete `2.3.1` Changsets:
Expand Down
74 changes: 74 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Minimum Required Review](#minimum-required-review)
- [Review Types](#review-types)
- [Manual Testing Criteria](#manual-testing-criteria)
- [__Dart 1 & Dart 2 Dual-Releases__](#dart-1--dart-2-dual-releases)

---

Expand Down Expand Up @@ -122,3 +123,76 @@ __If none of these apply, then manual testing instructions may be omitted.__
_Examples:_
- Test runner was updated.
- New test suites were added.

---

## Dart 1 & Dart 2 Dual-Releases

Long story short, we are unable to maintain a single codebase of over_react that
is compatible with both Dart 1 and Dart 2. Transformers only work on Dart 1 and
builders only work on Dart 2, and we can't maintain the transformer and builder
together due to dependency conflicts.

> For more information on our Dart 2 migration, [see this guide](/doc/dart2_migration.md).

Fortunately, this problem can be shouldered entirely by the maintainers via a
dual-release strategy (credit to the [dart2_constant](https://pub.dartlang.org/packages/dart2_constant)
for the idea). The result is that consumers will depend on a version range of
this library like they normally do and everything will _just work_.

For every _version_ we want to release, we will actually release two releases -
a Dart 1 (transformer) version and a Dart 2 (builder) version. These two
releases will have the same semantic version, but will have unique build
suffixes. The `pub` client will then decide which one to install based on their
`environment.sdk` constraints and the active Dart SDK version.

### Branches

- `master`
- Dart2-only
- Provides a builder
- Environment constraint: `>=2.1.0 <3.0.0`

- `master_dart1`
- Dart1-only
- Provides a transformer
- Environment constraint: `>=1.24.3 <2.0.0`

### Release Process

> Note that these steps are intended to be followed by Workiva employees, and as
> such have some references to internal tooling.

For every release, do the following:

1. Ensure the next release versions exist in MARV:

Name | Branch
---- | ------
over_react 2.x.x+dart1 | master_dart1
over_react 2.x.x+dart2 | master

1. Trigger the `over_react 2.x.x+dart1` release first and review the PR:

- Ensure the updated `pubspec.yaml` version is correct, including the
`+dart1` suffix.

- Ensure the build passes.

- Merge the Dart 1 release and publish it to pub.

1. Trigger the `over_react 2.x.x+dart2` release second and review the PR:

- Ensure the updated `pubspec.yaml` version is correct, including the
`+dart2` suffix.

- Ensure the build passes.

- **Add any necessary changelog updates to the Dart 2 version.**

- Merge the Dart 2 release and publish it to pub.

1. Re-recreate the Dart 1 release in MARV (it does not get recreated
automatically like the default release does).

1. Add the `+dart2` suffix to the automatically created release.
13 changes: 5 additions & 8 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ targets:
exclude:
# This tests un-built behavior, and therefore should not be built
- "test/over_react/component_declaration/builder_helpers_test.dart"

over_react|overReactBuilder:
enabled: false

Expand All @@ -37,13 +38,9 @@ targets:
- "web/**"
exclude:
- "lib/src/builder/**"

build_web_compilers|entrypoint:
generate_for:
include:
- "example/**"
- "lib/*.dart"
- "test/*.dart"
- "web/**/index.dart"
exclude:
- "lib/src/builder/**"

- "example/**.dart"
- "test/*.browser_test.dart"
- "web/*.dart"
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.

4 changes: 2 additions & 2 deletions 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.

142 changes: 138 additions & 4 deletions lib/src/builder/builder.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,142 @@
import 'dart:async';

import 'package:analyzer/analyzer.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'package:dart_style/dart_style.dart';
import 'package:over_react/src/builder/generation/declaration_parsing.dart';
import 'package:over_react/src/builder/generation/impl_generation.dart';
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';

import './util.dart';
import './generator.dart';

Builder overReactBuilder(BuilderOptions options) =>
new PartBuilder([new OverReactGenerator()], outputExtension);
Builder overReactBuilder(BuilderOptions options) => new OverReactBuilder();

class OverReactBuilder extends Builder {
@override
Map<String, List<String>> get buildExtensions => {
'.dart': [outputExtension],
};

@override
FutureOr<void> build(BuildStep buildStep) async {
final source = await buildStep.readAsString(buildStep.inputId);
final libraryUnit = _tryParseCompilationUnit(source, buildStep.inputId);
if (libraryUnit == null) {
return;
}
if (isPart(libraryUnit)) {
return;
}

final outputs = <String>[];
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 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);
if (!await buildStep.canRead(partId)) {
continue;
}
final partSource = await buildStep.readAsString(partId);
final partUnit = _tryParseCompilationUnit(partSource, partId);
if (partUnit == null) {
continue;
}
generateForFile(partSource, partId, partUnit);
}

if (outputs.isNotEmpty) {
final outputId = buildStep.inputId.changeExtension(outputExtension);

// Verify that the library file has an `.over_react.g.dart` part.
final expectedPart = p.basename(outputId.path);
final partUris = libraryUnit.directives
.whereType<PartDirective>()
.map((p) => p.uri.stringValue);
if (!partUris.contains(expectedPart)) {
log.warning('Missing "part \'$expectedPart\';".');
}

await _writePart(buildStep, outputId, 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);
}

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;
}
}

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

final buffer = new StringBuffer()
..writeln('// GENERATED CODE - DO NOT MODIFY BY HAND')
..writeln()
..writeln('part of $partOf;')
..writeln()
..writeln(_headerLine)
..writeln('// OverReactBuilder (package:over_react/src/builder.dart)')
..writeln(_headerLine);

for (final item in outputs) {
buffer
..writeln()
..writeln(item);
}

final output = _formatter.format(buffer.toString());
await buildStep.writeAsString(outputId, output);
}
}
Loading