Commit 0758c592 authored by James Robinson's avatar James Robinson

Instantiate ArtifactStore explicit with packageRoot

This teaches commands that need binary artifacts to explicitly instantiate an
instance of the ArtifactStore with the appropriate packageRoot string. The
ArtifactStore can then remember the package root and compute the engine
revision when created and remember those for subsequence calls.
parent e5d65bcc
...@@ -8,16 +8,24 @@ import 'dart:async'; ...@@ -8,16 +8,24 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;
final Logger _logging = new Logger('sky_tools.device'); final Logger _logging = new Logger('sky_tools.artifacts');
enum Artifact { FlutterCompiler, SkyViewerMojo, } enum Artifact { FlutterCompiler, SkyViewerMojo, }
class _ArtifactStore { class ArtifactStore {
_ArtifactStore._(); String _engineRevision;
final String packageRoot;
ArtifactStore(this.packageRoot) {
_engineRevision = new File(path.join(packageRoot, 'sky_engine', 'REVISION')).readAsStringSync();
}
String get engineRevision => _engineRevision;
// Keep in sync with https://github.com/flutter/engine/blob/master/sky/tools/big_red_button.py#L50 // Keep in sync with https://github.com/flutter/engine/blob/master/sky/tools/big_red_button.py#L50
String googleStorageUrl(String category, String platform, String engineRevision) { String googleStorageUrl(String category, String platform) {
return 'https://storage.googleapis.com/mojo/sky/${category}/${platform}/${engineRevision}/'; return 'https://storage.googleapis.com/mojo/sky/${category}/${platform}/${engineRevision}/';
} }
...@@ -34,8 +42,8 @@ class _ArtifactStore { ...@@ -34,8 +42,8 @@ class _ArtifactStore {
_logging.fine('Wrote file'); _logging.fine('Wrote file');
} }
Future<Directory> _cacheDir(String engineRevision, String packageRoot) async { Future<Directory> _cacheDir() async {
String cacheDirPath = '${packageRoot}/sky_tools/cache/sky_engine/${engineRevision}/'; String cacheDirPath = path.join(packageRoot, 'sky_tools', 'cache', 'sky_engine', engineRevision);
Directory cacheDir = new Directory(cacheDirPath); Directory cacheDir = new Directory(cacheDirPath);
if (!await cacheDir.exists()) { if (!await cacheDir.exists()) {
await cacheDir.create(recursive: true); await cacheDir.create(recursive: true);
...@@ -48,14 +56,8 @@ class _ArtifactStore { ...@@ -48,14 +56,8 @@ class _ArtifactStore {
return artifact == Artifact.FlutterCompiler; return artifact == Artifact.FlutterCompiler;
} }
Future<String> getPath(Artifact artifact) async {
Future<String> getEngineRevision(String packageRoot) { Directory cacheDir = await _cacheDir();
return new File(packageRoot + '/sky_engine/REVISION').readAsString();
}
Future<String> getPath(Artifact artifact, String packageRoot) async {
String engineRevision = await getEngineRevision(packageRoot);
Directory cacheDir = await _cacheDir(engineRevision, packageRoot);
String category, name; String category, name;
...@@ -70,10 +72,10 @@ class _ArtifactStore { ...@@ -70,10 +72,10 @@ class _ArtifactStore {
return ''; return '';
} }
File cachedFile = new File(cacheDir.path + name); File cachedFile = new File(path.join(cacheDir.path, name));
if (!await cachedFile.exists()) { if (!await cachedFile.exists()) {
_logging.info('Downloading ${name} from the cloud, one moment please...'); _logging.info('Downloading ${name} from the cloud, one moment please...');
String url = googleStorageUrl(category, 'linux-x64', engineRevision) + name; String url = googleStorageUrl(category, 'linux-x64') + name;
await _downloadFile(url, cachedFile); await _downloadFile(url, cachedFile);
if (_needsToBeExecutable(artifact)) { if (_needsToBeExecutable(artifact)) {
ProcessResult result = await Process.run('chmod', ['u+x', cachedFile.path]); ProcessResult result = await Process.run('chmod', ['u+x', cachedFile.path]);
...@@ -83,5 +85,3 @@ class _ArtifactStore { ...@@ -83,5 +85,3 @@ class _ArtifactStore {
return cachedFile.path; return cachedFile.path;
} }
} }
final _ArtifactStore artifactStore = new _ArtifactStore._();
...@@ -105,8 +105,10 @@ Future _compileSnapshot({ ...@@ -105,8 +105,10 @@ Future _compileSnapshot({
String packageRoot, String packageRoot,
String snapshotPath String snapshotPath
}) async { }) async {
if (compilerPath == null) if (compilerPath == null) {
compilerPath = await artifactStore.getPath(Artifact.FlutterCompiler, packageRoot); ArtifactStore artifacts = new ArtifactStore(packageRoot);
compilerPath = await artifacts.getPath(Artifact.FlutterCompiler);
}
ProcessResult result = await Process.run(compilerPath, [ ProcessResult result = await Process.run(compilerPath, [
mainPath, mainPath,
'--package-root=$packageRoot', '--package-root=$packageRoot',
......
...@@ -38,8 +38,8 @@ class RunMojoCommandHandler extends CommandHandler { ...@@ -38,8 +38,8 @@ class RunMojoCommandHandler extends CommandHandler {
return file.absolute.path; return file.absolute.path;
} }
Future<int> _runAndroid(ArgResults results, String appPath, String engineRevision) async { Future<int> _runAndroid(ArgResults results, String appPath, ArtifactStore artifacts) async {
String skyViewerUrl = artifactStore.googleStorageUrl('viewer', 'android-arm', engineRevision); String skyViewerUrl = artifacts.googleStorageUrl('viewer', 'android-arm');
String command = await _makePathAbsolute(path.join(results['mojo-path'], 'mojo', 'devtools', 'common', 'mojo_run')); String command = await _makePathAbsolute(path.join(results['mojo-path'], 'mojo', 'devtools', 'common', 'mojo_run'));
String appName = path.basename(appPath); String appName = path.basename(appPath);
String appDir = path.dirname(appPath); String appDir = path.dirname(appPath);
...@@ -58,8 +58,8 @@ class RunMojoCommandHandler extends CommandHandler { ...@@ -58,8 +58,8 @@ class RunMojoCommandHandler extends CommandHandler {
return runCommandAndStreamOutput(command, args); return runCommandAndStreamOutput(command, args);
} }
Future<int> _runLinux(ArgResults results, String appPath, String packageRoot, String engineRevision) async { Future<int> _runLinux(ArgResults results, String appPath, ArtifactStore artifacts) async {
String viewerPath = await _makePathAbsolute(await artifactStore.getPath(Artifact.SkyViewerMojo, packageRoot)); String viewerPath = await _makePathAbsolute(await artifacts.getPath(Artifact.SkyViewerMojo));
String mojoShellPath = await _makePathAbsolute(path.join(results['mojo-path'], 'out', 'Release', 'mojo_shell')); String mojoShellPath = await _makePathAbsolute(path.join(results['mojo-path'], 'out', 'Release', 'mojo_shell'));
List<String> mojoRunArgs = [ List<String> mojoRunArgs = [
'mojo:window_manager file://${appPath}', 'mojo:window_manager file://${appPath}',
...@@ -79,12 +79,12 @@ class RunMojoCommandHandler extends CommandHandler { ...@@ -79,12 +79,12 @@ class RunMojoCommandHandler extends CommandHandler {
return 1; return 1;
} }
String packageRoot = results['package-root']; String packageRoot = results['package-root'];
String engineRevision = await artifactStore.getEngineRevision(packageRoot); ArtifactStore artifacts = new ArtifactStore(packageRoot);
String appPath = await _makePathAbsolute(results['app']); String appPath = await _makePathAbsolute(results['app']);
if (results['android']) { if (results['android']) {
return _runAndroid(results, appPath, engineRevision); return _runAndroid(results, appPath, artifacts);
} else { } else {
return _runLinux(results, appPath, packageRoot, engineRevision); return _runLinux(results, appPath, artifacts);
} }
} }
} }
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