-
Notifications
You must be signed in to change notification settings - Fork 235
feat: very_good packages get --recursive #201
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df08e1b
feat: very_good packages get --recursive
felangel c4e2acf
chore: revert README
felangel e8c7cd1
test: use mock analytics and pub updater
felangel 1fe0ee3
chore: --recursive is not negatable
felangel ff24a92
use mocks
felangel 27b5dc0
Update lib/src/commands/packages.dart
felangel 9309b52
refactor: minor simplification
felangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export 'create.dart'; | ||
| export 'packages.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import 'package:args/args.dart'; | ||
| import 'package:args/command_runner.dart'; | ||
| import 'package:io/io.dart'; | ||
| import 'package:mason/mason.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:path/path.dart' as path; | ||
| import 'package:universal_io/io.dart'; | ||
| import 'package:very_good_cli/src/cli/cli.dart'; | ||
|
|
||
| /// {@template packages_command} | ||
| /// `very_good packages` command for managing packages. | ||
| /// {@endtemplate} | ||
| class PackagesCommand extends Command<int> { | ||
| /// {@macro packages_command} | ||
| PackagesCommand({Logger? logger}) { | ||
| addSubcommand(PackagesGetCommand(logger: logger)); | ||
| } | ||
|
|
||
| @override | ||
| String get description => 'Command for managing packages.'; | ||
|
|
||
| @override | ||
| String get name => 'packages'; | ||
| } | ||
|
|
||
| /// {@template packages_get_command} | ||
| /// `very_good packages` command for installing packages. | ||
felangel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// {@endtemplate} | ||
| class PackagesGetCommand extends Command<int> { | ||
| /// {@macro packages_get_command} | ||
| PackagesGetCommand({Logger? logger}) : _logger = logger ?? Logger() { | ||
| argParser.addFlag( | ||
| 'recursive', | ||
| abbr: 'r', | ||
| help: 'Install dependencies recursively for all nested packages.', | ||
| negatable: false, | ||
| ); | ||
| } | ||
|
|
||
| final Logger _logger; | ||
|
|
||
| @override | ||
| String get description => 'Get packages in a Dart or Flutter project.'; | ||
|
|
||
| @override | ||
| String get name => 'get'; | ||
|
|
||
| /// [ArgResults] which can be overridden for testing. | ||
| @visibleForTesting | ||
| ArgResults? argResultOverrides; | ||
|
|
||
| ArgResults get _argResults => argResultOverrides ?? argResults!; | ||
|
|
||
| @override | ||
| Future<int> run() async { | ||
| if (_argResults.rest.length > 1) { | ||
| throw UsageException('Too many arguments', usage); | ||
| } | ||
|
|
||
| final recursive = _argResults['recursive'] as bool; | ||
| final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.'; | ||
| final targetDir = Directory(target); | ||
| final targetPath = path.normalize(targetDir.absolute.path); | ||
| final isFlutterInstalled = await Flutter.installed(); | ||
| if (isFlutterInstalled) { | ||
| final installDependenciesDone = _logger.progress( | ||
| 'Running "flutter packages get" in $targetPath', | ||
| ); | ||
| try { | ||
| await Flutter.packagesGet(cwd: targetDir.path, recursive: recursive); | ||
| installDependenciesDone(); | ||
| } on PubspecNotFound catch (_) { | ||
| installDependenciesDone(); | ||
| _logger.err('Could not find a pubspec.yaml in $targetPath'); | ||
| return ExitCode.noInput.code; | ||
| } catch (error) { | ||
| installDependenciesDone(); | ||
| _logger.err('$error'); | ||
| return ExitCode.unavailable.code; | ||
| } | ||
| } | ||
| return ExitCode.success.code; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:io/io.dart'; | ||
| import 'package:mason/mason.dart'; | ||
| import 'package:mocktail/mocktail.dart'; | ||
| import 'package:path/path.dart' as path; | ||
| import 'package:pub_updater/pub_updater.dart'; | ||
| import 'package:test/test.dart'; | ||
| import 'package:universal_io/io.dart'; | ||
| import 'package:usage/usage.dart'; | ||
| import 'package:very_good_cli/src/command_runner.dart'; | ||
|
|
||
| class MockAnalytics extends Mock implements Analytics {} | ||
|
|
||
| class MockLogger extends Mock implements Logger {} | ||
|
|
||
| class MockPubUpdater extends Mock implements PubUpdater {} | ||
|
|
||
| const expectedPackagesUsage = [ | ||
| // ignore: no_adjacent_strings_in_list | ||
| 'Command for managing packages.\n' | ||
| '\n' | ||
| 'Usage: very_good packages <subcommand> [arguments]\n' | ||
| '-h, --help Print this usage information.\n' | ||
| '\n' | ||
| 'Available subcommands:\n' | ||
| ' get Get packages in a Dart or Flutter project.\n' | ||
| '\n' | ||
| 'Run "very_good help" to see global options.' | ||
| ]; | ||
|
|
||
| const expectedPackagesGetUsage = [ | ||
| // ignore: no_adjacent_strings_in_list | ||
| 'Get packages in a Dart or Flutter project.\n' | ||
| '\n' | ||
| 'Usage: very_good packages get [arguments]\n' | ||
| '-h, --help Print this usage information.\n' | ||
| '''-r, --recursive Install dependencies recursively for all nested packages.\n''' | ||
| '\n' | ||
| 'Run "very_good help" to see global options.' | ||
| ]; | ||
|
|
||
| void main() { | ||
| group('packages', () { | ||
| late Analytics analytics; | ||
| late Logger logger; | ||
| late PubUpdater pubUpdater; | ||
| late List<String> printLogs; | ||
| late List<String> progressLogs; | ||
| late VeryGoodCommandRunner commandRunner; | ||
|
|
||
| void Function() overridePrint(void Function() fn) { | ||
| return () { | ||
| final spec = ZoneSpecification(print: (_, __, ___, String msg) { | ||
| printLogs.add(msg); | ||
| }); | ||
| return Zone.current.fork(specification: spec).run<void>(fn); | ||
| }; | ||
| } | ||
|
|
||
| setUp(() { | ||
| analytics = MockAnalytics(); | ||
| logger = MockLogger(); | ||
| pubUpdater = MockPubUpdater(); | ||
| printLogs = []; | ||
| progressLogs = []; | ||
| commandRunner = VeryGoodCommandRunner( | ||
| analytics: analytics, | ||
| logger: logger, | ||
| pubUpdater: pubUpdater, | ||
| ); | ||
|
|
||
| when(() => analytics.firstRun).thenReturn(false); | ||
| when(() => analytics.enabled).thenReturn(false); | ||
| when( | ||
| () => analytics.sendEvent(any(), any(), label: any(named: 'label')), | ||
| ).thenAnswer((_) async {}); | ||
| when( | ||
| () => analytics.waitForLastPing(timeout: any(named: 'timeout')), | ||
| ).thenAnswer((_) async {}); | ||
|
|
||
| when(() => logger.progress(any())).thenReturn( | ||
| ([_]) { | ||
| if (_ != null) progressLogs.add(_); | ||
| }, | ||
| ); | ||
|
|
||
| when( | ||
| () => pubUpdater.isUpToDate( | ||
| packageName: any(named: 'packageName'), | ||
| currentVersion: any(named: 'currentVersion'), | ||
| ), | ||
| ).thenAnswer((_) => Future.value(true)); | ||
| }); | ||
|
|
||
| test('help', overridePrint(() async { | ||
| final result = await commandRunner.run(['packages', '--help']); | ||
| expect(printLogs, equals(expectedPackagesUsage)); | ||
| expect(result, equals(ExitCode.success.code)); | ||
|
|
||
| printLogs.clear(); | ||
|
|
||
| final resultAbbr = await commandRunner.run(['packages', '-h']); | ||
| expect(printLogs, equals(expectedPackagesUsage)); | ||
| expect(resultAbbr, equals(ExitCode.success.code)); | ||
| })); | ||
|
|
||
| group('get', () { | ||
| test('help', overridePrint(() async { | ||
| final result = await commandRunner.run(['packages', 'get', '--help']); | ||
| expect(printLogs, equals(expectedPackagesGetUsage)); | ||
| expect(result, equals(ExitCode.success.code)); | ||
|
|
||
| printLogs.clear(); | ||
|
|
||
| final resultAbbr = await commandRunner.run(['packages', 'get', '-h']); | ||
| expect(printLogs, equals(expectedPackagesGetUsage)); | ||
| expect(resultAbbr, equals(ExitCode.success.code)); | ||
| })); | ||
|
|
||
| test( | ||
| 'throws usage exception ' | ||
| 'when too many arguments are provided', () async { | ||
| final result = await commandRunner.run( | ||
| ['packages', 'get', 'arg1', 'arg2'], | ||
| ); | ||
| expect(result, equals(ExitCode.usage.code)); | ||
| }); | ||
|
|
||
| test( | ||
| 'throws pubspec not found exception ' | ||
| 'when no pubspec.yaml exists', () async { | ||
| final result = await commandRunner.run(['packages', 'get', 'test']); | ||
| expect(result, equals(ExitCode.noInput.code)); | ||
| verify(() { | ||
| logger.err(any(that: contains('Could not find a pubspec.yaml in'))); | ||
| }).called(1); | ||
| }); | ||
|
|
||
| test( | ||
| 'throws pubspec not found exception ' | ||
| 'when no pubspec.yaml exists (recursive)', () async { | ||
| final result = await commandRunner.run( | ||
| ['packages', 'get', '-r', 'test'], | ||
| ); | ||
| expect(result, equals(ExitCode.noInput.code)); | ||
| verify(() { | ||
| logger.err(any(that: contains('Could not find a pubspec.yaml in'))); | ||
| }).called(1); | ||
| }); | ||
|
|
||
| test('throws when installation fails', () async { | ||
| final directory = Directory.systemTemp.createTempSync(); | ||
| File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync(''); | ||
| final result = await commandRunner.run( | ||
| ['packages', 'get', directory.path], | ||
| ); | ||
| expect(result, equals(ExitCode.unavailable.code)); | ||
| }); | ||
|
|
||
| test( | ||
| 'completes normally ' | ||
| 'when pubspec.yaml exists', () async { | ||
| final result = await commandRunner.run(['packages', 'get']); | ||
| expect(result, equals(ExitCode.success.code)); | ||
| verify(() { | ||
| logger.progress( | ||
| any(that: contains('Running "flutter packages get" in')), | ||
| ); | ||
| }).called(1); | ||
| }); | ||
|
|
||
| test( | ||
| 'completes normally ' | ||
| 'when pubspec.yaml exists (recursive)', () async { | ||
| final result = await commandRunner.run( | ||
| ['packages', 'get', '--recursive'], | ||
| ); | ||
| expect(result, equals(ExitCode.success.code)); | ||
| verify(() { | ||
| logger.progress( | ||
| any(that: contains('Running "flutter packages get" in')), | ||
| ); | ||
| }).called(1); | ||
| }); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.