prepare_package.dart 32.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6
import 'dart:convert';
7
import 'dart:io' hide Platform;
8
import 'dart:typed_data';
9 10

import 'package:args/args.dart';
11 12
import 'package:crypto/crypto.dart';
import 'package:crypto/src/digest_sink.dart';
13
import 'package:http/http.dart' as http;
14
import 'package:path/path.dart' as path;
15
import 'package:platform/platform.dart' show Platform, LocalPlatform;
16
import 'package:process/process.dart';
17

18
const String chromiumRepo = 'https://chromium.googlesource.com/external/github.com/flutter/flutter';
19
const String githubRepo = 'https://github.com/flutter/flutter.git';
20
const String mingitForWindowsUrl = 'https://storage.googleapis.com/flutter_infra_release/mingit/'
21
    '603511c649b00bbef0a6122a827ac419b656bc19/mingit.zip';
22
const String releaseFolder = '/releases';
23 24 25
const String gsBase = 'gs://flutter_infra_release';
const String gsReleaseFolder = '$gsBase$releaseFolder';
const String baseUrl = 'https://storage.googleapis.com/flutter_infra_release';
26
const int shortCacheSeconds = 60;
27 28
const String frameworkVersionTag = 'frameworkVersionFromGit';
const String dartVersionTag = 'dartSdkVersion';
29
const String dartTargetArchTag = 'dartTargetArch';
30

31
/// Exception class for when a process fails to run, so we can catch
32
/// it and provide something more readable than a stack trace.
33
class PreparePackageException implements Exception {
34
  PreparePackageException(this.message, [this.result]);
35

36
  final String message;
37
  final ProcessResult? result;
38
  int get exitCode => result?.exitCode ?? -1;
39 40

  @override
41 42 43 44 45
  String toString() {
    String output = runtimeType.toString();
    if (message != null) {
      output += ': $message';
    }
46
    final String stderr = result?.stderr as String? ?? '';
47 48 49 50 51
    if (stderr.isNotEmpty) {
      output += ':\n$stderr';
    }
    return output;
  }
52 53
}

54
enum Branch { dev, beta, stable }
55 56 57 58 59 60 61

String getBranchName(Branch branch) {
  switch (branch) {
    case Branch.beta:
      return 'beta';
    case Branch.dev:
      return 'dev';
62 63
    case Branch.stable:
      return 'stable';
64 65 66 67 68 69 70 71 72
  }
}

Branch fromBranchName(String name) {
  switch (name) {
    case 'beta':
      return Branch.beta;
    case 'dev':
      return Branch.dev;
73 74
    case 'stable':
      return Branch.stable;
75
    default:
76
      throw ArgumentError('Invalid branch name.');
77 78 79
  }
}

80 81 82 83 84
/// A helper class for classes that want to run a process, optionally have the
/// stderr and stdout reported as the process runs, and capture the stdout
/// properly without dropping any.
class ProcessRunner {
  ProcessRunner({
85
    ProcessManager? processManager,
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    this.subprocessOutput = true,
    this.defaultWorkingDirectory,
    this.platform = const LocalPlatform(),
  }) : processManager = processManager ?? const LocalProcessManager() {
    environment = Map<String, String>.from(platform.environment);
  }

  /// The platform to use for a starting environment.
  final Platform platform;

  /// Set [subprocessOutput] to show output as processes run. Stdout from the
  /// process will be printed to stdout, and stderr printed to stderr.
  final bool subprocessOutput;

  /// Set the [processManager] in order to inject a test instance to perform
  /// testing.
  final ProcessManager processManager;

  /// Sets the default directory used when `workingDirectory` is not specified
  /// to [runProcess].
106
  final Directory? defaultWorkingDirectory;
107 108

  /// The environment to run processes with.
109
  late Map<String, String> environment;
110 111 112 113 114 115 116 117 118

  /// Run the command and arguments in `commandLine` as a sub-process from
  /// `workingDirectory` if set, or the [defaultWorkingDirectory] if not. Uses
  /// [Directory.current] if [defaultWorkingDirectory] is not set.
  ///
  /// Set `failOk` if [runProcess] should not throw an exception when the
  /// command completes with a non-zero exit code.
  Future<String> runProcess(
    List<String> commandLine, {
119
    Directory? workingDirectory,
120 121 122 123 124 125 126 127 128
    bool failOk = false,
  }) async {
    workingDirectory ??= defaultWorkingDirectory ?? Directory.current;
    if (subprocessOutput) {
      stderr.write('Running "${commandLine.join(' ')}" in ${workingDirectory.path}.\n');
    }
    final List<int> output = <int>[];
    final Completer<void> stdoutComplete = Completer<void>();
    final Completer<void> stderrComplete = Completer<void>();
129
    late Process process;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    Future<int> allComplete() async {
      await stderrComplete.future;
      await stdoutComplete.future;
      return process.exitCode;
    }

    try {
      process = await processManager.start(
        commandLine,
        workingDirectory: workingDirectory.absolute.path,
        environment: environment,
      );
      process.stdout.listen(
        (List<int> event) {
          output.addAll(event);
          if (subprocessOutput) {
            stdout.add(event);
          }
        },
        onDone: () async => stdoutComplete.complete(),
      );
      if (subprocessOutput) {
        process.stderr.listen(
          (List<int> event) {
            stderr.add(event);
          },
          onDone: () async => stderrComplete.complete(),
        );
      } else {
        stderrComplete.complete();
      }
    } on ProcessException catch (e) {
      final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
163
          'failed with:\n$e';
164 165 166
      throw PreparePackageException(message);
    } on ArgumentError catch (e) {
      final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
167
          'failed with:\n$e';
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      throw PreparePackageException(message);
    }

    final int exitCode = await allComplete();
    if (exitCode != 0 && !failOk) {
      final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} failed';
      throw PreparePackageException(
        message,
        ProcessResult(0, exitCode, null, 'returned $exitCode'),
      );
    }
    return utf8.decoder.convert(output).trim();
  }
}

183
typedef HttpReader = Future<Uint8List> Function(Uri url, {Map<String, String> headers});
184

185 186
/// Creates a pre-populated Flutter archive from a git repo.
class ArchiveCreator {
187
  /// [tempDir] is the directory to use for creating the archive.  The script
188 189 190
  /// will place several GiB of data there, so it should have available space.
  ///
  /// The processManager argument is used to inject a mock of [ProcessManager] for
191
  /// testing purposes.
192 193 194
  ///
  /// If subprocessOutput is true, then output from processes invoked during
  /// archive creation is echoed to stderr and stdout.
195 196 197 198 199 200
  factory ArchiveCreator(
    Directory tempDir,
    Directory outputDir,
    String revision,
    Branch branch, {
    bool strict = true,
201
    ProcessManager? processManager,
202
    bool subprocessOutput = true,
203
    Platform platform = const LocalPlatform(),
204
    HttpReader? httpReader,
205 206 207 208 209 210 211 212 213 214
  }) {
    final Directory flutterRoot = Directory(path.join(tempDir.path, 'flutter'));
    final ProcessRunner processRunner = ProcessRunner(
      processManager: processManager,
      subprocessOutput: subprocessOutput,
      platform: platform,
    )..environment['PUB_CACHE'] = path.join(
      flutterRoot.absolute.path, '.pub-cache',
    );
    final String flutterExecutable = path.join(
215
      flutterRoot.absolute.path,
216
      'bin',
217
      'flutter',
218
    );
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    final String dartExecutable = path.join(
      flutterRoot.absolute.path,
      'bin',
      'cache',
      'dart-sdk',
      'bin',
      'dart',
    );

    return ArchiveCreator._(
      tempDir: tempDir,
      platform: platform,
      flutterRoot: flutterRoot,
      outputDir: outputDir,
      revision: revision,
      branch: branch,
      strict: strict,
      processRunner: processRunner,
      httpReader: httpReader ?? http.readBytes,
      flutterExecutable: flutterExecutable,
      dartExecutable: dartExecutable,
    );
241 242
  }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  ArchiveCreator._({
    required this.tempDir,
    required this.platform,
    required this.flutterRoot,
    required this.outputDir,
    required this.revision,
    required this.branch,
    required this.strict,
    required ProcessRunner processRunner,
    required this.httpReader,
    required String flutterExecutable,
    required String dartExecutable,
  }) :
    assert(revision.length == 40),
    _processRunner = processRunner,
    _flutter = flutterExecutable,
    _dart = dartExecutable;

261 262
  /// The platform to use for the environment and determining which
  /// platform we're running on.
263
  final Platform platform;
264 265

  /// The branch to build the archive for.  The branch must contain [revision].
266
  final Branch branch;
267 268 269 270 271

  /// The git revision hash to build the archive for. This revision has
  /// to be available in the [branch], although it doesn't have to be
  /// at HEAD, since we clone the branch and then reset to this revision
  /// to create the archive.
272
  final String revision;
273 274

  /// The flutter root directory in the [tempDir].
275
  final Directory flutterRoot;
276 277

  /// The temporary directory used to build the archive in.
278
  final Directory tempDir;
279 280

  /// The directory to write the output file to.
281
  final Directory outputDir;
282

283 284 285 286 287
  /// True if the creator should be strict about checking requirements or not.
  ///
  /// In strict mode, will insist that the [revision] be a tagged revision.
  final bool strict;

288
  final Uri _minGitUri = Uri.parse(mingitForWindowsUrl);
289 290
  final ProcessRunner _processRunner;

291 292 293 294 295
  /// Used to tell the [ArchiveCreator] which function to use for reading
  /// bytes from a URL. Used in tests to inject a fake reader. Defaults to
  /// [http.readBytes].
  final HttpReader httpReader;

296
  final Map<String, String> _version = <String, String>{};
297
  late String _flutter;
298 299 300 301 302 303 304
  late String _dart;

  late final Future<String> _dartArch = (() async {
    // Parse 'arch' out of a string like '... "os_arch"\n'.
    return (await _runDart(<String>['--version']))
        .trim().split(' ').last.replaceAll('"', '').split('_')[1];
  })();
305 306 307

  /// Get the name of the channel as a string.
  String get branchName => getBranchName(branch);
308 309 310

  /// Returns a default archive name when given a Git revision.
  /// Used when an output filename is not given.
311
  Future<String> get _archiveName async {
312
    final String os = platform.operatingSystem.toLowerCase();
313 314
    // Include the intended host archetecture in the file name for non-x64.
    final String arch = await _dartArch == 'x64' ? '' : '${await _dartArch}_';
315 316 317 318 319 320 321 322
    // We don't use .tar.xz on Mac because although it can unpack them
    // on the command line (with tar), the "Archive Utility" that runs
    // when you double-click on them just does some crazy behavior (it
    // converts it to a compressed cpio archive, and when you double
    // click on that, it converts it back to .tar.xz, without ever
    // unpacking it!) So, we use .zip for Mac, and the files are about
    // 220MB larger than they need to be. :-(
    final String suffix = platform.isLinux ? 'tar.xz' : 'zip';
323
    return 'flutter_${os}_$arch${_version[frameworkVersionTag]}-$branchName.$suffix';
324 325 326 327
  }

  /// Checks out the flutter repo and prepares it for other operations.
  ///
328 329 330
  /// Returns the version for this release as obtained from the git tags, and
  /// the dart version as obtained from `flutter --version`.
  Future<Map<String, String>> initializeRepo() async {
331
    await _checkoutFlutter();
332 333 334
    if (_version.isEmpty) {
      _version.addAll(await _getVersion());
    }
335
    return _version;
336 337
  }

338
  /// Performs all of the steps needed to create an archive.
339
  Future<File> createArchive() async {
340
    assert(_version.isNotEmpty, 'Must run initializeRepo before createArchive');
341 342 343 344
    final File outputFile = File(path.join(
      outputDir.absolute.path,
      await _archiveName,
    ));
345 346
    await _installMinGitIfNeeded();
    await _populateCaches();
347
    await _validate();
348 349
    await _archiveFiles(outputFile);
    return outputFile;
350 351
  }

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  /// Validates the integrity of the release package.
  ///
  /// Currently only checks that macOS binaries are codesigned. Will throw a
  /// [PreparePackageException] if the test failes.
  Future<void> _validate() async {
    // Only validate in strict mode, which means `--publish`
    if (!strict || !platform.isMacOS) {
      return;
    }
    // Validate that the dart binary is codesigned
    try {
      // TODO(fujino): Use the conductor https://github.com/flutter/flutter/issues/81701
      await _processRunner.runProcess(
        <String>[
          'codesign',
          '-vvvv',
          '--check-notarization',
369
          _dart,
370 371 372 373 374
        ],
        workingDirectory: flutterRoot,
      );
    } on PreparePackageException catch (e) {
      throw PreparePackageException(
375
        'The binary $_dart was not codesigned!\n${e.message}',
376 377 378 379
      );
    }
  }

380 381
  /// Returns the version map of this release, according the to tags in the
  /// repo and the output of `flutter --version --machine`.
382 383 384 385 386 387 388
  ///
  /// This looks for the tag attached to [revision] and, if it doesn't find one,
  /// git will give an error.
  ///
  /// If [strict] is true, the exact [revision] must be tagged to return the
  /// version.  If [strict] is not true, will look backwards in time starting at
  /// [revision] to find the most recent version tag.
389 390 391 392 393 394
  ///
  /// The version found as a git tag is added to the information given by
  /// `flutter --version --machine` with the `frameworkVersionFromGit` tag, and
  /// returned.
  Future<Map<String, String>> _getVersion() async {
    String gitVersion;
395 396
    if (strict) {
      try {
397
        gitVersion = await _runGit(<String>['describe', '--tags', '--exact-match', revision]);
398
      } on PreparePackageException catch (exception) {
399
        throw PreparePackageException(
400 401 402 403
          'Git error when checking for a version tag attached to revision $revision.\n'
          'Perhaps there is no tag at that revision?:\n'
          '$exception'
        );
404 405
      }
    } else {
406
      gitVersion = await _runGit(<String>['describe', '--tags', '--abbrev=0', revision]);
407
    }
408 409 410 411 412 413 414 415 416
    // Run flutter command twice, once to make sure the flutter command is built
    // and ready (and thus won't output any junk on stdout the second time), and
    // once to capture theJSON output. The second run should be fast.
    await _runFlutter(<String>['--version', '--machine']);
    final String versionJson = await _runFlutter(<String>['--version', '--machine']);
    final Map<String, String> versionMap = <String, String>{};
    final Map<String, dynamic> result = json.decode(versionJson) as Map<String, dynamic>;
    result.forEach((String key, dynamic value) => versionMap[key] = value.toString());
    versionMap[frameworkVersionTag] = gitVersion;
417
    versionMap[dartTargetArchTag] = await _dartArch;
418
    return versionMap;
419
  }
420 421 422

