|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io' as io; |
| 6 | + |
| 7 | +import 'package:args/args.dart'; |
| 8 | +import 'package:conductor_core/conductor_core.dart'; |
| 9 | +import 'package:conductor_core/packages_autoroller.dart'; |
| 10 | +import 'package:file/file.dart'; |
| 11 | +import 'package:file/local.dart'; |
| 12 | +import 'package:platform/platform.dart'; |
| 13 | +import 'package:process/process.dart'; |
| 14 | + |
| 15 | +const String kTokenOption = 'token'; |
| 16 | +const String kGithubClient = 'github-client'; |
| 17 | +const String kMirrorRemote = 'mirror-remote'; |
| 18 | +const String kUpstreamRemote = 'upstream-remote'; |
| 19 | + |
| 20 | +Future<void> main(List<String> args) async { |
| 21 | + final ArgParser parser = ArgParser(); |
| 22 | + parser.addOption( |
| 23 | + kTokenOption, |
| 24 | + help: 'GitHub access token env variable name.', |
| 25 | + defaultsTo: 'GITHUB_TOKEN', |
| 26 | + ); |
| 27 | + parser.addOption( |
| 28 | + kGithubClient, |
| 29 | + help: 'Path to GitHub CLI client. If not provided, it is assumed `gh` is ' |
| 30 | + 'present on the PATH.', |
| 31 | + ); |
| 32 | + parser.addOption( |
| 33 | + kMirrorRemote, |
| 34 | + help: 'The mirror git remote that the feature branch will be pushed to. ' |
| 35 | + 'Required', |
| 36 | + mandatory: true, |
| 37 | + ); |
| 38 | + parser.addOption( |
| 39 | + kUpstreamRemote, |
| 40 | + help: 'The upstream git remote that the feature branch will be merged to.', |
| 41 | + hide: true, |
| 42 | + defaultsTo: 'https://github.com/flutter/flutter.git', |
| 43 | + ); |
| 44 | + |
| 45 | + final ArgResults results; |
| 46 | + try { |
| 47 | + results = parser.parse(args); |
| 48 | + } on FormatException { |
| 49 | + io.stdout.writeln(''' |
| 50 | +Usage: |
| 51 | +
|
| 52 | +${parser.usage} |
| 53 | +'''); |
| 54 | + rethrow; |
| 55 | + } |
| 56 | + |
| 57 | + final String mirrorUrl = results[kMirrorRemote]! as String; |
| 58 | + final String upstreamUrl = results[kUpstreamRemote]! as String; |
| 59 | + const Platform platform = LocalPlatform(); |
| 60 | + final String tokenName = results[kTokenOption]! as String; |
| 61 | + final String? token = platform.environment[tokenName]; |
| 62 | + if (token == null || token.isEmpty) { |
| 63 | + throw FormatException( |
| 64 | + 'Tried to read a GitHub access token from env variable \$$tokenName but it was undefined or empty', |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + final FrameworkRepository framework = FrameworkRepository( |
| 69 | + _localCheckouts, |
| 70 | + mirrorRemote: Remote.mirror(mirrorUrl), |
| 71 | + upstreamRemote: Remote.upstream(upstreamUrl), |
| 72 | + ); |
| 73 | + |
| 74 | + await PackageAutoroller( |
| 75 | + framework: framework, |
| 76 | + githubClient: results[kGithubClient] as String? ?? 'gh', |
| 77 | + orgName: _parseOrgName(mirrorUrl), |
| 78 | + token: token, |
| 79 | + processManager: const LocalProcessManager(), |
| 80 | + ).roll(); |
| 81 | +} |
| 82 | + |
| 83 | +String _parseOrgName(String remoteUrl) { |
| 84 | + final RegExp pattern = RegExp(r'^https:\/\/github\.com\/(.*)\/'); |
| 85 | + final RegExpMatch? match = pattern.firstMatch(remoteUrl); |
| 86 | + if (match == null) { |
| 87 | + throw FormatException( |
| 88 | + 'Malformed upstream URL "$remoteUrl", should start with "https://github.com/"', |
| 89 | + ); |
| 90 | + } |
| 91 | + return match.group(1)!; |
| 92 | +} |
| 93 | + |
| 94 | +Checkouts get _localCheckouts { |
| 95 | + const FileSystem fileSystem = LocalFileSystem(); |
| 96 | + const ProcessManager processManager = LocalProcessManager(); |
| 97 | + const Platform platform = LocalPlatform(); |
| 98 | + final Stdio stdio = VerboseStdio( |
| 99 | + stdout: io.stdout, |
| 100 | + stderr: io.stderr, |
| 101 | + stdin: io.stdin, |
| 102 | + ); |
| 103 | + return Checkouts( |
| 104 | + fileSystem: fileSystem, |
| 105 | + parentDirectory: _localFlutterRoot.parent, |
| 106 | + platform: platform, |
| 107 | + processManager: processManager, |
| 108 | + stdio: stdio, |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +Directory get _localFlutterRoot { |
| 113 | + String filePath; |
| 114 | + const FileSystem fileSystem = LocalFileSystem(); |
| 115 | + const Platform platform = LocalPlatform(); |
| 116 | + |
| 117 | + filePath = platform.script.toFilePath(); |
| 118 | + final String checkoutsDirname = fileSystem.path.normalize( |
| 119 | + fileSystem.path.join( |
| 120 | + fileSystem.path.dirname(filePath), // flutter/dev/conductor/core/bin |
| 121 | + '..', // flutter/dev/conductor/core |
| 122 | + '..', // flutter/dev/conductor |
| 123 | + '..', // flutter/dev |
| 124 | + '..', // flutter |
| 125 | + ), |
| 126 | + ); |
| 127 | + return fileSystem.directory(checkoutsDirname); |
| 128 | +} |
0 commit comments