Skip to content

Commit

Permalink
[Tool] Show usage on 'flutter pub'
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-sharma committed May 6, 2023
1 parent 835b892 commit d26d02d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class CustomDevicesCommand extends FlutterCommand {

final CustomDevicesConfig _customDevicesConfig;
final FeatureFlags _featureFlags;
final void Function(Object) _usagePrintFn;
final PrintFn _usagePrintFn;

@override
String get description {
Expand Down
13 changes: 12 additions & 1 deletion packages/flutter_tools/lib/src/commands/packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import '../project.dart';
import '../reporting/reporting.dart';
import '../runner/flutter_command.dart';

/// The function signature of the [print] function.
typedef PrintFn = void Function(Object?);

class PackagesCommand extends FlutterCommand {
PackagesCommand() {
PackagesCommand({
PrintFn usagePrintFn = print,
}) : _usagePrintFn = usagePrintFn
{
addSubcommand(PackagesGetCommand('get', "Get the current package's dependencies.", PubContext.pubGet));
addSubcommand(PackagesGetCommand('upgrade', "Upgrade the current package's dependencies to latest versions.", PubContext.pubUpgrade));
addSubcommand(PackagesGetCommand('add', 'Add a dependency to pubspec.yaml.', PubContext.pubAdd));
Expand All @@ -40,6 +46,8 @@ class PackagesCommand extends FlutterCommand {
addSubcommand(PackagesPassthroughCommand());
}

final PrintFn _usagePrintFn;

@override
final String name = 'pub';

Expand All @@ -54,6 +62,9 @@ class PackagesCommand extends FlutterCommand {

@override
Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail();

@override
void printUsage() => _usagePrintFn(usage);
}

class PackagesTestCommand extends FlutterCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,16 @@ class FlutterCommandRunner extends CommandRunner<void> {

@override
Future<void> run(Iterable<String> args) {
// Have an invocation of 'build' print out it's sub-commands.
// Have invocations of 'build', 'custom-devices', and 'pub' print out
// their sub-commands.
// TODO(ianh): Move this to the Build command itself somehow.
if (args.length == 1) {
if (args.first == 'build') {
args = <String>['build', '-h'];
} else if (args.first == 'custom-devices') {
args = <String>['custom-devices', '-h'];
} else if (args.first == 'pub') {
args = <String>['pub', '-h'];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ void main() {
Cache.enableLocking();
});

testUsingContext('pub shows help', () async {
Object? usage;
final PackagesCommand command = PackagesCommand(
usagePrintFn: (Object? object) => usage = object,
);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['pub']);

expect(usage, allOf(
contains('Commands for managing Flutter packages.'),
contains('Usage: flutter pub <subcommand> [arguments]'),
));
});

testUsingContext('pub get usage values are resilient to missing package config files before running "pub get"', () async {
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
fileSystem.currentDirectory.childFile('.flutter-plugins').createSync();
Expand Down

0 comments on commit d26d02d

Please sign in to comment.