  /// Clone the Flutter repo and make sure that the git environment is sane
  /// for when the user will unpack it.
423
  Future<void> _checkoutFlutter() async {
424 425
    // We want the user to start out the in the specified branch instead of a
    // detached head. To do that, we need to make sure the branch points at the
426
    // desired revision.
427
    await _runGit(<String>['clone', '-b', branchName, chromiumRepo], workingDirectory: tempDir);
428
    await _runGit(<String>['reset', '--hard', revision]);
429 430

    // Make the origin point to github instead of the chromium mirror.
431
    await _runGit(<String>['remote', 'set-url', 'origin', githubRepo]);
432 433 434
  }

  /// Retrieve the MinGit executable from storage and unpack it.
435
  Future<void> _installMinGitIfNeeded() async {
436
    if (!platform.isWindows) {
437 438
      return;
    }
439
    final Uint8List data = await httpReader(_minGitUri);
440
    final File gitFile = File(path.join(tempDir.absolute.path, 'mingit.zip'));
441 442
    await gitFile.writeAsBytes(data, flush: true);

443
    final Directory minGitPath = Directory(path.join(flutterRoot.absolute.path, 'bin', 'mingit'));
444
    await minGitPath.create(recursive: true);
445
    await _unzipArchive(gitFile, workingDirectory: minGitPath);
446 447 448
  }

