cache.dart 20.7 KB
Newer Older
1 2 3 4 5 6
// 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';

7 8
import 'package:meta/meta.dart';

9
import 'base/context.dart';
10
import 'base/file_system.dart';
11
import 'base/io.dart' show SocketException;
12
import 'base/logger.dart';
13
import 'base/net.dart';
14
import 'base/os.dart';
15
import 'base/platform.dart';
16 17
import 'globals.dart';

18
/// A wrapper around the `bin/cache/` directory.
19
class Cache {
20
  /// [rootOverride] is configurable for testing.
21 22 23
  /// [artifacts] is configurable for testing.
  Cache({ Directory rootOverride, List<CachedArtifact> artifacts }) : _rootOverride = rootOverride {
    if (artifacts == null) {
24 25 26
      _artifacts.add(MaterialFonts(this));
      _artifacts.add(FlutterEngine(this));
      _artifacts.add(GradleWrapper(this));
27 28 29 30
    } else {
      _artifacts.addAll(artifacts);
    }
  }
31

32
  static const List<String> _hostsBlockedInChina = <String> [
33 34 35
    'storage.googleapis.com',
  ];

36
  final Directory _rootOverride;
37
  final List<CachedArtifact> _artifacts = <CachedArtifact>[];
38

39 40 41
  // Initialized by FlutterCommandRunner on startup.
  static String flutterRoot;

42 43 44 45
  // Whether to cache artifacts for all platforms. Defaults to only caching
  // artifacts for the current platform.
  bool includeAllPlatforms = false;

46 47 48 49 50 51 52
  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.
53
  @visibleForTesting
54 55 56 57
  static void disableLocking() {
    _lockEnabled = false;
  }

58 59 60 61 62 63 64 65
  /// Turn on the [lock]/[releaseLockEarly] mechanism.
  ///
  /// This is used by the tests.
  @visibleForTesting
  static void enableLocking() {
    _lockEnabled = true;
  }

66 67 68 69 70 71 72
  /// 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.
73
  static Future<void> lock() async {
74 75 76
    if (!_lockEnabled)
      return null;
    assert(_lock == null);
77
    _lock = await fs.file(fs.path.join(flutterRoot, 'bin', 'cache', 'lockfile')).open(mode: FileMode.write);
78 79 80 81 82 83 84 85
    bool locked = false;
    bool printed = false;
    while (!locked) {
      try {
        await _lock.lock();
        locked = true;
      } on FileSystemException {
        if (!printed) {
86 87
          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...');
88 89
          printed = true;
        }
90
        await Future<void>.delayed(const Duration(milliseconds: 50));
91 92 93 94 95 96
      }
    }
  }

  /// Releases the lock. This is not necessary unless the process is long-lived.
  static void releaseLockEarly() {
97
    if (!_lockEnabled || _lock == null)
98 99 100 101 102
      return;
    _lock.closeSync();
    _lock = null;
  }

103 104 105
  /// Checks if the current process owns the lock for the cache directory at
  /// this very moment; throws a [StateError] if it doesn't.
  static void checkLockAcquired() {
106
    if (_lockEnabled && _lock == null && platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
107
      throw StateError(
108 109 110 111 112
        'The current process does not own the lock for the cache directory. This is a bug in Flutter CLI tools.',
      );
    }
  }

113
  String _dartSdkVersion;
114

115
  String get dartSdkVersion => _dartSdkVersion ??= platform.version;
116

117
  String _engineRevision;
118

119 120
  String get engineRevision {
    _engineRevision ??= getVersionFor('engine');
121 122 123
    return _engineRevision;
  }

124
  static Cache get instance => context[Cache];
125 126

  /// Return the top-level directory in the cache; this is `bin/cache`.
127 128
  Directory getRoot() {
    if (_rootOverride != null)
129
      return fs.directory(fs.path.join(_rootOverride.path, 'bin', 'cache'));
130
    else
131
      return fs.directory(fs.path.join(flutterRoot, 'bin', 'cache'));
132
  }
133

134 135
  /// Return a directory in the cache dir. For `pkg`, this will return `bin/cache/pkg`.
  Directory getCacheDir(String name) {
136
    final Directory dir = fs.directory(fs.path.join(getRoot().path, name));
137
    if (!dir.existsSync())
138
      dir.createSync(recursive: true);
139
    return dir;
140 141
  }

142 143 144
  /// Return the top-level directory for artifact downloads.
  Directory getDownloadDir() => getCacheDir('downloads');

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

148 149 150
  /// 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) {
151
    return getCacheArtifacts().childDirectory(name);
152 153
  }

154
  String getVersionFor(String artifactName) {
155
    final File versionFile = fs.file(fs.path.join(_rootOverride?.path ?? flutterRoot, 'bin', 'internal', '$artifactName.version'));
156 157 158
    return versionFile.existsSync() ? versionFile.readAsStringSync().trim() : null;
  }

159
  String getStampFor(String artifactName) {
160
    final File stampFile = getStampFileFor(artifactName);
161 162 163
    return stampFile.existsSync() ? stampFile.readAsStringSync().trim() : null;
  }

164 165 166 167 168
  void setStampFor(String artifactName, String version) {
    getStampFileFor(artifactName).writeAsStringSync(version);
  }

  File getStampFileFor(String artifactName) {
169
    return fs.file(fs.path.join(getRoot().path, '$artifactName.stamp'));
170 171
  }

172 173 174
  /// Returns `true` if either [entity] is older than the tools stamp or if
  /// [entity] doesn't exist.
  bool isOlderThanToolsStamp(FileSystemEntity entity) {
175
    final File flutterToolsStamp = getStampFileFor('flutter_tools');
176
    return isOlderThanReference(entity: entity, referenceFile: flutterToolsStamp);
177 178
  }

179
  bool isUpToDate() => _artifacts.every((CachedArtifact artifact) => artifact.isUpToDate());
180

181
  Future<String> getThirdPartyFile(String urlStr, String serviceName) async {
182 183
    final Uri url = Uri.parse(urlStr);
    final Directory thirdPartyDir = getArtifactDirectory('third_party');
184

185
    final Directory serviceDir = fs.directory(fs.path.join(thirdPartyDir.path, serviceName));
186 187 188
    if (!serviceDir.existsSync())
      serviceDir.createSync(recursive: true);

189
    final File cachedFile = fs.file(fs.path.join(serviceDir.path, url.pathSegments.last));
190 191
    if (!cachedFile.existsSync()) {
      try {
192
        await _downloadFile(url, cachedFile);
193 194
      } catch (e) {
        printError('Failed to fetch third-party artifact $url: $e');
195
        rethrow;
196 197 198 199 200 201
      }
    }

    return cachedFile.path;
  }

202
  Future<void> updateAll() async {
203 204
    if (!_lockEnabled)
      return null;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    try {
      for (CachedArtifact artifact in _artifacts) {
        if (!artifact.isUpToDate())
          await artifact.update();
      }
    } on SocketException catch (e) {
      if (_hostsBlockedInChina.contains(e.address?.host)) {
        printError(
          'Failed to retrieve Flutter tool depedencies: ${e.message}.\n'
          "If you're in China, please follow "
          'https://github.com/flutter/flutter/wiki/Using-Flutter-in-China',
          emphasis: true,
        );
      }
      rethrow;
220 221 222 223
    }
  }
}

224 225 226
/// An artifact managed by the cache.
abstract class CachedArtifact {
  CachedArtifact(this.name, this.cache);
227

228
  final String name;
229 230
  final Cache cache;

231 232 233
  Directory get location => cache.getArtifactDirectory(name);
  String get version => cache.getVersionFor(name);

234
  /// Keep track of the files we've downloaded for this execution so we
235
  /// can delete them after completion. We don't delete them right after
236 237 238 239
  /// extraction in case [update] is interrupted, so we can restart without
  /// starting from scratch.
  final List<File> _downloadedFiles = <File>[];

240
  bool isUpToDate() {
241 242 243
    if (!location.existsSync())
      return false;
    if (version != cache.getStampFor(name))
244
      return false;
245
    return isUpToDateInner();
246 247
  }

248
  Future<void> update() async {
249 250 251
    if (location.existsSync())
      location.deleteSync(recursive: true);
    location.createSync(recursive: true);
252 253
    await updateInner();
    cache.setStampFor(name, version);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    _removeDownloadedFiles();
  }

