Skip to content

Commit

Permalink
Merge pull request #86 from davidmarne/dart-2-dev
Browse files Browse the repository at this point in the history
Dart 7.4.5 - Dart 2
  • Loading branch information
davidmarne authored Aug 2, 2018
2 parents 06f6556 + b9b0934 commit eb2bc1e
Show file tree
Hide file tree
Showing 20 changed files with 512 additions and 439 deletions.
22 changes: 14 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
language: dart
dart:
- stable
dart: dev
sudo: required
addons:
chrome: stable
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- "t=0; until (xdpyinfo -display :99 &> /dev/null || test $t -gt 10); do sleep 1; let t=$t+1; done"

script:
- dart tool/build.dart
- pub run dart_dev format --check
- pub run dart_dev analyze
- pub run dart_dev test -p vm
- pub run dart_dev coverage --no-html
- bash <(curl -s https://codecov.io/bash) -f coverage/coverage.lcov
- pub run build_runner build --delete-conflicting-outputs
- dartfmt -n --set-exit-if-changed lib/
- dartanalyzer --fatal-warnings --fatal-infos lib/
- pub run build_runner test --delete-conflicting-outputs
- pub run build_runner test --delete-conflicting-outputs -- -p chrome
51 changes: 51 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
## 7.4.5

* open sdk range to dart 2!
* update built_value and source_gen dependencies
* use SharedPartBuilder rather than PartBuilder in the generator

## 7.4.4-dev

* use typedef names in action generator

## 7.4.3-dev

* update dependency range on analyzer
* update dependency range on test
* fix broken test caused by dart 2's new asyc behavior

## 7.4.2

* add lint ignores to generated files

## 7.4.1-dev

* open sourc-gen range
* run tests on the dev compiler

## 7.4.1

* fix cast issues around store changes in dart 2
* open analyzer version range

## 7.4.0-dev

* add build.yaml and build.dart to comply with build_runner 0.7.x

With the dev tag you can consume the latest versions of built_value, build, and build_runner that are only compatable with dart 2.

In order to migrate:

* remove your old build scripts
* update your pubspec to include

```yaml
build_runner: ^0.7.9
```
* add a build.yaml to the root of your repo that contains the following:
```yaml
targets:
$default:
builders:
built_value_generator|built_value:
enabled: false
```
* run `pub run build_runner build`.

The build script is only a temporary measure until https://github.com/dart-lang/source_gen/issues/319 is resolved.


## 7.4.0

* add built_redux_test_utils library - exposes an expectDispatched function for expecting a given action gets dispatched asynchronously
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
[![Pub](https://img.shields.io/pub/v/built_redux.svg)](https://pub.dartlang.org/packages/built_redux)
[![codecov.io](http://codecov.io/github/davidmarne/built_redux/coverage.svg?branch=master)](http://codecov.io/github/davidmarne/built_redux?branch=master)

----

built_redux is a state management library written in dart that enforces immutability.
built_redux is not only an implementation of [redux][redux_git], but also a framework for building middleware and reducers in a type safe manner.

Inspired by [redux][redux_git]

Built using [built_value][built_value_git]

## Using dart 2?
Check out the [dart 2 dev branch](https://github.com/davidmarne/built_redux/tree/dart-2-dev), which works with the latest versions of build, build_runner, and built_value.

## Framework bindings

[flutter]
Expand Down
15 changes: 15 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
targets:
$default:
builders:
built_redux|built_redux:
enabled: true

builders:
built_redux:
target: ":built_redux"
import: "package:built_redux/builder.dart"
builder_factories: ["builtRedux"]
build_extensions: {".dart": [".built_redux.g.part"]}
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
38 changes: 7 additions & 31 deletions doc/basics/setup.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
# Setup

built_value and built_redux both rely on code generation.
built_value and built_redux both rely on build_runner for code generation.

1. Add the `built_redux` package as a dependency in your `pubspec.yaml`.

```yaml
dependencies:
built_redux: ^7.0.0
built_value: ^5.0.0
built_redux: ^7.4.5
built_value: '>=5.5.5 <7.0.0'
dev_dependencies:
build_runner: ^0.6.0
built_value_generator: '>=4.1.0 < 6.0.0'
source_gen: ^0.7.0
build_runner: ^0.9.0
built_value_generator: ^6.0.0
```
2. Create a script to run generators for generating built_values and built_redux action classes.
```dart
import 'dart:async';

import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';
import 'package:built_redux/generator.dart';

Future main(List<String> args) async {
await build([
new BuildAction(
new PartBuilder([
new BuiltValueGenerator(),
new BuiltReduxGenerator(),
]),
// your lib name here
'built_redux',
// tweak the files that invoke the generator here
inputs: const ['lib/**/*.dart'])
], deleteFilesByDefault: true);
}
```

3. Run the build script from the command line to generate your built_values and built_redux action classes by running `dart [build script name].dart`
2. Use build runner to build the generated files by executing `pub run build_runner build`
64 changes: 33 additions & 31 deletions example/example.g.dart

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

8 changes: 8 additions & 0 deletions lib/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import 'generator.dart';

Builder builtRedux(BuilderOptions _) => new SharedPartBuilder([
new BuiltReduxGenerator(),
], 'built_redux');
39 changes: 26 additions & 13 deletions lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ String _allActionDispatcherFieldsTemplate(ClassElement element) =>
element, _reduxActionFieldTemplate, _needsReduxActions);

String _actionDispatcherFieldTemplate(ClassElement e, FieldElement f) {
final genericType = _getActionGenericType(f);
final genericType =
_getActionGenericType((f.type as InterfaceType).typeArguments.first);
return 'final ActionDispatcher<${genericType}> ${f.name} = new ActionDispatcher<${genericType}>(\'${e.name}-${f.name}\');';
}

Expand Down Expand Up @@ -99,7 +100,8 @@ String _allActionNamesFieldsTemplate(ClassElement element) =>
element, _actionNameTemplate, _isActionDispatcher);

String _actionNameTemplate(ClassElement e, FieldElement f) {
final genericType = _getActionGenericType(f);
final genericType =
_getActionGenericType((f.type as InterfaceType).typeArguments.first);
return 'static final ActionName<${genericType}> ${f.name} = new ActionName<${genericType}>(\'${e.name}-${f.name}\');';
}

Expand Down Expand Up @@ -147,25 +149,36 @@ bool _hasSuperType(ClassElement classElement, String type) =>
.any((interfaceType) => interfaceType.name == type) &&
!classElement.displayName.startsWith('_\$');

String _getActionGenericType(FieldElement e) {
var typeArgument =
(e.type as InterfaceType).typeArguments.first as ParameterizedType;
// generic type has generic type parameters?
if (typeArgument.typeArguments.isEmpty ||
typeArgument.typeArguments.every((ta) => ta.name == 'dynamic')) {
return typeArgument.name;
String _getActionGenericType(DartType type) {
if (type is FunctionType) {
final generics =
_correctUnresolvedGenerics(type.typeArguments, type.typeParameters);
if (generics.isEmpty) return typeNameOf(type);
return typeNameOf(type) + '<${generics.join(',')}>';
}

final typeArguments =
typeArgument.typeArguments.map((ta) => ta.toString()).toList();
if (type is ParameterizedType) {
final generics =
_correctUnresolvedGenerics(type.typeArguments, type.typeParameters);
if (generics.isEmpty) return type.name;
return type.name + '<${generics.join(',')}>';
}

if (type.isVoid) return 'void';

return 'dynamic';
}

List<String> _correctUnresolvedGenerics(
List<DartType> args, List<TypeParameterElement> params) {
// hack for thunks/repatches or any other type argument list where
// any given argument is a Built and the proceding is a Builder
// and the builder is dynamic in the typeArguments list becauses it is
// yet to be generated. This is complex and a bit awkward but
// it is written this way to be very careful not to make any unintended
// changes the the typeArguments list.
final boundParams = typeArgument.typeParameters.map((e) => e.bound);
final typeArguments = args.map((ta) => ta.toString()).toList();
final boundParams = params.map((e) => e.bound);
for (int i = 0; i < boundParams.length; i++) {
// get the bound param at this spot
final b = boundParams.elementAt(i);
Expand All @@ -192,5 +205,5 @@ String _getActionGenericType(FieldElement e) {
}
}

return '${typeArgument.name}<${typeArguments.join(',')}>';
return typeArguments;
}
6 changes: 4 additions & 2 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class Store<
if (_state == state) return;

// update the internal state and publish the change
_stateController.add(
new StoreChange<State, StateBuilder, dynamic>(state, _state, action));
if (!_stateController.isClosed)
_stateController.add(new StoreChange<State, StateBuilder, dynamic>(
state, _state, action));

_state = state;
};

Expand Down
29 changes: 12 additions & 17 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
name: built_redux
version: 7.4.2
version: 7.4.5
description:
A state management library written in dart that enforces immutability
authors:
- David Marne <davemarne@gmail.com>
homepage: https://github.com/davidmarne/built_redux
dependencies:
analyzer: '>=0.29.0 <0.32.0'
build: '>=0.10.0 <0.12.0'
built_collection: '>=1.0.0 <4.0.0'
built_value: '>=4.1.0 < 6.0.0'
source_gen: ^0.7.0
test: ^0.12.0
analyzer: ^0.32.1
build: ^0.12.0
built_collection: '>=2.0.0 <4.0.0'
built_value: '>=5.5.5 <7.0.0'
source_gen: ^0.9.0
test: '>=0.12.0 <2.0.0'

dev_dependencies:
build_runner: ^0.6.0
built_value_generator: ^5.0.0
dart_dev: ^1.9.2
dart_style: ^1.0.0
coverage: ^0.9.3
build_runner: ^0.10.0
build_test: ^0.10.0
built_value_generator: ^6.0.0
build_web_compilers: ^0.4.0

environment:
sdk: ">=1.17.1 <2.0.0"

transformers:
- test/pub_serve:
$include: test/**_test{.*,}.dart
sdk: '>=2.0.0-dev <3.0.0'
Loading

0 comments on commit eb2bc1e

Please sign in to comment.