Unverified Commit 9a68b0df authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Warn when build number and version can't be parsed on iOS (#40611)

parent b4324aed
...@@ -152,6 +152,9 @@ String validatedBuildNumberForPlatform(TargetPlatform targetPlatform, String bui ...@@ -152,6 +152,9 @@ String validatedBuildNumberForPlatform(TargetPlatform targetPlatform, String bui
// See CFBundleVersion at https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html // See CFBundleVersion at https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
final RegExp disallowed = RegExp(r'[^\d\.]'); final RegExp disallowed = RegExp(r'[^\d\.]');
String tmpBuildNumber = buildNumber.replaceAll(disallowed, ''); String tmpBuildNumber = buildNumber.replaceAll(disallowed, '');
if (tmpBuildNumber.isEmpty) {
return null;
}
final List<String> segments = tmpBuildNumber final List<String> segments = tmpBuildNumber
.split('.') .split('.')
.where((String segment) => segment.isNotEmpty) .where((String segment) => segment.isNotEmpty)
...@@ -196,6 +199,9 @@ String validatedBuildNameForPlatform(TargetPlatform targetPlatform, String build ...@@ -196,6 +199,9 @@ String validatedBuildNameForPlatform(TargetPlatform targetPlatform, String build
// See CFBundleShortVersionString at https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html // See CFBundleShortVersionString at https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
final RegExp disallowed = RegExp(r'[^\d\.]'); final RegExp disallowed = RegExp(r'[^\d\.]');
String tmpBuildName = buildName.replaceAll(disallowed, ''); String tmpBuildName = buildName.replaceAll(disallowed, '');
if (tmpBuildName.isEmpty) {
return null;
}
final List<String> segments = tmpBuildName final List<String> segments = tmpBuildName
.split('.') .split('.')
.where((String segment) => segment.isNotEmpty) .where((String segment) => segment.isNotEmpty)
......
...@@ -159,20 +159,52 @@ List<String> _xcodeBuildSettingsLines({ ...@@ -159,20 +159,52 @@ List<String> _xcodeBuildSettingsLines({
} }
final String buildNameToParse = buildInfo?.buildName ?? project.manifest.buildName; final String buildNameToParse = buildInfo?.buildName ?? project.manifest.buildName;
final String buildName = validatedBuildNameForPlatform(TargetPlatform.ios, buildNameToParse); final bool buildNameIsMissing = buildNameToParse == null || buildNameToParse.isEmpty;
if (buildName != null) { String buildName;
xcodeBuildSettings.add('FLUTTER_BUILD_NAME=$buildName'); const String defaultBuildName = '1.0.0';
if (buildNameIsMissing) {
buildName = defaultBuildName;
} else {
buildName = validatedBuildNameForPlatform(TargetPlatform.ios, buildNameToParse);
} }
String buildNumber = validatedBuildNumberForPlatform(TargetPlatform.ios, buildInfo?.buildNumber ?? project.manifest.buildNumber); final String buildNumberToParse = buildInfo?.buildNumber ?? project.manifest.buildNumber;
// Drop back to parsing build name if build number is not present. Build number is optional in the manifest, but final bool buildNumberIsMissing =
// FLUTTER_BUILD_NUMBER is required as the backing value for the required CFBundleVersion. (buildNumberToParse == null || buildNumberToParse.isEmpty) && buildNameIsMissing;
buildNumber ??= validatedBuildNumberForPlatform(TargetPlatform.ios, buildNameToParse); String buildNumber;
const String defaultBuildNumber = '1';
if (buildNumberIsMissing) {
buildNumber = defaultBuildNumber;
} else {
buildNumber = validatedBuildNumberForPlatform(TargetPlatform.ios, buildNumberToParse);
// Drop back to parsing build name if build number is not present. Build number is optional in the manifest, but
// FLUTTER_BUILD_NUMBER is required as the backing value for the required CFBundleVersion.
buildNumber ??= validatedBuildNumberForPlatform(TargetPlatform.ios, buildNameToParse);
}
if (buildNameIsMissing) {
printError('Warning: Missing build name (CFBundleShortVersionString), defaulting to $defaultBuildName.');
}
if (buildNumberIsMissing) {
printError('Warning: Missing build number (CFBundleVersion), defaulting to $defaultBuildNumber.');
}
if (buildNameIsMissing || buildNumberIsMissing) {
printError('Action Required: You must set a build name and number in the pubspec.yaml '
'file version field before submitting to the App Store.');
}
if (buildNumber != null) { if (buildName == null && buildNumber == null) {
xcodeBuildSettings.add('FLUTTER_BUILD_NUMBER=$buildNumber'); throwToolExit('Cannot parse build number $buildNumberToParse or build name $buildNameToParse, check pubspec.yaml version.');
} else if (buildName == null) {
throwToolExit('Cannot parse build name $buildNameToParse, check pubspec.yaml version.');
} else if (buildNumber == null) {
throwToolExit('Cannot parse build number $buildNumberToParse, check pubspec.yaml version.');
} }
xcodeBuildSettings.add('FLUTTER_BUILD_NAME=$buildName');
xcodeBuildSettings.add('FLUTTER_BUILD_NUMBER=$buildNumber');
if (artifacts is LocalEngineArtifacts) { if (artifacts is LocalEngineArtifacts) {
final LocalEngineArtifacts localEngineArtifacts = artifacts; final LocalEngineArtifacts localEngineArtifacts = artifacts;
final String engineOutPath = localEngineArtifacts.engineOutPath; final String engineOutPath = localEngineArtifacts.engineOutPath;
......
...@@ -16,7 +16,7 @@ void main() { ...@@ -16,7 +16,7 @@ void main() {
testUsingContext('CFBundleVersion for iOS', () async { testUsingContext('CFBundleVersion for iOS', () async {
String buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, 'xyz'); String buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, 'xyz');
expect(buildName, '0'); expect(buildName, isNull);
buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '0.0.1'); buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '0.0.1');
expect(buildName, '0.0.1'); expect(buildName, '0.0.1');
buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '123.xyz'); buildName = validatedBuildNumberForPlatform(TargetPlatform.ios, '123.xyz');
...@@ -38,7 +38,7 @@ void main() { ...@@ -38,7 +38,7 @@ void main() {
testUsingContext('CFBundleShortVersionString for iOS', () async { testUsingContext('CFBundleShortVersionString for iOS', () async {
String buildName = validatedBuildNameForPlatform(TargetPlatform.ios, 'xyz'); String buildName = validatedBuildNameForPlatform(TargetPlatform.ios, 'xyz');
expect(buildName, '0.0.0'); expect(buildName, isNull);
buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '0.0.1'); buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '0.0.1');
expect(buildName, '0.0.1'); expect(buildName, '0.0.1');
buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '123.456.xyz'); buildName = validatedBuildNameForPlatform(TargetPlatform.ios, '123.456.xyz');
......
...@@ -629,6 +629,41 @@ flutter: ...@@ -629,6 +629,41 @@ flutter:
expectedBuildNumber: '3', expectedBuildNumber: '3',
); );
}); });
testUsingOsxContext('default build name and number when version is missing', () async {
const String manifest = '''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
''';
const BuildInfo buildInfo = BuildInfo(BuildMode.release, null);
await checkBuildVersion(
manifestString: manifest,
buildInfo: buildInfo,
expectedBuildName: '1.0.0',
expectedBuildNumber: '1',
);
});
testUsingOsxContext('fail when build name cannot be parsed', () async {
const String manifest = '''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
''';
const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: 'abc', buildNumber: '1');
const String stderr = 'Cannot parse build name abc, check pubspec.yaml version.';
expect(() async => await checkBuildVersion(
manifestString: manifest,
buildInfo: buildInfo
),
throwsToolExit(message: stderr));
});
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment