Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter-tool: enum cleanup #124760

Merged
merged 15 commits into from
Apr 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ class AndroidApk extends ApplicationPackage implements PrebuiltApplicationPackag
if (buildInfo == null) {
filename = 'app.apk';
} else if (buildInfo.flavor == null) {
filename = 'app-${buildInfo.mode.name}.apk';
filename = 'app-${buildInfo.mode.cliName}.apk';
} else {
filename = 'app-${buildInfo.lowerCasedFlavor}-${buildInfo.mode.name}.apk';
filename = 'app-${buildInfo.lowerCasedFlavor}-${buildInfo.mode.cliName}.apk';
}

if (androidProject.isUsingGradle && androidProject.isSupportedVersion) {
Directory apkDirectory = getApkDirectory(androidProject.parent);
if (androidProject.parent.isModule) {
// Module builds output the apk in a subdirectory that corresponds
// to the buildmode of the apk.
apkDirectory = apkDirectory.childDirectory(buildInfo!.mode.name);
apkDirectory = apkDirectory.childDirectory(buildInfo!.mode.cliName);
}
apkFile = apkDirectory.childFile(filename);
if (apkFile.existsSync()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void validateBuild(AndroidBuildInfo androidBuildInfo) {
}
if (buildInfo.mode.isPrecompiled && androidBuildInfo.targetArchs.contains(AndroidArch.x86)) {
throwToolExit(
'Cannot build ${androidBuildInfo.buildInfo.mode.name} mode for x86 ABI.\n'
'Cannot build ${androidBuildInfo.buildInfo.mode.cliName} mode for x86 ABI.\n'
'For more information see $kSupportedAbis .'
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_tools/lib/src/artifacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ class CachedArtifacts implements Artifacts {
// https://github.com/flutter/flutter/issues/38935
String platformDirName = _enginePlatformDirectoryName(platform);
if (mode == BuildMode.profile || mode == BuildMode.release) {
platformDirName = '$platformDirName-${getNameForBuildMode(mode!)}';
platformDirName = '$platformDirName-${mode!.cliName}';
}
final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
return _fileSystem.path.join(engineArtifactsPath, platformDirName, _artifactToFileName(artifact, _platform, mode));
Expand Down Expand Up @@ -713,7 +713,7 @@ class CachedArtifacts implements Artifacts {
if (mode == BuildMode.debug || mode == null) {
return _fileSystem.path.join(engineDir, platformName);
}
final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode), '-')}' : '';
final String suffix = mode != BuildMode.debug ? '-${snakeCase(mode.cliName, '-')}' : '';
return _fileSystem.path.join(engineDir, platformName + suffix);
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
Expand All @@ -727,7 +727,7 @@ class CachedArtifacts implements Artifacts {
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
assert(mode != null, 'Need to specify a build mode for platform $platform.');
final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode!), '-')}' : '';
final String suffix = mode != BuildMode.debug ? '-${snakeCase(mode!.cliName, '-')}' : '';
return _fileSystem.path.join(engineDir, platformName + suffix);
case TargetPlatform.android:
assert(false, 'cannot use TargetPlatform.android to look up artifacts');
Expand Down
19 changes: 12 additions & 7 deletions packages/flutter_tools/lib/src/base/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ String snakeCase(String str, [ String sep = '_' ]) {
(Match m) => '${m.start == 0 ? '' : sep}${m[0]!.toLowerCase()}');
}

abstract interface class CliEnum implements Enum {
String get cliName;
String get helpText;

static Map<String, String> allowedHelp<T extends CliEnum>(List<T> values) =>
kevmoo marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String>.fromEntries(
values.map(
(T e) => MapEntry<String, String>(e.cliName, e.helpText),
),
);
}

