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

Get name suffix from the build.yaml file #1898

Merged
merged 22 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -42,6 +42,7 @@ jobs:
# TODO(rrousselGit) update riverpod_cli test setup to be supported by the CI
# - packages/riverpod_cli
- packages/riverpod_generator
- packages/riverpod_generator/integration/build_yaml
# TODO(rrousselGit) update riverpod_graph test setup to be supported by the CI
# - packages/riverpod_graph
- packages/riverpod_lint
Expand All @@ -62,6 +63,11 @@ jobs:
run: echo "PUB_CACHE="$HOME/.pub-cache"" >> $GITHUB_ENV
- name: Install dependencies
run: flutter pub get
- name: Install dependencies (integration/buid.yaml)
run: |
if test -d "integration/build_yaml"; then
flutter pub get integration/build_yaml
fi

- name: Check format
run: flutter format --set-exit-if-changed .
Expand Down
6 changes: 4 additions & 2 deletions codecov.yml
@@ -1,5 +1,5 @@
codecov:
require_ci_to_pass: yes
require_ci_to_pass: true

coverage:
precision: 2
Expand All @@ -21,8 +21,10 @@ ignore:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "packages/riverpod_cli"
- "packages/riverpod_generator"
- "packages/riverpod_lint"

comment:
layout: "reach,diff,flags,tree"
behavior: default
require_changes: no
require_changes: false
15 changes: 15 additions & 0 deletions packages/riverpod_generator/README.md
Expand Up @@ -156,6 +156,21 @@ class Home extends ConsumerWidget {
}
```

## Global configuration
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this being in the root examples folder.
it probably should be in the riverpod_generator folder

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes more sense. I've just changed


You can change provider name suffix in the build.yaml file:

```yaml
targets:
$default:
builders:
riverpod_generator:
options:
# Could be changed to "Pod", such that riverpod_generator
# would generate "countPod" instead of "countProvider"
provider_name_suffix: "Provider" # (default)
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
```

[family]: https://riverpod.dev/docs/concepts/modifiers/family
[provider]: https://github.com/rrousselGit/provider
[riverpod]: https://github.com/rrousselGit/riverpod
Expand Down
6 changes: 6 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/.gitignore
@@ -0,0 +1,6 @@
# Files and directories created by pub.
.dart_tool/
.packages

# Conventional directory for build output.
build/
23 changes: 23 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/README.md
@@ -0,0 +1,23 @@
# Pub

Install needed global packages

```bash
cd riverpod
dart pub global activate melos
```

Build the riverpod packages

```bash
fluttter pub get
```

## Start the tests

```bash
cd examples/build_yaml
flutter pub run build_runner build

