Repository: VeryGoodOpenSource/very_good_cli (dart)

Issue:
feat(create)! subcommand: Very Good Flutter Package Usage
Split the create command to use this subcommand: 

```sh
# Very Good Flutter Package Usage
very_good create flutter_pkg <project-name> <options>

options:
    --description
```

Pull request:
<!--
  Thanks for contributing!

  Provide a description of your changes below and a general summary in the title

  Please look at the following checklist to ensure that your PR can be accepted quickly:
-->

## Description

closes #586 

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [x] ✨ New feature (non-breaking change which adds functionality)
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
- [ ] 🧹 Code refactor
- [ ] ✅ Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore

Patch:
diff --git a/.github/workflows/very_good_cli.yaml b/.github/workflows/very_good_cli.yaml
index 145b8d0dd..c616ae177 100644
--- a/.github/workflows/very_good_cli.yaml
+++ b/.github/workflows/very_good_cli.yaml
@@ -56,6 +56,7 @@ jobs:
           # E2E tests for the create command
           - test/src/commands/create/e2e/flutter_app/core_test.dart
           - test/src/commands/create/e2e/dart_package/dart_pkg_test.dart
+          - test/src/commands/create/e2e/flutter_package/flutter_pkg_test.dart
           - test/src/commands/create/e2e/dart_cli/dart_cli_test.dart
           - test/src/commands/create/e2e/docs_site/docs_site_test.dart
 
diff --git a/lib/src/commands/commands.dart b/lib/src/commands/commands.dart
index 295f75184..2a90abed5 100644
--- a/lib/src/commands/commands.dart
+++ b/lib/src/commands/commands.dart
@@ -1,6 +1,5 @@
 export 'create/commands/commands.dart';
 export 'create/create.dart';
-export 'create/create.dart';
 export 'packages.dart';
 export 'test/test.dart';
 export 'update.dart';
diff --git a/lib/src/commands/create/commands/commands.dart b/lib/src/commands/create/commands/commands.dart
index 145d95516..2245b0c08 100644
--- a/lib/src/commands/create/commands/commands.dart
+++ b/lib/src/commands/create/commands/commands.dart
@@ -3,4 +3,5 @@ export 'dart_cli.dart';
 export 'dart_package.dart';
 export 'docs_site.dart';
 export 'flutter_app.dart';
+export 'flutter_package.dart';
 export 'legacy.dart';
diff --git a/lib/src/commands/create/commands/create_subcommand.dart b/lib/src/commands/create/commands/create_subcommand.dart
index d0865a52d..d94399874 100644
--- a/lib/src/commands/create/commands/create_subcommand.dart
+++ b/lib/src/commands/create/commands/create_subcommand.dart
@@ -44,6 +44,9 @@ typedef MasonGeneratorFromBrick = Future<MasonGenerator> Function(Brick);
 ///
 /// For sub commands that receive an org name, sub classes must mix with
 /// [OrgName].