  /// Clear any zip/gzip files downloaded.
  void _removeDownloadedFiles() {
    for (File f in _downloadedFiles) {
      f.deleteSync();
      for (Directory d = f.parent; d.absolute.path != cache.getDownloadDir().absolute.path; d = d.parent) {
        if (d.listSync().isEmpty) {
          d.deleteSync();
        } else {
          break;
        }
      }
    }
269 270 271 272
  }

  /// Hook method for extra checks for being up-to-date.
  bool isUpToDateInner() => true;
273

274
  /// Template method to perform artifact update.
275
  Future<void> updateInner();
276 277 278 279 280 281 282 283 284 285

  String get _storageBaseUrl {
    final String overrideUrl = platform.environment['FLUTTER_STORAGE_BASE_URL'];
    if (overrideUrl == null)
      return 'https://storage.googleapis.com';
    _maybeWarnAboutStorageOverride(overrideUrl);
    return overrideUrl;
  }

  Uri _toStorageUri(String path) => Uri.parse('$_storageBaseUrl/$path');
286 287

  /// Download an archive from the given [url] and unzip it to [location].
288
  Future<void> _downloadArchive(String message, Uri url, Directory location, bool verifier(File f), void extractor(File f, Directory d)) {
289
    return _withDownloadFile('${flattenNameSubdirs(url)}', (File tempFile) async {
290 291
      if (!verifier(tempFile)) {
        final Status status = logger.startProgress(message, expectSlowOperation: true);
292 293
        try {
          await _downloadFile(url, tempFile);
294
          status.stop();
295 296 297 298
        } catch (exception) {
          status.cancel();
          rethrow;
        }
299
      } else {
300
        logger.printTrace('$message (cached)');
301 302 303 304 305 306 307
      }
      _ensureExists(location);
      extractor(tempFile, location);
    });
  }

  /// Download a zip archive from the given [url] and unzip it to [location].
308
  Future<void> _downloadZipArchive(String message, Uri url, Directory location) {
309 310 311 312
    return _downloadArchive(message, url, location, os.verifyZip, os.unzip);
  }

  /// Download a gzipped tarball from the given [url] and unpack it to [location].
313
  Future<void> _downloadZippedTarball(String message, Uri url, Directory location) {
314 315 316 317 318
    return _downloadArchive(message, url, location, os.verifyGzip, os.unpack);
  }

  /// Create a temporary file and invoke [onTemporaryFile] with the file as
  /// argument, then add the temporary file to the [_downloadedFiles].
319
  Future<void> _withDownloadFile(String name, Future<void> onTemporaryFile(File file)) async {
320 321 322 323
    final File tempFile = fs.file(fs.path.join(cache.getDownloadDir().path, name));
    _downloadedFiles.add(tempFile);
    await onTemporaryFile(tempFile);
  }
324 325 326 327 328 329 330 331 332 333 334 335
}

bool _hasWarnedAboutStorageOverride = false;

void _maybeWarnAboutStorageOverride(String overrideUrl) {
  if (_hasWarnedAboutStorageOverride)
    return;
  logger.printStatus(
    'Flutter assets will be downloaded from $overrideUrl. Make sure you trust this source!',
    emphasis: true,
  );
  _hasWarnedAboutStorageOverride = true;
336
}
337

338 339 340 341 342
/// A cached artifact containing fonts used for Material Design.
class MaterialFonts extends CachedArtifact {
  MaterialFonts(Cache cache): super('material_fonts', cache);

