Commit 1ea2f403 authored by Dan Rubel's avatar Dan Rubel Committed by GitHub

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
parent 5f7af56f
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as path;
import '../base/process.dart'; import '../base/process.dart';
import '../dart/pub.dart'; import '../dart/pub.dart';
import '../cache.dart'; import '../cache.dart';
...@@ -61,9 +63,11 @@ class UpgradeCommand extends FlutterCommand { ...@@ -61,9 +63,11 @@ class UpgradeCommand extends FlutterCommand {
printStatus(''); printStatus('');
printStatus(FlutterVersion.getVersion(Cache.flutterRoot).toString()); printStatus(FlutterVersion.getVersion(Cache.flutterRoot).toString());
if (FileSystemEntity.isFileSync('pubspec.yaml')) { String projRoot = findProjectRoot();
if (projRoot != null) {
printStatus(''); printStatus('');
code = await pubGet(upgrade: true, checkLastModified: false); code = await pubGet(
directory: projRoot, upgrade: true, checkLastModified: false);
if (code != 0) if (code != 0)
return code; return code;
...@@ -86,4 +90,20 @@ class UpgradeCommand extends FlutterCommand { ...@@ -86,4 +90,20 @@ class UpgradeCommand extends FlutterCommand {
|| _gitChangedRegex.hasMatch(line) || _gitChangedRegex.hasMatch(line)
|| line == 'Fast-forward'; || 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 @@ ...@@ -2,9 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // 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:flutter_tools/src/commands/upgrade.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'src/common.dart';
import 'src/context.dart';
void main() { void main() {
group('upgrade', () { group('upgrade', () {
bool _match(String line) => UpgradeCommand.matchesGitLine(line); bool _match(String line) => UpgradeCommand.matchesGitLine(line);
...@@ -24,5 +34,42 @@ void main() { ...@@ -24,5 +34,42 @@ void main() {
expect(_match('Updating 79cfe1e..5046107'), false); expect(_match('Updating 79cfe1e..5046107'), false);
expect(_match('229 files changed, 6179 insertions(+), 3065 deletions(-)'), 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);
});
});
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment