Commit 75079684 authored by Devon Carew's avatar Devon Carew

move services to using cache.dart (#3211)

parent 65b36f13
......@@ -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.
......
......@@ -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<List<int>> _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<int> 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<Null> _downloadFileToCache(Uri url, FileSystemEntity cachedFile, bool unzip) async {
if (!cachedFile.parent.existsSync())
cachedFile.parent.createSync(recursive: true);
List<int> 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<String> 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;
}
}
......@@ -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<String> 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<Null> 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);
......
......@@ -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');
......
......@@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' as http;
import '../base/common.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<String> 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);
......
......@@ -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;
......
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