Skip to content

Commit

Permalink
Set iOS version in example/ios/Podfile and ios/plugin_name.podspec to…
Browse files Browse the repository at this point in the history
… 13.0.
  • Loading branch information
buijs-dev committed May 12, 2024
1 parent 4336892 commit 144bdc9
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/src/cli/task_project_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import "dart:io";

import "../common/common.dart";
import "../consumer/consumer.dart";
import "cli.dart";
import "context.dart";

Expand Down Expand Up @@ -151,6 +152,7 @@ class CreateProject extends Task {
.resolveDirectory("ios")
..verifyDirectoryExists;

setIosVersionInPodFile(iosWorkingDirectory);
for (final step in ["install", "update"]) {
_executor
..executable = "pod"
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/task_project_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ extension on _Producer {
void get setupIOS {
final pathToIos = "$pathToRoot/ios";
createIosKlutterFolder(pathToIos);
addFrameworkToPodspec(
addFrameworkAndSetIosVersionInPodspec(
pathToIos: "$pathToRoot/ios",
pluginName: findPluginName(pathToRoot),
);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/common/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions lib/src/consumer/consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
library consumer;

export "android.dart";
export "ios.dart";
63 changes: 63 additions & 0 deletions lib/src/consumer/ios.dart
Original file line number Diff line number Diff line change
@@ -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 = <String>[];

// 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"));
}
}
35 changes: 22 additions & 13 deletions lib/src/producer/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -31,16 +30,16 @@ void createIosKlutterFolder(String pathToIos) => pathToIos.verifyExists
..createKlutterReadmeFile;

/// Edit the root/ios/<plugin-name>.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 {
Expand All @@ -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'.
Expand All @@ -78,24 +77,34 @@ extension on File {
final newLines = <String>[];

// 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.
Expand Down
4 changes: 2 additions & 2 deletions test/src/producer/ios_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
..createSync();

expect(
() => addFrameworkToPodspec(
() => addFrameworkAndSetIosVersionInPodspec(
pluginName: "some_plugin", pathToIos: folder.absolutePath),
throwsA(predicate((e) =>
e is KlutterException &&
Expand All @@ -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 &&
Expand Down
22 changes: 22 additions & 0 deletions test/src/systemtest/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 144bdc9

Please sign in to comment.