diff --git a/packages/flutter_tools/lib/src/android/gradle.dart b/packages/flutter_tools/lib/src/android/gradle.dart index fafe472665..32220f0613 100644 --- a/packages/flutter_tools/lib/src/android/gradle.dart +++ b/packages/flutter_tools/lib/src/android/gradle.dart @@ -69,7 +69,7 @@ Directory getRepoDirectory(Directory buildDirectory) { String _taskFor(String prefix, BuildInfo buildInfo) { final String buildType = camelCase(buildInfo.modeName); final String productFlavor = buildInfo.flavor ?? ''; - return '$prefix${toTitleCase(productFlavor)}${toTitleCase(buildType)}'; + return '$prefix${sentenceCase(productFlavor)}${sentenceCase(buildType)}'; } /// Returns the task to build an APK. @@ -592,7 +592,7 @@ class AndroidGradleBuilder implements AndroidBuilder { command.addAll(androidBuildInfo.buildInfo.toGradleConfig()); if (buildInfo.dartObfuscation && buildInfo.mode != BuildMode.release) { _logger.printStatus( - 'Dart obfuscation is not supported in ${toTitleCase(buildInfo.friendlyModeName)}' + 'Dart obfuscation is not supported in ${sentenceCase(buildInfo.friendlyModeName)}' ' mode, building as un-obfuscated.', ); } diff --git a/packages/flutter_tools/lib/src/base/utils.dart b/packages/flutter_tools/lib/src/base/utils.dart index 6e5d8d0f94..5883049232 100644 --- a/packages/flutter_tools/lib/src/base/utils.dart +++ b/packages/flutter_tools/lib/src/base/utils.dart @@ -34,11 +34,20 @@ String snakeCase(String str, [ String sep = '_' ]) { (Match m) => '${m.start == 0 ? '' : sep}${m[0]!.toLowerCase()}'); } -String toTitleCase(String str) { +/// Converts `fooBar` to `FooBar`. +/// +/// This uses [toBeginningOfSentenceCase](https://pub.dev/documentation/intl/latest/intl/toBeginningOfSentenceCase.html), +/// with the input and return value of non-nullable. +String sentenceCase(String str, [String? locale]) { if (str.isEmpty) { return str; } - return str.substring(0, 1).toUpperCase() + str.substring(1); + return toBeginningOfSentenceCase(str, locale)!; +} + +/// Converts `foo_bar` to `Foo Bar`. +String snakeCaseToTitleCase(String snakeCaseString) { + return snakeCaseString.split('_').map(camelCase).map(sentenceCase).join(' '); } /// Return the plural of the given word (`cat(s)`). diff --git a/packages/flutter_tools/lib/src/commands/build_ios.dart b/packages/flutter_tools/lib/src/commands/build_ios.dart index eeeee80ab8..8402eb17d3 100644 --- a/packages/flutter_tools/lib/src/commands/build_ios.dart +++ b/packages/flutter_tools/lib/src/commands/build_ios.dart @@ -236,7 +236,7 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand { throwToolExit('Building for iOS is only supported on macOS.'); } if (environmentType == EnvironmentType.simulator && !buildInfo.supportsSimulator) { - throwToolExit('${toTitleCase(buildInfo.friendlyModeName)} mode is not supported for simulators.'); + throwToolExit('${sentenceCase(buildInfo.friendlyModeName)} mode is not supported for simulators.'); } if (configOnly && buildInfo.codeSizeDirectory != null) { throwToolExit('Cannot analyze code size without performing a full build.'); diff --git a/packages/flutter_tools/lib/src/commands/build_ios_framework.dart b/packages/flutter_tools/lib/src/commands/build_ios_framework.dart index 2855277b1d..c6c1c02f8c 100644 --- a/packages/flutter_tools/lib/src/commands/build_ios_framework.dart +++ b/packages/flutter_tools/lib/src/commands/build_ios_framework.dart @@ -179,7 +179,7 @@ class BuildIOSFrameworkCommand extends BuildSubCommand { for (final BuildInfo buildInfo in buildInfos) { final String productBundleIdentifier = await _project.ios.productBundleIdentifier(buildInfo); globals.printStatus('Building frameworks for $productBundleIdentifier in ${getNameForBuildMode(buildInfo.mode)} mode...'); - final String xcodeBuildConfiguration = toTitleCase(getNameForBuildMode(buildInfo.mode)); + final String xcodeBuildConfiguration = sentenceCase(getNameForBuildMode(buildInfo.mode)); final Directory modeDirectory = outputDirectory.childDirectory(xcodeBuildConfiguration); if (modeDirectory.existsSync()) { @@ -446,7 +446,7 @@ end } // Always build debug for simulator. - final String simulatorConfiguration = toTitleCase(getNameForBuildMode(BuildMode.debug)); + final String simulatorConfiguration = sentenceCase(getNameForBuildMode(BuildMode.debug)); pluginsBuildCommand = [ ...globals.xcode.xcrunCommand(), 'xcodebuild', diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 2766feb04a..86650c967b 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -10,6 +10,7 @@ import '../base/context.dart'; import '../base/file_system.dart'; import '../base/net.dart'; import '../base/terminal.dart'; +import '../base/utils.dart'; import '../convert.dart'; import '../dart/pub.dart'; import '../features.dart'; @@ -247,9 +248,13 @@ class CreateCommand extends CreateBase { ); } + // The dart project_name is in snake_case, this variable is the Title Case of the Project Name. + final String titleCaseProjectName = snakeCaseToTitleCase(projectName); + final Map templateContext = createTemplateContext( organization: organization, projectName: projectName, + titleCaseProjectName: titleCaseProjectName, projectDescription: stringArg('description'), flutterRoot: flutterRoot, withPluginHook: generatePlugin, diff --git a/packages/flutter_tools/lib/src/commands/create_base.dart b/packages/flutter_tools/lib/src/commands/create_base.dart index 599aa9b56e..89fb128bfa 100644 --- a/packages/flutter_tools/lib/src/commands/create_base.dart +++ b/packages/flutter_tools/lib/src/commands/create_base.dart @@ -317,6 +317,7 @@ abstract class CreateBase extends FlutterCommand { throwToolExit(error); } } + assert(projectName != null); return projectName; } @@ -325,6 +326,7 @@ abstract class CreateBase extends FlutterCommand { Map createTemplateContext({ String organization, String projectName, + String titleCaseProjectName, String projectDescription, String androidLanguage, String iosDevelopmentTeam, @@ -361,6 +363,7 @@ abstract class CreateBase extends FlutterCommand { return { 'organization': organization, 'projectName': projectName, + 'titleCaseProjectName': titleCaseProjectName, 'androidIdentifier': androidIdentifier, 'iosIdentifier': appleIdentifier, 'macosIdentifier': appleIdentifier, diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index c9c320a4a7..8e636d33ad 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -462,7 +462,7 @@ class AppDomain extends Domain { }) async { if (!await device.supportsRuntimeMode(options.buildInfo.mode)) { throw Exception( - '${toTitleCase(options.buildInfo.friendlyModeName)} ' + '${sentenceCase(options.buildInfo.friendlyModeName)} ' 'mode is not supported for ${device.name}.', ); } diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 464c80f61b..48d5dd803f 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -587,7 +587,7 @@ class RunCommand extends RunCommandBase { for (final Device device in devices) { if (!await device.supportsRuntimeMode(buildMode)) { throwToolExit( - '${toTitleCase(getFriendlyModeName(buildMode))} ' + '${sentenceCase(getFriendlyModeName(buildMode))} ' 'mode is not supported by ${device.name}.', ); } diff --git a/packages/flutter_tools/lib/src/ios/xcodeproj.dart b/packages/flutter_tools/lib/src/ios/xcodeproj.dart index b890699918..8afba4c76b 100644 --- a/packages/flutter_tools/lib/src/ios/xcodeproj.dart +++ b/packages/flutter_tools/lib/src/ios/xcodeproj.dart @@ -435,7 +435,7 @@ class XcodeProjectInfo { /// The expected scheme for [buildInfo]. @visibleForTesting static String expectedSchemeFor(BuildInfo? buildInfo) { - return toTitleCase(buildInfo?.flavor ?? 'runner'); + return sentenceCase(buildInfo?.flavor ?? 'runner'); } /// The expected build configuration for [buildInfo] and [scheme]. diff --git a/packages/flutter_tools/lib/src/linux/build_linux.dart b/packages/flutter_tools/lib/src/linux/build_linux.dart index 2c0e1c7bb2..c5545e32ff 100644 --- a/packages/flutter_tools/lib/src/linux/build_linux.dart +++ b/packages/flutter_tools/lib/src/linux/build_linux.dart @@ -119,7 +119,7 @@ Future _runCmake(String buildModeName, Directory sourceDir, Directory buil await buildDir.create(recursive: true); - final String buildFlag = toTitleCase(buildModeName); + final String buildFlag = sentenceCase(buildModeName); final bool needCrossBuildOptionsForArm64 = needCrossBuild && targetPlatform == TargetPlatform.linux_arm64; int result; diff --git a/packages/flutter_tools/lib/src/macos/application_package.dart b/packages/flutter_tools/lib/src/macos/application_package.dart index c782ab9632..b528d7273a 100644 --- a/packages/flutter_tools/lib/src/macos/application_package.dart +++ b/packages/flutter_tools/lib/src/macos/application_package.dart @@ -154,7 +154,7 @@ class BuildableMacOSApp extends MacOSApp { getMacOSBuildDirectory(), 'Build', 'Products', - toTitleCase(getNameForBuildMode(buildMode)), + sentenceCase(getNameForBuildMode(buildMode)), appBundleNameFile.readAsStringSync().trim()); } diff --git a/packages/flutter_tools/lib/src/windows/application_package.dart b/packages/flutter_tools/lib/src/windows/application_package.dart index fee954b30e..a1f74ad99e 100644 --- a/packages/flutter_tools/lib/src/windows/application_package.dart +++ b/packages/flutter_tools/lib/src/windows/application_package.dart @@ -63,7 +63,7 @@ class BuildableWindowsApp extends WindowsApp { return globals.fs.path.join( getWindowsBuildDirectory(), 'runner', - toTitleCase(getNameForBuildMode(buildMode)), + sentenceCase(getNameForBuildMode(buildMode)), '$binaryName.exe', ); } diff --git a/packages/flutter_tools/lib/src/windows/build_windows.dart b/packages/flutter_tools/lib/src/windows/build_windows.dart index f1be5b3193..fefa23fada 100644 --- a/packages/flutter_tools/lib/src/windows/build_windows.dart +++ b/packages/flutter_tools/lib/src/windows/build_windows.dart @@ -279,7 +279,7 @@ Future _runBuild( '--build', buildDir.path, '--config', - toTitleCase(buildModeName), + sentenceCase(buildModeName), if (install) ...['--target', 'INSTALL'], if (globals.logger.isVerbose) diff --git a/packages/flutter_tools/lib/src/windows/windows_device.dart b/packages/flutter_tools/lib/src/windows/windows_device.dart index c2e2d4e71c..1b50490336 100644 --- a/packages/flutter_tools/lib/src/windows/windows_device.dart +++ b/packages/flutter_tools/lib/src/windows/windows_device.dart @@ -194,7 +194,7 @@ class WindowsUWPDevice extends Device { } final String binaryDir = _fileSystem.path.absolute( _fileSystem.path.join('build', 'winuwp', 'runner_uwp', 'AppPackages', binaryName)); - final String config = toTitleCase(getNameForBuildMode(_buildMode ?? BuildMode.debug)); + final String config = sentenceCase(getNameForBuildMode(_buildMode ?? BuildMode.debug)); // If a multi-architecture package exists, install that; otherwise install // the single-architecture package. diff --git a/packages/flutter_tools/lib/src/xcode_project.dart b/packages/flutter_tools/lib/src/xcode_project.dart index fa0222a6cb..1657a7260a 100644 --- a/packages/flutter_tools/lib/src/xcode_project.dart +++ b/packages/flutter_tools/lib/src/xcode_project.dart @@ -485,11 +485,17 @@ class IosProject extends XcodeBasedProject { terminal: globals.terminal, ); + final String projectName = parent.manifest.appName; + + // The dart project_name is in snake_case, this variable is the Title Case of the Project Name. + final String titleCaseProjectName = snakeCaseToTitleCase(projectName); + template.render( target, { 'ios': true, - 'projectName': parent.manifest.appName, + 'projectName': projectName, + 'titleCaseProjectName': titleCaseProjectName, 'iosIdentifier': iosBundleIdentifier, 'hasIosDevelopmentTeam': iosDevelopmentTeam != null && iosDevelopmentTeam.isNotEmpty, 'iosDevelopmentTeam': iosDevelopmentTeam ?? '', diff --git a/packages/flutter_tools/templates/app_shared/ios.tmpl/Runner/Info.plist.tmpl b/packages/flutter_tools/templates/app_shared/ios.tmpl/Runner/Info.plist.tmpl index d9bd8c7b00..4d224546da 100644 --- a/packages/flutter_tools/templates/app_shared/ios.tmpl/Runner/Info.plist.tmpl +++ b/packages/flutter_tools/templates/app_shared/ios.tmpl/Runner/Info.plist.tmpl @@ -4,6 +4,8 @@ CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + {{titleCaseProjectName}} CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier diff --git a/packages/flutter_tools/templates/module/ios/host_app_ephemeral/Runner.tmpl/Info.plist.tmpl b/packages/flutter_tools/templates/module/ios/host_app_ephemeral/Runner.tmpl/Info.plist.tmpl index d9bd8c7b00..23f020ccce 100644 --- a/packages/flutter_tools/templates/module/ios/host_app_ephemeral/Runner.tmpl/Info.plist.tmpl +++ b/packages/flutter_tools/templates/module/ios/host_app_ephemeral/Runner.tmpl/Info.plist.tmpl @@ -12,6 +12,8 @@ 6.0 CFBundleName {{projectName}} + CFBundleDisplayName + {{titleCaseProjectName}} CFBundlePackageType APPL CFBundleShortVersionString diff --git a/packages/flutter_tools/test/commands.shard/hermetic/build_linux_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/build_linux_test.dart index a3d18ba31c..52b93f5748 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/build_linux_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/build_linux_test.dart @@ -78,7 +78,7 @@ void main() { 'cmake', '-G', 'Ninja', - '-DCMAKE_BUILD_TYPE=${toTitleCase(buildMode)}', + '-DCMAKE_BUILD_TYPE=${sentenceCase(buildMode)}', '-DFLUTTER_TARGET_PLATFORM=linux-$target', '/linux', ], diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index 848486096f..f68c7d2fff 100755 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -1363,6 +1363,114 @@ void main() { ProcessManager: () => fakeProcessManager, }); + testUsingContext('display name is Title Case for objc iOS project.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=app', '--no-pub', '--org', 'com.foo.bar','--ios-language=objc', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }); + + testUsingContext('display name is Title Case for swift iOS project.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=app', '--no-pub', '--org', 'com.foo.bar','--ios-language=swift', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }); + + testUsingContext('display name is Title Case for objc iOS module.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=module', '--offline', '--org', 'com.foo.bar','--ios-language=objc', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('.ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }, overrides: { + Pub: () => Pub( + fileSystem: globals.fs, + logger: globals.logger, + processManager: globals.processManager, + usage: globals.flutterUsage, + botDetector: globals.botDetector, + platform: globals.platform, + ), + }); + + testUsingContext('display name is Title Case for swift iOS module.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=module', '--offline', '--org', 'com.foo.bar','--ios-language=swift', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('.ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }, overrides: { + Pub: () => Pub( + fileSystem: globals.fs, + logger: globals.logger, + processManager: globals.processManager, + usage: globals.flutterUsage, + botDetector: globals.botDetector, + platform: globals.platform, + ), + }); + + testUsingContext('display name is Title Case for swift iOS plugin.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=plugin', '--no-pub', '--org', 'com.foo.bar', '--platforms=ios', '--ios-language=swift', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('example', 'ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }); + + testUsingContext('display name is Title Case for objc iOS plugin.', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--template=plugin', '--no-pub', '--org', 'com.foo.bar', '--platforms=ios', '--ios-language=objc', '--project-name=my_project', projectDir.path]); + + final String plistPath = globals.fs.path.join('example', 'ios', 'Runner', 'Info.plist'); + final File plistFile = globals.fs.file(globals.fs.path.join(projectDir.path, plistPath)); + expect(plistFile, exists); + final String displayName = _getStringValueFromPlist(plistFile: plistFile, key: 'CFBundleDisplayName'); + expect(displayName, 'My Project'); + }); + testUsingContext('has correct content and formatting with macOS app template', () async { Cache.flutterRoot = '../..'; @@ -2858,3 +2966,10 @@ class LoggingProcessManager extends LocalProcessManager { commands.clear(); } } + +String _getStringValueFromPlist({File plistFile, String key}) { + final List plist = plistFile.readAsLinesSync().map((String line) => line.trim()).toList(); + final int keyIndex = plist.indexOf('$key'); + assert(keyIndex > 0); + return plist[keyIndex+1].replaceAll('', '').replaceAll('', ''); +} diff --git a/packages/flutter_tools/test/general.shard/plugins_test.dart b/packages/flutter_tools/test/general.shard/plugins_test.dart index c710e6dbc0..f6ee05aef1 100644 --- a/packages/flutter_tools/test/general.shard/plugins_test.dart +++ b/packages/flutter_tools/test/general.shard/plugins_test.dart @@ -170,7 +170,7 @@ void main() { mode: FileMode.writeOnlyAppend); pluginDirectory.childFile('pubspec.yaml') ..createSync(recursive: true) - ..writeAsStringSync(pluginYamlTemplate.replaceAll('PLUGIN_CLASS', toTitleCase(camelCase(name)))); + ..writeAsStringSync(pluginYamlTemplate.replaceAll('PLUGIN_CLASS', sentenceCase(camelCase(name)))); directories.add(pluginDirectory); } return directories; diff --git a/packages/flutter_tools/test/general.shard/utils_test.dart b/packages/flutter_tools/test/general.shard/utils_test.dart index a80f2b8ac6..0b78963995 100644 --- a/packages/flutter_tools/test/general.shard/utils_test.dart +++ b/packages/flutter_tools/test/general.shard/utils_test.dart @@ -69,6 +69,28 @@ baz=qux expect(snakeCase('ABc'), equals('a_bc')); expect(snakeCase('ABC'), equals('a_b_c')); }); + + testWithoutContext('sentenceCase', () async { + expect(sentenceCase('abc'), equals('Abc')); + expect(sentenceCase('ab_c'), equals('Ab_c')); + expect(sentenceCase('a b c'), equals('A b c')); + expect(sentenceCase('a B c'), equals('A B c')); + expect(sentenceCase('Abc'), equals('Abc')); + expect(sentenceCase('ab_c'), equals('Ab_c')); + expect(sentenceCase('a_bc'), equals('A_bc')); + expect(sentenceCase('a_b_c'), equals('A_b_c')); + }); + + testWithoutContext('snakeCaseToTitleCase', () async { + expect(snakeCaseToTitleCase('abc'), equals('Abc')); + expect(snakeCaseToTitleCase('ab_c'), equals('Ab C')); + expect(snakeCaseToTitleCase('a_b_c'), equals('A B C')); + expect(snakeCaseToTitleCase('a_B_c'), equals('A B C')); + expect(snakeCaseToTitleCase('Abc'), equals('Abc')); + expect(snakeCaseToTitleCase('ab_c'), equals('Ab C')); + expect(snakeCaseToTitleCase('a_bc'), equals('A Bc')); + expect(snakeCaseToTitleCase('a_b_c'), equals('A B C')); + }); }); group('text wrapping', () {