Commit cbc35dfa authored by James Robinson's avatar James Robinson

Download sky_snapshot from the cloud

This adds logic to download and use the sky_snapshot binary from
Google cloud storage when running the 'sky_tools build' command.
The downloaded binary is put into lib/cache/... The binary is
chosen to match the REVISION in the sky_engine package in the
packages directory of whichever package the user wishes to
build a flx from.

Known issues:
*) Assumes linux-x64 host
*) Assumes download will always produce valid executable
*) No clearing of stale cache entries
parent 3e9ceec0
...@@ -8,15 +8,52 @@ import 'dart:async'; ...@@ -8,15 +8,52 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
enum Artifact { enum Artifact {
FlutterCompiler FlutterCompiler,
} }
class _ArtifactStore { class _ArtifactStore {
_ArtifactStore._(); _ArtifactStore._();
Future<File> getPath(Artifact artifact) async { Future _downloadFile(String url, File file) async {
// TODO(abarth): Download artifacts from cloud storage. HttpClient httpClient = new HttpClient();
return new File(''); HttpClientRequest request = await httpClient.getUrl(Uri.parse(url));
HttpClientResponse response = await request.close();
if (response.statusCode != 200) throw new Exception(response.reasonPhrase);
IOSink sink = file.openWrite();
await sink.addStream(response);
await sink.close();
}
Future<String> _getEngineRevision(String packageRoot) {
return new File(packageRoot + '/sky_engine/REVISION').readAsString();
}
Future<Directory> _cacheDir(String engineRevision, String packageRoot) async {
String cacheDirPath = '${packageRoot}/sky_tools/cache/sky_engine/${engineRevision}/';
Directory cacheDir = new Directory(cacheDirPath);
if (!await cacheDir.exists()) {
await cacheDir.create(recursive: true);
}
return cacheDir;
}
Future<String> getPath(Artifact artifact, String packageRoot) async {
String engineRevision = await _getEngineRevision(packageRoot);
Directory cacheDir = await _cacheDir(engineRevision, packageRoot);
if (artifact == Artifact.FlutterCompiler) {
File skySnapshotFile = new File(cacheDir.path + 'sky_snapshot');
if (!await skySnapshotFile.exists()) {
print('Downloading sky_snapshot from the cloud, one moment please...');
String googleStorageUrl = 'https://storage.googleapis.com/mojo/sky/shell/linux-x64/${engineRevision}/sky_snapshot';
await _downloadFile(googleStorageUrl, skySnapshotFile);
ProcessResult result = await Process.run('chmod', ['u+x', skySnapshotFile.path]);
if (result.exitCode != 0) throw new Exception(result.stderr);
}
return skySnapshotFile.path;
}
return '';
} }
} }
......
...@@ -105,10 +105,9 @@ Future _compileSnapshot({ ...@@ -105,10 +105,9 @@ Future _compileSnapshot({
String packageRoot, String packageRoot,
String snapshotPath String snapshotPath
}) async { }) async {
File compiler = compilerPath == null ? if (compilerPath == null)
await artifactStore.getPath(Artifact.FlutterCompiler) : compilerPath = await artifactStore.getPath(Artifact.FlutterCompiler, packageRoot);
new File(compilerPath); ProcessResult result = await Process.run(compilerPath, [
ProcessResult result = await Process.run(compiler.path, [
mainPath, mainPath,
'--package-root=$packageRoot', '--package-root=$packageRoot',
'--snapshot=$snapshotPath' '--snapshot=$snapshotPath'
......
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