From 2710e0f468cb1ef9807e1aa534ed2aeffe5b2f8c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sat, 7 Nov 2015 15:02:27 -0800 Subject: [PATCH] Teach flutter init how to depend on flutter using a relative path --- bin/cache/.gitignore | 1 + bin/flutter | 2 +- packages/flutter_tools/lib/src/artifacts.dart | 12 ++++++-- .../src/commands/flutter_command_runner.dart | 3 ++ .../flutter_tools/lib/src/commands/init.dart | 29 ++++++++++++++----- packages/flutter_tools/test/init_test.dart | 2 ++ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/bin/cache/.gitignore b/bin/cache/.gitignore index a46a10366c..565eb5edbd 100644 --- a/bin/cache/.gitignore +++ b/bin/cache/.gitignore @@ -1,2 +1,3 @@ *.snapshot *.stamp +artifacts diff --git a/bin/flutter b/bin/flutter index a83fb57d17..31c97377f5 100755 --- a/bin/flutter +++ b/bin/flutter @@ -3,7 +3,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -FLUTTER_ROOT=$(dirname $(dirname "${BASH_SOURCE[0]}")) +export FLUTTER_ROOT=$(dirname $(dirname "${BASH_SOURCE[0]}")) FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools" SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot" STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp" diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart index bc2a8502e6..17da362ded 100644 --- a/packages/flutter_tools/lib/src/artifacts.dart +++ b/packages/flutter_tools/lib/src/artifacts.dart @@ -10,6 +10,7 @@ import 'package:path/path.dart' as path; import 'build_configuration.dart'; import 'os_utils.dart'; +import 'process.dart'; final Logger _logging = new Logger('sky_tools.artifacts'); @@ -155,7 +156,10 @@ class ArtifactStore { return null; } + // These values are initialized by FlutterCommandRunner on startup. + static String flutterRoot; static String packageRoot; + static String _engineRevision; static String get engineRevision { @@ -190,7 +194,11 @@ class ArtifactStore { } static Directory _getBaseCacheDir() { - Directory cacheDir = new Directory(path.join(packageRoot, 'sky_tools', 'cache')); + if (flutterRoot == null) { + _logging.severe('FLUTTER_ROOT not specified. Cannot find artifact cache.'); + throw new ProcessExit(2); + } + Directory cacheDir = new Directory(path.join(flutterRoot, 'bin', 'cache', 'artifacts')); if (!cacheDir.existsSync()) cacheDir.createSync(recursive: true); return cacheDir; @@ -198,8 +206,6 @@ class ArtifactStore { static Directory _getCacheDirForArtifact(Artifact artifact) { Directory baseDir = _getBaseCacheDir(); - // For now, all downloaded artifacts are release mode host binaries so use - // a path that mirrors a local release build. // TODO(jamesr): Add support for more configurations. String config = 'Release'; Directory artifactSpecificDir = new Directory(path.join( diff --git a/packages/flutter_tools/lib/src/commands/flutter_command_runner.dart b/packages/flutter_tools/lib/src/commands/flutter_command_runner.dart index 36b39be21c..f4659be1a3 100644 --- a/packages/flutter_tools/lib/src/commands/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/commands/flutter_command_runner.dart @@ -29,6 +29,8 @@ class FlutterCommandRunner extends CommandRunner { 'shell commands executed.'); argParser.addOption('package-root', help: 'Path to your packages directory.', defaultsTo: 'packages'); + argParser.addOption('flutter-root', + help: 'The root directory of the Flutter repository.'); argParser.addOption('android-device-id', help: 'Serial number of the target Android device.'); @@ -105,6 +107,7 @@ class FlutterCommandRunner extends CommandRunner { Logger.root.level = Level.FINE; _globalResults = globalResults; + ArtifactStore.flutterRoot = globalResults['flutter-root'] ?? Platform.environment['FLUTTER_ROOT']; ArtifactStore.packageRoot = globalResults['package-root']; return super.runCommand(globalResults); diff --git a/packages/flutter_tools/lib/src/commands/init.dart b/packages/flutter_tools/lib/src/commands/init.dart index ed1db5ba7e..a3b9914b7a 100644 --- a/packages/flutter_tools/lib/src/commands/init.dart +++ b/packages/flutter_tools/lib/src/commands/init.dart @@ -9,6 +9,7 @@ import 'package:args/command_runner.dart'; import 'package:mustache4dart/mustache4dart.dart' as mustache; import 'package:path/path.dart' as p; +import '../artifacts.dart'; import '../process.dart'; class InitCommand extends Command { @@ -30,17 +31,30 @@ class InitCommand extends Command { return 2; } + if (ArtifactStore.flutterRoot == null) { + stderr.writeln('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment'); + stderr.writeln('variable was specified. Unable to find package:flutter.'); + return 2; + } + String flutterRoot = p.absolute(ArtifactStore.flutterRoot); + + String flutterPackagePath = p.join(flutterRoot, 'packages', 'flutter'); + if (!FileSystemEntity.isFileSync(p.join(flutterPackagePath, 'pubspec.yaml'))) { + print('Unable to find package:flutter in ${flutterPackagePath}'); + return 2; + } + // TODO: Confirm overwrite of an existing directory with the user. Directory out = new Directory(argResults['out']); - new FlutterSimpleTemplate().generateInto(out); + new FlutterSimpleTemplate().generateInto(out, flutterPackagePath); print(''); String message = '''All done! To run your application: \$ cd ${out.path} - \$ flutter start --checked + \$ flutter start '''; if (argResults['pub']) { @@ -66,14 +80,16 @@ abstract class Template { Template(this.name, this.description); - void generateInto(Directory dir) { + void generateInto(Directory dir, String flutterPackagePath) { String dirPath = p.normalize(dir.absolute.path); String projectName = _normalizeProjectName(p.basename(dirPath)); print('Creating ${p.basename(projectName)}...'); dir.createSync(recursive: true); + String relativeFlutterPackagePath = p.relative(flutterPackagePath, from: dirPath); + files.forEach((String path, String contents) { - Map m = {'projectName': projectName, 'description': description}; + Map m = {'projectName': projectName, 'description': description, 'flutterPackagePath': relativeFlutterPackagePath}; contents = mustache.render(contents, m); path = path.replaceAll('/', Platform.pathSeparator); File file = new File(p.join(dir.path, path)); @@ -129,9 +145,8 @@ const String _pubspec = r''' name: {{projectName}} description: {{description}} dependencies: - flutter: ">=0.0.2 <0.1.0" -dev_dependencies: - sky_tools: any + flutter: + path: {{flutterPackagePath}} '''; const String _flutterYaml = r''' diff --git a/packages/flutter_tools/test/init_test.dart b/packages/flutter_tools/test/init_test.dart index c14e4f8d4b..f93bf14f71 100644 --- a/packages/flutter_tools/test/init_test.dart +++ b/packages/flutter_tools/test/init_test.dart @@ -6,6 +6,7 @@ import 'dart:io'; import 'package:args/command_runner.dart'; import 'package:path/path.dart' as p; +import 'package:sky_tools/src/artifacts.dart'; import 'package:sky_tools/src/commands/init.dart'; import 'package:sky_tools/src/process.dart'; import 'package:test/test.dart'; @@ -29,6 +30,7 @@ defineTests() { if (!Platform.isWindows) { // Verify that we create a project that is well-formed. test('flutter-simple', () async { + ArtifactStore.flutterRoot = '../..'; InitCommand command = new InitCommand(); CommandRunner runner = new CommandRunner('test_flutter', '') ..addCommand(command);