Skip to content
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

feat: add support for excluding packages #640

Merged
merged 25 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
805a87f
feat: add support for excluding packages
willhlas Feb 6, 2023
8bde724
fix packages_test
willhlas Feb 6, 2023
d077383
add extension check for ignored directories
willhlas Feb 6, 2023
2fc74cb
Merge branch 'main' into feat/exclude-packages
willhlas Feb 13, 2023
a3cff14
add glob support
willhlas Feb 13, 2023
c33744b
Merge branch 'main' into feat/exclude-packages
willhlas Feb 13, 2023
d5fd461
Merge branch 'main' into feat/exclude-packages
willhlas Feb 14, 2023
90b91f7
Merge branch 'main' into feat/exclude-packages
willhlas Feb 17, 2023
ac9858e
Merge branch 'main' into feat/exclude-packages
willhlas Feb 21, 2023
dc70016
Merge branch 'main' into feat/exclude-packages
willhlas Feb 22, 2023
169f3c2
glob check
willhlas Feb 22, 2023
d189268
add tests
willhlas Feb 22, 2023
91b882f
Merge branch 'main' into feat/exclude-packages
willhlas Feb 22, 2023
7dd41b3
test commit
willhlas Feb 22, 2023
614f573
Merge branch 'main' into feat/exclude-packages
willhlas Feb 22, 2023
700ed44
Merge branch 'main' into feat/exclude-packages
renancaraujo Feb 27, 2023
0c222fa
Merge branch 'main' into feat/exclude-packages
renancaraujo Feb 27, 2023
241e3f5
Merge branch 'main' into feat/exclude-packages
willhlas Feb 28, 2023
74e154d
improve tests
willhlas Feb 28, 2023
1e74886
Merge branch 'main' into feat/exclude-packages
willhlas Mar 1, 2023
ecac386
Merge branch 'main' into feat/exclude-packages
willhlas Mar 2, 2023
5e3e6b2
revert to original glob implementation
willhlas Mar 2, 2023
cf1d0ec
apply suggestion
willhlas Mar 3, 2023
5247c89
Merge branch 'main' into feat/exclude-packages
willhlas Mar 3, 2023
5f9330c
test-commit
willhlas Mar 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,31 @@ 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();
willhlas marked this conversation as resolved.
Show resolved Hide resolved
if (segments.intersection(_ignoredDirectories).isNotEmpty) return true;
if (segments.intersection(this).isNotEmpty) return true;

for (var value in this) {
if (value.contains('/') && !value.contains('*')) {
if (value.endsWith('/')) {
value = '$value**';
} else {
value = '$value/**';
}
}

final glob = Glob(value);
if (glob.matches(entity.path)) {
return true;
}
willhlas marked this conversation as resolved.
Show resolved Hide resolved
}

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
98 changes: 80 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,87 @@ 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();
final ignoredDirectoryTwo = Directory(
p.join(directory.path, 'test_plugin_two'),
)..createSync();
final ignoredDirectoryThree = Directory(
p.join(directory.path, 'test_plugin_three'),
)..createSync();
final ignoredDirectoryFour = Directory(
p.join(directory.path, 'test_plugin_four'),
)..createSync();

final ignoredDirectories = [
ignoredDirectory,
ignoredDirectoryTwo,
ignoredDirectoryThree,
ignoredDirectoryFour
];

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

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

for (final directory in ignoredDirectories) {
verifyNever(() {
logger.progress(
any(
that: contains(
'Running "flutter packages get" in '
'${directory.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(() {
willhlas marked this conversation as resolved.
Show resolved Hide resolved
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}',
),
),
);
});
}),
);
});
});
}