  /// Prepare the archive repo so that it has all of the caches warmed up and
449
  /// is configured for the user to begin working.
450
  Future<void> _populateCaches() async {
451 452 453 454 455 456
    await _runFlutter(<String>['doctor']);
    await _runFlutter(<String>['update-packages']);
    await _runFlutter(<String>['precache']);
    await _runFlutter(<String>['ide-config']);

    // Create each of the templates, since they will call 'pub get' on
457 458
    // themselves when created, and this will warm the cache with their
    // dependencies too.
459
    for (final String template in <String>['app', 'package', 'plugin']) {
460
      final String createName = path.join(tempDir.path, 'create_$template');
461
      await _runFlutter(
462
        <String>['create', '--template=$template', createName],
463 464 465
        // Run it outside the cloned Flutter repo to not nest git repos, since
        // they'll be git repos themselves too.
        workingDirectory: tempDir,
466 467 468 469 470 471
      );
    }

    // Yes, we could just skip all .packages files when constructing
    // the archive, but some are checked in, and we don't want to skip
    // those.
472 473 474 475 476 477 478 479
    await _runGit(<String>[
      'clean',
      '-f',
      // Do not -X as it could lead to entire bin/cache getting cleaned
      '-x',
      '--',
      '**/.packages',
    ]);
480
    /// Remove package_config files and any contents in .dart_tool
481 482 483 484 485 486 487 488 489 490 491 492 493 494
    await _runGit(<String>[
      'clean',
      '-f',
      '-x',
      '--',
      '**/.dart_tool/',
    ]);

    // Ensure the above commands do not clean out the cache
    final Directory flutterCache = Directory(path.join(flutterRoot.absolute.path, 'bin', 'cache'));
    if (!flutterCache.existsSync()) {
      throw Exception('The flutter cache was not found at ${flutterCache.path}!');
    }

495 496 497 498 499 500
    /// Remove git subfolder from .pub-cache, this contains the flutter goldens
    /// and new flutter_gallery.
    final Directory gitCache = Directory(path.join(flutterRoot.absolute.path, '.pub-cache', 'git'));
    if (gitCache.existsSync()) {
      gitCache.deleteSync(recursive: true);
    }
501 502
  }

503
  /// Write the archive to the given output file.
504
  Future<void> _archiveFiles(File outputFile) async {
505
    if (outputFile.path.toLowerCase().endsWith('.zip')) {
506
      await _createZipArchive(outputFile, flutterRoot);
507
    } else if (outputFile.path.toLowerCase().endsWith('.tar.xz')) {
508
      await _createTarArchive(outputFile, flutterRoot);
509 510 511
    }
  }

512 513 514 515 516 517 518
  Future<String> _runDart(List<String> args, {Directory? workingDirectory}) {
    return _processRunner.runProcess(
      <String>[_dart, ...args],
      workingDirectory: workingDirectory ?? flutterRoot,
    );
  }

519
  Future<String> _runFlutter(List<String> args, {Directory? workingDirectory}) {
520 521
    return _processRunner.runProcess(
      <String>[_flutter, ...args],
522 523
      workingDirectory: workingDirectory ?? flutterRoot,
    );
524
  }
525

526
  Future<String> _runGit(List<String> args, {Directory? workingDirectory}) {
527 528 529 530
    return _processRunner.runProcess(
      <String>['git', ...args],
      workingDirectory: workingDirectory ?? flutterRoot,
    );
531 532
  }

533 534
  /// Unpacks the given zip file into the currentDirectory (if set), or the
  /// same directory as the archive.
535
  Future<String> _unzipArchive(File archive, {Directory? workingDirectory}) {
536
    workingDirectory ??= Directory(path.dirname(archive.absolute.path));
537 538 539 540 541 542 543 544 545 546 547 548 549
    List<String> commandLine;
    if (platform.isWindows) {
      commandLine = <String>[
        '7za',
        'x',
        archive.absolute.path,
      ];
    } else {
      commandLine = <String>[
        'unzip',
        archive.absolute.path,
      ];
    }
550
    return _processRunner.runProcess(commandLine, workingDirectory: workingDirectory);
551 552
  }

553
  /// Create a zip archive from the directory source.
554
  Future<String> _createZipArchive(File output, Directory source) async {
555 556
    List<String> commandLine;
    if (platform.isWindows) {
557
      // Unhide the .git folder, https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib.
558
      await _processRunner.runProcess(
559 560 561
        <String>['attrib', '-h', '.git'],
        workingDirectory: Directory(source.absolute.path),
      );
562 563 564 565 566 567 568 569 570 571 572 573 574
      commandLine = <String>[
        '7za',
        'a',
        '-tzip',
        '-mx=9',
        output.absolute.path,
        path.basename(source.path),
      ];
    } else {
      commandLine = <String>[
        'zip',
        '-r',
        '-9',
575
        '--symlinks',
576 577 578 579
        output.absolute.path,
        path.basename(source.path),
      ];
    }
580
    return _processRunner.runProcess(
581
      commandLine,
582
      workingDirectory: Directory(path.dirname(source.absolute.path)),
583
    );
584 585
  }

586
  /// Create a tar archive from the directory source.
587 588 589 590 591 592 593
  Future<String> _createTarArchive(File output, Directory source) {
    return _processRunner.runProcess(<String>[
      'tar',
      'cJf',
      output.absolute.path,
      path.basename(source.absolute.path),
    ], workingDirectory: Directory(path.dirname(source.absolute.path)));
594
  }
595
}
596

597 598 599 600 601 602
class ArchivePublisher {
  ArchivePublisher(
    this.tempDir,
    this.revision,
    this.branch,
    this.version,
603 604
    this.outputFile,
    this.dryRun, {
605
    ProcessManager? processManager,
606 607
    bool subprocessOutput = true,
    this.platform = const LocalPlatform(),
608 609
  })  : assert(revision.length == 40),
        platformName = platform.operatingSystem.toLowerCase(),
610
        metadataGsPath = '$gsReleaseFolder/${getMetadataFilename(platform)}',
611 612
        _processRunner = ProcessRunner(
          processManager: processManager,
613
          subprocessOutput: subprocessOutput,
614
        );
615 616 617 618 619 620

