Merge pull request #2960 from abarth/use_packages

Use --packages rather than --package-root
diff --git a/bin/cache/engine.version b/bin/cache/engine.version
index 5e830c2..1ce3d8c 100644
--- a/bin/cache/engine.version
+++ b/bin/cache/engine.version
@@ -1 +1 @@
-f7909035f11b1347814ea2c2942426ef3cf7f44d
+ce3b4547ac579f67a6e473f7f5b5eac4357424ff
diff --git a/bin/flutter b/bin/flutter
index 883e48f..42f5c83 100755
--- a/bin/flutter
+++ b/bin/flutter
@@ -35,7 +35,7 @@
   echo Building flutter tool...
   FLUTTER_DIR="$FLUTTER_ROOT/packages/flutter"
   (cd "$FLUTTER_TOOLS_DIR"; "../../bin/cache/dart-sdk/bin/pub" get --verbosity=warning)
-  "$DART" --snapshot="$SNAPSHOT_PATH" --package-root="$FLUTTER_TOOLS_DIR/packages" "$SCRIPT_PATH"
+  "$DART" --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH"
   echo $REVISION > "$STAMP_PATH"
 fi
 
@@ -51,7 +51,7 @@
 
 if [ $FLUTTER_DEV ]; then
   echo -e "(FLUTTER_DEV set - ignoring $SNAPSHOT_PATH)\n"
-  "$DART" --package-root="$FLUTTER_TOOLS_DIR/packages" "$SCRIPT_PATH" "$@"
+  "$DART" --packages="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH" "$@"
 else
   "$DART" "$SNAPSHOT_PATH" "$@"
 fi
@@ -65,5 +65,5 @@
 
 set -e
 
-"$DART" --snapshot="$SNAPSHOT_PATH" --package-root="$FLUTTER_TOOLS_DIR/packages" "$SCRIPT_PATH"
+"$DART" --snapshot="$SNAPSHOT_PATH" --package="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH"
 "$DART" "$SNAPSHOT_PATH" "$@"
diff --git a/bin/flutter.bat b/bin/flutter.bat
index d047aa6..8532b3d 100644
--- a/bin/flutter.bat
+++ b/bin/flutter.bat
@@ -38,7 +38,7 @@
 REM Allows us to check if sky_engine's REVISION is correct
 CALL pub.bat get
 CD "%flutter_root%"
-CALL %dart% --snapshot="%snapshot_path%" --package-root="%flutter_tools_dir%\packages" "%script_path%"
+CALL %dart% --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
 <nul SET /p=%revision%> "%stamp_path%"
 
 :after_snapshot
@@ -48,6 +48,6 @@
 CALL %dart% "%snapshot_path%" %*
 
 IF /I "%ERRORLEVEL%" EQU "253" (
-   CALL %dart% --snapshot="%snapshot_path%" --package-root="%flutter_tools_dir%\packages" "%script_path%"
+   CALL %dart% --snapshot="%snapshot_path%" --packages="%flutter_tools_dir%\.packages" "%script_path%"
    CALL %dart% "%snapshot_path%" %*
 )
diff --git a/packages/flutter/pubspec.yaml b/packages/flutter/pubspec.yaml
index 1c0dd54..ac749df 100644
--- a/packages/flutter/pubspec.yaml
+++ b/packages/flutter/pubspec.yaml
@@ -9,6 +9,9 @@
   intl: '>=0.12.4+2 <0.13.0'
   vector_math: '>=1.4.5 <2.0.0'
 
+  # We need to pin crypto because archive can't handle larger numbers.
+  crypto: 0.9.2
+
   sky_engine:
     path: ../../bin/cache/pkg/sky_engine
   sky_services:
diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart
index 2c5e375..bbf2e52 100644
--- a/packages/flutter_tools/lib/src/artifacts.dart
+++ b/packages/flutter_tools/lib/src/artifacts.dart
@@ -165,27 +165,8 @@
     return null;
   }
 
-  // These values are initialized by FlutterCommandRunner on startup.
+  // Initialized by FlutterCommandRunner on startup.
   static String flutterRoot;
-  static String packageRoot = 'packages';
-
-  static bool get isPackageRootValid {
-    return FileSystemEntity.isDirectorySync(packageRoot);
-  }
-
-  static void ensurePackageRootIsValid() {
-    if (!isPackageRootValid) {
-      String message = '$packageRoot is not a valid directory.';
-      if (packageRoot == 'packages') {
-        if (FileSystemEntity.isFileSync('pubspec.yaml'))
-          message += '\nDid you run `pub get` in this directory?';
-        else
-          message += '\nDid you run this command from the same directory as your pubspec.yaml file?';
-      }
-      printError(message);
-      throw new ProcessExit(2);
-    }
-  }
 
   static String _engineRevision;
 
diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart
index 4828cbb..cf5d98d 100644
--- a/packages/flutter_tools/lib/src/commands/test.dart
+++ b/packages/flutter_tools/lib/src/commands/test.dart
@@ -11,6 +11,7 @@
 import '../artifacts.dart';
 import '../build_configuration.dart';
 import '../globals.dart';
+import '../package_map.dart';
 import '../runner/flutter_command.dart';
 import '../test/flutter_platform.dart' as loader;
 
@@ -99,8 +100,8 @@
 
     // If we're running the flutter tests, we want to use the packages directory
     // from the flutter package in order to find the proper shell binary.
-    if (runFlutterTests && ArtifactStore.packageRoot == 'packages')
-      ArtifactStore.packageRoot = path.join(ArtifactStore.flutterRoot, 'packages', 'flutter', 'packages');
+    if (runFlutterTests)
+      PackageMap.instance = new PackageMap(path.join(ArtifactStore.flutterRoot, 'packages', 'flutter', '.packages'));
 
     Directory testDir = runFlutterTests ? _flutterUnitTestDir : _currentPackageTestDir;
 
diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart
index 2703232..7d28f4e 100644
--- a/packages/flutter_tools/lib/src/ios/simulators.dart
+++ b/packages/flutter_tools/lib/src/ios/simulators.dart
@@ -467,7 +467,7 @@
     List<String> args = <String>[
       "--flx=${path.absolute(path.join('build', 'app.flx'))}",
       "--dart-main=${path.absolute(mainPath)}",
-      "--package-root=${path.absolute('packages')}",
+      "--packages=${path.absolute('.packages')}",
     ];
 
     if (checked)
diff --git a/packages/flutter_tools/lib/src/package_map.dart b/packages/flutter_tools/lib/src/package_map.dart
new file mode 100644
index 0000000..473a10e
--- /dev/null
+++ b/packages/flutter_tools/lib/src/package_map.dart
@@ -0,0 +1,42 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:io';
+
+import 'package:package_config/packages_file.dart' as packages_file;
+import 'package:path/path.dart' as path;
+
+const String _kPackages = '.packages';
+
+Map<String, Uri> _parse(String packagesPath) {
+  List<int> source = new File(packagesPath).readAsBytesSync();
+  return packages_file.parse(source, new Uri.file(packagesPath));
+}
+
+class PackageMap {
+  PackageMap(this.packagesPath);
+
+  final String packagesPath;
+
+  Map<String, Uri> get map {
+    if (_map == null)
+      _map = _parse(packagesPath);
+    return _map;
+  }
+  Map<String, Uri> _map;
+
+  static PackageMap instance;
+
+  String checkValid() {
+    if (FileSystemEntity.isFileSync(packagesPath))
+      return null;
+    String message = '$packagesPath does not exist.';
+    String pubspecPath = path.absolute(path.dirname(packagesPath), 'pubspec.yaml');
+    if (FileSystemEntity.isFileSync(pubspecPath))
+      message += '\nDid you run `pub get` in this directory?';
+    else
+      message += '\nDid you run this command from the same directory as your pubspec.yaml file?';
+    return message;
+  }
+}
diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart
index 522563c..9aa4d3a 100644
--- a/packages/flutter_tools/lib/src/runner/flutter_command.dart
+++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart
@@ -7,12 +7,13 @@
 
 import 'package:args/command_runner.dart';
 
-import '../dart/pub.dart';
 import '../application_package.dart';
 import '../build_configuration.dart';
+import '../dart/pub.dart';
 import '../device.dart';
 import '../flx.dart' as flx;
 import '../globals.dart';
+import '../package_map.dart';
 import '../toolchain.dart';
 import 'flutter_command_runner.dart';
 
@@ -147,6 +148,12 @@
       }
     }
 
+    String error = PackageMap.instance.checkValid();
+    if (error != null) {
+      printError(error);
+      return false;
+    }
+
     return true;
   }
 
diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
index 12677af0..2350e4a 100644
--- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
+++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart
@@ -9,13 +9,14 @@
 import 'package:args/command_runner.dart';
 import 'package:path/path.dart' as path;
 
-import '../android/android_sdk.dart';
 import '../artifacts.dart';
+import '../android/android_sdk.dart';
 import '../base/context.dart';
 import '../base/logger.dart';
 import '../base/process.dart';
 import '../build_configuration.dart';
 import '../globals.dart';
