Skip to content

feat: Add scripts support and merge hooks to scripts #13

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

Merged
merged 2 commits into from
Jan 14, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ Also, the Flutter Tool does not give chance to do extra work during command runn

To solve these (and other related) problems, flutterw is created.

**Flutterw wraps the flutter tool to support command hooks system.**
**`pre` and `post` hooks enable you to do extra work before and after command running**
**and `command` hooks enable you to customize command behavior.**
**Flutterw wraps flutter tool to support scripts and command hooks.**
**Hooks are pre, post and command scripts.**
**`pre` and `post` scripts enable you to do extra work before and after running command**
**and `command` scripts enable you to customize command behavior.**

### What can Flutterw do?

- Dispatch arguments to flutter tool when no command hook configured.
- `pre` hooks are executed before running command.
- `post` hooks are executed after running command.
- `command` hooks are executed to replace original command.
- Hooks can also be packages in [Pub](https://pub.dev)
- `pre` scripts are executed before running command.
- `post` scripts are executed after running command.
- `command` scripts are executed to replace original command.
- Command scripts can be packages in [Pub](https://pub.dev/packages?q=flutterw)
- packages created by flutterw author
- [flutterw_clean](https://pub.dev/packages/flutterw_clean)
- [flutterw_hook](https://pub.dev/packages/flutterw_hook)
- packages created by other developers.
- Add custom commands to `flutterw` dart package.
- Add custom to `flutterw`.

## Install

Expand Down
3 changes: 1 addition & 2 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"sidebar": [
["Overview", "/"],
["Getting Started", "/getting-started"],
["Hooks", "/hooks"],
["Adding Custom Command", "/adding-custom-command"]
["Command Hooks", "/command-hooks"]
]
}
34 changes: 0 additions & 34 deletions docs/adding-custom-command.mdx

This file was deleted.

53 changes: 53 additions & 0 deletions docs/command-hooks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Command Hooks
description: "Learn more about all the command hooks system in Flutterw."
---

# Command Hook Script

A command hook is a script with name of flutter builtin commands. For example:

```yaml
scripts:
clean: flutter pub run flutterw_clean
```
When running `flutterw clean`, `flutter pub run flutterw_clean` will be executed instead.

## Pre & Post Command Hook Script

Each Flutter command can be hooked with `pre:command` and `post:comand`:

```yaml
scripts:
pre:clean: echo 'pre clean'
post:clean: echo 'post clean'
```
When running `flutterw clean`, `pre:clean` will be executed first, then the `clean` command runs,
and `post:clean` will be executed finally.

## Sub Command Hook

For sub command, hook name is command parts joined with colon(`:`)

```yaml
scripts:
pre:build:aar: # This is pre hook for 'flutterw build aar'
build:aar: # This is command hook 'flutterw build aar'
post:build:aar: # This is post hook for 'flutterw build aar'
```

# Script Args

Scripts don't take arguments from running command by default.

If a command hook script needs to use the command arguments, use a `<args>` placeholder.

For example:

```yaml
scripts:
pre:build:
- dart ./scripts/pre_build.dart <args> # arguments passed to build command will be here.
post:build:
- dart ./scripts/post_build.dart <args> # arguments passed to build command will be here.
```
47 changes: 18 additions & 29 deletions docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,34 @@ Flutterw can be installed as a global package via [pub.dev](https://pub.dev/):
dart pub global activate flutterw
```

### Setup
## Setup Scripts

To set up your project to use Flutterw, create a `pubspec.yaml` file in the root of the project.
After installed, flutterw can be used just as flutter tool.

Within the `pubspec.yaml` file, add `hooks` fields:
To use flutterw with custom scripts, just add `scripts` fields within the `pubspec.yaml` file:

```yaml
hooks:
pre:clean:
- echo 'before clean'
clean:
- rm -fr .dart_tool
post:clean:
- echo 'after clean'
```

The `pre:command` hook scripts will be executed in order **before** command running.

The `command` hook scripts will be executed in order to replace original flutter command.

The `post:command` hook scripts will be executed in order **after** command running.
scripts:
generate: flutter pub run build_runner build --delete-conflicting-outputs

```

## Next steps

Besides shell scripts, you can set the took to a package listed in `dev_dependencies`.

Flutterw will internally tranlate the package to `dart pub run package`.
Then running `flutterw generate` will execute `flutter pub run build_runner build --delete-conflicting-outputs`.

For example, your `pubspec.yaml` has `flutterw_clean` in `dev_dependencies`:
A Script's value can be multi executable string joined with `&&`, or just a spearated list.

```yaml
dev_dependencies:
flutter_clean: any
scripts:
pull: git pull && git submodule update --init --recursive
```
Then set hook `clean` to package `flutter_clean` in `pubspec.yaml`:
or
```yaml
hooks:
clean: flutterw_clean
scripts:
pull:
- git pull
- git submodule update --init --recursive
```
When running `flutterw pull`, this two scripts will be executable by in roder.


When running `flutterw clean`, package `flutterw_clean` will be executed.
**Notice: scripts' names (eg: generate and pull) must not be the same with flutter command name, or they will be considered as a command hooks.**
69 changes: 0 additions & 69 deletions docs/hooks.mdx

This file was deleted.

9 changes: 5 additions & 4 deletions docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ To solve these (and other related) problems, flutterw is created.
Features include:

- Dispatch arguments to flutter tool when no command hook configured.
- `pre` hooks are executed before running command.
- `post` hooks are executed after running command.
- `command` hooks are executed to replace original command.
- Hooks can also be packages in [Pub](https://pub.dev/packages?q=flutterw)
- `pre` scripts are executed before running command.
- `post` scripts are executed after running command.
- `command` scripts are executed to replace original command.
- Command scripts can be packages in [Pub](https://pub.dev/packages?q=flutterw)
- packages created by flutterw author
- [flutterw_clean](https://pub.dev/packages/flutterw_clean)
- [flutterw_hook](https://pub.dev/packages/flutterw_hook)
- packages created by other developers.
- Add custom to `flutterw`.

## Projects using Flutterw

Expand Down
21 changes: 11 additions & 10 deletions packages/flutterw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ Also, the Flutter Tool does not give chance to do extra work during command runn

To solve these (and other related) problems, flutterw is created.

**Flutterw wraps the flutter tool to support command hooks system.**
**`pre` and `post` hooks enable you to do extra work before and after command running**
**and `command` hooks enable you to customize command behavior.**
**Flutterw wraps flutter tool to support scripts and command hooks.**
**Hooks are pre, post and command scripts.**
**`pre` and `post` scripts enable you to do extra work before and after running command**
**and `command` scripts enable you to customize command behavior.**

## What can Flutterw do?

- Dispatch arguments to flutter tool when no command hook configured.
- `pre` hooks are executed before running command.
- `post` hooks are executed after running command.
- `command` hooks are executed to replace original command.
- Hooks can also be packages in [Pub](https://pub.dev/packages?q=flutterw)
- `pre` scripts are executed before running command.
- `post` scripts are executed after running command.
- `command` scripts are executed to replace original command.
- Command scripts can be packages in [Pub](https://pub.dev/packages?q=flutterw)
- packages created by flutterw author
- [flutterw_clean](https://pub.dev/packages/flutterw_clean)
- [flutterw_hook](https://pub.dev/packages/flutterw_hook)
- packages created by other developers.
- Add custom commands to `flutterw` dart package.
- Add custom to `flutterw`.

## Who is using Flutterw?

Expand All @@ -60,7 +61,7 @@ Full commands list and args can be viewed by running flutterw -h.
```
> flutterw -h

flutterw wraps flutter tool with advanced usage.
flutterw wraps flutter with scripts and command hooks support

Usage: flutterw <command> [arguments]

Expand All @@ -72,7 +73,7 @@ Available commands:

Run "flutterw help <command>" for more information about a command.

And use flutterw as flutter to enable hooks and plugins:
And use flutterw as flutter with scripts and command hooks support:
flutterw doctor
flutterw clean
flutterw pub get
Expand Down
2 changes: 1 addition & 1 deletion packages/flutterw/bin/flutterw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Future<void> main(List<String> args) async {
final config = file.existsSync()
? FlutterwConfig.fromFile(file)
: FlutterwConfig.empty();
await FlutterwRunner(config: config).run(args);
await FlutterwRunner(scripts: config.scripts).run(args);
}
4 changes: 2 additions & 2 deletions packages/flutterw/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ homepage:
environment:
sdk: '>=2.12.0 <3.0.0'

hooks:
scripts:
pre:clean:
- echo 'task 1'
- echo 'task 2'
clean: flutterw_clean
clean: flutter pub run flutterw_clean
post:clean:
- echo 'task 3'
- echo 'task 4'
Expand Down
1 change: 0 additions & 1 deletion packages/flutterw/lib/flutterw.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export 'src/config.dart';
export 'src/hook.dart';
export 'src/runner.dart';
Loading