Skip to content

Commit

Permalink
feat: add support for excluding packages (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
willhlas committed Mar 7, 2023
1 parent 00a9211 commit 3701efa
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 28 deletions.
18 changes: 16 additions & 2 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,22 @@ const _ignoredDirectories = {
};

bool _isPubspec(FileSystemEntity entity) {
final segments = p.split(entity.path).toSet();
if (segments.intersection(_ignoredDirectories).isNotEmpty) return false;
if (entity is! File) return false;
return p.basename(entity.path) == 'pubspec.yaml';
}

extension on Set<String> {
bool excludes(FileSystemEntity entity) {
final segments = p.split(entity.path).toSet();
if (segments.intersection(_ignoredDirectories).isNotEmpty) return true;
if (segments.intersection(this).isNotEmpty) return true;

for (final value in this) {
if (Glob(value).matches(entity.path)) {
return true;
}
}

return false;
}
}
3 changes: 2 additions & 1 deletion lib/src/cli/dart_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Dart {
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
if (!recursive) {
final pubspec = File(p.join(cwd, 'pubspec.yaml'));
Expand All @@ -40,7 +41,7 @@ class Dart {
workingDirectory: entity.parent.path,
logger: logger,
),
where: _isPubspec,
where: (entity) => !ignore.excludes(entity) && _isPubspec(entity),
cwd: cwd,
);

Expand Down
9 changes: 8 additions & 1 deletion lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Flutter {
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
await _runCommand(
cmd: (cwd) async {
Expand Down Expand Up @@ -107,6 +108,7 @@ class Flutter {
},
cwd: cwd,
recursive: recursive,
ignore: ignore,
);
}

Expand All @@ -115,6 +117,7 @@ class Flutter {
required Logger logger,
String cwd = '.',
bool recursive = false,
Set<String> ignore = const {},
}) async {
await _runCommand(
cmd: (cwd) => _Cmd.run(
Expand All @@ -125,6 +128,7 @@ class Flutter {
),
cwd: cwd,
recursive: recursive,
ignore: ignore,
);
}

Expand All @@ -136,6 +140,7 @@ class Flutter {
bool recursive = false,
bool collectCoverage = false,
bool optimizePerformance = false,
Set<String> ignore = const {},
double? minCoverage,
String? excludeFromCoverage,
String? randomSeed,
Expand Down Expand Up @@ -237,6 +242,7 @@ class Flutter {
},
cwd: cwd,
recursive: recursive,
ignore: ignore,
);
}

Expand Down Expand Up @@ -287,6 +293,7 @@ Future<List<T>> _runCommand<T>({
required Future<T> Function(String cwd) cmd,
required String cwd,
required bool recursive,
required Set<String> ignore,
}) async {
if (!recursive) {
final pubspec = File(p.join(cwd, 'pubspec.yaml'));
Expand All @@ -297,7 +304,7 @@ Future<List<T>> _runCommand<T>({

final processes = _Cmd.runWhere<T>(
run: (entity) => cmd(entity.parent.path),
where: _isPubspec,
where: (entity) => !ignore.excludes(entity) && _isPubspec(entity),
cwd: cwd,
);

Expand Down
19 changes: 13 additions & 6 deletions lib/src/commands/packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ class PackagesCommand extends Command<int> {
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,
);
argParser
..addFlag(
'recursive',
abbr: 'r',
help: 'Install dependencies recursively for all nested packages.',
negatable: false,
)
..addMultiOption(
'ignore',
help: 'Exclude packages from installing dependencies.',
);
}

final Logger _logger;
Expand All @@ -57,6 +62,7 @@ class PackagesGetCommand extends Command<int> {
}

final recursive = _argResults['recursive'] as bool;
final ignore = (_argResults['ignore'] as List<String>).toSet();
final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.';
final targetPath = path.normalize(Directory(target).absolute.path);
final isFlutterInstalled = await Flutter.installed(logger: _logger);
Expand All @@ -65,6 +71,7 @@ class PackagesGetCommand extends Command<int> {
await Flutter.packagesGet(
cwd: targetPath,
recursive: recursive,
ignore: ignore,
logger: _logger,
);
} on PubspecNotFound catch (_) {
Expand Down
72 changes: 54 additions & 18 deletions test/src/cli/flutter_cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,61 @@ void main() {
);
});

test('completes when there is a pubspec.yaml (recursive)', () {
final directory = Directory.systemTemp.createTempSync();
final nestedDirectory = Directory(p.join(directory.path, 'test'))
..createSync();
File(p.join(nestedDirectory.path, 'pubspec.yaml'))
.writeAsStringSync(_pubspec);

ProcessOverrides.runZoned(
() => expectLater(
Flutter.packagesGet(
cwd: directory.path,
recursive: true,
logger: logger,
test(
'completes when there is a pubspec.yaml and '
'directory is ignored (recursive)',
() {
final directory = Directory.systemTemp.createTempSync();
final nestedDirectory = Directory(p.join(directory.path, 'test'))
..createSync();
final ignoredDirectory = Directory(
p.join(directory.path, 'test_plugin'),
)..createSync();

File(p.join(nestedDirectory.path, 'pubspec.yaml'))
.writeAsStringSync(_pubspec);
File(p.join(ignoredDirectory.path, 'pubspec.yaml'))
.writeAsStringSync(_pubspec);

ProcessOverrides.runZoned(
() => expectLater(
Flutter.packagesGet(
cwd: directory.path,
recursive: true,
ignore: {
'test_plugin',
'/**/test_plugin_two/**',
},
logger: logger,
),
completes,
),
completes,
),
runProcess: process.run,
);
});
runProcess: process.run,
).whenComplete(() {
verify(() {
logger.progress(
any(
that: contains(
'Running "flutter packages get" in '
'${nestedDirectory.path}',
),
),
);
}).called(1);

verifyNever(() {
logger.progress(
any(
that: contains(
'Running "flutter packages get" in '
'${ignoredDirectory.path}',
),
),
);
});
});
},
);
});

group('.pubGet', () {
Expand Down
72 changes: 72 additions & 0 deletions test/src/commands/packages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const expectedPackagesGetUsage = [
'Usage: very_good packages get [arguments]\n'
'-h, --help Print this usage information.\n'
'''-r, --recursive Install dependencies recursively for all nested packages.\n'''
' --ignore Exclude packages from installing dependencies.\n'
'\n'
'Run "very_good help" to see global options.'
];
Expand Down Expand Up @@ -265,6 +266,77 @@ void main() {
}).called(2);
}),
);

test(
'completes normally '
'when pubspec.yaml exists and directory is ignored (recursive)',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
final directoryA = Directory(
path.join(tempDirectory.path, 'plugin_a'),
);
final directoryB = Directory(
path.join(tempDirectory.path, 'plugin_b'),
);
final pubspecA = File(
path.join(directoryA.path, 'example_a', 'pubspec.yaml'),
);
final pubspecB = File(
path.join(directoryB.path, 'example_b', 'pubspec.yaml'),
);
pubspecA
..createSync(recursive: true)
..writeAsStringSync(
'''
name: example_a
version: 0.1.0
environment:
sdk: ">=2.12.0 <3.0.0"
''',
);
pubspecB
..createSync(recursive: true)
..writeAsStringSync(
'''
name: example_b
version: 0.1.0
environment:
sdk: ">=2.12.0 <3.0.0"
''',
);

final result = await commandRunner.run(
[
'packages',
'get',
'--recursive',
'--ignore=plugin_b',
tempDirectory.path,
],
);
expect(result, equals(ExitCode.success.code));
verify(() {
logger.progress(
any(
that: contains(
'Running "flutter packages get" in ${directoryA.path}',
),
),
);
}).called(1);
verifyNever(() {
logger.progress(
any(
that: contains(
'Running "flutter packages get" in ${directoryB.path}',
),
),
);
});
}),
);
});
});
}

0 comments on commit 3701efa

Please sign in to comment.