  @override
343
  Future<void> updateInner() {
344
    final Uri archiveUri = _toStorageUri(version);
345
    return _downloadZipArchive('Downloading Material fonts...', archiveUri, location);
346 347
  }
}
348

349 350 351
/// A cached artifact containing the Flutter engine binaries.
class FlutterEngine extends CachedArtifact {
  FlutterEngine(Cache cache): super('engine', cache);
352

353
  List<String> _getPackageDirs() => const <String>['sky_engine'];
Devon Carew's avatar
Devon Carew committed
354

355 356
  // Return a list of (cache directory path, download URL path) tuples.
  List<List<String>> _getBinaryDirs() {
357
    final List<List<String>> binaryDirs = <List<String>>[];
358

359 360
    binaryDirs.add(<String>['common', 'flutter_patched_sdk.zip']);

361
    if (cache.includeAllPlatforms)
362 363 364
      binaryDirs
        ..addAll(_osxBinaryDirs)
        ..addAll(_linuxBinaryDirs)
365
        ..addAll(_windowsBinaryDirs)
366
        ..addAll(_androidBinaryDirs)
367 368
        ..addAll(_iosBinaryDirs)
        ..addAll(_dartSdks);
369
    else if (platform.isLinux)
370 371 372
      binaryDirs
        ..addAll(_linuxBinaryDirs)
        ..addAll(_androidBinaryDirs);
373
    else if (platform.isMacOS)
374 375 376 377
      binaryDirs
        ..addAll(_osxBinaryDirs)
        ..addAll(_androidBinaryDirs)
        ..addAll(_iosBinaryDirs);
378 379 380 381
    else if (platform.isWindows)
      binaryDirs
        ..addAll(_windowsBinaryDirs)
        ..addAll(_androidBinaryDirs);
382 383

    return binaryDirs;
Devon Carew's avatar
Devon Carew committed
384
  }
385

386
  List<List<String>> get _osxBinaryDirs => <List<String>>[
387 388 389
    <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'],
390 391
    <String>['android-arm64-profile/darwin-x64', 'android-arm64-profile/darwin-x64.zip'],
    <String>['android-arm64-release/darwin-x64', 'android-arm64-release/darwin-x64.zip'],
392 393
  ];

394
  List<List<String>> get _linuxBinaryDirs => <List<String>>[
395 396 397
    <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'],
398 399
    <String>['android-arm64-profile/linux-x64', 'android-arm64-profile/linux-x64.zip'],
    <String>['android-arm64-release/linux-x64', 'android-arm64-release/linux-x64.zip'],
400 401 402 403
    <String>['android-arm-dynamic-profile/linux-x64', 'android-arm-dynamic-profile/linux-x64.zip'],
    <String>['android-arm-dynamic-release/linux-x64', 'android-arm-dynamic-release/linux-x64.zip'],
    <String>['android-arm64-dynamic-profile/linux-x64', 'android-arm64-dynamic-profile/linux-x64.zip'],
    <String>['android-arm64-dynamic-release/linux-x64', 'android-arm64-dynamic-release/linux-x64.zip'],
404 405
  ];

406
  List<List<String>> get _windowsBinaryDirs => <List<String>>[
407
    <String>['windows-x64', 'windows-x64/artifacts.zip'],
408 409
    <String>['android-arm-profile/windows-x64', 'android-arm-profile/windows-x64.zip'],
    <String>['android-arm-release/windows-x64', 'android-arm-release/windows-x64.zip'],
410 411
    <String>['android-arm64-profile/windows-x64', 'android-arm64-profile/windows-x64.zip'],
    <String>['android-arm64-release/windows-x64', 'android-arm64-release/windows-x64.zip'],
412 413
  ];

414 415 416 417 418 419
  List<List<String>> get _androidBinaryDirs => <List<String>>[
    <String>['android-x86', 'android-x86/artifacts.zip'],
    <String>['android-x64', 'android-x64/artifacts.zip'],
    <String>['android-arm', 'android-arm/artifacts.zip'],
    <String>['android-arm-profile', 'android-arm-profile/artifacts.zip'],
    <String>['android-arm-release', 'android-arm-release/artifacts.zip'],
420 421 422
    <String>['android-arm64', 'android-arm64/artifacts.zip'],
    <String>['android-arm64-profile', 'android-arm64-profile/artifacts.zip'],
    <String>['android-arm64-release', 'android-arm64-release/artifacts.zip'],
423 424 425 426
    <String>['android-arm-dynamic-profile', 'android-arm-dynamic-profile/artifacts.zip'],
    <String>['android-arm-dynamic-release', 'android-arm-dynamic-release/artifacts.zip'],
    <String>['android-arm64-dynamic-profile', 'android-arm64-dynamic-profile/artifacts.zip'],
    <String>['android-arm64-dynamic-release', 'android-arm64-dynamic-release/artifacts.zip'],
427 428 429 430 431 432 433 434
  ];

