Skip to content

Commit

Permalink
fix: flutter_gen breaks current template (#1002)
Browse files Browse the repository at this point in the history
* fix: `flutter_gen` breaks current template

* fix: `flutter_gen` breaks current template

* fix: `flutter_gen` breaks current template

* Update lib/src/commands/create/templates/post_generate_actions.dart

Co-authored-by: Alejandro Santiago <dev@alestiago.com>

* Update lib/src/commands/create/templates/post_generate_actions.dart

Co-authored-by: Alejandro Santiago <dev@alestiago.com>

---------

Co-authored-by: Alejandro Santiago <dev@alestiago.com>
  • Loading branch information
wolfenrain and alestiago authored Apr 24, 2024
1 parent 74a8047 commit 35f7866
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 155 deletions.
44 changes: 44 additions & 0 deletions lib/src/cli/dart_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,50 @@ class Dart {
}
}

/// Install dart dependencies (`dart pub get`).
static Future<bool> pubGet({
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
final initialCwd = cwd;

final result = await _runCommand(
cmd: (cwd) async {
final relativePath = p.relative(cwd, from: initialCwd);
final path =
relativePath == '.' ? '.' : '.${p.context.separator}$relativePath';

final installProgress = logger.progress(
'Running "dart pub get" in $path',
);

try {
await _verifyGitDependencies(cwd, logger: logger);
} catch (_) {
installProgress.fail();
rethrow;
}

try {
return await _Cmd.run(
'dart',
['pub', 'get'],
workingDirectory: cwd,
logger: logger,
);
} finally {
installProgress.complete();
}
},
cwd: cwd,
recursive: recursive,
ignore: ignore,
);
return result.every((e) => e.exitCode == ExitCode.success.code);
}

/// Apply all fixes (`dart fix --apply`).
static Future<void> applyFixes({
required Logger logger,
Expand Down
36 changes: 8 additions & 28 deletions lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ part of 'cli.dart';

const _testOptimizerFileName = '.test_optimizer.dart';

/// Thrown when `flutter packages get` or `flutter pub get`
/// is executed without a `pubspec.yaml`.
/// Thrown when `flutter pub get` is executed without a `pubspec.yaml`.
class PubspecNotFound implements Exception {}

/// {@template coverage_not_met}
Expand Down Expand Up @@ -77,23 +76,23 @@ class Flutter {
}
}

/// Install flutter dependencies (`flutter packages get`).
static Future<void> packagesGet({
/// Install dart dependencies (`flutter pub get`).
static Future<bool> pubGet({
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
final initialCwd = cwd;

await _runCommand(
final result = await _runCommand(
cmd: (cwd) async {
final relativePath = p.relative(cwd, from: initialCwd);
final path =
relativePath == '.' ? '.' : '.${p.context.separator}$relativePath';

final installProgress = logger.progress(
'Running "flutter packages get" in $path ',
'Running "flutter pub get" in $path ',
);

try {
Expand All @@ -104,9 +103,9 @@ class Flutter {
}

try {
await _Cmd.run(
return await _Cmd.run(
'flutter',
['packages', 'get'],
['pub', 'get'],
workingDirectory: cwd,
logger: logger,
);
Expand All @@ -118,26 +117,7 @@ class Flutter {
recursive: recursive,
ignore: ignore,
);
}

/// Install dart dependencies (`flutter pub get`).
static Future<void> pubGet({
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
await _runCommand(
cmd: (cwd) => _Cmd.run(
'flutter',
['pub', 'get'],
workingDirectory: cwd,
logger: logger,
),
cwd: cwd,
recursive: recursive,
ignore: ignore,
);
return result.every((e) => e.exitCode == ExitCode.success.code);
}

/// Run tests (`flutter test`).
Expand Down
3 changes: 1 addition & 2 deletions lib/src/cli/git_cli.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
part of 'cli.dart';

/// {@template unreachable_git_dependency}
/// Thrown when `flutter packages get` or `flutter pub get`
/// encounters an unreachable git dependency.
/// Thrown when `flutter pub get` encounters an unreachable git dependency.
/// {@endtemplate}
class UnreachableGitDependency implements Exception {
/// {@macro unreachable_git_dependency}
Expand Down
33 changes: 20 additions & 13 deletions lib/src/commands/create/templates/post_generate_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@ import 'package:mason/mason.dart';
import 'package:universal_io/io.dart';
import 'package:very_good_cli/src/cli/cli.dart';

/// Runs `flutter pub get` in the [outputDir].
Future<void> installDartPackages(
/// Runs `dart pub get` in the [outputDir].
///
/// Completes with `true` is the execution was successful, `false` otherwise.
Future<bool> installDartPackages(
Logger logger,
Directory outputDir,
) async {
final isFlutterInstalled = await Flutter.installed(logger: logger);
if (isFlutterInstalled) {
final installDependenciesProgress = logger.progress(
'Running "flutter pub get" in ${outputDir.path}',
Directory outputDir, {
bool recursive = false,
}) async {
final isDartInstalled = await Dart.installed(logger: logger);
if (isDartInstalled) {
return Dart.pubGet(
cwd: outputDir.path,
recursive: recursive,
logger: logger,
);
await Flutter.pubGet(cwd: outputDir.path, logger: logger);
installDependenciesProgress.complete();
}
return false;
}

/// Runs `flutter packages get` in the [outputDir].
Future<void> installFlutterPackages(
/// Runs `flutter pub get` in the [outputDir].
///
/// Completes with `true` is the execution was successful, `false` otherwise.
Future<bool> installFlutterPackages(
Logger logger,
Directory outputDir, {
bool recursive = false,
}) async {
final isFlutterInstalled = await Flutter.installed(logger: logger);
if (isFlutterInstalled) {
await Flutter.packagesGet(
return Flutter.pubGet(
cwd: outputDir.path,
recursive: recursive,
logger: logger,
);
}
return false;
}

/// Runs `dart fix --apply` in the [outputDir].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class VeryGoodCoreTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installFlutterPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installFlutterPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger, outputDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class VeryGoodDartCLITemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installDartPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installDartPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class DartPkgTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installDartPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installDartPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class VeryGoodFlameGameTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installDartPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installFlutterPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class FlutterPkgTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installFlutterPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installFlutterPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class FlutterPluginTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installFlutterPackages(logger, outputDir, recursive: true);
await applyDartFixes(logger, outputDir, recursive: true);
if (await installFlutterPackages(logger, outputDir, recursive: true)) {
await applyDartFixes(logger, outputDir, recursive: true);
}
_logSummary(logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class VeryGoodWearAppTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
await installFlutterPackages(logger, outputDir);
await applyDartFixes(logger, outputDir);
if (await installFlutterPackages(logger, outputDir)) {
await applyDartFixes(logger, outputDir);
}
_logSummary(logger);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/commands/packages/commands/get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PackagesGetCommand extends Command<int> {
final isFlutterInstalled = await Flutter.installed(logger: _logger);
if (isFlutterInstalled) {
try {
await Flutter.packagesGet(
await Flutter.pubGet(
cwd: targetPath,
recursive: recursive,
ignore: ignore,
Expand Down
Loading

0 comments on commit 35f7866

Please sign in to comment.