Skip to content

Commit

Permalink
Migrated to null-safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
simphotonics committed Mar 19, 2021
1 parent 0e78f19 commit 92c5952
Show file tree
Hide file tree
Showing 43 changed files with 249 additions and 128 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions CHANGELOG.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0-nullsafety

Migrated to null-safety.

## 0.1.6

Renamed classes `LibDir` and `PackageDir` to `LibDir` and `PackageDir`.
Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified analysis_options.yaml
100644 → 100755
Empty file.
Empty file modified example/README.md
100644 → 100755
Empty file.
Empty file modified example/researcher/bin/main.dart
100644 → 100755
Empty file.
Empty file modified example/researcher/build.yaml
100644 → 100755
Empty file.
Empty file modified example/researcher/lib/input/researcher_a.dart
100644 → 100755
Empty file.
Empty file modified example/researcher/lib/input/researcher_b.dart
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions example/researcher/lib/output/researchers.dart

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

Empty file modified example/researcher/lib/researcher.dart
100644 → 100755
Empty file.
Empty file modified example/researcher/lib/src/annotations/add_integers.dart
100644 → 100755
Empty file.
Empty file modified example/researcher/lib/src/annotations/add_names.dart
100644 → 100755
Empty file.
Empty file modified example/researcher/lib/src/annotations/add_numbers.dart
100644 → 100755
Empty file.
22 changes: 14 additions & 8 deletions example/researcher/pubspec.yaml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
name: researcher
description:
Example demonstrating how to use the library merging_builder.
description: Example demonstrating how to use the library merging_builder.

environment:
sdk: '>=2.8.1 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
dependencies: null

dev_dependencies:
build_runner: ^1.10.0
build_test: ^1.2.0
researcher_builder:
path: ../researcher_builder
source_gen: ^0.9.6
test: ^1.15.2
build_runner: ^1.12.2
build_test: ^2.0.0
source_gen: ^0.9.10+4
test: ^1.16.8

