From 144bdc9ed0895695290bb98739251365dcddec64 Mon Sep 17 00:00:00 2001 From: Gillian <72627535+buijs-dev@users.noreply.github.com> Date: Sun, 12 May 2024 17:27:41 +0200 Subject: [PATCH] Set iOS version in example/ios/Podfile and ios/plugin_name.podspec to 13.0. --- CHANGELOG.md | 1 + lib/src/cli/task_project_create.dart | 2 + lib/src/cli/task_project_init.dart | 2 +- lib/src/common/config.dart | 3 ++ lib/src/consumer/consumer.dart | 1 + lib/src/consumer/ios.dart | 63 ++++++++++++++++++++++++++++ lib/src/producer/ios.dart | 35 ++++++++++------ test/src/producer/ios_test.dart | 4 +- test/src/systemtest/e2e_test.dart | 22 ++++++++++ 9 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 lib/src/consumer/ios.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index ed10040..602c021 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.0.0 * Use klutter_ui 1.1.0. * Use Klutter Gradle v2024.1.1.beta with support for protobuf. +* Set iOS to 13.0 in Podfile and podspec. * Move post-build tasks in root/platform build.gradle.kts to gradle plugin. * Add kradle script (which replaces producer and consumer). * Add interactive cli mode. diff --git a/lib/src/cli/task_project_create.dart b/lib/src/cli/task_project_create.dart index 93396c8..9913b13 100644 --- a/lib/src/cli/task_project_create.dart +++ b/lib/src/cli/task_project_create.dart @@ -21,6 +21,7 @@ import "dart:io"; import "../common/common.dart"; +import "../consumer/consumer.dart"; import "cli.dart"; import "context.dart"; @@ -151,6 +152,7 @@ class CreateProject extends Task { .resolveDirectory("ios") ..verifyDirectoryExists; + setIosVersionInPodFile(iosWorkingDirectory); for (final step in ["install", "update"]) { _executor ..executable = "pod" diff --git a/lib/src/cli/task_project_init.dart b/lib/src/cli/task_project_init.dart index fdb74e5..b62198c 100644 --- a/lib/src/cli/task_project_init.dart +++ b/lib/src/cli/task_project_init.dart @@ -245,7 +245,7 @@ extension on _Producer { void get setupIOS { final pathToIos = "$pathToRoot/ios"; createIosKlutterFolder(pathToIos); - addFrameworkToPodspec( + addFrameworkAndSetIosVersionInPodspec( pathToIos: "$pathToRoot/ios", pluginName: findPluginName(pathToRoot), ); diff --git a/lib/src/common/config.dart b/lib/src/common/config.dart index 121bc7f..74ef0b7 100644 --- a/lib/src/common/config.dart +++ b/lib/src/common/config.dart @@ -26,6 +26,9 @@ const androidMinSdk = 24; /// The compile SDK version for Android. const androidCompileSdk = 33; +/// The minimum iOS version. +const iosVersion = 13.0; + /// Flutter SDK versions which can be used for a Producer project. const supportedFlutterVersions = { "3.0.5": Version(major: 3, minor: 0, patch: 5), diff --git a/lib/src/consumer/consumer.dart b/lib/src/consumer/consumer.dart index 593f0aa..a3b1c59 100644 --- a/lib/src/consumer/consumer.dart +++ b/lib/src/consumer/consumer.dart @@ -25,3 +25,4 @@ library consumer; export "android.dart"; +export "ios.dart"; diff --git a/lib/src/consumer/ios.dart b/lib/src/consumer/ios.dart new file mode 100644 index 0000000..53ff159 --- /dev/null +++ b/lib/src/consumer/ios.dart @@ -0,0 +1,63 @@ +// Copyright (c) 2021 - 2024 Buijs Software +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import "dart:io"; + +import "../common/common.dart"; + +/// Create the root/ios/Klutter directory and add a readme file. +/// +/// {@category consumer} +void setIosVersionInPodFile(Directory iosDirectory) => + iosDirectory.resolveFile("Podfile") + ..verifyFileExists + ..setIosVersion; + +extension on File { + void get setIosVersion { + // INPUT + final lines = readAsLinesSync(); + + // OUTPUT + final newLines = []; + + // Used to check if adding framework is done. + var hasExplicitPlatformVersion = false; + + for (final line in lines) { + final trimmed = line.replaceAll(" ", ""); + // Check if line sets ios platform version + // and if so then update the version. + if (trimmed.contains("platform:ios,")) { + newLines.add("platform :ios, '$iosVersion'"); + hasExplicitPlatformVersion = true; + } else { + newLines.add(line); + } + } + + if (!hasExplicitPlatformVersion) { + throw const KlutterException("Failed to set ios version in Podfile."); + } + + // Write the editted line to the podspec file. + writeAsStringSync(newLines.join("\n")); + } +} diff --git a/lib/src/producer/ios.dart b/lib/src/producer/ios.dart index e000afd..a90e607 100644 --- a/lib/src/producer/ios.dart +++ b/lib/src/producer/ios.dart @@ -20,8 +20,7 @@ import "dart:io"; -import "../common/exception.dart"; -import "../common/utilities.dart"; +import "../common/common.dart"; /// Create the root/ios/Klutter directory and add a readme file. /// @@ -31,16 +30,16 @@ void createIosKlutterFolder(String pathToIos) => pathToIos.verifyExists ..createKlutterReadmeFile; /// Edit the root/ios/.podspec file to depend on the -/// fat-framework build by the platform module. +/// xcframework build by the platform module. /// /// The generated framework will be copied to the root/ios/Klutter folder. /// /// {@category producer} -void addFrameworkToPodspec({ +void addFrameworkAndSetIosVersionInPodspec({ required String pathToIos, required String pluginName, }) => - pathToIos.verifyExists.toPodspec(pluginName).addFramework; + pathToIos.verifyExists.toPodspec(pluginName).addFrameworkAndSetIosVersion; extension on String { void get createKlutterFolder { @@ -59,7 +58,7 @@ extension on String { } extension on File { - void get addFramework { + void get addFrameworkAndSetIosVersion { final regex = RegExp("Pod::Spec.new.+?do.+?.([^|]+?)."); /// Check the prefix used in the podspec or default to 's'. @@ -78,24 +77,34 @@ extension on File { final newLines = []; // Used to check if adding framework is done. - var hasAdded = false; + var hasAddedVendoredFramework = lines.any((line) => line + .contains('ios.vendored_frameworks = "Klutter/Platform.xcframework"')); for (final line in lines) { - newLines.add(line); + final trimmed = line.replaceAll(" ", ""); + // Check if line sets ios platform version and + // if so then update the version. + if (trimmed.contains("s.platform=:ios,")) { + newLines.add(" s.platform = :ios, '$iosVersion'"); + } else { + newLines.add(line); + } // Check if line contains Flutter dependency (which should always be present). // If so then add the vendored framework dependency. // This is done so the line is added at a fixed point in the podspec. if (line.replaceAll(" ", "").contains("$prefix.dependency'Flutter'")) { - newLines.add( - """ $prefix.ios.vendored_frameworks = "Klutter/Platform.xcframework" """, - ); + if (!hasAddedVendoredFramework) { + newLines.add( + """ $prefix.ios.vendored_frameworks = "Klutter/Platform.xcframework" """, + ); - hasAdded = true; + hasAddedVendoredFramework = true; + } } } - if (!hasAdded) { + if (!hasAddedVendoredFramework) { throw KlutterException( """ |Failed to add Platform.framework to ios folder. diff --git a/test/src/producer/ios_test.dart b/test/src/producer/ios_test.dart index 198f701..0ad3601 100644 --- a/test/src/producer/ios_test.dart +++ b/test/src/producer/ios_test.dart @@ -31,7 +31,7 @@ void main() { ..createSync(); expect( - () => addFrameworkToPodspec( + () => addFrameworkAndSetIosVersionInPodspec( pluginName: "some_plugin", pathToIos: folder.absolutePath), throwsA(predicate((e) => e is KlutterException && @@ -45,7 +45,7 @@ void main() { File("${folder.absolutePath}/some_plugin.podspec").createSync(); expect( - () => addFrameworkToPodspec( + () => addFrameworkAndSetIosVersionInPodspec( pluginName: "some_plugin", pathToIos: folder.absolutePath), throwsA(predicate((e) => e is KlutterException && diff --git a/test/src/systemtest/e2e_test.dart b/test/src/systemtest/e2e_test.dart index 01ff675..9a7ac04 100644 --- a/test/src/systemtest/e2e_test.dart +++ b/test/src/systemtest/e2e_test.dart @@ -97,6 +97,17 @@ void main() { reason: "Plugin should be created in: '${producerPlugin.absolute.path}'"); + /// iOS version should be set to 13.0 in podspec + final producerPodspec = File( + "${producerPlugin.absolutePath}/ios/${pluginName}.podspec".normalize); + expect(producerPodspec.existsSync(), true, + reason: "${producerPodspec.absolutePath} should exist"); + + final producerPodspecContent = producerPodspec.readAsStringSync(); + expect(producerPodspecContent.contains("s.platform = :ios, '13.0'"), true, + reason: + "${producerPodspec.absolutePath} should contain ios version 13"); + /// Gradle files should be copied to root folder. final gradlew = File("${producerPlugin.absolutePath}/gradlew".normalize); expect(gradlew.existsSync(), true, @@ -211,6 +222,17 @@ void main() { expect(registryContainsPlugin, true, reason: "add task should have added plugin name to the .klutter-plugins file: ${registry.readAsStringSync()}"); + + /// iOS version should be set to 13.0 in podspec + final consumerPodfile = + File("${consumerPlugin.absolutePath}/ios/Podfile".normalize); + expect(consumerPodfile.existsSync(), true, + reason: "${consumerPodfile.absolutePath} should exist"); + + final consumerPodfileContent = consumerPodfile.readAsStringSync(); + expect(consumerPodfileContent.contains("platform :ios, '13.0'"), true, + reason: + "${consumerPodfile.absolutePath} should contain ios version 13"); }); } catch (e, s) { print(s);