From 750796846cd50bbe2d0655933920f0fed235edd8 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 8 Apr 2016 10:23:47 -0700 Subject: [PATCH] move services to using cache.dart (#3211) --- .../flutter_tools/flutter_analysis_options | 2 +- packages/flutter_tools/lib/src/artifacts.dart | 83 ++----------------- packages/flutter_tools/lib/src/cache.dart | 28 ++++++- .../lib/src/commands/run_mojo.dart | 6 +- .../flutter_tools/lib/src/commands/skia.dart | 1 + packages/flutter_tools/lib/src/services.dart | 3 +- packages/flutter_tools/lib/src/zip.dart | 2 +- 7 files changed, 38 insertions(+), 87 deletions(-) diff --git a/packages/flutter_tools/flutter_analysis_options b/packages/flutter_tools/flutter_analysis_options index 616925d2f2..15a7f08093 100644 --- a/packages/flutter_tools/flutter_analysis_options +++ b/packages/flutter_tools/flutter_analysis_options @@ -4,7 +4,7 @@ # to opt-in to all desired lints (https://github.com/dart-lang/sdk/issues/25843). # For a list of lints, see: http://dart-lang.github.io/linter/lints/ -# This file is the .analyzis_options file used by "flutter analyze". +# This file is the .analysis_options file used by "flutter analyze". # It isn't named that because otherwise editors like Atom would try # to use it, and that wouldn't work because it enables things that # need to be silenced, in particular, public_member_api_docs. diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart index bbf2e52dba..a9b5a82394 100644 --- a/packages/flutter_tools/lib/src/artifacts.dart +++ b/packages/flutter_tools/lib/src/artifacts.dart @@ -2,10 +2,8 @@ // 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:archive/archive.dart'; import 'package:path/path.dart' as path; import 'base/process.dart'; @@ -179,86 +177,21 @@ class ArtifactStore { return _engineRevision; } - static Directory getBaseCacheDir() { - if (flutterRoot == null) { - printError('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()) { - printError('${cacheDir.path} does not exist. Cannot find artifact cache.'); - throw new ProcessExit(2); - } - return cacheDir; + static Directory _getBaseCacheDir() { + return new Directory(path.join(flutterRoot, 'bin', 'cache', 'artifacts')); } + // TODO(devoncarew): There are 5 call-sites of this (run_mojo, build_apk, the + // test command, toolchain, setup_xcodeproj); move them over to using + // something from `cache.dart`. static String getPath(Artifact artifact) { - File cachedFile = new File(path.join( - getBaseCacheDir().path, 'engine', artifact.platform, artifact.fileName - )); + File cachedFile = new File( + path.join(_getBaseCacheDir().path, 'engine', artifact.platform, artifact.fileName) + ); if (!cachedFile.existsSync()) { printError('File not found in the platform artifacts: ${cachedFile.path}'); throw new ProcessExit(2); } return cachedFile.path; } - - /// Download a file from the given URL and return the bytes. - static Future> _downloadFile(Uri url) async { - printStatus('Downloading $url.'); - - HttpClient httpClient = new HttpClient(); - HttpClientRequest request = await httpClient.getUrl(url); - HttpClientResponse response = await request.close(); - printTrace('Received response statusCode=${response.statusCode}'); - if (response.statusCode != 200) - throw new Exception(response.reasonPhrase); - - BytesBuilder responseBody = new BytesBuilder(copy: false); - await for (List chunk in response) - responseBody.add(chunk); - - return responseBody.takeBytes(); - } - - /// Download a file from the given url and write it to the cache. - /// If [unzip] is true, treat the url as a zip file, and unzip it to the - /// directory given. - static Future _downloadFileToCache(Uri url, FileSystemEntity cachedFile, bool unzip) async { - if (!cachedFile.parent.existsSync()) - cachedFile.parent.createSync(recursive: true); - - List fileBytes = await _downloadFile(url); - if (unzip) { - if (cachedFile is Directory && !cachedFile.existsSync()) - cachedFile.createSync(recursive: true); - - Archive archive = new ZipDecoder().decodeBytes(fileBytes); - for (ArchiveFile archiveFile in archive) { - File subFile = new File(path.join(cachedFile.path, archiveFile.name)); - subFile.writeAsBytesSync(archiveFile.content, flush: true); - } - } else { - File asFile = new File(cachedFile.path); - asFile.writeAsBytesSync(fileBytes, flush: true); - } - } - - static Future getThirdPartyFile(String urlStr, String cacheSubdir, bool unzip) async { - Uri url = Uri.parse(urlStr); - Directory baseDir = getBaseCacheDir(); - Directory cacheDir = new Directory(path.join( - baseDir.path, 'third_party', cacheSubdir)); - File cachedFile = new File( - path.join(cacheDir.path, url.pathSegments[url.pathSegments.length-1])); - if (!cachedFile.existsSync()) { - try { - await _downloadFileToCache(url, cachedFile, unzip); - } catch (e) { - printError('Failed to fetch third-party artifact: $url: $e'); - throw new ProcessExit(2); - } - } - return cachedFile.path; - } } diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index e17f7a5c0c..712d1272ad 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -14,6 +14,7 @@ import 'base/os.dart'; import 'base/process.dart'; import 'globals.dart'; +/// A warpper around the `bin/cache/` directory. class Cache { static Cache get instance => context[Cache] ?? (context[Cache] = new Cache()); @@ -52,6 +53,29 @@ class Cache { stampFile.writeAsStringSync(version); } + Future getThirdPartyFile(String urlStr, String serviceName, { + bool unzip: false + }) async { + Uri url = Uri.parse(urlStr); + Directory thirdPartyDir = getArtifactDirectory('third_party'); + + Directory serviceDir = new Directory(path.join(thirdPartyDir.path, serviceName)); + if (!serviceDir.existsSync()) + serviceDir.createSync(recursive: true); + + File cachedFile = new File(path.join(serviceDir.path, url.pathSegments.last)); + if (!cachedFile.existsSync()) { + try { + await _downloadFileToCache(url, cachedFile, unzip); + } catch (e) { + printError('Failed to fetch third-party artifact $url: $e'); + throw e; + } + } + + return cachedFile.path; + } + Future updateAll() async { MaterialFonts materialFonts = new MaterialFonts(cache); if (!materialFonts.isUpToDate()) @@ -133,10 +157,6 @@ class MaterialFonts { } } -// TODO(devoncarew): Move the services download to here. -// gs://flutter_infra/flutter/ed3014b3d337d025393bd894ffa2897e05d43e91/firebase/ -// gs://flutter_infra/flutter/ed3014b3d337d025393bd894ffa2897e05d43e91/gcm/ - class FlutterEngine { FlutterEngine(this.cache); diff --git a/packages/flutter_tools/lib/src/commands/run_mojo.dart b/packages/flutter_tools/lib/src/commands/run_mojo.dart index 916bc43d42..8368cdb475 100644 --- a/packages/flutter_tools/lib/src/commands/run_mojo.dart +++ b/packages/flutter_tools/lib/src/commands/run_mojo.dart @@ -105,9 +105,8 @@ class RunMojoCommand extends FlutterCommand { flutterPath = _makePathAbsolute(localPath); } - if (argResults['android']) { + if (argResults['android']) args.add('--android'); - } final Uri appUri = Uri.parse(targetApp); if (appUri.scheme.isEmpty || appUri.scheme == 'file') { @@ -141,9 +140,8 @@ class RunMojoCommand extends FlutterCommand { args.add('--verbose'); } - if (argResults['checked']) { + if (argResults['checked']) args.add('--args-for=mojo:flutter --enable-checked-mode'); - } args.addAll(argResults.rest); printStatus('$args'); diff --git a/packages/flutter_tools/lib/src/commands/skia.dart b/packages/flutter_tools/lib/src/commands/skia.dart index 3aeba2d39d..3e5f0f46a2 100644 --- a/packages/flutter_tools/lib/src/commands/skia.dart +++ b/packages/flutter_tools/lib/src/commands/skia.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:io'; + import 'package:http/http.dart' as http; import '../base/common.dart'; diff --git a/packages/flutter_tools/lib/src/services.dart b/packages/flutter_tools/lib/src/services.dart index 1fc1fe8d74..f041293bda 100644 --- a/packages/flutter_tools/lib/src/services.dart +++ b/packages/flutter_tools/lib/src/services.dart @@ -9,7 +9,6 @@ import 'dart:io'; import 'package:path/path.dart' as path; import 'package:yaml/yaml.dart'; -import 'artifacts.dart'; import 'globals.dart'; import 'package_map.dart'; @@ -75,7 +74,7 @@ Future getServiceFromUrl( return url.replaceAll('android-sdk:', '${androidSdk.directory}/'); } else if (url.startsWith("http")) { // It's a regular file to download. - return await ArtifactStore.getThirdPartyFile(url, serviceName, unzip); + return await cache.getThirdPartyFile(url, serviceName, unzip: unzip); } else { // Assume url is a path relative to the service's root dir. return path.join(rootDir, url); diff --git a/packages/flutter_tools/lib/src/zip.dart b/packages/flutter_tools/lib/src/zip.dart index f607ed9ba9..b1edc908c0 100644 --- a/packages/flutter_tools/lib/src/zip.dart +++ b/packages/flutter_tools/lib/src/zip.dart @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:io'; import 'dart:convert' show UTF8; +import 'dart:io'; import 'package:archive/archive.dart'; import 'package:path/path.dart' as path;