  List<List<String>> get _iosBinaryDirs => <List<String>>[
    <String>['ios', 'ios/artifacts.zip'],
    <String>['ios-profile', 'ios-profile/artifacts.zip'],
    <String>['ios-release', 'ios-release/artifacts.zip'],
  ];

435 436 437 438 439 440
  List<List<String>> get _dartSdks => <List<String>> [
    <String>['darwin-x64', 'dart-sdk-darwin-x64.zip'],
    <String>['linux-x64', 'dart-sdk-linux-x64.zip'],
    <String>['windows-x64', 'dart-sdk-windows-x64.zip'],
  ];

441 442 443 444 445 446 447 448
  // A list of cache directory paths to which the LICENSE file should be copied.
  List<String> _getLicenseDirs() {
    if (cache.includeAllPlatforms || platform.isMacOS) {
      return const <String>['ios', 'ios-profile', 'ios-release'];
    }
    return const <String>[];
  }

449 450
  @override
  bool isUpToDateInner() {
451
    final Directory pkgDir = cache.getCacheDir('pkg');
452
    for (String pkgName in _getPackageDirs()) {
453
      final String pkgPath = fs.path.join(pkgDir.path, pkgName);
454
      if (!fs.directory(pkgPath).existsSync())
455
        return false;
456 457
    }

458
    for (List<String> toolsDir in _getBinaryDirs()) {
459
      final Directory dir = fs.directory(fs.path.join(location.path, toolsDir[0]));
Devon Carew's avatar
Devon Carew committed
460 461 462
      if (!dir.existsSync())
        return false;
    }
463 464 465 466 467 468

    for (String licenseDir in _getLicenseDirs()) {
      final File file = fs.file(fs.path.join(location.path, licenseDir, 'LICENSE'));
      if (!file.existsSync())
        return false;
    }
469
    return true;
470 471
  }

472
  @override
473
  Future<void> updateInner() async {
474
    final String url = '$_storageBaseUrl/flutter_infra/flutter/$version/';
475

476
    final Directory pkgDir = cache.getCacheDir('pkg');
477
    for (String pkgName in _getPackageDirs()) {
478 479
      final String pkgPath = fs.path.join(pkgDir.path, pkgName);
      final Directory dir = fs.directory(pkgPath);
480 481
      if (dir.existsSync())
        dir.deleteSync(recursive: true);
482
      await _downloadZipArchive('Downloading package $pkgName...', Uri.parse(url + pkgName + '.zip'), pkgDir);
483 484
    }

485
    for (List<String> toolsDir in _getBinaryDirs()) {
486 487
      final String cacheDir = toolsDir[0];
      final String urlPath = toolsDir[1];
488
      final Directory dir = fs.directory(fs.path.join(location.path, cacheDir));
489
      await _downloadZipArchive('Downloading $cacheDir tools...', Uri.parse(url + urlPath), dir);
490 491 492

      _makeFilesExecutable(dir);

493
      final File frameworkZip = fs.file(fs.path.join(dir.path, 'Flutter.framework.zip'));
494
      if (frameworkZip.existsSync()) {
495
        final Directory framework = fs.directory(fs.path.join(dir.path, 'Flutter.framework'));
496 497
        framework.createSync();
        os.unzip(frameworkZip, framework);
Devon Carew's avatar
Devon Carew committed
498 499
      }
    }
500 501 502 503 504 505

    final File licenseSource = fs.file(fs.path.join(Cache.flutterRoot, 'LICENSE'));
    for (String licenseDir in _getLicenseDirs()) {
      final String licenseDestinationPath = fs.path.join(location.path, licenseDir, 'LICENSE');
      await licenseSource.copy(licenseDestinationPath);
    }
506 507 508 509 510
  }

