Skip to content

Commit

Permalink
Merge pull request #270 from yumemi-inc/feature/improve_bricks
Browse files Browse the repository at this point in the history
featureを追加するbrickでriverpodやfreezedの依存を追加できるようにした
  • Loading branch information
K9i-0 committed Jun 25, 2024
2 parents bb3b80b + 1a38abb commit 65df999
Show file tree
Hide file tree
Showing 29 changed files with 215 additions and 40 deletions.
2 changes: 2 additions & 0 deletions mason.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ bricks:
path: tools/bricks/cores_package_flutter
cores_package_dart:
path: tools/bricks/cores_package_dart
features_package_core:
path: tools/bricks/features_package_core
features_package:
path: tools/bricks/features_package
brick:
Expand Down
2 changes: 1 addition & 1 deletion tools/bricks/features_package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 0.1.0
# 0.1.0+1

- TODO: Describe initial release.
48 changes: 18 additions & 30 deletions tools/bricks/features_package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,26 @@

[![Powered by Mason](https://img.shields.io/endpoint?url=https%3A%2F%2Ftinyurl.com%2Fmason-badge)](https://github.com/felangel/mason)

新しい feature パッケージを作成します.
featureパッケージを作成するbrickです。既存パッケージにriverpodやfreezedの依存を追加する際も使えます。

## Getting Started 🚀
_Generated by [mason][1] 🧱_

以下のコマンドを実行します。
## Getting Started 🚀

```shell
mason make features_package
```
This is a starting point for a new brick.
A few resources to get you started if this is your first brick template:

feature 名を尋ねられるため、回答します。
回答すると、以下の実行例のような構成のパッケージが `packages/features` 配下に作成されます。
- [Official Mason Documentation][2]
- [Code generation with Mason Blog][3]
- [Very Good Livestream: Felix Angelov Demos Mason][4]
- [Flutter Package of the Week: Mason][5]
- [Observable Flutter: Building a Mason brick][6]
- [Meet Mason: Flutter Vikings 2022][7]

```text
❯ mason make features_package
? What is feature name? (example: foo_bar) foo_bar
✓ Generated 16 files. (72ms)
created packages/features/foo_bar/test/foo_bar_test.dart
created packages/features/foo_bar/l10n.yaml
created packages/features/foo_bar/.metadata
created packages/features/foo_bar/README.md
created packages/features/foo_bar/pubspec.yaml
created packages/features/foo_bar/.gitignore
created packages/features/foo_bar/lib/ui.dart
created packages/features/foo_bar/lib/l10n.dart
created packages/features/foo_bar/lib/src/ui/components/foo_bar_text.dart
created packages/features/foo_bar/lib/src/ui/pages/foo/components/foo_text.dart
created packages/features/foo_bar/lib/src/ui/pages/foo/foo_page.dart
created packages/features/foo_bar/lib/src/ui/pages/bar/bar_page.dart
created packages/features/foo_bar/lib/src/ui/pages/bar/components/bar_text.dart
created packages/features/foo_bar/lib/src/l10n/features_foo_bar_ja.arb
created packages/features/foo_bar/lib/src/l10n/features_foo_bar_en.arb
created packages/features/foo_bar/build.yaml
✓ Compiled post_gen.dart (2.4s)
```
[1]: https://github.com/felangel/mason
[2]: https://docs.brickhub.dev
[3]: https://verygood.ventures/blog/code-generation-with-mason
[4]: https://youtu.be/G4PTjA6tpTU
[5]: https://youtu.be/qjA0JFiPMnQ
[6]: https://youtu.be/o8B1EfcUisw
[7]: https://youtu.be/LXhgiF5HiQg
12 changes: 11 additions & 1 deletion tools/bricks/features_package/brick.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: features_package
description: A new brick created with the Mason CLI.
description: featureパッケージを作成するbrickです。既存パッケージにriverpodやfreezedの依存を追加する際も使えます。

version: 0.1.0

Expand All @@ -11,3 +11,13 @@ vars:
type: string
description: Feature name
prompt: 'What is feature name? (example: foo_bar)'
use_riverpod:
type: boolean
description: Whether to use riverpod
default: true
prompt: Are you need riverpod?
use_freezed:
type: boolean
description: Whether to use freezed
default: true
prompt: Are you need freezed?
98 changes: 92 additions & 6 deletions tools/bricks/features_package/hooks/post_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,98 @@ import 'dart:io';
import 'package:mason/mason.dart';

void run(HookContext context) {
Process.runSync(
'melos',
['bs'],
final featureName = context.vars['feature_name'] as String;
final useRiverpod = context.vars['use_riverpod'] as bool;
final useFreezed = context.vars['use_freezed'] as bool;

_addDependency(
featureName: featureName,
useRiverpod: useRiverpod,
useFreezed: useFreezed,
);
Process.runSync(
'melos',
['run', 'regenerate_by_using_gen_l10n'],

_replaceAnalysisOptions(
featureName: featureName,
useRiverpod: useRiverpod,
useFreezed: useFreezed,
);

if (useRiverpod || useFreezed) {
Process.runSync('melos', ['bs']);
}
}

void _addDependency({
required String featureName,
required bool useRiverpod,
required bool useFreezed,
}) {
if (useRiverpod) {
// 依存の追加
Process.runSync(
'dart',
[
'pub',
'add',
'flutter_hooks',
'hooks_riverpod',
'riverpod_annotation',
'dev:build_runner',
'dev:riverpod_generator',
'dev:riverpod_lint',
'dev:custom_lint'
],
workingDirectory: 'packages/features/$featureName',
);
}

if (useFreezed) {
// 依存の追加
Process.runSync(
'dart',
[
'pub',
'add',
'freezed_annotation',
'json_annotation',
'dev:build_runner',
'dev:freezed',
'dev:json_serializable',
],
workingDirectory: 'packages/features/$featureName',
);
}
}

void _replaceAnalysisOptions({
required String featureName,
required bool useRiverpod,
required bool useFreezed,
}) {
final filePath = 'packages/features/$featureName/analysis_options.yaml';

final sb = StringBuffer();
sb.write('''
include: package:yumemi_lints/flutter/3.22/recommended.yaml
analyzer:
''');

if (useRiverpod) {
sb.write('''
plugins:
# https://riverpod.dev/docs/introduction/getting_started#enabling-riverpod_lintcustom_lint
- custom_lint
''');
}
if (useFreezed) {
sb.write('''
errors:
# https://pub.dev/packages/freezed#install
invalid_annotation_target: ignore
''');
}

// 変更をファイルに書き込む
File(filePath).writeAsStringSync(sb.toString());
}
21 changes: 21 additions & 0 deletions tools/bricks/features_package/hooks/pre_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:io';

import 'package:mason/mason.dart';

void run(HookContext context) {
final featureName = context.vars['feature_name'] as String;

final isExists = Directory('packages/features/$featureName').existsSync();

if (isExists) {
return;
}

// パッケージがまだ存在しない場合は、features_package_core brickでパッケージを作成する
Process.runSync('mason', [
'make',
'features_package_core',
'--feature_name',
featureName,
]);
}
4 changes: 2 additions & 2 deletions tools/bricks/features_package/hooks/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: features_package_hooks

environment:
sdk: ^3.4.1
sdk: ">=2.12.0 <3.0.0"

dependencies:
mason: ">=0.1.0-dev.52 <0.1.0"
mason: ">=0.1.0-dev.56 <0.1.0"
3 changes: 3 additions & 0 deletions tools/bricks/features_package_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.0+1

- TODO: Describe initial release.
27 changes: 27 additions & 0 deletions tools/bricks/features_package_core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# features_package_core

[![Powered by Mason](https://img.shields.io/endpoint?url=https%3A%2F%2Ftinyurl.com%2Fmason-badge)](https://github.com/felangel/mason)

featureパッケージを作成するbrickです。features_packageが内部で使うことを想定しています。

_Generated by [mason][1] 🧱_

## Getting Started 🚀

This is a starting point for a new brick.
A few resources to get you started if this is your first brick template:

- [Official Mason Documentation][2]
- [Code generation with Mason Blog][3]
- [Very Good Livestream: Felix Angelov Demos Mason][4]
- [Flutter Package of the Week: Mason][5]
- [Observable Flutter: Building a Mason brick][6]
- [Meet Mason: Flutter Vikings 2022][7]

[1]: https://github.com/felangel/mason
[2]: https://docs.brickhub.dev
[3]: https://verygood.ventures/blog/code-generation-with-mason
[4]: https://youtu.be/G4PTjA6tpTU
[5]: https://youtu.be/qjA0JFiPMnQ
[6]: https://youtu.be/o8B1EfcUisw
[7]: https://youtu.be/LXhgiF5HiQg
13 changes: 13 additions & 0 deletions tools/bricks/features_package_core/brick.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: features_package_core
description: featureパッケージの主要部分を作成するためのbrickです。直接使うのではなく、features_package brickから呼ぶことを想定しています。

version: 0.1.0

environment:
mason: ">=0.1.0-dev.52 <0.1.0"

vars:
feature_name:
type: string
description: Feature name
prompt: 'What is feature name? (example: foo_bar)'
4 changes: 4 additions & 0 deletions tools/bricks/features_package_core/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dart_tool
.packages
pubspec.lock
build
14 changes: 14 additions & 0 deletions tools/bricks/features_package_core/hooks/post_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'dart:io';

import 'package:mason/mason.dart';

void run(HookContext context) {
Process.runSync(
'melos',
['bs'],
);
Process.runSync(
'melos',
['run', 'regenerate_by_using_gen_l10n'],
);
}
7 changes: 7 additions & 0 deletions tools/bricks/features_package_core/hooks/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: features_package_core_hooks

environment:
sdk: ^3.4.1

dependencies:
mason: ">=0.1.0-dev.52 <0.1.0"

0 comments on commit 65df999

Please sign in to comment.