Skip to content

Commit 82d5579

Browse files
committed
feat(nx-flutter): add support for flutter pub related sub-commands
Closes #234
1 parent 1a40ba7 commit 82d5579

File tree

4 files changed

+98
-28
lines changed

4 files changed

+98
-28
lines changed

packages/nx-flutter/README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,47 +133,63 @@ Once your app is generated, you can now use **executors** to manage it.
133133
134134
Here the list of available executors<sup>1</sup>:
135135
136-
| Executor | Arguments | Description |
137-
| ------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
138-
| `analyze` | _see `flutter help analyze`_ | Analyze the project's Dart code |
139-
| `assemble` | _see `flutter help assemble`_ | Assemble and build Flutter resources |
140-
| `attach` | _see `flutter help attach`_ | Attach to a running app |
141-
| `build-aar` | _see `flutter help build aar`_ | Build a repository containing an AAR and a POM file |
142-
| `build-apk` | _see `flutter help build apk`_ | Build an Android APK file from your app |
143-
| `build-appbundle` | _see `flutter help build appbundle`_ | Build an Android App Bundle file from your app |
144-
| `build-bundle` | _see `flutter help build bundle`_ | Build the Flutter assets directory from your app |
145-
| `build-ios` | _see `flutter help build ios`_ | Build an iOS application bundle (Mac OS X host only) |
146-
| `buildIosframework` | _see `flutter help build ios-framework`_ | Produces a .framework directory for a Flutter module and its plugins for integration into existing, plain Xcode projects |
147-
| `build-ipa` | _see `flutter help build ipa`_ | Build an iOS archive bundle (Mac OS X host only) |
148-
| `clean` | _see `flutter help clean`_ | Delete the `build/` and `dart_tool/` directories |
149-
| `drive` | _see `flutter help drive`_ | Run integration tests for the project on an attached device or emulator |
150-
| `format` | _see `dart help format`_ | Format one or more Dart files |
151-
| `gen-l10n` | _see `flutter help gen-l10n`_ | Generate localizations for the current project |
152-
| `install` | _see `flutter help install`_ | Install a Flutter app on an attached device |
153-
| `run` | _see `flutter help run`_ | Run your Flutter app on an attached device |
154-
| `screenshot` | _see `flutter help screenshot`_ | Take a screenshot from a connected device |
155-
| `symbolize` | _see `flutter help symbolize`_ | Symbolize a stack trace from an AOT-compiled Flutter app |
156-
| `test` | _see `flutter help test`_ | Run Flutter unit tests for the current project |
136+
| Executor | Arguments | Description |
137+
| ------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
138+
| `analyze` | _see `flutter help analyze`_ | Analyze the project's Dart code |
139+
| `assemble` | _see `flutter help assemble`_ | Assemble and build Flutter resources |
140+
| `attach` | _see `flutter help attach`_ | Attach to a running app |
141+
| `build-aar` | _see `flutter help build aar`_ | Build a repository containing an AAR and a POM file |
142+
| `build-apk` | _see `flutter help build apk`_ | Build an Android APK file from your app |
143+
| `build-appbundle` | _see `flutter help build appbundle`_ | Build an Android App Bundle file from your app |
144+
| `build-bundle` | _see `flutter help build bundle`_ | Build the Flutter assets directory from your app |
145+
| `build-ios` | _see `flutter help build ios`_ | Build an iOS application bundle (Mac OS X host only) |
146+
| `buildIosframework` | _see `flutter help build ios-framework`_ | Produces a .framework directory for a Flutter module and its plugins for integration into existing, plain Xcode projects |
147+
| `build-ipa` | _see `flutter help build ipa`_ | Build an iOS archive bundle (Mac OS X host only) |
148+
| `clean` | _see `flutter help clean`_ | Delete the `build/` and `dart_tool/` directories |
149+
| `drive` | _see `flutter help drive`_ | Run integration tests for the project on an attached device or emulator |
150+
| `format` | _see `dart help format`_ | Format one or more Dart files |
151+
| `gen-l10n` | _see `flutter help gen-l10n`_ | Generate localizations for the current project |
152+
| `install` | _see `flutter help install`_ | Install a Flutter app on an attached device |
153+
| `run` | _see `flutter help run`_ | Run your Flutter app on an attached device |
154+
| `screenshot` | _see `flutter help screenshot`_ | Take a screenshot from a connected device |
155+
| `symbolize` | _see `flutter help symbolize`_ | Symbolize a stack trace from an AOT-compiled Flutter app |
156+
| `test` | _see `flutter help test`_ | Run Flutter unit tests for the current project |
157+
| `pub-get` | _see `flutter help pub get`_ | Fetches and installs the dependencies listed in your `pubspec.yaml` file. |
158+
| `pub-upgrade` | _see `flutter help pub upgrade`_ | Upgrades dependencies to the latest versions that match the constraints in `pubspec.yaml`. |
159+
| `pub-downgrade` | _see `flutter help pub downgrade`_ | Downgrades dependencies to the earliest versions that match the constraints in `pubspec.yaml`. |
160+
| `pub-outdated` | _see `flutter help pub outdated`_ | Lists outdated dependencies, showing current, upgradable, and latest versions. |
161+
| `pub-add` | `--package=<pkg_name>` (required) _see `flutter help pub add`_ for the rest of options | Adds a new dependency to `pubspec.yaml` and fetches it. |
162+
| `pub-remove` | `--package=<pkg_name>` (required) _see `flutter help pub remove`_ for the rest of options | Removes a dependency from `pubspec.yaml` and updates the package list. |
163+
| `pub-run` | `--executable="<executable>"` (required) _see `flutter help pub run`_ for the rest of options | Runs an executable from one of your dependencies. |
164+
| `pub-publish` | _see `flutter help pub publish`_ | Publishes your package to the Dart package repository (`pub.dev`). |
165+
| `pub-deps` | _see `flutter help pub deps`_ | Displays a dependency graph of your package's dependencies. |
166+
| `pub-version` | _see `flutter help pub version`_ | Shows the current version of the Dart SDK. |
157167
158168
<sup>1</sup> : _Actual executors in your `workspace.json` will depend on the type of `flutter` project (`template`), target `platforms` that you choose to generate._
159169
160170
Each executor is based on an original project-level `flutter` command. The name is just **kebab-cased** to match executors' naming conventions.
161-
Besides, the arguments accepted by each executor, are the same as the original `flutter` command they are based upon, encapsulated
162-
under a generic `--args='...'` option.
171+
Besides, the addtional arguments accepted by each executor, are the same as the original `flutter` command they are based upon, and can be provided directly like in `--option=value`. Read this [guide](https://nx.dev/recipes/running-tasks/pass-args-to-commands#pass-args-when-running-the-command-in-the-terminal) for more about the syntax to pass arguments along.
163172
164173
For example:
165174
166175
```
167176
$ flutter gen-l10n --header "/// my header"
177+
$ flutter pub add builder_runner
178+
$ flutter pub run build_runner build
168179

169-
becomes 👉🏾
180+
become 👉🏾
170181

171-
$ nx gen-l10n your-flutterapp --args='--header="/// my header"'
172-
```
182+
$ nx gen-l10n your-flutterapp --header="/// my header"
183+
$ nx pub-add your-flutterapp --package=build_runner
184+
$ nx pub-run your-flutterapp --executable="build_runner build" // note the "" around the executable command, because it has spaces
185+
186+
or
173187

174-
> Note that the original `flutter` command name (`gen-l10n`) has been camelcased for creating its `nx-flutter` equivalent (`gen-l10n`)
188+
$ nx run your-flutterapp:gen-l10n --header="/// my header"
189+
$ nx run your-flutterapp:pub-add --package=build_runner
190+
$ nx run your-flutterapp:pub-run --executabe="build_runner build" // note the "" around the executable command, because it has spaces
175191

176-
> Note that the arguments of the original `flutter` command are wrapped under `--args='...'` option in the `nx-flutter` equivalent
192+
```
177193
178194
## Compatibility with Nx
179195

packages/nx-flutter/src/generators/project/generator.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ describe('application generator', () => {
254254
"formatTargetName": "format",
255255
"genL10nTargetName": "gen-l10n",
256256
"installTargetName": "install",
257+
"pubAddTargetName": "pub-add",
258+
"pubDepsTargetName": "pub-deps",
259+
"pubDowngradeTargetName": "pub-downgrade",
260+
"pubGetTargetName": "pub-get",
261+
"pubOutdatedTargetName": "pub-outdated",
262+
"pubPublishTargetName": "pub-publish",
263+
"pubRemoveTargetName": "pub-remove",
264+
"pubRunTargetName": "pub-run",
265+
"pubUpgradeTargetName": "pub-upgrade",
266+
"pubVersionTargetName": "pub-version",
257267
"runTargetName": "run",
258268
"testTargetName": "test",
259269
},

packages/nx-flutter/src/graph/plugin.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ export interface NxFlutterPluginOptions {
1919
buildIosTargetName?: string;
2020
buildIosFrameworkTargetName?: string;
2121
buildIpaTargetName?: string;
22+
23+
// pub related sub-commands
24+
pubGetTargetName?: string;
25+
pubUpgradeTargetName?: string;
26+
pubDowngradeTargetName?: string;
27+
pubOutdatedTargetName?: string;
28+
pubAddTargetName?: string;
29+
pubRemoveTargetName?: string;
30+
pubRunTargetName?: string;
31+
//pubCacheTargetName?: string;
32+
//pubGlobalTargetName?: string;
33+
pubPublishTargetName?: string;
34+
pubDepsTargetName?: string;
35+
pubVersionTargetName?: string;
2236
}
2337

2438
export function normalizePluginOptions(
@@ -49,5 +63,19 @@ export function normalizePluginOptions(
4963
buildIosFrameworkTargetName:
5064
options.buildIosFrameworkTargetName ?? 'build-ios-framework',
5165
buildIpaTargetName: options.buildIpaTargetName ?? 'build-ipa',
66+
67+
// pub related sub-commands
68+
pubGetTargetName: options.pubGetTargetName ?? 'pub-get',
69+
pubUpgradeTargetName: options.pubUpgradeTargetName ?? 'pub-upgrade',
70+
pubDowngradeTargetName: options.pubDowngradeTargetName ?? 'pub-downgrade',
71+
pubOutdatedTargetName: options.pubOutdatedTargetName ?? 'pub-outdated',
72+
pubAddTargetName: options.pubAddTargetName ?? 'pub-add',
73+
pubRemoveTargetName: options.pubRemoveTargetName ?? 'pub-remove',
74+
pubRunTargetName: options.pubRunTargetName ?? 'pub-run',
75+
//pubCacheTargetName: options.pubCacheTargetName ?? 'pub-cache',
76+
//pubGlobalTargetName: options.pubGlobalTargetName ?? 'pub-global',
77+
pubPublishTargetName: options.pubPublishTargetName ?? 'pub-publish',
78+
pubDepsTargetName: options.pubDepsTargetName ?? 'pub-deps',
79+
pubVersionTargetName: options.pubVersionTargetName ?? 'pub-version',
5280
};
5381
}

packages/nx-flutter/src/utils/plugin-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ function getProjectTypeAndTargets(
7171
},
7272
{ key: pluginOptions.testTargetName, value: 'test' },
7373
{ key: pluginOptions.genL10nTargetName, value: 'gen-l10n' },
74+
75+
{ key: pluginOptions.pubGetTargetName, value: 'pub get' },
76+
{ key: pluginOptions.pubUpgradeTargetName, value: 'pub upgrade' },
77+
{ key: pluginOptions.pubDowngradeTargetName, value: 'pub downgrade' },
78+
{ key: pluginOptions.pubOutdatedTargetName, value: 'pub outdated' },
79+
{ key: pluginOptions.pubAddTargetName, value: 'pub add {args.package}' },
80+
{
81+
key: pluginOptions.pubRemoveTargetName,
82+
value: 'pub remove {args.package}',
83+
},
84+
{ key: pluginOptions.pubRunTargetName, value: 'pub run {args.executable}' },
85+
//{ key: pluginOptions.pubCacheTargetName, value: 'pub cache' },
86+
//{ key: pluginOptions.pubGlobalTargetName, value: 'pub global' },
87+
{ key: pluginOptions.pubPublishTargetName, value: 'pub publish' },
88+
{ key: pluginOptions.pubDepsTargetName, value: 'pub deps' },
89+
{ key: pluginOptions.pubVersionTargetName, value: 'pub version' },
7490
];
7591

7692
if (template === 'app') {

0 commit comments

Comments
 (0)