enhance flutter upgrade to find project root (#5552)
* enhance flutter upgrade to find project root fixes https://github.com/flutter/flutter/issues/2990 * address comments * restore original formatting * fix lints
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../base/process.dart';
|
||||
import '../dart/pub.dart';
|
||||
import '../cache.dart';
|
||||
@@ -61,9 +63,11 @@ class UpgradeCommand extends FlutterCommand {
|
||||
printStatus('');
|
||||
printStatus(FlutterVersion.getVersion(Cache.flutterRoot).toString());
|
||||
|
||||
if (FileSystemEntity.isFileSync('pubspec.yaml')) {
|
||||
String projRoot = findProjectRoot();
|
||||
if (projRoot != null) {
|
||||
printStatus('');
|
||||
code = await pubGet(upgrade: true, checkLastModified: false);
|
||||
code = await pubGet(
|
||||
directory: projRoot, upgrade: true, checkLastModified: false);
|
||||
|
||||
if (code != 0)
|
||||
return code;
|
||||
@@ -86,4 +90,20 @@ class UpgradeCommand extends FlutterCommand {
|
||||
|| _gitChangedRegex.hasMatch(line)
|
||||
|| line == 'Fast-forward';
|
||||
}
|
||||
|
||||
/// Find and return the project root directory relative to the specified
|
||||
/// directory or the current working directory if none specified.
|
||||
/// Return `null` if the project root could not be found
|
||||
/// or if the project root is the flutter repository root.
|
||||
static String findProjectRoot([String directory]) {
|
||||
directory ??= Directory.current.path;
|
||||
while (true) {
|
||||
if (FileSystemEntity.isFileSync(path.join(directory, 'pubspec.yaml')))
|
||||
return directory;
|
||||
String parent = FileSystemEntity.parentOf(directory);
|
||||
if (directory == parent)
|
||||
return null;
|
||||
directory = parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,19 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/create.dart';
|
||||
import 'package:flutter_tools/src/commands/upgrade.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'src/common.dart';
|
||||
import 'src/context.dart';
|
||||
|
||||
void main() {
|
||||
group('upgrade', () {
|
||||
bool _match(String line) => UpgradeCommand.matchesGitLine(line);
|
||||
@@ -24,5 +34,42 @@ void main() {
|
||||
expect(_match('Updating 79cfe1e..5046107'), false);
|
||||
expect(_match('229 files changed, 6179 insertions(+), 3065 deletions(-)'), false);
|
||||
});
|
||||
|
||||
group('findProjectRoot', () {
|
||||
Directory temp;
|
||||
|
||||
setUp(() async {
|
||||
temp = Directory.systemTemp.createTempSync('flutter_tools');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
temp.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
Future<Null> createProject() async {
|
||||
CreateCommand command = new CreateCommand();
|
||||
CommandRunner runner = createTestCommandRunner(command);
|
||||
int code = await runner.run(<String>['create', '--no-pub', temp.path]);
|
||||
expect(code, 0);
|
||||
}
|
||||
|
||||
testUsingContext('in project', () async {
|
||||
await createProject();
|
||||
|
||||
String proj = temp.path;
|
||||
expect(UpgradeCommand.findProjectRoot(proj), proj);
|
||||
expect(UpgradeCommand.findProjectRoot(path.join(proj, 'lib')), proj);
|
||||
|
||||
String hello = path.join(Cache.flutterRoot, 'examples', 'hello_world');
|
||||
expect(UpgradeCommand.findProjectRoot(hello), hello);
|
||||
expect(UpgradeCommand.findProjectRoot(path.join(hello, 'lib')), hello);
|
||||
});
|
||||
|
||||
testUsingContext('outside project', () async {
|
||||
await createProject();
|
||||
expect(UpgradeCommand.findProjectRoot(temp.parent.path), null);
|
||||
expect(UpgradeCommand.findProjectRoot(Cache.flutterRoot), null);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user