  final Platform platform;
  final String platformName;
  final String metadataGsPath;
  final Branch branch;
  final String revision;
621
  final Map<String, String> version;
622 623 624
  final Directory tempDir;
  final File outputFile;
  final ProcessRunner _processRunner;
625
  final bool dryRun;
626
  String get branchName => getBranchName(branch);
627 628
  String get destinationArchivePath => '$branchName/$platformName/${path.basename(outputFile.path)}';
  static String getMetadataFilename(Platform platform) => 'releases_${platform.operatingSystem.toLowerCase()}.json';
629

630 631 632 633 634 635 636 637 638 639 640 641
  Future<String> _getChecksum(File archiveFile) async {
    final DigestSink digestSink = DigestSink();
    final ByteConversionSink sink = sha256.startChunkedConversion(digestSink);

    final Stream<List<int>> stream = archiveFile.openRead();
    await stream.forEach((List<int> chunk) {
      sink.add(chunk);
    });
    sink.close();
    return digestSink.value.toString();
  }

642
  /// Publish the archive to Google Storage.
643 644 645 646
  ///
  /// This method will throw if the target archive already exists on cloud
  /// storage.
  Future<void> publishArchive([bool forceUpload = false]) async {
647 648 649 650 651 652
    final String destGsPath = '$gsReleaseFolder/$destinationArchivePath';
    if (!forceUpload) {
      if (await _cloudPathExists(destGsPath) && !dryRun) {
        throw PreparePackageException(
          'File $destGsPath already exists on cloud storage!',
        );
653 654
      }
    }
655 656 657 658 659 660
    await _cloudCopy(
      src: outputFile.absolute.path,
      dest: destGsPath,
    );
    assert(tempDir.existsSync());
    await _updateMetadata('$gsReleaseFolder/${getMetadataFilename(platform)}');
661 662
  }

663 664
  Future<Map<String, dynamic>> _addRelease(Map<String, dynamic> jsonData) async {
    jsonData['base_url'] = '$baseUrl$releaseFolder';
665 666 667
    if (!jsonData.containsKey('current_release')) {
      jsonData['current_release'] = <String, String>{};
    }
668
    (jsonData['current_release'] as Map<String, dynamic>)[branchName] = revision;
669 670 671 672 673 674 675
    if (!jsonData.containsKey('releases')) {
      jsonData['releases'] = <Map<String, dynamic>>[];
    }

    final Map<String, dynamic> newEntry = <String, dynamic>{};
    newEntry['hash'] = revision;
    newEntry['channel'] = branchName;
676 677
    newEntry['version'] = version[frameworkVersionTag];
    newEntry['dart_sdk_version'] = version[dartVersionTag];
678
    newEntry['dart_sdk_arch'] = version[dartTargetArchTag];
679
    newEntry['release_date'] = DateTime.now().toUtc().toIso8601String();
680
    newEntry['archive'] = destinationArchivePath;
681
    newEntry['sha256'] = await _getChecksum(outputFile);
682 683

    // Search for any entries with the same hash and channel and remove them.
684
    final List<dynamic> releases = jsonData['releases'] as List<dynamic>;
685
    jsonData['releases'] = <Map<String, dynamic>>[
686
      for (final Map<String, dynamic> entry in releases.cast<Map<String, dynamic>>())
687 688 689
        if (entry['hash'] != newEntry['hash'] ||
            entry['channel'] != newEntry['channel'] ||
            entry['dart_sdk_arch'] != newEntry['dart_sdk_arch'])
690 691 692
          entry,
      newEntry,
    ]..sort((Map<String, dynamic> a, Map<String, dynamic> b) {
693 694
      final DateTime aDate = DateTime.parse(a['release_date'] as String);
      final DateTime bDate = DateTime.parse(b['release_date'] as String);
695 696 697 698 699
      return bDate.compareTo(aDate);
    });
    return jsonData;
  }

700
  Future<void> _updateMetadata(String gsPath) async {
701 702 703 704
    // We can't just cat the metadata from the server with 'gsutil cat', because
    // Windows wants to echo the commands that execute in gsutil.bat to the
    // stdout when we do that. So, we copy the file locally and then read it
    // back in.
705
    final File metadataFile = File(
706 707
      path.join(tempDir.absolute.path, getMetadataFilename(platform)),
    );
708
    await _runGsUtil(<String>['cp', gsPath, metadataFile.absolute.path]);
709
    Map<String, dynamic> jsonData = <String, dynamic>{};
710 711 712 713 714 715 716 717 718 719 720
    if (!dryRun) {
      final String currentMetadata = metadataFile.readAsStringSync();
      if (currentMetadata.isEmpty) {
        throw PreparePackageException('Empty metadata received from server');
      }
      try {
        jsonData = json.decode(currentMetadata) as Map<String, dynamic>;
      } on FormatException catch (e) {
        throw PreparePackageException('Unable to parse JSON metadata received from cloud: $e');
      }
    }
721 722 723 724 725 726 727
    // Run _addRelease, even on a dry run, so we can inspect the metadata on a
    // dry run. On a dry run, the only thing in the metadata file be the new
    // release.
    jsonData = await _addRelease(jsonData);

    const JsonEncoder encoder = JsonEncoder.withIndent('  ');
    metadataFile.writeAsStringSync(encoder.convert(jsonData));
728 729 730 731 732 733 734 735
    await _cloudCopy(
      src: metadataFile.absolute.path,
      dest: gsPath,
      // This metadata file is used by the website, so we don't want a long
      // latency between publishing a release and it being available on the
      // site.
      cacheSeconds: shortCacheSeconds,
    );
736 737
  }

738 739
  Future<String> _runGsUtil(
    List<String> args, {
740
    Directory? workingDirectory,
741
    bool failOk = false,
742
  }) async {
743 744 745 746
    if (dryRun) {
      print('gsutil.py -- $args');
      return '';
    }
747
    if (platform.isWindows) {
748
      return _processRunner.runProcess(
749
        <String>['python3', path.join(platform.environment['DEPOT_TOOLS']!, 'gsutil.py'), '--', ...args],
750 751 752 753 754
        workingDirectory: workingDirectory,
        failOk: failOk,
      );
    }

755
    return _processRunner.runProcess(
756
      <String>['gsutil.py', '--', ...args],
757 758 759 760 761
      workingDirectory: workingDirectory,
      failOk: failOk,
    );
  }

762 763 764 765 766 767 768 769 770 771 772 773 774
  /// Determine if a file exists at a given [cloudPath].
  Future<bool> _cloudPathExists(String cloudPath) async {
    try {
      await _runGsUtil(
        <String>['stat', cloudPath],
      );
    } on PreparePackageException {
      // `gsutil stat gs://path/to/file` will exit with 1 if file does not exist
      return false;
    }
    return true;
  }

775
  Future<String> _cloudCopy({
776 777 778
    required String src,
    required String dest,
    int? cacheSeconds,
779
  }) async {
780 781
    // We often don't have permission to overwrite, but
    // we have permission to remove, so that's what we do.
782
    await _runGsUtil(<String>['rm', dest], failOk: true);
783
    String? mimeType;
784 785 786 787 788 789 790 791 792
    if (dest.endsWith('.tar.xz')) {
      mimeType = 'application/x-gtar';
    }
    if (dest.endsWith('.zip')) {
      mimeType = 'application/zip';
    }
    if (dest.endsWith('.json')) {
      mimeType = 'application/json';
    }
793
    return _runGsUtil(<String>[
794 795 796
      // Use our preferred MIME type for the files we care about
      // and let gsutil figure it out for anything else.
      if (mimeType != null) ...<String>['-h', 'Content-Type:$mimeType'],
797
      if (cacheSeconds != null) ...<String>['-h', 'Cache-Control:max-age=$cacheSeconds'],
798 799 800 801
      'cp',
      src,
      dest,
    ]);
802 803 804 805 806 807 808
  }
}

