mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
[flutter_tools] re-use findProjectRoot on flutter command (#104850)
This commit is contained in:
parent
7df7e8abad
commit
3da9eee2f0
@ -58,7 +58,6 @@ class PackagesCommand extends FlutterCommand {
|
||||
|
||||
class PackagesGetCommand extends FlutterCommand {
|
||||
PackagesGetCommand(this.name, this.upgrade) {
|
||||
requiresPubspecYaml();
|
||||
argParser.addFlag('offline',
|
||||
negatable: false,
|
||||
help: 'Use cached packages instead of accessing the network.',
|
||||
|
@ -12,6 +12,7 @@ import '../application_package.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/io.dart' as io;
|
||||
import '../base/os.dart';
|
||||
import '../base/user_messages.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
@ -1475,16 +1476,12 @@ abstract class FlutterCommand extends Command<void> {
|
||||
|
||||
// If there is no pubspec in the current directory, look in the parent
|
||||
// until one can be found.
|
||||
bool changedDirectory = false;
|
||||
while (!globals.fs.isFileSync('pubspec.yaml')) {
|
||||
final Directory nextCurrent = globals.fs.currentDirectory.parent;
|
||||
if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
|
||||
throw ToolExit(userMessages.flutterNoPubspec);
|
||||
}
|
||||
globals.fs.currentDirectory = nextCurrent;
|
||||
changedDirectory = true;
|
||||
final String? path = findProjectRoot(globals.fs, globals.fs.currentDirectory.path);
|
||||
if (path == null) {
|
||||
throwToolExit(userMessages.flutterNoPubspec);
|
||||
}
|
||||
if (changedDirectory) {
|
||||
if (path != globals.fs.currentDirectory.path) {
|
||||
globals.fs.currentDirectory = path;
|
||||
globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}');
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/packages.dart';
|
||||
import 'package:flutter_tools/src/dart/pub.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
import 'package:flutter_tools/src/reporting/reporting.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:test/fake.dart';
|
||||
@ -77,6 +78,24 @@ void main() {
|
||||
FileSystem: () => fileSystem,
|
||||
});
|
||||
|
||||
testUsingContext('pub get on target directory', () async {
|
||||
fileSystem.currentDirectory.childDirectory('target').createSync();
|
||||
final Directory targetDirectory = fileSystem.currentDirectory.childDirectory('target');
|
||||
targetDirectory.childFile('pubspec.yaml').createSync();
|
||||
|
||||
final PackagesGetCommand command = PackagesGetCommand('get', false);
|
||||
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
|
||||
|
||||
await commandRunner.run(<String>['get', targetDirectory.path]);
|
||||
final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory);
|
||||
expect(rootProject.packageConfigFile.existsSync(), true);
|
||||
expect(await rootProject.packageConfigFile.readAsString(), '{"configVersion":2,"packages":[]}');
|
||||
}, overrides: <Type, Generator>{
|
||||
Pub: () => pub,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
FileSystem: () => fileSystem,
|
||||
});
|
||||
|
||||
testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async {
|
||||
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
|
||||
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
|
||||
@ -116,7 +135,7 @@ class FakePub extends Fake implements Pub {
|
||||
bool shouldSkipThirdPartyGenerator = true,
|
||||
bool printProgress = true,
|
||||
}) async {
|
||||
fileSystem.currentDirectory
|
||||
fileSystem.directory(directory)
|
||||
.childDirectory('.dart_tool')
|
||||
.childFile('package_config.json')
|
||||
..createSync(recursive: true)
|
||||
|
Loading…
Reference in New Issue
Block a user