  void _makeFilesExecutable(Directory dir) {
    for (FileSystemEntity entity in dir.listSync()) {
      if (entity is File) {
511
        final String name = fs.path.basename(entity.path);
512
        if (name == 'flutter_tester')
513 514 515 516
          os.makeExecutable(entity);
      }
    }
  }
517 518 519 520 521 522 523
}

/// A cached artifact containing Gradle Wrapper scripts and binaries.
class GradleWrapper extends CachedArtifact {
  GradleWrapper(Cache cache): super('gradle_wrapper', cache);

  @override
524
  Future<void> updateInner() {
525
    final Uri archiveUri = _toStorageUri(version);
526
    return _downloadZippedTarball('Downloading Gradle Wrapper...', archiveUri, location).then<void>((_) {
527 528
      // Delete property file, allowing templates to provide it.
      fs.file(fs.path.join(location.path, 'gradle', 'wrapper', 'gradle-wrapper.properties')).deleteSync();
529 530
      // Remove NOTICE file. Should not be part of the template.
      fs.file(fs.path.join(location.path, 'NOTICE')).deleteSync();
531
    });
532 533
  }
}
534

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
// Many characters are problematic in filenames, especially on Windows.
final Map<int, List<int>> _flattenNameSubstitutions = <int, List<int>>{
  r'@'.codeUnitAt(0): '@@'.codeUnits,
  r'/'.codeUnitAt(0): '@s@'.codeUnits,
  r'\'.codeUnitAt(0): '@bs@'.codeUnits,
  r':'.codeUnitAt(0): '@c@'.codeUnits,
  r'%'.codeUnitAt(0): '@per@'.codeUnits,
  r'*'.codeUnitAt(0): '@ast@'.codeUnits,
  r'<'.codeUnitAt(0): '@lt@'.codeUnits,
  r'>'.codeUnitAt(0): '@gt@'.codeUnits,
  r'"'.codeUnitAt(0): '@q@'.codeUnits,
  r'|'.codeUnitAt(0): '@pip@'.codeUnits,
  r'?'.codeUnitAt(0): '@ques@'.codeUnits
};

/// Given a name containing slashes, colons, and backslashes, expand it into
/// something that doesn't.
String _flattenNameNoSubdirs(String fileName) {
  final List<int> replacedCodeUnits = <int>[];
  for (int codeUnit in fileName.codeUnits) {
    replacedCodeUnits.addAll(_flattenNameSubstitutions[codeUnit] ?? <int>[codeUnit]);
  }
557
  return String.fromCharCodes(replacedCodeUnits);
558 559
}

560 561
@visibleForTesting
String flattenNameSubdirs(Uri url) {
562
  final List<String> pieces = <String>[url.host]..addAll(url.pathSegments);
563
  final Iterable<String> convertedPieces = pieces.map<String>(_flattenNameNoSubdirs);
564 565 566
  return fs.path.joinAll(convertedPieces);
}

567
/// Download a file from the given [url] and write it to [location].
568
Future<void> _downloadFile(Uri url, File location) async {
569 570 571 572 573 574 575 576 577 578
  _ensureExists(location.parent);
  final List<int> fileBytes = await fetchUrl(url);
  location.writeAsBytesSync(fileBytes, flush: true);
}

/// Create the given [directory] and parents, as necessary.
void _ensureExists(Directory directory) {
  if (!directory.existsSync())
    directory.createSync(recursive: true);
}