cache.dart 12.7 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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 9
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/dart/summary.dart';
10 11 12 13
import 'package:path/path.dart' as path;

import 'base/context.dart';
import 'base/logger.dart';
14
import 'base/net.dart';
15
import 'base/os.dart';
16 17
import 'globals.dart';

18
/// A wrapper around the `bin/cache/` directory.
19
class Cache {
20 21 22 23 24 25 26
  /// [rootOverride] is configurable for testing.
  Cache({ Directory rootOverride }) {
    this._rootOverride = rootOverride;
  }

  Directory _rootOverride;

27 28 29
  // Initialized by FlutterCommandRunner on startup.
  static String flutterRoot;

30 31 32 33
  // Whether to cache artifacts for all platforms. Defaults to only caching
  // artifacts for the current platform.
  bool includeAllPlatforms = false;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  static RandomAccessFile _lock;
  static bool _lockEnabled = true;

  /// Turn off the [lock]/[releaseLockEarly] mechanism.
  ///
  /// This is used by the tests since they run simultaneously and all in one
  /// process and so it would be a mess if they had to use the lock.
  static void disableLocking() {
    _lockEnabled = false;
  }

  /// Lock the cache directory.
  ///
  /// This happens automatically on startup (see [FlutterCommandRunner.runCommand]).
  ///
  /// Normally the lock will be held until the process exits (this uses normal
  /// POSIX flock semantics). Long-lived commands should release the lock by
  /// calling [Cache.releaseLockEarly] once they are no longer touching the cache.
  static Future<Null> lock() async {
    if (!_lockEnabled)
      return null;
    assert(_lock == null);
    _lock = new File(path.join(flutterRoot, 'bin', 'cache', 'lockfile')).openSync(mode: FileMode.WRITE);
    bool locked = false;
    bool printed = false;
    while (!locked) {
      try {
        await _lock.lock();
        locked = true;
      } on FileSystemException {
        if (!printed) {
65 66
          printTrace('Waiting to be able to obtain lock of Flutter binary artifacts directory: ${_lock.path}');
          printStatus('Waiting for another flutter command to release the startup lock...');
67 68
          printed = true;
        }
69
        await new Future<Null>.delayed(const Duration(milliseconds: 50));
70 71 72 73 74 75
      }
    }
  }

  /// Releases the lock. This is not necessary unless the process is long-lived.
  static void releaseLockEarly() {
76
    if (!_lockEnabled || _lock == null)
77 78 79 80 81
      return;
    _lock.closeSync();
    _lock = null;
  }

82 83 84 85 86 87 88 89 90
  static String _dartSdkVersion;

  static String get dartSdkVersion {
    if (_dartSdkVersion == null) {
      _dartSdkVersion = Platform.version;
    }
    return _dartSdkVersion;
  }

91 92 93 94
  static String _engineRevision;

  static String get engineRevision {
    if (_engineRevision == null) {
Dan Rubel's avatar
Dan Rubel committed
95
      File revisionFile = new File(path.join(flutterRoot, 'bin', 'internal', 'engine.version'));
96 97 98 99 100 101
      if (revisionFile.existsSync())
        _engineRevision = revisionFile.readAsStringSync().trim();
    }
    return _engineRevision;
  }

102 103 104
  static Cache get instance => context[Cache] ?? (context[Cache] = new Cache());

  /// Return the top-level directory in the cache; this is `bin/cache`.
105 106 107 108
  Directory getRoot() {
    if (_rootOverride != null)
      return new Directory(path.join(_rootOverride.path, 'bin', 'cache'));
    else
109
      return new Directory(path.join(flutterRoot, 'bin', 'cache'));
110
  }
111

112 113 114 115
  /// Return a directory in the cache dir. For `pkg`, this will return `bin/cache/pkg`.
  Directory getCacheDir(String name) {
    Directory dir = new Directory(path.join(getRoot().path, name));
    if (!dir.existsSync())
116
      dir.createSync(recursive: true);
117
    return dir;
118 119
  }

120 121 122
  /// Return the top-level mutable directory in the cache; this is `bin/cache/artifacts`.
  Directory getCacheArtifacts() => getCacheDir('artifacts');

123 124 125 126 127 128
  /// Get a named directory from with the cache's artifact directory; for example,
  /// `material_fonts` would return `bin/cache/artifacts/material_fonts`.
  Directory getArtifactDirectory(String name) {
    return new Directory(path.join(getCacheArtifacts().path, name));
  }

129
  String getVersionFor(String artifactName) {
Dan Rubel's avatar
Dan Rubel committed
130
    File versionFile = new File(path.join(_rootOverride?.path ?? flutterRoot, 'bin', 'internal', '$artifactName.version'));
131 132 133
    return versionFile.existsSync() ? versionFile.readAsStringSync().trim() : null;
  }

134 135
  String getStampFor(String artifactName) {
    File stampFile = getStampFileFor(artifactName);
136 137 138
    return stampFile.existsSync() ? stampFile.readAsStringSync().trim() : null;
  }

139 140 141 142 143 144 145 146 147 148 149 150 151
  void setStampFor(String artifactName, String version) {
    getStampFileFor(artifactName).writeAsStringSync(version);
  }