/// Converts `fooBar` to `FooBar`.
///
/// This uses [toBeginningOfSentenceCase](https://pub.dev/documentation/intl/latest/intl/toBeginningOfSentenceCase.html),
Expand All @@ -53,13 +65,6 @@ String snakeCaseToTitleCase(String snakeCaseString) {
/// Return the plural of the given word (`cat(s)`).
String pluralize(String word, int count) => count == 1 ? word : '${word}s';

/// Return the name of an enum item.
String getEnumName(dynamic enumItem) {
final String name = '$enumItem';
final int index = name.indexOf('.');
return index == -1 ? name : name.substring(index + 1);
}

String toPrettyJson(Object jsonable) {
final String value = const JsonEncoder.withIndent(' ').convert(jsonable);
return '$value\n';
Expand Down
61 changes: 16 additions & 45 deletions packages/flutter_tools/lib/src/build_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class BuildInfo {
bool get usesAot => isAotBuildMode(mode);
bool get supportsEmulator => isEmulatorBuildMode(mode);
bool get supportsSimulator => isEmulatorBuildMode(mode);
String get modeName => getModeName(mode);
String get modeName => mode.cliName;
String get friendlyModeName => getFriendlyModeName(mode);

/// the flavor name in the output apk files is lower-cased (see flutter.gradle),
Expand All @@ -233,7 +233,7 @@ class BuildInfo {
// packagesPath and performanceMeasurementFile are not passed into
// the Environment map.
return <String, String>{
kBuildMode: getNameForBuildMode(mode),
kBuildMode: mode.cliName,
if (dartDefines.isNotEmpty)
kDartDefines: encodeDartDefines(dartDefines),
kDartObfuscation: dartObfuscation.toString(),
Expand Down Expand Up @@ -377,41 +377,25 @@ class AndroidBuildInfo {
}

/// A summary of the compilation strategy used for Dart.
class BuildMode {
const BuildMode._(this.name);

factory BuildMode.fromName(String value) {
switch (value) {
case 'debug':
return BuildMode.debug;
case 'profile':
return BuildMode.profile;
case 'release':
return BuildMode.release;
case 'jit_release':
return BuildMode.jitRelease;
}
throw ArgumentError('$value is not a supported build mode');
}

enum BuildMode {
/// Built in JIT mode with no optimizations, enabled asserts, and a VM service.
static const BuildMode debug = BuildMode._('debug');
debug,

/// Built in AOT mode with some optimizations and a VM service.
static const BuildMode profile = BuildMode._('profile');
profile,

/// Built in AOT mode with all optimizations and no VM service.
static const BuildMode release = BuildMode._('release');
release,

/// Built in JIT mode with all optimizations and no VM service.
static const BuildMode jitRelease = BuildMode._('jit_release');
jitRelease;

factory BuildMode.fromCliName(String value) => values.singleWhere(
(BuildMode element) => element.cliName == value,
orElse: () =>
throw ArgumentError('$value is not a supported build mode'),
);

static const List<BuildMode> values = <BuildMode>[
debug,
profile,
release,
jitRelease,
];
static const Set<BuildMode> releaseModes = <BuildMode>{
release,
jitRelease,
Expand All @@ -433,21 +417,10 @@ class BuildMode {
/// Whether this mode is using the precompiled runtime.
bool get isPrecompiled => !isJit;

/// The name for this build mode.
final String name;
String get cliName => snakeCase(name);

@override
String toString() => name;
}

/// Return the name for the build mode, or "any" if null.
String getNameForBuildMode(BuildMode buildMode) {
return buildMode.name;
}

/// Returns the [BuildMode] for a particular `name`.
BuildMode getBuildModeForName(String name) {
return BuildMode.fromName(name);
String toString() => cliName;
}

/// Environment type of the target device.
Expand Down Expand Up @@ -540,10 +513,8 @@ String? validatedBuildNameForPlatform(TargetPlatform targetPlatform, String? bui
return buildName;
}

String getModeName(BuildMode mode) => getEnumName(mode);

String getFriendlyModeName(BuildMode mode) {
return snakeCase(getModeName(mode)).replaceAll('_', ' ');
return snakeCase(mode.cliName).replaceAll('_', ' ');
}

// Returns true if the selected build mode uses ahead-of-time compilation.
Expand Down
12 changes: 6 additions & 6 deletions packages/flutter_tools/lib/src/build_system/targets/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class AndroidAssetBundle extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final Directory outputDirectory = environment.outputDir
.childDirectory('flutter_assets')
..createSync(recursive: true);
Expand Down Expand Up @@ -167,7 +167,7 @@ class AndroidAot extends AotElfBase {
}

@override
String get name => 'android_aot_${getNameForBuildMode(buildMode)}_'
String get name => 'android_aot_${buildMode.cliName}_'
'${getNameForTargetPlatform(targetPlatform)}';

/// The specific Android ABI we are building for.
Expand Down Expand Up @@ -229,7 +229,7 @@ class AndroidAot extends AotElfBase {
extraGenSnapshotOptions.add('--loading_unit_manifest=$manifestPath');
outputs.add(environment.fileSystem.file(manifestPath));
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final bool dartObfuscation = environment.defines[kDartObfuscation] == 'true';
final String? codeSizeDirectory = environment.defines[kCodeSizeDirectory];

Expand Down Expand Up @@ -299,7 +299,7 @@ class AndroidAotBundle extends Target {
}

@override
String get name => 'android_aot_bundle_${getNameForBuildMode(dependency.buildMode)}_'
String get name => 'android_aot_bundle_${dependency.buildMode.cliName}_'
'${getNameForTargetPlatform(dependency.targetPlatform)}';

TargetPlatform get targetPlatform => dependency.targetPlatform;
Expand Down Expand Up @@ -390,7 +390,7 @@ class AndroidAotDeferredComponentsBundle extends Target {
}

@override
String get name => 'android_aot_deferred_components_bundle_${getNameForBuildMode(dependency.buildMode)}_'
String get name => 'android_aot_deferred_components_bundle_${dependency.buildMode.cliName}_'
'${getNameForTargetPlatform(dependency.targetPlatform)}';

TargetPlatform get targetPlatform => dependency.targetPlatform;
Expand Down Expand Up @@ -499,7 +499,7 @@ Depfile copyDeferredComponentSoFiles(
.childDirectory(component.name)
.childDirectory('intermediates')
.childDirectory('flutter')
.childDirectory(buildMode.name)
.childDirectory(buildMode.cliName)
.childDirectory('deferred_libs')
.childDirectory(abi)
.childFile('libapp.so-${unit.id}.part.so');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Future<Depfile> copyAssets(
// If deferred components are disabled, then copy assets to regular location.
final File file = environment.defines[kDeferredComponents] == 'true'
? environment.fileSystem.file(
environment.fileSystem.path.join(componentOutputDir.path, buildMode.name, 'deferred_assets', 'flutter_assets', entry.key))
environment.fileSystem.path.join(componentOutputDir.path, buildMode.cliName, 'deferred_assets', 'flutter_assets', entry.key))
: environment.fileSystem.file(
environment.fileSystem.path.join(outputDirectory.path, entry.key));
outputs.add(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CopyFlutterBundle extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, 'copy_flutter_bundle');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
environment.outputDir.createSync(recursive: true);

// Only copy the prebuilt runtimes and kernel blob in debug mode.
Expand Down Expand Up @@ -167,7 +167,7 @@ class KernelSnapshot extends Target {
if (targetPlatformEnvironment == null) {
throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final String targetFile = environment.defines[kTargetFile] ?? environment.fileSystem.path.join('lib', 'main.dart');
final File packagesFile = environment.projectDir
.childDirectory('.dart_tool')
Expand Down Expand Up @@ -272,7 +272,7 @@ abstract class AotElfBase extends Target {
throw MissingDefineException(kTargetPlatform, 'aot_elf');
}
final List<String> extraGenSnapshotOptions = decodeCommaSeparated(environment.defines, kExtraGenSnapshotOptions);
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
final String? splitDebugInfo = environment.defines[kSplitDebugInfo];
final bool dartObfuscation = environment.defines[kDartObfuscation] == 'true';
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/lib/src/build_system/targets/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class AotAssemblyBase extends Target {
}

final List<String> extraGenSnapshotOptions = decodeCommaSeparated(environment.defines, kExtraGenSnapshotOptions);
final BuildMode buildMode = getBuildModeForName(environmentBuildMode);
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
final TargetPlatform targetPlatform = getTargetPlatformForName(environmentTargetPlatform);
final String? splitDebugInfo = environment.defines[kSplitDebugInfo];
final bool dartObfuscation = environment.defines[kDartObfuscation] == 'true';
Expand Down Expand Up @@ -461,7 +461,7 @@ abstract class IosAssetBundle extends Target {
if (environmentBuildMode == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = getBuildModeForName(environmentBuildMode);
final BuildMode buildMode = BuildMode.fromCliName(environmentBuildMode);
final Directory frameworkDirectory = environment.outputDir.childDirectory('App.framework');
final String frameworkBinaryPath = frameworkDirectory.childFile('App').path;
final Directory assetDirectory = frameworkDirectory.childDirectory('flutter_assets');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UnpackLinux extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, name);
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final String engineSourcePath = environment.artifacts
.getArtifactPath(
Artifact.linuxDesktopPath,
Expand Down Expand Up @@ -125,7 +125,7 @@ abstract class BundleLinuxAssets extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, 'bundle_linux_assets');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final Directory outputDirectory = environment.outputDir
.childDirectory('flutter_assets');
if (!outputDirectory.existsSync()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class UnpackMacOS extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, 'unpack_macos');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final String basePath = environment.artifacts.getArtifactPath(Artifact.flutterMacOSFramework, mode: buildMode);

final ProcessResult result = environment.processManager.runSync(<String>[
Expand Down Expand Up @@ -245,7 +245,7 @@ class CompileMacOSFramework extends Target {
if (targetPlatformEnvironment == null) {
throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
if (buildMode == BuildMode.debug) {
throw Exception('precompiled macOS framework only supported in release/profile builds.');
}
Expand Down Expand Up @@ -372,7 +372,7 @@ abstract class MacOSBundleFlutterAssets extends Target {
if (buildModeEnvironment == null) {
throw MissingDefineException(kBuildMode, 'compile_macos_framework');
}
final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
final BuildMode buildMode = BuildMode.fromCliName(buildModeEnvironment);
final Directory frameworkRootDirectory = environment
.outputDir
.childDirectory('App.framework');
Expand Down