globals.dart 4.09 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:args/args.dart';
6 7 8 9
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:platform/platform.dart';

10 11
const String kUpstreamRemote = 'https://github.com/flutter/flutter.git';

12 13
const String gsutilBinary = 'gsutil.py';

14 15 16 17 18 19 20
const List<String> kReleaseChannels = <String>[
  'stable',
  'beta',
  'dev',
  'master',
];

21 22 23 24 25 26
const String kReleaseDocumentationUrl = 'https://github.com/flutter/flutter/wiki/Flutter-Cherrypick-Process';

final RegExp releaseCandidateBranchRegex = RegExp(
  r'flutter-(\d+)\.(\d+)-candidate\.(\d+)',
);

27 28 29 30 31
/// Cast a dynamic to String and trim.
String stdoutToString(dynamic input) {
  final String str = input as String;
  return str.trim();
}
32 33 34 35 36 37 38 39 40 41

class ConductorException implements Exception {
  ConductorException(this.message);

  final String message;

  @override
  String toString() => 'Exception: $message';
}

42
Directory? _flutterRoot;
43 44
Directory get localFlutterRoot {
  if (_flutterRoot != null) {
45
    return _flutterRoot!;
46 47 48 49 50 51 52 53 54 55 56
  }
  String filePath;
  const FileSystem fileSystem = LocalFileSystem();
  const Platform platform = LocalPlatform();

  // If a test
  if (platform.script.scheme == 'data') {
    final RegExp pattern = RegExp(
      r'(file:\/\/[^"]*[/\\]dev\/tools[/\\][^"]+\.dart)',
      multiLine: true,
    );
57
    final Match? match =
58 59 60 61 62 63
        pattern.firstMatch(Uri.decodeFull(platform.script.path));
    if (match == null) {
      throw Exception(
        'Cannot determine path of script!\n${platform.script.path}',
      );
    }
64
    filePath = Uri.parse(match.group(1)!).path.replaceAll(r'%20', ' ');
65 66 67 68 69 70 71 72 73 74 75 76
  } else {
    filePath = platform.script.toFilePath();
  }
  final String checkoutsDirname = fileSystem.path.normalize(
    fileSystem.path.join(
      fileSystem.path.dirname(filePath),
      '..', // flutter/dev/tools
      '..', // flutter/dev
      '..', // flutter
    ),
  );
  _flutterRoot = fileSystem.directory(checkoutsDirname);
77
  return _flutterRoot!;
78 79 80 81 82 83 84 85 86 87 88 89
}

bool assertsEnabled() {
  // Verify asserts enabled
  bool assertsEnabled = false;

  assert(() {
    assertsEnabled = true;
    return true;
  }());
  return assertsEnabled;
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

/// Either return the value from [env] or fall back to [argResults].
///
/// If the key does not exist in either the environment or CLI args, throws a
/// [ConductorException].
///
/// The environment is favored over CLI args since the latter can have a default
/// value, which the environment should be able to override.
String getValueFromEnvOrArgs(
  String name,
  ArgResults argResults,
  Map<String, String> env,
) {
  final String envName = fromArgToEnvName(name);
  if (env[envName] != null ) {
105
    return env[envName]!;
106
  }
107
  final String? argValue = argResults[name] as String?;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  if (argValue != null) {
    return argValue;
  }

  throw ConductorException(
    'Expected either the CLI arg --$name or the environment variable $envName '
    'to be provided!');
}

/// Return multiple values from the environment or fall back to [argResults].
///
/// Values read from an environment variable are assumed to be comma-delimited.
///
/// If the key does not exist in either the CLI args or environment, throws a
/// [ConductorException].
///
/// The environment is favored over CLI args since the latter can have a default
/// value, which the environment should be able to override.
List<String> getValuesFromEnvOrArgs(
  String name,
  ArgResults argResults,
  Map<String, String> env,
) {
  final String envName = fromArgToEnvName(name);
  if (env[envName] != null && env[envName] != '') {
133
    return env[envName]!.split(',');
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
  final List<String> argValues = argResults[name] as List<String>;
  if (argValues != null) {
    return argValues;
  }

  throw ConductorException(
    'Expected either the CLI arg --$name or the environment variable $envName '
    'to be provided!');
}

/// Translate CLI arg names to env variable names.
///
/// For example, 'state-file' -> 'STATE_FILE'.
String fromArgToEnvName(String argName) {
  return argName.toUpperCase().replaceAll(r'-', r'_');
}