artifacts.dart 8.04 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 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:async';
import 'dart:io';

8
import 'package:archive/archive.dart';
9
import 'package:path/path.dart' as path;
10

11
import 'base/process.dart';
Devon Carew's avatar
Devon Carew committed
12
import 'build_configuration.dart';
13
import 'globals.dart';
14

15 16 17 18 19 20 21 22 23
String _getNameForHostPlatform(HostPlatform platform) {
  switch (platform) {
    case HostPlatform.linux:
      return 'linux-x64';
    case HostPlatform.mac:
      return 'darwin-x64';
  }
}

Devon Carew's avatar
Devon Carew committed
24
String getNameForTargetPlatform(TargetPlatform platform) {
25
  switch (platform) {
26
    case TargetPlatform.android_arm:
27
      return 'android-arm';
28 29
    case TargetPlatform.ios:
      return 'ios';
30
    case TargetPlatform.darwin_x64:
Ian Hickson's avatar
Ian Hickson committed
31
      return 'darwin-x64';
32
    case TargetPlatform.linux_x64:
33 34 35 36 37 38 39
      return 'linux-x64';
  }
}

enum ArtifactType {
  snapshot,
  shell,
40
  mojo,
41
  androidClassesJar,
42 43 44
  androidIcuData,
  androidKeystore,
  androidLibSkyShell,
45
  iosXcodeProject,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

class Artifact {
  const Artifact._({
    this.name,
    this.fileName,
    this.type,
    this.hostPlatform,
    this.targetPlatform
  });

  final String name;
  final String fileName;
  final ArtifactType type;
  final HostPlatform hostPlatform;
  final TargetPlatform targetPlatform;

  String get platform {
    if (targetPlatform != null)
Devon Carew's avatar
Devon Carew committed
65
      return getNameForTargetPlatform(targetPlatform);
66 67 68 69 70
    if (hostPlatform != null)
      return _getNameForHostPlatform(hostPlatform);
    assert(false);
    return null;
  }
71
}
72

73
class ArtifactStore {
74 75
  static const List<Artifact> knownArtifacts = const <Artifact>[
    const Artifact._(
Adam Barth's avatar
Adam Barth committed
76
      name: 'Flutter Tester',
77 78
      fileName: 'sky_shell',
      type: ArtifactType.shell,
79
      targetPlatform: TargetPlatform.linux_x64
80
    ),
81 82 83 84 85 86 87 88 89 90 91 92 93
    const Artifact._(
      name: 'Sky Snapshot',
      fileName: 'sky_snapshot',
      type: ArtifactType.snapshot,
      hostPlatform: HostPlatform.linux
    ),
    const Artifact._(
      name: 'Sky Snapshot',
      fileName: 'sky_snapshot',
      type: ArtifactType.snapshot,
      hostPlatform: HostPlatform.mac
    ),
    const Artifact._(
94 95 96
      name: 'Flutter for Mojo',
      fileName: 'flutter.mojo',
      type: ArtifactType.mojo,
97
      targetPlatform: TargetPlatform.android_arm
98 99
    ),
    const Artifact._(
100 101 102
      name: 'Flutter for Mojo',
      fileName: 'flutter.mojo',
      type: ArtifactType.mojo,
103
      targetPlatform: TargetPlatform.linux_x64
104
    ),
105 106
    const Artifact._(
      name: 'Compiled Java code',
107 108
      fileName: 'classes.dex.jar',
      type: ArtifactType.androidClassesJar,
109
      targetPlatform: TargetPlatform.android_arm
110 111 112 113 114
    ),
    const Artifact._(
      name: 'ICU data table',
      fileName: 'icudtl.dat',
      type: ArtifactType.androidIcuData,
115
      targetPlatform: TargetPlatform.android_arm
116 117 118 119 120
    ),
    const Artifact._(
      name: 'Key Store',
      fileName: 'chromium-debug.keystore',
      type: ArtifactType.androidKeystore,
121
      targetPlatform: TargetPlatform.android_arm
122 123 124 125 126
    ),
    const Artifact._(
      name: 'Compiled C++ code',
      fileName: 'libsky_shell.so',
      type: ArtifactType.androidLibSkyShell,
127
      targetPlatform: TargetPlatform.android_arm
128
    ),
129 130 131 132 133 134
    const Artifact._(
      name: 'iOS Runner (Xcode Project)',
      fileName: 'FlutterXcode.zip',
      type: ArtifactType.iosXcodeProject,
      targetPlatform: TargetPlatform.ios
    ),
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  ];