  File getStampFileFor(String artifactName) {
    return new File(path.join(getRoot().path, '$artifactName.stamp'));
  }

  bool isUpToDate() {
    MaterialFonts materialFonts = new MaterialFonts(cache);
    FlutterEngine engine = new FlutterEngine(cache);

    return materialFonts.isUpToDate() && engine.isUpToDate();
152 153
  }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  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');
170
        rethrow;
171 172 173 174 175 176
      }
    }

    return cachedFile.path;
  }

177
  Future<Null> updateAll() async {
178 179
    if (!_lockEnabled)
      return null;
180 181 182 183 184 185 186 187 188
    MaterialFonts materialFonts = new MaterialFonts(cache);
    if (!materialFonts.isUpToDate())
      await materialFonts.download();

    FlutterEngine engine = new FlutterEngine(cache);
    if (!engine.isUpToDate())
      await engine.download();
  }

189 190 191 192 193 194 195
  /// 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 location, bool unzip) async {
    if (!location.parent.existsSync())
      location.parent.createSync(recursive: true);

196
    List<int> fileBytes = await fetchUrl(url);
197 198 199 200 201 202
    if (unzip) {
      if (location is Directory && !location.existsSync())
        location.createSync(recursive: true);

      File tempFile = new File(path.join(Directory.systemTemp.path, '${url.toString().hashCode}.zip'));
      tempFile.writeAsBytesSync(fileBytes, flush: true);
203
      os.unzip(tempFile, location);
204 205
      tempFile.deleteSync();
    } else {
206 207
      File file = location;
      file.writeAsBytesSync(fileBytes, flush: true);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    }
  }
}

class MaterialFonts {
  MaterialFonts(this.cache);

  static const String kName = 'material_fonts';

  final Cache cache;

  bool isUpToDate() {
    if (!cache.getArtifactDirectory(kName).existsSync())
      return false;
    return cache.getVersionFor(kName) == cache.getStampFor(kName);
  }

  Future<Null> download() {
    Status status = logger.startProgress('Downloading Material fonts...');

228 229 230 231
    Directory fontsDir = cache.getArtifactDirectory(kName);
    if (fontsDir.existsSync())
      fontsDir.deleteSync(recursive: true);

232
    return Cache._downloadFileToCache(
233
      Uri.parse(cache.getVersionFor(kName)), fontsDir, true
234 235
    ).then((_) {
      cache.setStampFor(kName, cache.getVersionFor(kName));
Devon Carew's avatar
Devon Carew committed
236
      status.stop();
237 238 239 240 241
    }).whenComplete(() {
      status.cancel();
    });
  }
}
242 243

class FlutterEngine {
244

245 246 247
  FlutterEngine(this.cache);

  static const String kName = 'engine';
248 249
  static const String kSkyEngine = 'sky_engine';
  static const String kSdkBundle = 'sdk.ds';
250 251 252

  final Cache cache;

253
  List<String> _getPackageDirs() => const <String>[kSkyEngine];
Devon Carew's avatar
Devon Carew committed
254

255
  List<String> _getEngineDirs() {
Devon Carew's avatar
Devon Carew committed
256 257
    List<String> dirs = <String>[
      'android-arm',
258
      'android-arm-profile',
259
      'android-arm-release',
260 261
      'android-x64',
      'android-x86',
Devon Carew's avatar
Devon Carew committed
262
    ];
263

264 265 266
    if (cache.includeAllPlatforms)
      dirs.addAll(<String>['ios', 'ios-profile', 'ios-release', 'linux-x64']);
    else if (Platform.isMacOS)
267
      dirs.addAll(<String>['ios', 'ios-profile', 'ios-release']);
268 269 270 271 272 273
    else if (Platform.isLinux)
      dirs.add('linux-x64');

    return dirs;
  }

274 275
  // Return a list of (cache directory path, download URL path) tuples.
  List<List<String>> _getToolsDirs() {
276 277 278 279 280 281
    if (cache.includeAllPlatforms)
      return <List<String>>[]
        ..addAll(_osxToolsDirs)
        ..addAll(_linuxToolsDirs);
    else if (Platform.isMacOS)
      return _osxToolsDirs;
Devon Carew's avatar
Devon Carew committed
282
    else if (Platform.isLinux)
283
      return _linuxToolsDirs;
Devon Carew's avatar
Devon Carew committed
284
    else
285
      return <List<String>>[];
Devon Carew's avatar
Devon Carew committed
286
  }
287

288 289 290 291 292 293 294 295 296 297 298 299
  List<List<String>> get _osxToolsDirs => <List<String>>[
    <String>['darwin-x64', 'darwin-x64/artifacts.zip'],
    <String>['android-arm-profile/darwin-x64', 'android-arm-profile/darwin-x64.zip'],
    <String>['android-arm-release/darwin-x64', 'android-arm-release/darwin-x64.zip'],
  ];