+import '../package_map.dart';
 import 'version.dart';
 
 const String kFlutterRootEnvironmentVariableName = 'FLUTTER_ROOT'; // should point to //flutter/ (root of flutter/flutter repo)
@@ -41,13 +42,13 @@
         help: 'Reports the version of this tool.');
 
     String packagesHelp;
-    if (ArtifactStore.isPackageRootValid)
-      packagesHelp = '\n(defaults to "${ArtifactStore.packageRoot}")';
+    if (FileSystemEntity.isFileSync('.packages'))
+      packagesHelp = '\n(defaults to ".packages")';
     else
-      packagesHelp = '\n(required, since the current directory does not contain a "packages" subdirectory)';
-    argParser.addOption('package-root',
+      packagesHelp = '\n(required, since the current directory does not contain a ".packages" file)';
+    argParser.addOption('packages',
         hide: !verboseHelp,
-        help: 'Path to your packages directory.$packagesHelp');
+        help: 'Path to your ".packages" file.$packagesHelp');
     argParser.addOption('flutter-root',
         help: 'The root directory of the Flutter repository (uses \$$kFlutterRootEnvironmentVariableName if set).',
               defaultsTo: _defaultFlutterRoot);
@@ -184,8 +185,9 @@
     // we must set ArtifactStore.flutterRoot early because other features use it
     // (e.g. enginePath's initialiser uses it)
     ArtifactStore.flutterRoot = path.normalize(path.absolute(globalResults['flutter-root']));
-    if (globalResults.wasParsed('package-root'))
-      ArtifactStore.packageRoot = path.normalize(path.absolute(globalResults['package-root']));
+    PackageMap.instance = new PackageMap(path.normalize(path.absolute(
+      globalResults.wasParsed('packages') ? globalResults['packages'] : '.packages'
+    )));
 
     // See if the user specified a specific device.
     deviceManager.specifiedDeviceId = globalResults['device-id'];
