Skip to content

Commit

Permalink
Cocoapods Gradle configuration cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kpgalligan committed Dec 4, 2024
1 parent b30233e commit 45f5cc9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SONATYPE_HOST=DEFAULT
RELEASE_SIGNING_ENABLED=true

GROUP=co.touchlab.kmmbridge
VERSION_NAME=1.1.0-a3
VERSION_NAME=1.1.0-a4
VERSION_NAME_3x=0.3.7

POM_URL=https://github.com/touchlab/KMMBridge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ internal class MavenPublishArtifactManager(
// There may be more than one repo, but it's also possible we get none. This will allow us to continue and trying
// to use the dependency should fail.
// If there are multiple repos, the repo name needs to be specified.
val mavenArtifactRepositoryUrl = if (!isMavenCentral) {
return if (!isMavenCentral) {
findArtifactRepository(publishingExtension).url.toString()
} else {
"https://repo.maven.apache.org/maven2/"
"https://repo.maven.apache.org/maven2"
}
return mavenArtifactRepositoryUrl
}

private fun publishingTasks(project: Project): List<TaskProvider<Task>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.gradle.api.Task
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.cocoapods.CocoapodsExtension
import org.jetbrains.kotlin.gradle.plugin.cocoapods.CocoapodsExtension.PodspecPlatformSettings
import org.jetbrains.kotlin.gradle.plugin.cocoapods.KotlinCocoapodsPlugin
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
Expand All @@ -36,6 +36,24 @@ internal sealed class SpecRepo {
class Private(val url: String) : SpecRepo()
}

/**
* State storage to help avoid Gradle configuration cache issues
*/
internal data class SafeCocoapodsData(
val ios: PodspecPlatformSettings,
val osx: PodspecPlatformSettings,
val tvos: PodspecPlatformSettings,
val watchos: PodspecPlatformSettings,
val extraSpecAttributes: MutableMap<String, String>,
val version: String?,
val name: String,
val homepage: String?,
val license: String?,
val authors: String?,
val summary: String?,
val podsDependencies: String
)

internal class CocoapodsDependencyManager(
private val specRepoDeferred: () -> SpecRepo,
private val allowWarnings: Boolean,
Expand All @@ -58,14 +76,33 @@ internal class CocoapodsDependencyManager(
dependsOn(uploadTask)

val cocoapodsExtension = project.kotlin.cocoapods

val safeCocoapodsData = SafeCocoapodsData(
cocoapodsExtension.ios,
cocoapodsExtension.osx,
cocoapodsExtension.tvos,
cocoapodsExtension.watchos,
cocoapodsExtension.extraSpecAttributes,
cocoapodsExtension.version,
cocoapodsExtension.name,
cocoapodsExtension.homepage,
cocoapodsExtension.license,
cocoapodsExtension.authors,
cocoapodsExtension.summary,
cocoapodsExtension.pods.joinToString(separator = "\n") { pod ->
val versionSuffix = if (pod.version != null) ", '${pod.version}'" else ""
"| spec.dependency '${pod.name}'$versionSuffix"
}
)

val urlFileLocal = project.urlFile
val frameworkName = findFrameworkName(project)

@Suppress("ObjectLiteralToLambda")
doLast(object : Action<Task> {
override fun execute(t: Task) {
generatePodspec(
cocoapodsExtension, urlFileLocal, version, podSpecFile, frameworkName
safeCocoapodsData, urlFileLocal, version, podSpecFile, frameworkName
)
}
})
Expand All @@ -77,26 +114,31 @@ internal class CocoapodsDependencyManager(
dependsOn(generatePodspecTask)
outputs.upToDateWhen { false } // We want to always upload when this task is called

val allowWarningsLocal = allowWarnings
val verboseErrorsLocal = verboseErrors
val specRepo = specRepoDeferred()

@Suppress("ObjectLiteralToLambda")
doLast(object : Action<Task> {
override fun execute(t: Task) {
val extras = mutableListOf<String>()

if (allowWarnings) {
if (allowWarningsLocal) {
extras.add("--allow-warnings")
}

if (verboseErrors) {
if (verboseErrorsLocal) {
extras.add("--verbose")
}

when (val specRepo = specRepoDeferred()) {
when (specRepo) {
is SpecRepo.Trunk -> {
providers.exec {
val execOutput = providers.exec {
commandLine(
"pod", "trunk", "push", podSpecFile, *extras.toTypedArray()
)
}.standardOutput.asText.get()
}
t.logger.info(execOutput.standardOutput.asText.get())
}

is SpecRepo.Private -> {
Expand All @@ -110,12 +152,7 @@ internal class CocoapodsDependencyManager(
*extras.toTypedArray()
)
}
logger.info(execOutput.standardOutput.asText.get())
val errorOut = execOutput.standardError.asText.get()
val anyError = errorOut.lines().filter { it.isNotBlank() }.isNotEmpty()
if(anyError){
logger.error(errorOut)
}
t.logger.info(execOutput.standardOutput.asText.get())
}
}
}
Expand All @@ -130,7 +167,7 @@ internal class CocoapodsDependencyManager(
override val needsGitTags: Boolean = false
}

private fun findFrameworkName(project: Project): org.gradle.api.provider.Provider<String> {
private fun findFrameworkName(project: Project): Provider<String> {
val anyPodFramework = project.provider {
val anyTarget = project.kotlin.targets
.withType(KotlinNativeTarget::class.java)
Expand All @@ -150,22 +187,19 @@ private fun findFrameworkName(project: Project): org.gradle.api.provider.Provide
// TODO it might be nice to migrate this back to using the kotlin.cocoapods podspec task directly, but not worth the
// effort to wire it up right now.
private fun generatePodspec(
cocoapodsExtension: CocoapodsExtension,
safeCocoapodsData: SafeCocoapodsData,
urlFile: File,
projectVersion:String,
projectVersion: String,
outputFile: File,
frameworkName: Provider<String>
) = with(cocoapodsExtension) {
) = with(safeCocoapodsData) {
val deploymentTargets = run {
listOf(ios, osx, tvos, watchos).filter { it.deploymentTarget != null }.joinToString("\n") {
if (extraSpecAttributes.containsKey("${it.name}.deployment_target")) "" else "| spec.${it.name}.deployment_target = '${it.deploymentTarget}'"
}
}

val dependencies = pods.joinToString(separator = "\n") { pod ->
val versionSuffix = if (pod.version != null) ", '${pod.version}'" else ""
"| spec.dependency '${pod.name}'$versionSuffix"
}
val dependencies = podsDependencies

val vendoredFramework = "${frameworkName.get()}.xcframework"
val vendoredFrameworks =
Expand Down

0 comments on commit 45f5cc9

Please sign in to comment.