Skip to content

Commit b9561e4

Browse files
Merge pull request #278 from touchlab/ks/CocoapodsUssValueSource
Use ValueSource to run pod commands for CocoaPods publishing
2 parents 610b732 + e7f0315 commit b9561e4

File tree

1 file changed

+95
-20
lines changed

1 file changed

+95
-20
lines changed

kmmbridge/src/main/kotlin/co/touchlab/kmmbridge/dependencymanager/CocoapodsDependencyManager.kt

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ import org.gradle.api.Project
2424
import org.gradle.api.Task
2525
import org.gradle.api.provider.Provider
2626
import org.gradle.api.provider.ProviderFactory
27+
import org.gradle.api.provider.ValueSource
28+
import org.gradle.api.provider.ValueSourceParameters
2729
import org.gradle.api.tasks.TaskProvider
30+
import org.gradle.kotlin.dsl.of
31+
import org.gradle.process.ExecOperations
32+
import org.gradle.process.ExecSpec
33+
import org.gradle.process.internal.ExecException
2834
import org.jetbrains.kotlin.gradle.plugin.cocoapods.CocoapodsExtension.PodspecPlatformSettings
2935
import org.jetbrains.kotlin.gradle.plugin.cocoapods.KotlinCocoapodsPlugin
3036
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
3137
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
38+
import java.io.ByteArrayOutputStream
3239
import java.io.File
40+
import java.nio.charset.Charset
41+
import javax.inject.Inject
3342

3443
internal sealed class SpecRepo {
3544
object Trunk : SpecRepo()
@@ -54,11 +63,72 @@ internal data class SafeCocoapodsData(
5463
val podsDependencies: String
5564
)
5665

66+
private abstract class PodPushValueSource<T:ValueSourceParameters> : ValueSource<String, T> {
67+
68+
@get:Inject
69+
abstract val execOperations: ExecOperations
70+
71+
abstract fun ExecSpec.command()
72+
73+
override fun obtain(): String {
74+
val output = ByteArrayOutputStream()
75+
76+
val result = execOperations.exec {
77+
command()
78+
standardOutput = output
79+
// Exit Value handled below
80+
isIgnoreExitValue = true
81+
}
82+
val outputString = String(output.toByteArray(), Charset.defaultCharset())
83+
if (result.exitValue != 0) {
84+
// Handling the exception ourselves
85+
throw ExecException(outputString)
86+
}
87+
return outputString
88+
}
89+
}
90+
91+
private abstract class PodPrivatePushValueSource : PodPushValueSource<PodPrivatePushValueSource.Params>() {
92+
93+
interface Params : ValueSourceParameters {
94+
var specUrl: String
95+
var podSpecFile: File
96+
var extras: Array<String>
97+
}
98+
99+
override fun ExecSpec.command() {
100+
commandLine(
101+
"pod",
102+
"repo",
103+
"push",
104+
parameters.specUrl,
105+
parameters.podSpecFile,
106+
*parameters.extras
107+
)
108+
}
109+
}
110+
111+
private abstract class PodTrunkPushValueSource : PodPushValueSource<PodTrunkPushValueSource.Params>() {
112+
113+
interface Params : ValueSourceParameters {
114+
var podSpecFile: File
115+
var extras: Array<String>
116+
}
117+
118+
override fun ExecSpec.command() {
119+
commandLine(
120+
"pod", "trunk", "push", parameters.podSpecFile, *parameters.extras
121+
)
122+
}
123+
}
124+
125+
57126
internal class CocoapodsDependencyManager(
58127
private val specRepoDeferred: () -> SpecRepo,
59128
private val allowWarnings: Boolean,
60129
private val verboseErrors: Boolean
61130
) : DependencyManager {
131+
62132
override fun configure(
63133
providers: ProviderFactory,
64134
project: Project,
@@ -133,26 +203,24 @@ internal class CocoapodsDependencyManager(
133203

134204
when (specRepo) {
135205
is SpecRepo.Trunk -> {
136-
val execOutput = providers.exec {
137-
commandLine(
138-
"pod", "trunk", "push", podSpecFile, *extras.toTypedArray()
139-
)
206+
val podPushProvider = providers.of(PodTrunkPushValueSource::class) {
207+
parameters {
208+
this.podSpecFile = podSpecFile
209+
this.extras = extras.toTypedArray()
210+
}
140211
}
141-
t.logger.info(execOutput.standardOutput.asText.get())
212+
t.logger.info(podPushProvider.get())
142213
}
143214

144215
is SpecRepo.Private -> {
145-
val execOutput = providers.exec {
146-
commandLine(
147-
"pod",
148-
"repo",
149-
"push",
150-
specRepo.url,
151-
podSpecFile,
152-
*extras.toTypedArray()
153-
)
216+
val podPushProvider = providers.of(PodPrivatePushValueSource::class) {
217+
parameters {
218+
this.specUrl = specRepo.url
219+
this.podSpecFile = podSpecFile
220+
this.extras = extras.toTypedArray()
221+
}
154222
}
155-
t.logger.info(execOutput.standardOutput.asText.get())
223+
t.logger.info(podPushProvider.get())
156224
}
157225
}
158226
}
@@ -208,7 +276,8 @@ private fun generatePodspec(
208276
val libraries =
209277
if (extraSpecAttributes.containsKey("libraries")) "" else "| spec.libraries = 'c++'"
210278

211-
val customSpec = extraSpecAttributes.map { "| spec.${it.key} = ${it.value}" }.joinToString("\n")
279+
val customSpec =
280+
extraSpecAttributes.map { "| spec.${it.key} = ${it.value}" }.joinToString("\n")
212281

213282
val url = urlFile.readText()
214283
val version = version ?: projectVersion
@@ -219,14 +288,20 @@ private fun generatePodspec(
219288
|Pod::Spec.new do |spec|
220289
| spec.name = '$name'
221290
| spec.version = '$version'
222-
| spec.homepage = ${homepage.orEmpty().surroundWithSingleQuotesIfNeeded()}
291+
| spec.homepage = ${
292+
homepage.orEmpty().surroundWithSingleQuotesIfNeeded()
293+
}
223294
| spec.source = {
224295
| :http => '${url}',
225296
| :type => 'zip',
226-
| :headers => ["'Accept: application/octet-stream'"]
297+
| :headers => ["Accept: application/octet-stream"]
227298
| }
228-
| spec.authors = ${authors.orEmpty().surroundWithSingleQuotesIfNeeded()}
229-
| spec.license = ${license.orEmpty().surroundWithSingleQuotesIfNeeded()}
299+
| spec.authors = ${
300+
authors.orEmpty().surroundWithSingleQuotesIfNeeded()
301+
}
302+
| spec.license = ${
303+
license.orEmpty().surroundWithSingleQuotesIfNeeded()
304+
}
230305
| spec.summary = '${summary.orEmpty()}'
231306
$vendoredFrameworks
232307
$libraries

0 commit comments

Comments
 (0)