@@ -219,16 +221,13 @@
     bool isRelease = globalResults['release'];
 
     if (engineSourcePath == null && (isDebug || isRelease)) {
-      if (ArtifactStore.isPackageRootValid) {
-        Directory engineDir = new Directory(path.join(ArtifactStore.packageRoot, kFlutterEnginePackageName));
-        try {
-          String realEnginePath = engineDir.resolveSymbolicLinksSync();
-          engineSourcePath = path.dirname(path.dirname(path.dirname(path.dirname(realEnginePath))));
-          bool dirExists = FileSystemEntity.isDirectorySync(path.join(engineSourcePath, 'out'));
-          if (engineSourcePath == '/' || engineSourcePath.isEmpty || !dirExists)
-            engineSourcePath = null;
-        } on FileSystemException { }
-      }
+      try {
+        Uri engineUri = PackageMap.instance.map[kFlutterEnginePackageName];
+        engineSourcePath = path.dirname(path.dirname(path.dirname(path.dirname(engineUri.path))));
+        bool dirExists = FileSystemEntity.isDirectorySync(path.join(engineSourcePath, 'out'));
+        if (engineSourcePath == '/' || engineSourcePath.isEmpty || !dirExists)
+          engineSourcePath = null;
+      } on FileSystemException { } on FormatException { }
 
       if (engineSourcePath == null)
         engineSourcePath = _tryEnginePath(path.join(ArtifactStore.flutterRoot, '../engine/src'));
diff --git a/packages/flutter_tools/lib/src/services.dart b/packages/flutter_tools/lib/src/services.dart
index 552acef..1fc1fe8 100644
--- a/packages/flutter_tools/lib/src/services.dart
+++ b/packages/flutter_tools/lib/src/services.dart
@@ -11,6 +11,7 @@
 
 import 'artifacts.dart';
 import 'globals.dart';
+import 'package_map.dart';
 
 const String _kFlutterManifestPath = 'flutter.yaml';
 const String _kFlutterServicesManifestPath = 'flutter_services.yaml';
@@ -28,22 +29,25 @@
 Future<Null> parseServiceConfigs(
   List<Map<String, String>> services, { List<File> jars }
 ) async {
-  if (!ArtifactStore.isPackageRootValid) {
-    printTrace("Artifact store invalid while parsing service configs");
+  Map<String, Uri> packageMap;
+  try {
+    packageMap = PackageMap.instance.map;
+  } on FormatException catch(e) {
+    printTrace('Invalid ".packages" file while parsing service configs:\n$e');
     return;
   }
 
   dynamic manifest = _loadYamlFile(_kFlutterManifestPath);
   if (manifest == null || manifest['services'] == null) {
-    printTrace("No services specified in the manifest");
+    printTrace('No services specified in the manifest');
     return;
   }
 
   for (String service in manifest['services']) {
-    String serviceRoot = '${ArtifactStore.packageRoot}/$service';
+    String serviceRoot = packageMap[service].path;
     dynamic serviceConfig = _loadYamlFile('$serviceRoot/$_kFlutterServicesManifestPath');
     if (serviceConfig == null) {
-      printStatus("No $_kFlutterServicesManifestPath found for service '$serviceRoot'. Skipping.");
+      printStatus('No $_kFlutterServicesManifestPath found for service "$serviceRoot". Skipping.');
       continue;
     }
 
diff --git a/packages/flutter_tools/lib/src/test/flutter_platform.dart b/packages/flutter_tools/lib/src/test/flutter_platform.dart
index f2db417..b890b44 100644
--- a/packages/flutter_tools/lib/src/test/flutter_platform.dart
+++ b/packages/flutter_tools/lib/src/test/flutter_platform.dart
@@ -14,7 +14,7 @@
 import 'package:test/src/runner/plugin/platform.dart'; // ignore: implementation_imports
 import 'package:test/src/runner/plugin/hack_register_platform.dart' as hack; // ignore: implementation_imports
 
-import '../artifacts.dart';
+import '../package_map.dart';
 
 final String _kSkyShell = Platform.environment['SKY_SHELL'];
 const String _kHost = '127.0.0.1';
@@ -44,12 +44,12 @@
   return new _ServerInfo(server, 'ws://$_kHost:${server.port}$_kPath', socket.future);
 }
 
-Future<Process> _startProcess(String mainPath, { String packageRoot }) {
+Future<Process> _startProcess(String mainPath, { String packages }) {
   assert(shellPath != null || _kSkyShell != null); // Please provide the path to the shell in the SKY_SHELL environment variable.
   return Process.start(shellPath ?? _kSkyShell, [
     '--enable-checked-mode',
     '--non-interactive',
-    '--package-root=$packageRoot',
+    '--packages=$packages',
     mainPath,
   ]);
 }
@@ -90,8 +90,7 @@
 ''');
 
     Process process = await _startProcess(
-      listenerFile.path,
-      packageRoot: path.absolute(ArtifactStore.packageRoot)
+      listenerFile.path, packages: PackageMap.instance.packagesPath
     );
 
     void finalize() {
diff --git a/packages/flutter_tools/lib/src/toolchain.dart b/packages/flutter_tools/lib/src/toolchain.dart
index 9f4a9a5..25015d4 100644
--- a/packages/flutter_tools/lib/src/toolchain.dart
+++ b/packages/flutter_tools/lib/src/toolchain.dart
@@ -10,6 +10,7 @@
 import 'artifacts.dart';
 import 'base/process.dart';
 import 'build_configuration.dart';
+import 'package_map.dart';
 
 class SnapshotCompiler {
   SnapshotCompiler(this._path);
@@ -25,7 +26,7 @@
     final List<String> args = [
       _path,
       mainPath,
-      '--package-root=${ArtifactStore.packageRoot}',
+      '--packages=${PackageMap.instance.packagesPath}',
       '--snapshot=$snapshotPath'
     ];
     if (depfilePath != null)
diff --git a/packages/flutter_tools/pubspec.yaml b/packages/flutter_tools/pubspec.yaml
index 2224524..84fa2dc 100644
--- a/packages/flutter_tools/pubspec.yaml
+++ b/packages/flutter_tools/pubspec.yaml
@@ -11,11 +11,12 @@
   analyzer: '>=0.26.1+17' # see note below
   archive: ^1.0.20
   args: ^0.13.4
-  crypto: ^0.9.2
+  crypto: 0.9.2
   den_api: ^0.1.0
   file: ^0.1.0
   json_schema: ^1.0.3
   mustache4dart: ^1.0.0
+  package_config: ^0.1.3
   path: ^1.3.0
   pub_semver: ^1.0.0
   stack_trace: ^1.4.0
diff --git a/packages/flx/pubspec.yaml b/packages/flx/pubspec.yaml
index 00cd7e2..5cf07be 100644
--- a/packages/flx/pubspec.yaml
+++ b/packages/flx/pubspec.yaml
@@ -7,7 +7,7 @@
   bignum: ^0.1.0
   asn1lib: ^0.4.1
   pointycastle: 0.10.0
-  crypto: ^0.9.2
+  crypto: 0.9.2
 
 environment:
   sdk: '>=1.12.0 <2.0.0'