This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds custom preset - Adds test task - Fix generate task
- Loading branch information
1 parent
47fc340
commit 1fc44ba
Showing
13 changed files
with
289 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
gradle-plugin/src/main/kotlin/com/alecstrong/cocoapods/gradle/plugin/CocoapodsCompileTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.alecstrong.cocoapods.gradle.plugin | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.tasks.TaskAction | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink | ||
import org.jetbrains.kotlin.konan.target.Architecture | ||
import java.io.File | ||
|
||
open class CocoapodsCompileTask : DefaultTask() { | ||
internal lateinit var buildType: NativeBuildType | ||
internal lateinit var compilations: Collection<KotlinNativeLink> | ||
|
||
@TaskAction | ||
fun compileFatFramework() { | ||
compileFatBinary( | ||
binaryPath = project.name, | ||
bundleName = "${project.name}.framework" | ||
) | ||
} | ||
|
||
@TaskAction | ||
fun compileFatDsym() { | ||
compileFatBinary( | ||
binaryPath = "Contents/Resources/DWARF/${project.name}", | ||
bundleName = "${project.name}.framework.dSYM" | ||
) | ||
} | ||
|
||
private fun compileFatBinary( | ||
binaryPath: String, | ||
bundleName: String | ||
) { | ||
val finalContainerPath = "${project.buildDir.path}/$bundleName" | ||
val finalOutputPath = "$finalContainerPath/$binaryPath" | ||
|
||
var deviceParentDir: String? = null | ||
|
||
project.exec { exec -> | ||
File(finalOutputPath).parentFile.mkdirs() | ||
|
||
val args = mutableListOf("-create") | ||
|
||
compilations.forEach { compilation -> | ||
val output = compilation.outputFile.get().parentFile.absolutePath | ||
val target = compilation.binary.target.konanTarget | ||
if (target.architecture == Architecture.ARM64) { | ||
deviceParentDir = output | ||
} | ||
|
||
args.addAll(listOf( | ||
"-arch", target.architecture(), "$output/$bundleName/$binaryPath" | ||
)) | ||
} | ||
|
||
args.addAll(listOf( | ||
"-output", finalOutputPath | ||
)) | ||
|
||
exec.executable = "lipo" | ||
exec.args = args | ||
exec.isIgnoreExitValue = true | ||
} | ||
|
||
if (deviceParentDir == null) { | ||
throw IllegalStateException("You need to have a compilation target for X64") | ||
} | ||
|
||
val initialContainer = "$deviceParentDir/$bundleName" | ||
project.copy { copy -> | ||
copy.from(initialContainer) { from -> | ||
from.exclude(binaryPath) | ||
} | ||
copy.into(finalContainerPath) | ||
} | ||
|
||
// clean plist (only works for frameworks) | ||
val plistPath = "$finalContainerPath/Info.plist" | ||
if (File(plistPath).exists()) { | ||
project.exec { exec -> | ||
exec.executable = "/usr/libexec/PlistBuddy" | ||
exec.args = listOf("-c", "Delete :UIRequiredDeviceCapabilities", plistPath) | ||
exec.isIgnoreExitValue = true | ||
} | ||
|
||
project.exec { exec -> | ||
exec.executable = "/usr/libexec/PlistBuddy" | ||
exec.args = listOf("-c", "Add :CFBundleSupportedPlatforms:1 string iPhoneSimulator", plistPath) | ||
exec.isIgnoreExitValue = true | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...le-plugin/src/main/kotlin/com/alecstrong/cocoapods/gradle/plugin/CocoapodsTargetPreset.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.alecstrong.cocoapods.gradle.plugin | ||
|
||
import org.gradle.api.NamedDomainObjectContainer | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType.DEBUG | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeOutputKind.EXECUTABLE | ||
|
||
class CocoapodsTargetPreset( | ||
private val project: Project | ||
) : KotlinTargetPreset<KotlinNativeTarget> { | ||
override fun createTarget(name: String): KotlinNativeTarget { | ||
val extension = project.extensions.getByType(KotlinMultiplatformExtension::class.java) | ||
val simulator = (extension.targetFromPreset( | ||
extension.presets.getByName("iosX64"), name | ||
) as KotlinNativeTarget).apply { | ||
binaries { | ||
framework() | ||
} | ||
} | ||
|
||
val device = (extension.targetFromPreset( | ||
extension.presets.getByName("iosArm64"), "${name}Device" | ||
) as KotlinNativeTarget).apply { | ||
binaries { | ||
framework { | ||
embedBitcode("disable") | ||
} | ||
} | ||
} | ||
|
||
configureSources(name, device.compilations) | ||
|
||
project.tasks.register("${name}Test", CocoapodsTestTask::class.java) { task -> | ||
task.dependsOn(simulator.compilations.getByName("test").getLinkTask(EXECUTABLE, DEBUG)) | ||
task.group = CocoapodsPlugin.GROUP | ||
task.description = "Run tests for target '$name' on an iOS Simulator" | ||
task.target = simulator | ||
} | ||
|
||
return simulator | ||
} | ||
|
||
override fun getName() = "Cocoapods" | ||
|
||
private fun configureSources(name: String, compilations: NamedDomainObjectContainer<KotlinNativeCompilation>) { | ||
val extension = project.extensions.getByType(KotlinMultiplatformExtension::class.java) | ||
|
||
project.afterEvaluate { | ||
compilations.named("main").configure { compilation -> | ||
extension.sourceSets.findByName("${name}Main")?.let(compilation::source) | ||
} | ||
compilations.named("test").configure { compilation -> | ||
extension.sourceSets.findByName("${name}Test")?.let(compilation::source) | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
gradle-plugin/src/main/kotlin/com/alecstrong/cocoapods/gradle/plugin/CocoapodsTestTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.alecstrong.cocoapods.gradle.plugin | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType.DEBUG | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeOutputKind.EXECUTABLE | ||
|
||
open class CocoapodsTestTask : DefaultTask() { | ||
internal lateinit var target: KotlinNativeTarget | ||
|
||
@Input var device: String = "iPhone 8" | ||
|
||
@TaskAction | ||
fun performTest() { | ||
val binary = target.compilations.getByName("test").getBinary(EXECUTABLE, DEBUG) | ||
project.exec { exec -> | ||
exec.commandLine("xcrun", "simctl", "spawn", device, binary.absolutePath) | ||
} | ||
} | ||
} |
Oops, something went wrong.