flutter test
```
6 changes: 6 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/build.yaml
@@ -0,0 +1,6 @@
targets:
$default:
builders:
riverpod_generator:
options:
provider_name_suffix: 'Pod'
29 changes: 29 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/lib/main.dart
@@ -0,0 +1,29 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'main.g.dart';

@riverpod
int count(CountRef ref) {
return 1;
}

@riverpod
FutureOr<int> countFuture(CountFutureRef ref) {
return 1;
}

@riverpod
class CountNotifier extends _$CountNotifier {
@override
int build() {
return 1;
}
}

@riverpod
class CountAsyncNotifier extends _$CountAsyncNotifier {
@override
FutureOr<int> build() {
return 1;
}
}
87 changes: 87 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/lib/main.g.dart

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

16 changes: 16 additions & 0 deletions packages/riverpod_generator/integration/build_yaml/pubspec.yaml
@@ -0,0 +1,16 @@
name: build_yaml
description: A sample command-line application.
version: 1.0.0

environment:
sdk: ">=2.18.4 <3.0.0"

dependencies:
riverpod:
riverpod_annotation:

dev_dependencies:
build_runner: ^2.3.2
build_verify: ^3.0.0
riverpod_generator:
test: ^1.16.0
@@ -0,0 +1,8 @@
# melos_managed_dependency_overrides: riverpod,riverpod_annotation,riverpod_generator
dependency_overrides:
riverpod:
path: ../../../riverpod
riverpod_annotation:
path: ../../../riverpod_annotation
riverpod_generator:
path: ../..
@@ -0,0 +1,21 @@
import 'package:build_verify/build_verify.dart';
import 'package:build_yaml/main.dart';
import 'package:test/test.dart';

void main() {
test(
'ensure_build',
() => expectBuildClean(
packageRelativeDirectory:
'packages/riverpod_generator/integration/build_yaml',
),
timeout: const Timeout(Duration(minutes: 1)),
);

test('provider names', () {
expect(countPod.name, 'countPod');
expect(countFuturePod.name, 'countFuturePod');
expect(countNotifierPod.name, 'countNotifierPod');
expect(countAsyncNotifierPod.name, 'countAsyncNotifierPod');
});
}
26 changes: 24 additions & 2 deletions packages/riverpod_generator/lib/src/models.dart
Expand Up @@ -16,6 +16,8 @@ enum ProviderType {
asyncNotifier,
}

const _defaultProviderNameSuffix = 'Provider';

class Data {
Data.function({
required this.rawName,
Expand All @@ -29,6 +31,7 @@ class Data {
required this.providerDoc,
required this.createElement,
required this.createAst,
required this.buildYamlOptions,
}) : notifierName = null;

Data.notifier({
Expand All @@ -43,6 +46,7 @@ class Data {
required this.providerDoc,
required this.createElement,
required this.createAst,
required this.buildYamlOptions,
}) : functionName = null;

final ExecutableElement createElement;
Expand All @@ -57,6 +61,7 @@ class Data {
final List<ParameterElement> parameters;
final bool keepAlive;
final String providerDoc;
final BuildYamlOptions buildYamlOptions;

String get hashFunctionName => '_\$${rawName}Hash';

Expand All @@ -68,13 +73,13 @@ class Data {
/// foo -> fooProvider
/// Foo -> fooProvider
String get providerName {
return '${rawName.lowerFirst}Provider';
return '${rawName.lowerFirst}$_nameSuffix';
}

/// foo -> FooProvider
/// Foo -> FooProvider
String get providerTypeNameImpl {
return '${rawName.titled}Provider';
return '${rawName.titled}$_nameSuffix';
}

/// foo -> FooFamily
Expand Down Expand Up @@ -105,6 +110,9 @@ class Data {

bool get isNotifier => functionName == null;

String get _nameSuffix =>
buildYamlOptions.providerNameSuffix ?? _defaultProviderNameSuffix;

String notifierType({bool generics = true}) {
final trailing = generics ? '<$valueDisplayType>' : '';
final leading = keepAlive ? '' : 'AutoDispose';
Expand Down Expand Up @@ -253,6 +261,20 @@ class GlobalData {
final ElementHash hash = ElementHash();
}

class BuildYamlOptions {
BuildYamlOptions({
this.providerNameSuffix,
});

factory BuildYamlOptions.fromMap(Map<String, dynamic> map) {
return BuildYamlOptions(
providerNameSuffix: map['provider_name_suffix'] as String?,
);
}

final String? providerNameSuffix;
}

extension on String {
String get titled {
return replaceFirstMapped(
Expand Down
7 changes: 5 additions & 2 deletions packages/riverpod_generator/lib/src/riverpod_generator.dart
Expand Up @@ -22,9 +22,10 @@ const riverpodTypeChecker = TypeChecker.fromRuntime(Riverpod);
@immutable
// ignore: invalid_use_of_internal_member
class RiverpodGenerator extends ParserGenerator<GlobalData, Data, Riverpod> {
RiverpodGenerator(this.configs);
RiverpodGenerator(Map<String, Object?> mapConfig)
: config = BuildYamlOptions.fromMap(mapConfig);

final Map<String, Object?> configs;
final BuildYamlOptions config;

@override
GlobalData parseGlobalData(LibraryElement library) {
Expand Down Expand Up @@ -78,6 +79,7 @@ class RiverpodGenerator extends ParserGenerator<GlobalData, Data, Riverpod> {
parameters: element.parameters.skip(1).toList(),
valueDisplayType:
_getUserModelType(element).getDisplayString(withNullability: true),
buildYamlOptions: config,
);
}

Expand Down Expand Up @@ -129,6 +131,7 @@ class RiverpodGenerator extends ParserGenerator<GlobalData, Data, Riverpod> {
parameters: buildMethod.parameters,
valueDisplayType: _getUserModelType(buildMethod)
.getDisplayString(withNullability: true),
buildYamlOptions: config,
);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/riverpod_generator/test/build_yaml_config_test.dart
@@ -0,0 +1,10 @@
import 'package:riverpod_generator/src/models.dart';
import 'package:test/test.dart';

void main() {
test('custom suffix', () async {
const map = {'provider_name_suffix': 'Pod'};
final options = BuildYamlOptions.fromMap(map);
expect(options.providerNameSuffix, 'Pod');
});
}