Skip to content

Commit 8d040cc

Browse files
fix(flutter_plugin): throws PubspecNotFound exception (#199)
1 parent 75e8643 commit 8d040cc

File tree

7 files changed

+177
-149
lines changed

7 files changed

+177
-149
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ Usage: very_good create <output directory>
4444
[dart_pkg] Generate a reusable Dart package.
4545
[flutter_pkg] Generate a reusable Flutter package.
4646
[flutter_plugin] Generate a reusable Flutter federated plugin.
47+
--android The plugin supports the Android platform.
48+
--ios The plugin supports the iOS platform.
49+
--web The plugin supports the Web platform.
50+
--linux The plugin supports the Linux platform.
51+
--macos The plugin supports the macOS platform.
52+
--windows The plugin supports the Windows platform.
4753
```
4854

4955
### What's Included in Very Good Core? 📦

lib/src/cli/cli.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class _Cmd {
2626
return result;
2727
}
2828

29+
static Iterable<Future<ProcessResult>> runWhere({
30+
required Future<ProcessResult> Function(FileSystemEntity) run,
31+
required bool Function(FileSystemEntity) where,
32+
String cwd = '.',
33+
}) {
34+
return Directory(cwd).listSync(recursive: true).where(where).map(run);
35+
}
36+
2937
static void _throwIfProcessFailed(
3038
ProcessResult pr,
3139
String process,
@@ -46,3 +54,8 @@ class _Cmd {
4654
}
4755
}
4856
}
57+
58+
bool _isPubspec(FileSystemEntity entity) {
59+
if (entity is! File) return false;
60+
return p.basename(entity.path) == 'pubspec.yaml';
61+
}

lib/src/cli/dart_cli.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ part of 'cli.dart';
22

33
/// Dart CLI
44
class Dart {
5-
/// Determine whether dart is installed
5+
/// Determine whether dart is installed.
66
static Future<bool> installed() async {
77
try {
88
await _Cmd.run('dart', ['--version']);
@@ -13,7 +13,30 @@ class Dart {
1313
}
1414

1515
/// Apply all fixes (`dart fix --apply`).
16-
static Future<void> applyFixes() {
17-
return _Cmd.run('dart', ['fix', '--apply']);
16+
static Future<void> applyFixes({
17+
String cwd = '.',
18+
bool recursive = false,
19+
}) async {
20+
if (!recursive) {
21+
final pubspec = File(p.join(cwd, 'pubspec.yaml'));
22+
if (!pubspec.existsSync()) throw PubspecNotFound();
23+
24+
await _Cmd.run('dart', ['fix', '--apply'], workingDirectory: cwd);
25+
return;
26+
}
27+
28+
final processes = _Cmd.runWhere(
29+
run: (entity) => _Cmd.run(
30+
'dart',
31+
['fix', '--apply'],
32+
workingDirectory: entity.parent.path,
33+
),
34+
where: _isPubspec,
35+
cwd: cwd,
36+
);
37+
38+
if (processes.isEmpty) throw PubspecNotFound();
39+
40+
await Future.wait(processes);
1841
}
1942
}

lib/src/cli/flutter_cli.dart

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
part of 'cli.dart';
22

33
/// Thrown when `flutter packages get` or `flutter pub get`
4-
/// is exectuted without a pubspec.yaml
4+
/// is executed without a `pubspec.yaml`.
55
class PubspecNotFound implements Exception {}
66

77
/// Flutter CLI
88
class Flutter {
9+
/// Determine whether flutter is installed.
10+
static Future<bool> installed() async {
11+
try {
12+
await _Cmd.run('flutter', ['--version']);
13+
return true;
14+
} catch (_) {
15+
return false;
16+
}
17+
}
18+
919
/// Install flutter dependencies (`flutter packages get`).
1020
static Future<void> packagesGet({
1121
String cwd = '.',
@@ -38,16 +48,7 @@ class Flutter {
3848
);
3949
}
4050

41-
/// Determine whether flutter is installed
42-
static Future<bool> installed() async {
43-
try {
44-
await _Cmd.run('flutter', ['--version']);
45-
return true;
46-
} catch (_) {
47-
return false;
48-
}
49-
}
50-
51+
/// Install dependencies in directories with a `pubspec.yaml`.
5152
static Future<void> _installPackages({
5253
required Future<ProcessResult> Function(String cwd) cmd,
5354
required String cwd,
@@ -61,7 +62,7 @@ class Flutter {
6162
return;
6263
}
6364

64-
final processes = _process(
65+
final processes = _Cmd.runWhere(
6566
run: (entity) => cmd(entity.parent.path),
6667
where: _isPubspec,
6768
cwd: cwd,
@@ -71,17 +72,4 @@ class Flutter {
7172

7273
await Future.wait(processes);
7374
}
74-
75-
static Iterable<Future<ProcessResult>> _process({
76-
required Future<ProcessResult> Function(FileSystemEntity) run,
77-
required bool Function(FileSystemEntity) where,
78-
String cwd = '.',
79-
}) {
80-
return Directory(cwd).listSync(recursive: true).where(where).map(run);
81-
}
82-
}
83-
84-
bool _isPubspec(FileSystemEntity entity) {
85-
if (entity is! File) return false;
86-
return p.basename(entity.path) == 'pubspec.yaml';
8775
}

lib/src/commands/create.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ class CreateCommand extends Command<int> {
255255
}
256256
}
257257

258-
/// Extension on String to parse booleans.
259-
extension StringToBoolX on String {
260-
/// Returns if `String` is equal to `true`.
258+
extension on String {
261259
bool parseBool() => toLowerCase() == 'true';
262260
}

0 commit comments

Comments
 (0)