  List<List<String>> get _linuxToolsDirs => <List<String>>[
    <String>['linux-x64', 'linux-x64/artifacts.zip'],
    <String>['android-arm-profile/linux-x64', 'android-arm-profile/linux-x64.zip'],
    <String>['android-arm-release/linux-x64', 'android-arm-release/linux-x64.zip'],
  ];

300 301 302
  bool isUpToDate() {
    Directory pkgDir = cache.getCacheDir('pkg');
    for (String pkgName in _getPackageDirs()) {
303 304 305 306 307
      String pkgPath = path.join(pkgDir.path, pkgName);
      String dotPackagesPath = path.join(pkgPath, '.packages');
      if (!new Directory(pkgPath).existsSync())
        return false;
      if (!new File(dotPackagesPath).existsSync())
308 309 310
        return false;
    }

311 312 313
    if (!new File(path.join(pkgDir.path, kSkyEngine, kSdkBundle)).existsSync())
      return false;

314 315 316 317 318 319 320
    Directory engineDir = cache.getArtifactDirectory(kName);
    for (String dirName in _getEngineDirs()) {
      Directory dir = new Directory(path.join(engineDir.path, dirName));
      if (!dir.existsSync())
        return false;
    }

321 322
    for (List<String> toolsDir in _getToolsDirs()) {
      Directory dir = new Directory(path.join(engineDir.path, toolsDir[0]));
Devon Carew's avatar
Devon Carew committed
323 324 325 326
      if (!dir.existsSync())
        return false;
    }

327 328 329 330 331 332 333 334 335
    return cache.getVersionFor(kName) == cache.getStampFor(kName);
  }

  Future<Null> download() async {
    String engineVersion = cache.getVersionFor(kName);
    String url = 'https://storage.googleapis.com/flutter_infra/flutter/$engineVersion/';

    Directory pkgDir = cache.getCacheDir('pkg');
    for (String pkgName in _getPackageDirs()) {
336 337
      String pkgPath = path.join(pkgDir.path, pkgName);
      Directory dir = new Directory(pkgPath);
338 339 340
      if (dir.existsSync())
        dir.deleteSync(recursive: true);
      await _downloadItem('Downloading package $pkgName...', url + pkgName + '.zip', pkgDir);
341 342 343 344 345 346
      await pubGet(directory: pkgPath);
    }

    Status summaryStatus = logger.startProgress('Building Dart SDK summary...');
    try {
      String skyEnginePath = path.join(pkgDir.path, kSkyEngine);
347
      buildSkyEngineSdkSummary(skyEnginePath, kSdkBundle);
348
    } finally {
Devon Carew's avatar
Devon Carew committed
349
      summaryStatus.stop();
350 351 352
    }

    Directory engineDir = cache.getArtifactDirectory(kName);
353 354 355
    if (engineDir.existsSync())
      engineDir.deleteSync(recursive: true);

356 357
    for (String dirName in _getEngineDirs()) {
      Directory dir = new Directory(path.join(engineDir.path, dirName));
358 359 360 361 362 363 364
      await _downloadItem('Downloading engine artifacts $dirName...',
        url + dirName + '/artifacts.zip', dir);
      File frameworkZip = new File(path.join(dir.path, 'Flutter.framework.zip'));
      if (frameworkZip.existsSync()) {
        Directory framework = new Directory(path.join(dir.path, 'Flutter.framework'));
        framework.createSync();
        os.unzip(frameworkZip, framework);
Devon Carew's avatar
Devon Carew committed
365 366 367
      }
    }

368 369 370 371
    for (List<String> toolsDir in _getToolsDirs()) {
      String cacheDir = toolsDir[0];
      String urlPath = toolsDir[1];
      Directory dir = new Directory(path.join(engineDir.path, cacheDir));
372 373
      await _downloadItem('Downloading $cacheDir tools...', url + urlPath, dir);
      _makeFilesExecutable(dir);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    }

    cache.setStampFor(kName, cache.getVersionFor(kName));
  }

  void _makeFilesExecutable(Directory dir) {
    for (FileSystemEntity entity in dir.listSync()) {
      if (entity is File) {
        String name = path.basename(entity.path);
        if (name == 'sky_snapshot' || name == 'sky_shell')
          os.makeExecutable(entity);
      }
    }
  }

  Future<Null> _downloadItem(String message, String url, Directory dest) {
    Status status = logger.startProgress(message);
    return Cache._downloadFileToCache(Uri.parse(url), dest, true).then((_) {
Devon Carew's avatar
Devon Carew committed
392
      status.stop();
393 394 395 396 397
    }).whenComplete(() {
      status.cancel();
    });
  }
}