/// Prepares a flutter git repo to be packaged up for distribution.
/// It mainly serves to populate the .pub-cache with any appropriate Dart
/// packages, and the flutter cache in bin/cache with the appropriate
/// dependencies and snapshots.
809
///
Ian Hickson's avatar
Ian Hickson committed
810 811
/// Archives contain the executables and customizations for the platform that
/// they are created on.
Ian Hickson's avatar
Ian Hickson committed
812
Future<void> main(List<String> rawArguments) async {
813
  final ArgParser argParser = ArgParser();
814 815 816 817
  argParser.addOption(
    'temp_dir',
    help: 'A location where temporary files may be written. Defaults to a '
        'directory in the system temp folder. Will write a few GiB of data, '
818 819 820
        'so it should have sufficient free space. If a temp_dir is not '
        'specified, then the default temp_dir will be created, used, and '
        'removed automatically.',
821
  );
822 823 824
  argParser.addOption('revision',
      help: 'The Flutter git repo revision to build the '
          'archive with. Must be the full 40-character hash. Required.');
825
  argParser.addOption(
826
    'branch',
827
    allowed: Branch.values.map<String>((Branch branch) => getBranchName(branch)),
828
    help: 'The Flutter branch to build the archive with. Required.',
829 830 831
  );
  argParser.addOption(
    'output',
832 833 834 835 836 837 838
    help: 'The path to the directory where the output archive should be '
        'written. If --output is not specified, the archive will be written to '
        "the current directory. If the output directory doesn't exist, it, and "
        'the path to it, will be created.',
  );
  argParser.addFlag(
    'publish',
839 840
    help: 'If set, will publish the archive to Google Cloud Storage upon '
        'successful creation of the archive. Will publish under this '
841
        'directory: $baseUrl$releaseFolder',
842
  );
843 844 845 846 847
  argParser.addFlag(
    'force',
    abbr: 'f',
    help: 'Overwrite a previously uploaded package.',
  );
848 849 850 851 852
  argParser.addFlag(
    'dry_run',
    negatable: false,
    help: 'Prints gsutil commands instead of executing them.',
  );
853 854 855 856
  argParser.addFlag(
    'help',
    negatable: false,
    help: 'Print help for this command.',
857
  );
858

Ian Hickson's avatar
Ian Hickson committed
859
  final ArgResults parsedArguments = argParser.parse(rawArguments);
860

861
  if (parsedArguments['help'] as bool) {
862 863 864 865
    print(argParser.usage);
    exit(0);
  }

866 867 868 869 870 871
  void errorExit(String message, {int exitCode = -1}) {
    stderr.write('Error: $message\n\n');
    stderr.write('${argParser.usage}\n');
    exit(exitCode);
  }

872
  final String revision = parsedArguments['revision'] as String;
873
  if (!parsedArguments.wasParsed('revision')) {
874 875
    errorExit('Invalid argument: --revision must be specified.');
  }
876 877 878 879
  if (revision.length != 40) {
    errorExit('Invalid argument: --revision must be the entire hash, not just a prefix.');
  }

880
  if (!parsedArguments.wasParsed('branch')) {
881 882
    errorExit('Invalid argument: --branch must be specified.');
  }
883

884
  final String tempDirArg = parsedArguments['temp_dir'] as String;
885
  Directory tempDir;
886
  bool removeTempDir = false;
887
  if (tempDirArg == null || tempDirArg.isEmpty) {
888
    tempDir = Directory.systemTemp.createTempSync('flutter_package.');
889 890
    removeTempDir = true;
  } else {
891
    tempDir = Directory(tempDirArg);
892
    if (!tempDir.existsSync()) {
893
      errorExit("Temporary directory $tempDirArg doesn't exist.");
894 895 896
    }
  }

897
  Directory outputDir;
Ian Hickson's avatar
Ian Hickson committed
898
  if (parsedArguments['output'] == null) {
899
    outputDir = tempDir;
900
  } else {
901
    outputDir = Directory(parsedArguments['output'] as String);
902 903
    if (!outputDir.existsSync()) {
      outputDir.createSync(recursive: true);
904
    }
905 906
  }

907 908
  final bool publish = parsedArguments['publish'] as bool;
  final bool dryRun = parsedArguments['dry_run'] as bool;
909
  final Branch branch = fromBranchName(parsedArguments['branch'] as String);
910 911 912 913 914 915 916
  final ArchiveCreator creator = ArchiveCreator(
    tempDir,
    outputDir,
    revision,
    branch,
    strict: publish && !dryRun,
  );
917
  int exitCode = 0;
918
  late String message;
919
  try {
920
    final Map<String, String> version = await creator.initializeRepo();
921
    final File outputFile = await creator.createArchive();
922
    if (parsedArguments['publish'] as bool) {
923
      final ArchivePublisher publisher = ArchivePublisher(
924 925 926 927 928
        tempDir,
        revision,
        branch,
        version,
        outputFile,
929
        dryRun,
930
      );
931
      await publisher.publishArchive(parsedArguments['force'] as bool);
932
    }
933
  } on PreparePackageException catch (e) {
934
    exitCode = e.exitCode;
935 936
    message = e.message;
  } catch (e) {
937
    exitCode = -1;
938
    message = e.toString();
939 940
  } finally {
    if (removeTempDir) {
941
      tempDir.deleteSync(recursive: true);
942 943 944 945 946 947 948
    }
    if (exitCode != 0) {
      errorExit(message, exitCode: exitCode);
    }
    exit(0);
  }
}