dependency_overrides:
source_gen:
git:
url: [email protected]:dart-lang/source_gen.git
ref: master
path: source_gen
Empty file modified example/researcher/test/README.md
100644 → 100755
Empty file.
Empty file modified example/researcher/test/merging_builder_test.dart
100644 → 100755
Empty file.
16 changes: 12 additions & 4 deletions example/researcher/test/merging_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ import 'src/mock_builders/mock_merging_builder.dart';
/// Note: The path to the input files is specified relative to the main
/// directory of [merging_builder].
Future<void> main() async {
final libA = LibraryReader(await resolveSource(researcher_a_dot_dart,
(resolver) => resolver.findLibraryByName('researcher_a')));
final libA = LibraryReader(
await resolveSource(
researcher_a_dot_dart,
(resolver) async =>
(await resolver.findLibraryByName('researcher_a'))!),
);

final libB = LibraryReader(await resolveSource(researcher_b_dot_dart,
(resolver) => resolver.findLibraryByName('researcher_b')));
final libB = LibraryReader(
await resolveSource(
researcher_b_dot_dart,
(resolver) async =>
(await resolver.findLibraryByName('researcher_b'))!),
);

final numBuilder = MockMergingBuilder<num>(
generator: AddNumbersGenerator(),
Expand Down
Empty file modified example/researcher/test/src/input/mock_assets.dart
100644 → 100755
Empty file.
100 changes: 93 additions & 7 deletions example/researcher/test/src/mock_builders/mock_merging_builder.dart
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import 'package:crypto/src/digest.dart';
import 'package:build/src/resource/resource.dart';
import 'package:build/src/asset/id.dart';
import 'package:build/src/analyzer/resolver.dart';
import 'package:analyzer/dart/element/element.dart';
import 'dart:convert';

import 'dart:async';

import 'package:dart_style/dart_style.dart';
import 'package:glob/glob.dart';
import 'package:merging_builder/merging_builder.dart';
import 'package:merging_builder/src/builders/formatter.dart';
import 'package:source_gen/source_gen.dart';
import 'package:build/build.dart' show BuildStep;

/// Mock builder with methods: [mergedContent] and [_combinedStream].
/// Used for testing purposes.
class MockMergingBuilder<T> {
MockMergingBuilder({
this.generator,
this.header,
this.footer,
this.libraries,
Formatter formatOutput,
required this.generator,
this.header = '',
this.footer = '',
required this.libraries,
Formatter? formatOutput,
}) : _formatOutput = formatOutput ?? DartFormatter().format;

final MergingGenerator<T, dynamic> generator;
Expand All @@ -38,7 +49,7 @@ class MockMergingBuilder<T> {
buffer.writeln();

// Add footer.
if (footer != null) buffer.writeln(footer);
buffer.writeln(footer);

// Format output.
return _formatOutput(buffer.toString());
Expand All @@ -49,11 +60,86 @@ class MockMergingBuilder<T> {
/// by iterating over each file asset.
Stream<T> _combineStreams() async* {
for (final library in libraries) {
final streamOfT = await generator.generateStream(library, null);
final streamOfT = generator.generateStream(library, MockBuildStep());
// Combining all objects of type [T] into a stream.
await for (final T t in streamOfT) {
yield t;
}
}
}
}

class MockBuildStep extends BuildStep {
@override
Future<bool> canRead(AssetId id) {
// TODO: implement canRead
throw UnimplementedError();
}

@override
Future<Digest> digest(AssetId id) {
// TODO: implement digest
throw UnimplementedError();
}

@override
Future<T> fetchResource<T>(Resource<T> resource) {
// TODO: implement fetchResource
throw UnimplementedError();
}

@override
Stream<AssetId> findAssets(Glob glob) {
// TODO: implement findAssets
throw UnimplementedError();
}

@override
// TODO: implement inputId
AssetId get inputId => throw UnimplementedError();

@override
// TODO: implement inputLibrary
Future<LibraryElement> get inputLibrary => throw UnimplementedError();

@override
Future<List<int>> readAsBytes(AssetId id) {
// TODO: implement readAsBytes
throw UnimplementedError();
}

@override
Future<String> readAsString(AssetId id, {Encoding encoding = utf8}) {
// TODO: implement readAsString
throw UnimplementedError();
}

@override
void reportUnusedAssets(Iterable<AssetId> ids) {
// TODO: implement reportUnusedAssets
}

@override
// TODO: implement resolver
Resolver get resolver => throw UnimplementedError();

@override
T trackStage<T>(String label, T Function() action,
{bool isExternal = false}) {
// TODO: implement trackStage
throw UnimplementedError();
}

@override
Future<void> writeAsBytes(AssetId id, FutureOr<List<int>> bytes) {
// TODO: implement writeAsBytes
throw UnimplementedError();
}

@override
Future<void> writeAsString(AssetId id, FutureOr<String> contents,
{Encoding encoding = utf8}) {
// TODO: implement writeAsString
throw UnimplementedError();
}
}
3 changes: 1 addition & 2 deletions example/researcher/test/src/mock_builders/mock_standalone_builder.dart
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:merging_builder/merging_builder.dart';
import 'package:meta/meta.dart';
import 'package:source_gen/source_gen.dart';

/// StandaloneBuilder for testing purposes with
Expand All @@ -8,7 +7,7 @@ class MockStandaloneBuilder extends StandaloneBuilder<LibDir> {
MockStandaloneBuilder({
String inputFiles = 'lib/*.dart',
String outputFiles = 'lib/standalone_(*).dart',
@required Generator generator,
required Generator generator,
String header = '',
String footer = '',
}) : super(
Expand Down
Empty file modified example/researcher/test/standalone_builder_test.dart
100644 → 100755
Empty file.
Empty file modified example/researcher_builder/build.yaml
100644 → 100755
Empty file.
Empty file modified example/researcher_builder/lib/builder.dart
100644 → 100755
Empty file.
Empty file modified example/researcher_builder/lib/researcher_builder.dart
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion example/researcher_builder/lib/src/generators/add_names_generator.dart
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AddNamesGenerator extends MergingGenerator<List<String>, AddNames> {
}
return result;
}
return null;
return <String>['Could not read name'];
}

/// Returns the merged content.
Expand Down
6 changes: 4 additions & 2 deletions example/researcher_builder/lib/src/generators/add_numbers_generator.dart
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class AddNumbersGenerator extends MergingGenerator<num, AddNumbers> {
BuildStep buildStep,
) {
if (element is ClassElement) {
return element.getField('number')?.computeConstantValue()?.toIntValue();
return element.getField('number')?.computeConstantValue()?.toIntValue() ??
double.nan;
} else {
return double.nan;
}
return null;
}

/// Returns merged content.
Expand Down
2 changes: 1 addition & 1 deletion example/researcher_builder/lib/src/generators/assistant_generator.dart
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class AssistantGenerator extends GeneratorForAnnotation<AddNames> {
}
return 'final String assistants = \'${result.join(', ')}\';';
}
return null;
return '';
}
}
25 changes: 16 additions & 9 deletions example/researcher_builder/pubspec.yaml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
name: researcher_builder
description:
Example demonstrating how to use the library merging_builder.

description: Example demonstrating how to use the library merging_builder.

environment:
sdk: '>=2.8.1 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
analyzer: ^0.39.12
build: ^1.3.0
build_config: ^0.4.2
merging_builder:
path: ../../
researcher:
path: ../researcher
source_gen: ^0.9.5
quote_buffer: ^0.1.5
analyzer: ^1.2.0
build: ^2.0.0
build_config: ^0.4.7
quote_buffer: ^0.2.2
source_gen: ^0.9.10+4

dev_dependencies: null

dev_dependencies:
dependency_overrides:
source_gen:
git:
url: [email protected]:dart-lang/source_gen.git
ref: master
path: source_gen
Empty file modified images/merging_builder.svg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified lib/merging_builder.dart
100644 → 100755
Empty file.
Empty file modified lib/src/builders/formatter.dart
100644 → 100755
Empty file.
35 changes: 17 additions & 18 deletions lib/src/builders/merging_builder.dart
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';
import 'package:build/build.dart';
import 'package:exception_templates/exception_templates.dart';
import 'package:glob/glob.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path show equals;
import 'package:source_gen/source_gen.dart' show LibraryReader;

Expand All @@ -18,31 +17,31 @@ import 'synthetic_input.dart';
/// - Requires a generator extending [MergingGenerator].
class MergingBuilder<T, S extends SyntheticInput> extends SyntheticBuilder<S> {
/// Constructs a [MergingBuilder] object.
///
/// - [inputFiles] defaults to: `'lib/*.dart'`.
///
/// - [outputFile] defaults to: `'lib/merged_output.dart'`.
///
/// - [generator] extending [MergingGenerator<T, A>] is required.
///
/// - [header] defaults to: `''`
///
/// - [footer] defaults to : `''`
///
/// - [formatOutput] defaults to: `DartFormatter().format`.
/// * `inputFiles`: Path to the input files relative to the
/// package root directory. Glob-style syntax is
/// allowed, defaults to: `'lib/*.dart'`.
/// * `outputFile`: defaults to: `'lib/merged_output.dart'`.
/// * `generator`: Must extend `MergingGenerator<T, A>`.
/// * `header`: `String` that will be inserted at the top of the
/// generated file below the 'DO NOT EDIT' warning message.
/// * `footer`: String that will be inserted at the very bottom of the
/// generated file.
/// * `formatter`: A function with signature `String Function(String input)`
/// that is used to format the generated source code.
/// The default formatter is: `DartFormatter().format`.
MergingBuilder({
String inputFiles = 'lib/*.dart',
this.outputFile = 'lib/merged_output.dart',
@required this.generator,
required this.generator,
String header = '',
String footer = '',
this.sortAssets = false,
Formatter formatOutput,
Formatter? formatter,
}) : super(
inputFiles: inputFiles,
header: header,
footer: footer,
formatOutput: formatOutput,
formatter: formatter,
);

/// Path to output file relative to the package root directory.
Expand Down Expand Up @@ -108,7 +107,7 @@ class MergingBuilder<T, S extends SyntheticInput> extends SyntheticBuilder<S> {
/// by iterating over each library file asset.
Stream<T> _combineStreams(
BuildStep buildStep,
List<AssetId> libraryAssetIds,
Iterable<AssetId> libraryAssetIds,
) async* {
// Accessing libraries.
for (final libraryAssetId in libraryAssetIds) {
Expand All @@ -119,7 +118,7 @@ class MergingBuilder<T, S extends SyntheticInput> extends SyntheticBuilder<S> {
// Calling generator.generateStream. An object of type [T] is
// emitted for each class defined in library that is annotated with [A].
log.fine('Running ${generator.runtimeType} on: ${libraryAssetId.path}.');
final streamOfT = await generator.generateStream(library, buildStep);
final streamOfT = generator.generateStream(library, buildStep);

// Combining all objects of type [T] into a stream.
await for (final T t in streamOfT) {
Expand Down
Loading

0 comments on commit 92c5952

Please sign in to comment.