+///
+/// For sub commands that receive a publishable flag, sub classes must mix with
+/// [Publishable].
 abstract class CreateSubCommand extends Command<int> {
   /// {@macro create_subcommand}
   CreateSubCommand({
@@ -235,6 +238,7 @@ abstract class CreateSubCommand extends Command<int> {
   /// parameters.
   ///
   /// For subcommands that mix with [OrgName], it includes 'org_name'.
+  /// For subcommands that mix with [Publishable], it includes 'publishable'.
   @mustCallSuper
   Map<String, dynamic> getTemplateVars() {
     final projectName = this.projectName;
diff --git a/lib/src/commands/create/commands/flutter_package.dart b/lib/src/commands/create/commands/flutter_package.dart
new file mode 100644
index 000000000..51a509d3e
--- /dev/null
+++ b/lib/src/commands/create/commands/flutter_package.dart
@@ -0,0 +1,35 @@
+import 'package:mason_logger/mason_logger.dart';
+import 'package:usage/usage.dart';
+import 'package:very_good_cli/src/commands/commands.dart';
+import 'package:very_good_cli/src/commands/create/templates/templates.dart';
+
+/// {@template very_good_create_flutter_package_command}
+/// A [CreateSubCommand] for creating Flutter packages.
+/// {@endtemplate}
+class CreateFlutterPackage extends CreateSubCommand with Publishable {
+  /// {@macro very_good_create_flutter_package_command}
+  CreateFlutterPackage({
+    required Analytics analytics,
+    required Logger logger,
+    required MasonGeneratorFromBundle? generatorFromBundle,
+    required MasonGeneratorFromBrick? generatorFromBrick,
+  }) : super(
+          analytics: analytics,
+          logger: logger,
+          generatorFromBundle: generatorFromBundle,
+          generatorFromBrick: generatorFromBrick,
+        );
+
+  @override
+  String get name => 'flutter_package';
+
+  @override
+  List<String> get aliases => ['flutter_pkg'];
+
+  @override
+  String get description =>
+      'Creates a new very good Flutter package in the specified directory.';
+
+  @override
+  Template get template => FlutterPkgTemplate();
+}
diff --git a/lib/src/commands/create/create.dart b/lib/src/commands/create/create.dart
index 7b0c9c854..37e3e7e87 100644
--- a/lib/src/commands/create/create.dart
+++ b/lib/src/commands/create/create.dart
@@ -39,7 +39,7 @@ class CreateCommand extends Command<int> {
       ),
     );
 
-    // very_good create dart_pkg <args>
+    // very_good create dart_package <args>
     addSubcommand(
       CreateDartPackage(
         analytics: analytics,
@@ -68,6 +68,16 @@ class CreateCommand extends Command<int> {
         generatorFromBrick: generatorFromBrick,
       ),
     );
+
+    // very_good create flutter_package <args>
+    addSubcommand(
+      CreateFlutterPackage(
+        analytics: analytics,
+        logger: logger,
+        generatorFromBundle: generatorFromBundle,
+        generatorFromBrick: generatorFromBrick,
+      ),
+    );
   }
 
   @override


Test patch:
diff --git a/test/src/commands/create/commands/flutter_package_test.dart b/test/src/commands/create/commands/flutter_package_test.dart
new file mode 100644
index 000000000..808b034e5
--- /dev/null
+++ b/test/src/commands/create/commands/flutter_package_test.dart
@@ -0,0 +1,214 @@
+import 'dart:io';
+
+import 'package:args/args.dart';
+import 'package:mason/mason.dart';
+import 'package:mocktail/mocktail.dart';
+import 'package:path/path.dart' as path;
+import 'package:test/test.dart';
+import 'package:usage/usage.dart';
+import 'package:very_good_cli/src/commands/commands.dart';
+
+import '../../../../helpers/helpers.dart';
+
+class MockAnalytics extends Mock implements Analytics {}
+
+class MockLogger extends Mock implements Logger {}
+
+class MockMasonGenerator extends Mock implements MasonGenerator {}
+
+class MockGeneratorHooks extends Mock implements GeneratorHooks {}
+
+class MockArgResults extends Mock implements ArgResults {}
+
+class FakeLogger extends Fake implements Logger {}
+
+class FakeDirectoryGeneratorTarget extends Fake
+    implements DirectoryGeneratorTarget {}
+
+final expectedUsage = [
+  '''
+Creates a new very good Flutter package in the specified directory.
+
+Usage: very_good create flutter_package <project-name> [arguments]
+-h, --help                Print this usage information.
+-o, --output-directory    The desired output directory when creating a new project.
+    --description         The description for this new project.
+                          (defaults to "A Very Good Project created by Very Good CLI.")
+    --publishable         Whether the generated project is intended to be published.
+
+Run "very_good help" to see global options.''',
+];
+
+const pubspec = '''
+name: example
+environment:
+  sdk: ">=2.13.0 <3.0.0"
+''';
+
+void main() {
+  late Analytics analytics;
+  late Logger logger;
+
+  setUpAll(() {
+    registerFallbackValue(FakeDirectoryGeneratorTarget());
+    registerFallbackValue(FakeLogger());
+  });
+
+  setUp(() {
+    analytics = MockAnalytics();
+    when(
+      () => analytics.sendEvent(any(), any(), label: any(named: 'label')),
+    ).thenAnswer((_) async {});
+    when(
+      () => analytics.waitForLastPing(timeout: any(named: 'timeout')),
+    ).thenAnswer((_) async {});
+
+    logger = MockLogger();
+
+    final progress = MockProgress();
+
+    when(() => logger.progress(any())).thenReturn(progress);
+  });
+
+  group('can be instantiated', () {
+    test('with default options', () {
+      final logger = Logger();
+      final command = CreateFlutterPackage(
+        analytics: analytics,
+        logger: logger,
+        generatorFromBundle: null,
+        generatorFromBrick: null,
+      );
+      expect(command.name, equals('flutter_package'));
+      expect(
+        command.description,
+        equals(
+          'Creates a new very good Flutter package in the specified directory.',
+        ),
+      );
+      expect(command.logger, equals(logger));
+      expect(command, isA<Publishable>());
+    });
+  });
+
+  group('create flutter_package', () {
+    test(
+      'help',
+      withRunner((commandRunner, logger, pubUpdater, printLogs) async {
+        final result =
+            await commandRunner.run(['create', 'flutter_package', '--help']);
+        expect(printLogs, equals(expectedUsage));
+        expect(result, equals(ExitCode.success.code));
+
+        printLogs.clear();
+
+        final resultAbbr =
+            await commandRunner.run(['create', 'flutter_pkg', '-h']);
+        expect(printLogs, equals(expectedUsage));
+        expect(resultAbbr, equals(ExitCode.success.code));
+      }),
+    );
+
+    group('running the command', () {
+      final generatedFiles =
+          List.filled(10, const GeneratedFile.created(path: ''));
+
+      late GeneratorHooks hooks;
+      late MasonGenerator generator;
+
+      setUp(() {
+        hooks = MockGeneratorHooks();
+        generator = MockMasonGenerator();
+
+        when(() => generator.hooks).thenReturn(hooks);
+        when(
+          () => hooks.preGen(
+            vars: any(named: 'vars'),
+            onVarsChanged: any(named: 'onVarsChanged'),
+          ),
+        ).thenAnswer((_) async {});
+
+        when(
+          () => generator.generate(
+            any(),
+            vars: any(named: 'vars'),
+            logger: any(named: 'logger'),
+          ),
+        ).thenAnswer((_) async {
+          return generatedFiles;
+        });
+
+        when(() => generator.id).thenReturn('generator_id');
+        when(() => generator.description).thenReturn('generator description');
+        when(() => generator.hooks).thenReturn(hooks);
+
+        when(
+          () => hooks.preGen(
+            vars: any(named: 'vars'),
+            onVarsChanged: any(named: 'onVarsChanged'),
+          ),
+        ).thenAnswer((_) async {});
+        when(
+          () => generator.generate(
+            any(),
+            vars: any(named: 'vars'),
+            logger: any(named: 'logger'),
+          ),
+        ).thenAnswer((_) async {
+          final target =
+              _.positionalArguments.first as DirectoryGeneratorTarget;
+          File(path.join(target.dir.path, 'my_flutter_package', 'pubspec.yaml'))
+            ..createSync(recursive: true)
+            ..writeAsStringSync(pubspec);
+          return generatedFiles;
+        });
+      });
+
+      test('creates flutter package', () async {
+        final tempDir = Directory.systemTemp.createTempSync();
+        addTearDown(() => tempDir.deleteSync(recursive: true));
+        final argResults = MockArgResults();
+        final command = CreateFlutterPackage(
+          analytics: analytics,
+          logger: logger,
+          generatorFromBundle: (_) async => throw Exception('oops'),
+          generatorFromBrick: (_) async => generator,
+        )..argResultOverrides = argResults;
+        when(() => argResults['output-directory'] as String?)
+            .thenReturn(tempDir.path);
+        when(() => argResults.rest).thenReturn(['my_flutter_package']);
+
+        final result = await command.run();
+
+        expect(command.template.name, 'flutter_pkg');
+        expect(result, equals(ExitCode.success.code));
+
+        verify(() => logger.progress('Bootstrapping')).called(1);
+        verify(
+          () => hooks.preGen(
+            vars: <String, dynamic>{
+              'project_name': 'my_flutter_package',
+              'description': '',
+              'publishable': false,
+            },
+            onVarsChanged: any(named: 'onVarsChanged'),
+          ),
+        );
+        verify(
+          () => generator.generate(
+            any(),
+            vars: <String, dynamic>{
+              'project_name': 'my_flutter_package',
+              'description': '',
+              'publishable': false,
+            },
+            logger: logger,
+          ),
+        ).called(1);
+        verify(
+          () => logger.info('Created a Very Good Flutter Package! 🦄'),
+        ).called(1);
+      });
+    });
+  });
+}
diff --git a/test/src/commands/create/commands/legacy_test.dart b/test/src/commands/create/commands/legacy_test.dart
index 46b6a390d..175aaa7d7 100644
--- a/test/src/commands/create/commands/legacy_test.dart
+++ b/test/src/commands/create/commands/legacy_test.dart
@@ -23,10 +23,11 @@ Usage: very_good create <subcommand> <project-name> [arguments]
 -h, --help    Print this usage information.
 
 Available subcommands:
-  dart_cli       Creates a new very good Dart CLI in the specified directory.
-  dart_package   Creates a new very good Dart package in the specified directory.
-  docs_site      Creates a new very good docs site in the specified directory.
-  flutter_app    Creates a new very good Flutter app in the specified directory.
+  dart_cli          Creates a new very good Dart CLI in the specified directory.
+  dart_package      Creates a new very good Dart package in the specified directory.
+  docs_site         Creates a new very good docs site in the specified directory.
+  flutter_app       Creates a new very good Flutter app in the specified directory.
+  flutter_package   Creates a new very good Flutter package in the specified directory.
 
 Run "very_good help" to see global options.'''
 ];
diff --git a/test/src/commands/create/create_test.dart b/test/src/commands/create/create_test.dart
index f0859cd42..03ffee534 100644
--- a/test/src/commands/create/create_test.dart
+++ b/test/src/commands/create/create_test.dart
@@ -29,10 +29,11 @@ Usage: very_good create <subcommand> <project-name> [arguments]
 -h, --help    Print this usage information.
 
 Available subcommands:
-  dart_cli       Creates a new very good Dart CLI in the specified directory.
-  dart_package   Creates a new very good Dart package in the specified directory.
-  docs_site      Creates a new very good docs site in the specified directory.
-  flutter_app    Creates a new very good Flutter app in the specified directory.
+  dart_cli          Creates a new very good Dart CLI in the specified directory.
+  dart_package      Creates a new very good Dart package in the specified directory.
+  docs_site         Creates a new very good docs site in the specified directory.
+  flutter_app       Creates a new very good Flutter app in the specified directory.
+  flutter_package   Creates a new very good Flutter package in the specified directory.
 
 Run "very_good help" to see global options.'''
 ];
diff --git a/test/src/commands/create/e2e/flutter_package/flutter_pkg_test.dart b/test/src/commands/create/e2e/flutter_package/flutter_pkg_test.dart
new file mode 100644
index 000000000..b1cb682b9
--- /dev/null
+++ b/test/src/commands/create/e2e/flutter_package/flutter_pkg_test.dart
@@ -0,0 +1,67 @@
+@Tags(['e2e'])
+import 'package:mason/mason.dart';
+import 'package:path/path.dart' as path;
+import 'package:test/test.dart';
+import 'package:universal_io/io.dart';
+
+import '../../../../../helpers/helpers.dart';
+
+void main() {
+  test(
+    'create flutter_package',
+    withRunner((commandRunner, logger, updater, logs) async {
+      final directory = Directory.systemTemp.createTempSync();
+
+      final result = await commandRunner.run(
+        [
+          'create',
+          'flutter_package',
+          'very_good_flutter',
+          '-o',
+          directory.path,
+        ],
+      );
+      expect(result, equals(ExitCode.success.code));
+
+      final formatResult = await Process.run(
+        'flutter',
+        ['format', '--set-exit-if-changed', '.'],
+        workingDirectory: path.join(directory.path, 'very_good_flutter'),
+        runInShell: true,
+      );
+      expect(formatResult.exitCode, equals(ExitCode.success.code));
+      expect(formatResult.stderr, isEmpty);
+
+      final analyzeResult = await Process.run(
+        'flutter',
+        ['analyze', '.'],
+        workingDirectory: path.join(directory.path, 'very_good_flutter'),
+        runInShell: true,
+      );
+      expect(analyzeResult.exitCode, equals(ExitCode.success.code));
+      expect(analyzeResult.stderr, isEmpty);
+      expect(analyzeResult.stdout, contains('No issues found!'));
+
+      final testResult = await Process.run(
+        'flutter',
+        ['test', '--no-pub', '--coverage'],
+        workingDirectory: path.join(directory.path, 'very_good_flutter'),
+        runInShell: true,
+      );
+      expect(testResult.exitCode, equals(ExitCode.success.code));
+      expect(testResult.stderr, isEmpty);
+      expect(testResult.stdout, contains('All tests passed!'));
+
+      final testCoverageResult = await Process.run(
+        'genhtml',
+        ['coverage/lcov.info', '-o', 'coverage'],
+        workingDirectory: path.join(directory.path, 'very_good_flutter'),
+        runInShell: true,
+      );
+      expect(testCoverageResult.exitCode, equals(ExitCode.success.code));
+      expect(testCoverageResult.stderr, isEmpty);
+      expect(testCoverageResult.stdout, contains('lines......: 100.0%'));
+    }),
+    timeout: const Timeout(Duration(minutes: 2)),
+  );
+}