  static Artifact getArtifact({
    ArtifactType type,
    HostPlatform hostPlatform,
    TargetPlatform targetPlatform
  }) {
    for (Artifact artifact in ArtifactStore.knownArtifacts) {
      if (type != null &&
          type != artifact.type)
        continue;
      if (hostPlatform != null &&
          artifact.hostPlatform != null &&
          hostPlatform != artifact.hostPlatform)
        continue;
      if (targetPlatform != null &&
          artifact.targetPlatform != null &&
          targetPlatform != artifact.targetPlatform)
        continue;
      return artifact;
    }
    return null;
  }

159 160
  // These values are initialized by FlutterCommandRunner on startup.
  static String flutterRoot;
Ian Hickson's avatar
Ian Hickson committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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?';
      }
176
      printError(message);
Ian Hickson's avatar
Ian Hickson committed
177 178 179
      throw new ProcessExit(2);
    }
  }
180

181
  static String _engineRevision;
182

183
  static String get engineRevision {
184
    if (_engineRevision == null) {
185
      File revisionFile = new File(path.join(flutterRoot, 'bin', 'cache', 'engine.version'));
186
      if (revisionFile.existsSync())
187
        _engineRevision = revisionFile.readAsStringSync().trim();
188
    }
189
    return _engineRevision;
190 191
  }

192 193 194 195 196 197 198 199 200
  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);
201
    }
202
    return cacheDir;
203 204
  }

205 206 207 208 209 210 211 212 213
  static Future<String> getPath(Artifact artifact) async {
    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;
214 215
  }

216 217
  /// Download a file from the given URL and return the bytes.
  static Future<List<int>> _downloadFile(Uri url) async {
218
    printStatus('Downloading $url.');
219

220
    HttpClient httpClient = new HttpClient();
221
    HttpClientRequest request = await httpClient.getUrl(url);
222
    HttpClientResponse response = await request.close();
223
    printTrace('Received response statusCode=${response.statusCode}');
224 225
    if (response.statusCode != 200)
      throw new Exception(response.reasonPhrase);
226 227

    BytesBuilder responseBody = new BytesBuilder(copy: false);
228
    await for (List<int> chunk in response)
229 230
      responseBody.add(chunk);

231 232 233 234
    return responseBody.takeBytes();
  }

  /// Download a file from the given url and write it to the cache.
235 236
  /// If [unzip] is true, treat the url as a zip file, and unzip it to the
  /// directory given.
Ian Hickson's avatar
Ian Hickson committed
237
  static Future<Null> _downloadFileToCache(Uri url, FileSystemEntity cachedFile, bool unzip) async {
238 239 240 241
    if (!cachedFile.parent.existsSync())
      cachedFile.parent.createSync(recursive: true);

    List<int> fileBytes = await _downloadFile(url);
242 243 244 245 246 247 248 249 250 251 252 253 254
    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);
    }
255 256
  }

257
  static Future<String> getThirdPartyFile(String urlStr, String cacheSubdir, bool unzip) async {
258
    Uri url = Uri.parse(urlStr);
259
    Directory baseDir = getBaseCacheDir();
260 261 262 263 264
    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()) {
265
      try {
266
        await _downloadFileToCache(url, cachedFile, unzip);
267
      } catch (e) {
268
        printError('Failed to fetch third-party artifact: $url: $e');
269 270 271 272 273
        throw new ProcessExit(2);
      }
    }
    return cachedFile.path;
  }
274
}