@orbital_mcp/patchrelay 0.1.0-alpha04 → 0.1.0-alpha05
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.patchrelay-tools/build.gradle +22 -0
- package/.patchrelay-tools/settings.gradle +1 -0
- package/.patchrelay-tools/src/main/groovy/dev/patchrelay/gradle/FunctionsPlugin.groovy +328 -0
- package/{gradle-plugin → .patchrelay-tools}/src/main/groovy/dev/patchrelay/gradle/PatchRelayPlugin.groovy +226 -220
- package/.patchrelay-tools/src/main/kotlin/dev/patchrelay/compiler/PatchRelayCompilerRegistrar.kt +70 -0
- package/.patchrelay-tools/src/main/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar +1 -0
- package/.patchrelay-tools/src/main/resources/patchrelay/ComposeScreens.kt +139 -0
- package/README.md +249 -174
- package/package.json +2 -2
- package/src/main.mjs +511 -279
- package/src/project.mjs +196 -174
- package/gradle-plugin/build.gradle +0 -15
- package/gradle-plugin/settings.gradle +0 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
plugins { id 'groovy'; id 'java-gradle-plugin'; id 'org.jetbrains.kotlin.jvm' version '1.9.23' }
|
|
2
|
+
repositories { google(); mavenCentral() }
|
|
3
|
+
dependencies {
|
|
4
|
+
implementation localGroovy()
|
|
5
|
+
implementation 'org.jetbrains.kotlin:kotlin-compiler-embeddable:1.9.23'
|
|
6
|
+
testImplementation 'junit:junit:4.13.2'
|
|
7
|
+
compileOnly 'com.android.tools.build:gradle-api:8.6.0'
|
|
8
|
+
}
|
|
9
|
+
java { toolchain { languageVersion = JavaLanguageVersion.of(17) } }
|
|
10
|
+
kotlin { jvmToolchain(17) }
|
|
11
|
+
gradlePlugin {
|
|
12
|
+
plugins {
|
|
13
|
+
functions {
|
|
14
|
+
id = 'dev.patchrelay.functions'
|
|
15
|
+
implementationClass = 'dev.patchrelay.gradle.FunctionsPlugin'
|
|
16
|
+
}
|
|
17
|
+
baseline {
|
|
18
|
+
id = 'dev.patchrelay.baseline'
|
|
19
|
+
implementationClass = 'dev.patchrelay.gradle.PatchRelayPlugin'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rootProject.name = 'patchrelay-build-tools'
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
package dev.patchrelay.gradle
|
|
2
|
+
|
|
3
|
+
import groovy.json.JsonOutput
|
|
4
|
+
import org.gradle.api.*
|
|
5
|
+
import org.gradle.api.tasks.*
|
|
6
|
+
import org.gradle.api.file.FileCollection
|
|
7
|
+
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
|
8
|
+
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
|
9
|
+
import org.jetbrains.kotlin.config.CompilerConfiguration
|
|
10
|
+
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
|
|
11
|
+
import org.jetbrains.kotlin.psi.*
|
|
12
|
+
import org.jetbrains.kotlin.lexer.KtTokens
|
|
13
|
+
|
|
14
|
+
/** Kotlin PSI reads declarations; Kotlin/JVM and Kotlin/JS still type-check all bodies. */
|
|
15
|
+
class FunctionBindings {
|
|
16
|
+
static List<Map> inspect(Map<String, String> sources) { discover(sources).functions }
|
|
17
|
+
|
|
18
|
+
static Map discover(Map<String, String> sources, boolean compose = false) {
|
|
19
|
+
def disposable = Disposer.newDisposable()
|
|
20
|
+
try {
|
|
21
|
+
def env = KotlinCoreEnvironment.createForProduction(disposable, new CompilerConfiguration(), EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
|
22
|
+
def factory = new KtPsiFactory(env.project, false)
|
|
23
|
+
def bindings = []
|
|
24
|
+
def states = []
|
|
25
|
+
def entrypoints = new HashSet<String>()
|
|
26
|
+
sources.toSorted().each { path, text ->
|
|
27
|
+
def file = factory.createFile(path, text.replace('\r\n', '\n').replace('\r', '\n'))
|
|
28
|
+
if (compose && file.importDirectives.any { it.importedFqName?.asString()?.startsWith('androidx.compose.') && !it.importedFqName.asString().startsWith('androidx.compose.runtime') })
|
|
29
|
+
throw new GradleException("PatchRelay $path: only Compose runtime and dev.patchrelay.compose widgets are supported; Android Material/UI requires a different backend")
|
|
30
|
+
if (compose && file.importDirectives.any { it.importedFqName?.asString() in ['androidx.compose.runtime.LaunchedEffect', 'androidx.compose.runtime.produceState', 'androidx.compose.runtime.rememberCoroutineScope', 'androidx.compose.runtime.snapshotFlow'] })
|
|
31
|
+
throw new GradleException("PatchRelay $path: asynchronous Compose effects are unsupported, including aliased imports; use installed host operations")
|
|
32
|
+
if (file.importDirectives.any { it.aliasName && it.importedFqName?.shortName()?.asString() == 'PatchFunction' })
|
|
33
|
+
throw new GradleException("PatchRelay $path: annotation aliases are unsupported; use @PatchFunction")
|
|
34
|
+
def visit
|
|
35
|
+
visit = { element ->
|
|
36
|
+
if (!compose && element instanceof KtNamedFunction && element.annotationEntries.any { it.shortName?.asString() == 'Composable' })
|
|
37
|
+
throw new GradleException("PatchRelay $path: ${element.name}: Compose runtime/backend is unsupported; use the shared screen-tree DSL or keep Compose in the installed native host")
|
|
38
|
+
if (compose && element instanceof KtCallExpression && element.calleeExpression?.text in ['LaunchedEffect', 'produceState', 'rememberCoroutineScope', 'snapshotFlow'])
|
|
39
|
+
throw new GradleException("PatchRelay $path: ${element.calleeExpression.text}: asynchronous Compose effects are unsupported; use installed host operations and explicit render/event transactions")
|
|
40
|
+
if (element instanceof KtNamedFunction && element.parent instanceof KtFile) {
|
|
41
|
+
if (element.name?.startsWith('patchRelayIntercept_') || element.name?.startsWith('__patchrelay_original_'))
|
|
42
|
+
throw new GradleException("PatchRelay $path: reserved compiler-generated function name: ${element.name}")
|
|
43
|
+
entrypoints.add(element.name)
|
|
44
|
+
}
|
|
45
|
+
if (element instanceof KtProperty && element.annotationEntries.any { it.shortName?.asString() == 'PatchState' }) {
|
|
46
|
+
def property = element
|
|
47
|
+
def annotation = property.annotationEntries.find { it.shortName?.asString() == 'PatchState' }
|
|
48
|
+
String target = (file.packageFqName.asString() ? file.packageFqName.asString() + '.' : '') + property.name
|
|
49
|
+
String id = target.toLowerCase(Locale.ROOT)
|
|
50
|
+
if (annotation.valueArguments) {
|
|
51
|
+
def expression = annotation.valueArguments.size() == 1 ? annotation.valueArguments[0].getArgumentExpression() : null
|
|
52
|
+
if (!(expression instanceof KtStringTemplateExpression) || expression.entries.any { !(it instanceof KtLiteralStringTemplateEntry) })
|
|
53
|
+
throw new GradleException("PatchRelay $path: state ID must be a literal string")
|
|
54
|
+
id = expression.entries.collect { it.text }.join('')
|
|
55
|
+
}
|
|
56
|
+
if (!(property.parent instanceof KtFile) || !property.isVar() || !property.initializer || property.accessors || property.hasDelegate() ||
|
|
57
|
+
property.hasModifier(KtTokens.PRIVATE_KEYWORD) || !supportedType(property.typeReference?.text, false) ||
|
|
58
|
+
!property.name?.matches('[a-zA-Z][a-zA-Z0-9_]*') || !id.matches('[a-z][a-z0-9_.-]{0,95}'))
|
|
59
|
+
throw new GradleException("PatchRelay $path: @PatchState requires a top-level mutable scalar with explicit type, initializer and no custom accessors")
|
|
60
|
+
states << [id:id, target:target, type:property.typeReference.text]
|
|
61
|
+
}
|
|
62
|
+
if (element instanceof KtNamedFunction && element.annotationEntries.any { it.shortName?.asString() == 'PatchFunction' }) {
|
|
63
|
+
def fn = element
|
|
64
|
+
def fail = { reason -> throw new GradleException("PatchRelay $path: ${fn.name}: $reason. Supported: public top-level function, 0..8 scalar arguments, explicit scalar/Unit return type") }
|
|
65
|
+
if (!(fn.parent instanceof KtFile) || fn.receiverTypeReference || fn.typeParameters ||
|
|
66
|
+
fn.hasModifier(KtTokens.PRIVATE_KEYWORD) || fn.hasModifier(KtTokens.INTERNAL_KEYWORD) || fn.hasModifier(KtTokens.EXPECT_KEYWORD) || fn.hasModifier(KtTokens.EXTERNAL_KEYWORD) || fn.hasModifier(KtTokens.INLINE_KEYWORD)) fail('unsupported function declaration (inline interception is unsupported)')
|
|
67
|
+
def parameters = fn.valueParameters.collect { it.typeReference?.text }
|
|
68
|
+
String result = fn.typeReference?.text
|
|
69
|
+
if (!fn.name?.matches('[a-zA-Z][a-zA-Z0-9_]*') || !fn.hasBody() || fn.valueParameters.size() > 8 ||
|
|
70
|
+
!supportedType(result, true) || parameters.any { !supportedType(it, false) } ||
|
|
71
|
+
fn.valueParameters.any { it.defaultValue || it.isVarArg() }) fail('use Int, finite Double, Boolean, String, nullable scalars or Unit result; no defaults or varargs')
|
|
72
|
+
def annotation = fn.annotationEntries.find { it.shortName?.asString() == 'PatchFunction' }
|
|
73
|
+
def args = annotation.valueArguments
|
|
74
|
+
String qualified = (file.packageFqName.asString() ? file.packageFqName.asString() + '.' : '') + fn.name
|
|
75
|
+
String id = qualified.toLowerCase(Locale.ROOT) + '.v1'
|
|
76
|
+
if (args) {
|
|
77
|
+
def expression = args.size() == 1 ? args[0].getArgumentExpression() : null
|
|
78
|
+
if (!(expression instanceof KtStringTemplateExpression) || expression.entries.any { !(it instanceof KtLiteralStringTemplateEntry) }) fail('ID must be a literal string')
|
|
79
|
+
id = expression.entries.collect { it.text }.join('')
|
|
80
|
+
}
|
|
81
|
+
if (!id.matches('[a-z][a-z0-9_.-]{0,95}')) fail('ID must match [a-z][a-z0-9_.-]{0,95}')
|
|
82
|
+
boolean suspending = fn.hasModifier(KtTokens.SUSPEND_KEYWORD)
|
|
83
|
+
bindings << [id:id, signature:(suspending ? 'suspend ' : '') + '(' + parameters.join(',') + ')->' + result,
|
|
84
|
+
target:qualified, name:fn.name, parameters:parameters, result:result, suspending:suspending]
|
|
85
|
+
}
|
|
86
|
+
element.children.each { visit(it) }
|
|
87
|
+
}
|
|
88
|
+
visit(file)
|
|
89
|
+
}
|
|
90
|
+
if (bindings.size() > 64) throw new GradleException('PatchRelay supports at most 64 @PatchFunction declarations')
|
|
91
|
+
if (bindings*.id.toSet().size() != bindings.size() || bindings*.name.toSet().size() != bindings.size()) throw new GradleException('PatchRelay duplicate function ID or hook name; overloads are unsupported')
|
|
92
|
+
if (entrypoints.contains('main')) throw new GradleException('PatchRelay owns the JS main entrypoint; move application initialization into the screen module')
|
|
93
|
+
if (entrypoints.contains('takeHostRequests') != entrypoints.contains('handleHostResult')) throw new GradleException('PatchRelay host protocol requires both takeHostRequests and handleHostResult')
|
|
94
|
+
if (states.size() > 32 || states*.id.toSet().size() != states.size()) throw new GradleException('PatchRelay state IDs must be unique; maximum 32 values')
|
|
95
|
+
[functions:bindings.sort { it.id }, hostCalls:entrypoints.contains('takeHostRequests'), states:states.sort { it.id }, migration:entrypoints.contains('migrateAppState')]
|
|
96
|
+
} finally { Disposer.dispose(disposable) }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static boolean supportedType(String type, boolean result) {
|
|
100
|
+
type != null && (type in ['Int', 'Double', 'Boolean', 'String', 'Int?', 'Double?', 'Boolean?', 'String?'] || (result && type == 'Unit'))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static String decode(String type, String value) {
|
|
104
|
+
if (type == 'Unit') return "patchRelayUnit($value)"
|
|
105
|
+
String name = type.replace('?', '')
|
|
106
|
+
type.endsWith('?') ? "patchRelayNullable($value, ::patchRelay$name)" : "patchRelay$name($value)"
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static String encode(String type, String value) { type == 'Unit' ? "run { $value; JsonNull }" : "JsonPrimitive($value)" }
|
|
110
|
+
|
|
111
|
+
static void generate(List<Map> bindings, File output, boolean hostCalls = false, List<Map> states = [], boolean migration = false) {
|
|
112
|
+
File common = new File(output, 'common/PatchRelayFunctions.kt')
|
|
113
|
+
File js = new File(output, 'js/PatchRelayExports.kt')
|
|
114
|
+
common.parentFile.mkdirs(); js.parentFile.mkdirs()
|
|
115
|
+
String imports = bindings.withIndex().collect { b, index -> "import ${b.target} as patchRelayNative$index" }.join('\n')
|
|
116
|
+
imports += '\n' + states.withIndex().collect { state, index -> "import ${state.target} as patchRelayState$index" }.join('\n')
|
|
117
|
+
String schema = Contract.hash(JsonOutput.toJson(states.collect { [it.id, it.type] }).getBytes('UTF-8'))
|
|
118
|
+
String restore = states.withIndex().collect { state, index -> ' val state' + index + ' = ' + decode(state.type, 'values["' + state.id + '"] ?: error("Missing state")') }.join('\n') + '\n' +
|
|
119
|
+
states.withIndex().collect { state, index -> " patchRelayState$index = state$index" }.join('\n')
|
|
120
|
+
String stateCode = """
|
|
121
|
+
internal fun patchRelayExportState(): String = patchRelayText(JsonObject(mapOf(
|
|
122
|
+
"schema" to JsonPrimitive("$schema"), "values" to JsonObject(mapOf(${states.withIndex().collect { state, index -> '"' + state.id + '" to ' + encode(state.type, "patchRelayState$index") }.join(', ')})))))
|
|
123
|
+
internal fun patchRelayRestoreState(snapshot: String): String {
|
|
124
|
+
val root = patchRelayParse(snapshot) as? JsonObject ?: error("Expected state object")
|
|
125
|
+
val schema = patchRelayString(root["schema"] ?: error("Missing state schema"))
|
|
126
|
+
val oldValues = root["values"] as? JsonObject ?: error("Missing state values")
|
|
127
|
+
val values = if (schema == "$schema") oldValues else ${migration ? 'migrateAppState(schema, oldValues)' : 'return "incompatible"'}
|
|
128
|
+
require(values.keys == setOf<String>(${states.collect { '"' + it.id + '"' }.join(', ')})) { "Wrong state fields" }
|
|
129
|
+
$restore
|
|
130
|
+
return "restored"
|
|
131
|
+
}
|
|
132
|
+
"""
|
|
133
|
+
String hooks = bindings.withIndex().collect { b, index ->
|
|
134
|
+
String params = b.parameters.withIndex().collect { type, n -> "arg$n: $type" }.join(', ')
|
|
135
|
+
String args = b.parameters.indices.collect { "arg$it" }.join(', ')
|
|
136
|
+
String call = "patchRelayNative$index($args)"
|
|
137
|
+
String encodedArgs = b.parameters.withIndex().collect { type, n -> encode(type, "arg$n") }.join(', ')
|
|
138
|
+
""" fun ${b.name}(${params ? params + ', ' : ''}dispatch: PatchRelayDispatch? = null, onResult: (${b.result}) -> Unit) {
|
|
139
|
+
val native: suspend () -> ${b.result} = { $call }
|
|
140
|
+
if (dispatch == null) native.startCoroutine(Continuation(EmptyCoroutineContext) { onResult(it.getOrThrow()) })
|
|
141
|
+
else dispatch("${b.id}", patchRelayText(JsonArray(listOf($encodedArgs))), { done ->
|
|
142
|
+
native.startCoroutine(Continuation(EmptyCoroutineContext) { result -> done(result.map { value -> patchRelayText(${encode(b.result, 'value')}) }) })
|
|
143
|
+
}, { onResult(${decode(b.result, 'patchRelayParse(it)')}) })
|
|
144
|
+
}"""
|
|
145
|
+
}.join('\n')
|
|
146
|
+
String intercepted = bindings.findAll { it.suspending }.collect { b ->
|
|
147
|
+
String nativeName = 'patchRelayNative' + bindings.indexOf(b)
|
|
148
|
+
String params = b.parameters.withIndex().collect { type, n -> "arg$n: $type" }.join(', ')
|
|
149
|
+
String args = b.parameters.indices.collect { "arg$it" }.join(', ')
|
|
150
|
+
String encodedArgs = b.parameters.withIndex().collect { type, n -> encode(type, "arg$n") }.join(', ')
|
|
151
|
+
"""suspend fun patchRelayIntercept_${b.name}($params): ${b.result} {
|
|
152
|
+
val dispatch = PatchRelayFunctions.dispatcher
|
|
153
|
+
if (dispatch == null) return $nativeName($args)
|
|
154
|
+
return suspendCoroutine { continuation ->
|
|
155
|
+
var completed = false
|
|
156
|
+
val finish: (Result<String>) -> Unit = { result ->
|
|
157
|
+
if (!completed) {
|
|
158
|
+
completed = true
|
|
159
|
+
continuation.resumeWith(result.mapCatching { ${decode(b.result, 'patchRelayParse(it)')} })
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
dispatch("${b.id}", patchRelayText(JsonArray(listOf($encodedArgs))), { done ->
|
|
164
|
+
val native: suspend () -> ${b.result} = { $nativeName($args) }
|
|
165
|
+
native.startCoroutine(Continuation(EmptyCoroutineContext) { result -> done(result.mapCatching { value -> patchRelayText(${encode(b.result, 'value')}) }) })
|
|
166
|
+
}, finish)
|
|
167
|
+
} catch (failure: Throwable) { finish(Result.failure(failure)) }
|
|
168
|
+
}
|
|
169
|
+
}"""
|
|
170
|
+
}.join('\n')
|
|
171
|
+
common.setText("""// Generated by PatchRelay. Do not edit.
|
|
172
|
+
import kotlinx.serialization.json.*
|
|
173
|
+
import kotlin.coroutines.*
|
|
174
|
+
$imports
|
|
175
|
+
@Target(AnnotationTarget.FUNCTION)
|
|
176
|
+
@Retention(AnnotationRetention.BINARY)
|
|
177
|
+
annotation class PatchFunction(val id: String = "")
|
|
178
|
+
@Target(AnnotationTarget.PROPERTY)
|
|
179
|
+
@Retention(AnnotationRetention.SOURCE)
|
|
180
|
+
annotation class PatchState(val id: String = "")
|
|
181
|
+
|
|
182
|
+
typealias PatchRelayDispatch = (String, String, ((Result<String>) -> Unit) -> Unit, (String) -> Unit) -> Unit
|
|
183
|
+
typealias PatchRelaySuspendDispatch = (String, String, ((Result<String>) -> Unit) -> Unit, (Result<String>) -> Unit) -> Unit
|
|
184
|
+
|
|
185
|
+
internal fun patchRelayParse(value: String): JsonElement { require(value.length <= 8192); return Json.parseToJsonElement(value) }
|
|
186
|
+
internal fun patchRelayText(value: JsonElement): String = value.toString().also { require(it.length <= 8192) }
|
|
187
|
+
internal fun patchRelayInt(value: JsonElement): Int { require(value is JsonPrimitive && !value.isString); return value.intOrNull ?: error("Expected Int") }
|
|
188
|
+
internal fun patchRelayDouble(value: JsonElement): Double { require(value is JsonPrimitive && !value.isString); return (value.doubleOrNull ?: error("Expected Double")).also { require(it.isFinite()) } }
|
|
189
|
+
internal fun patchRelayBoolean(value: JsonElement): Boolean { require(value is JsonPrimitive && !value.isString); return value.booleanOrNull ?: error("Expected Boolean") }
|
|
190
|
+
internal fun patchRelayString(value: JsonElement): String { require(value is JsonPrimitive && value.isString); return value.content.also { require(it.length <= 4096) } }
|
|
191
|
+
internal fun patchRelayUnit(value: JsonElement) { require(value == JsonNull) }
|
|
192
|
+
internal fun <T> patchRelayNullable(value: JsonElement, read: (JsonElement) -> T): T? = if (value == JsonNull) null else read(value)
|
|
193
|
+
|
|
194
|
+
$stateCode
|
|
195
|
+
$intercepted
|
|
196
|
+
object PatchRelayFunctions {
|
|
197
|
+
@kotlin.concurrent.Volatile private var installedDispatcher: PatchRelaySuspendDispatch? = null
|
|
198
|
+
internal val dispatcher: PatchRelaySuspendDispatch? get() = installedDispatcher
|
|
199
|
+
fun installDispatcher(dispatch: PatchRelaySuspendDispatch?) { installedDispatcher = dispatch }
|
|
200
|
+
val contracts: Map<String, String> = mapOf(${bindings.collect { '"' + it.id + '" to "' + it.signature + '"' }.join(', ')})
|
|
201
|
+
val ids: Set<String> get() = contracts.keys
|
|
202
|
+
$hooks
|
|
203
|
+
}
|
|
204
|
+
""", 'UTF-8')
|
|
205
|
+
String ids = JsonOutput.toJson(bindings*.id)
|
|
206
|
+
String contracts = JsonOutput.toJson(bindings.collectEntries { [(it.id):it.signature] })
|
|
207
|
+
String cases = bindings.collect { b ->
|
|
208
|
+
String args = b.parameters.withIndex().collect { type, n -> decode(type, "args[$n]") }.join(', ')
|
|
209
|
+
' "' + b.id + '" -> { require(args.size == ' + b.parameters.size() + '); ' + encode(b.result, b.target + '(' + args + ')') + ' }'
|
|
210
|
+
}.join('\n')
|
|
211
|
+
String legacy = bindings.findAll { it.signature == '(Int,Int)->Int' }.collect {
|
|
212
|
+
' "' + it.id + '" -> ' + it.target + '(first, second).toString()'
|
|
213
|
+
}.join('\n')
|
|
214
|
+
js.setText("""@file:OptIn(kotlin.js.ExperimentalJsExport::class)
|
|
215
|
+
import kotlin.js.JsExport
|
|
216
|
+
import kotlin.js.Promise
|
|
217
|
+
import kotlin.coroutines.*
|
|
218
|
+
import kotlinx.serialization.json.*
|
|
219
|
+
@JsExport
|
|
220
|
+
fun renderScreen(screenId: String): String = renderAppScreen(screenId)
|
|
221
|
+
@JsExport
|
|
222
|
+
fun handleEvent(screenId: String, action: String, value: String? = null): String = handleAppEvent(screenId, action, value)
|
|
223
|
+
@JsExport
|
|
224
|
+
fun exportState(): String = patchRelayExportState()
|
|
225
|
+
@JsExport
|
|
226
|
+
fun restoreState(snapshot: String): String = patchRelayRestoreState(snapshot)
|
|
227
|
+
@JsExport
|
|
228
|
+
fun patchedFunctionIds(): String = ${JsonOutput.toJson(ids)}
|
|
229
|
+
@JsExport
|
|
230
|
+
fun patchedFunctionContracts(): String = ${JsonOutput.toJson(contracts)}
|
|
231
|
+
@JsExport
|
|
232
|
+
fun invokeFunction(functionId: String, arguments: String): Promise<String> = Promise { resolve, reject ->
|
|
233
|
+
val body: suspend () -> String = {
|
|
234
|
+
val args = patchRelayParse(arguments) as? JsonArray ?: error("Expected argument array")
|
|
235
|
+
require(args.size <= 8)
|
|
236
|
+
patchRelayText(when (functionId) {
|
|
237
|
+
$cases
|
|
238
|
+
else -> error("Unknown function: \$functionId")
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
body.startCoroutine(Continuation(EmptyCoroutineContext) { result -> result.fold(resolve, reject) })
|
|
242
|
+
}
|
|
243
|
+
@JsExport
|
|
244
|
+
fun invokePatch(functionId: String, first: Int, second: Int): String = when (functionId) {
|
|
245
|
+
$legacy
|
|
246
|
+
else -> error("Unknown legacy integer function: \$functionId")
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Stable SDK registration is independent of the Kotlin project's output-module name.
|
|
250
|
+
fun main() {
|
|
251
|
+
val api = js("({})")
|
|
252
|
+
api.exportState = ::exportState
|
|
253
|
+
api.restoreState = ::restoreState
|
|
254
|
+
api.renderScreen = ::renderScreen
|
|
255
|
+
api.handleEvent = ::handleEvent
|
|
256
|
+
api.patchedFunctionIds = ::patchedFunctionIds
|
|
257
|
+
api.patchedFunctionContracts = ::patchedFunctionContracts
|
|
258
|
+
api.invokeFunction = ::invokeFunction
|
|
259
|
+
api.invokePatch = ::invokePatch
|
|
260
|
+
${hostCalls ? ' api.takeHostRequests = ::takeHostRequests\n api.handleHostResult = ::handleHostResult' : ''}
|
|
261
|
+
js("globalThis").__patchrelay = api
|
|
262
|
+
}
|
|
263
|
+
""", 'UTF-8')
|
|
264
|
+
new File(output, 'functions.json').setText(JsonOutput.toJson([format:2, stateSchema:schema, state:states.collect { [id:it.id, type:it.type] }, functions:bindings.collect { [id:it.id, signature:it.signature] }]) + '\n', 'UTF-8')
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
abstract class GenerateFunctions extends DefaultTask {
|
|
269
|
+
@Input boolean compose
|
|
270
|
+
@InputDirectory File sources
|
|
271
|
+
@OutputDirectory File destination
|
|
272
|
+
@InputFiles FileCollection platformSources
|
|
273
|
+
@TaskAction void generate() {
|
|
274
|
+
if (!platformSources.empty) throw new GradleException('PatchRelay supports commonMain Kotlin only; platform implementations require a new native integration: ' + platformSources.files*.name)
|
|
275
|
+
def files = new TreeMap<String,String>()
|
|
276
|
+
sources.eachFileRecurse { if (it.isFile() && it.name.endsWith('.kt')) files[sources.toPath().relativize(it.toPath()).toString()] = it.getText('UTF-8') }
|
|
277
|
+
def program = FunctionBindings.discover(files, compose)
|
|
278
|
+
FunctionBindings.generate(program.functions, destination, program.hostCalls, program.states, program.migration)
|
|
279
|
+
def backend = new File(destination, 'common/PatchRelayCompose.kt')
|
|
280
|
+
if (compose) backend.setText(GenerateFunctions.getResourceAsStream('/patchrelay/ComposeScreens.kt').getText('UTF-8'), 'UTF-8')
|
|
281
|
+
else if (backend.exists()) backend.delete()
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
class FunctionsPlugin implements Plugin<Project> {
|
|
286
|
+
void apply(Project project) {
|
|
287
|
+
project.plugins.withId('org.jetbrains.kotlin.multiplatform') {
|
|
288
|
+
if (project.providers.gradleProperty('kotlin.js.ir.output.granularity').orNull != 'whole-program')
|
|
289
|
+
throw new GradleException('PatchRelay requires a self-contained program: add kotlin.js.ir.output.granularity=whole-program to gradle.properties')
|
|
290
|
+
def output = project.layout.buildDirectory.dir('generated/patchrelay')
|
|
291
|
+
boolean composeEnabled = project.providers.gradleProperty('patchrelay.compose').orNull == 'true'
|
|
292
|
+
def compilerJar = new File(FunctionsPlugin.protectionDomain.codeSource.location.toURI())
|
|
293
|
+
if (!compilerJar.isFile()) throw new GradleException('PatchRelay compiler plugin must run from its built JAR')
|
|
294
|
+
def task = project.tasks.register('generatePatchRelayFunctions', GenerateFunctions) {
|
|
295
|
+
sources = project.file('src/commonMain/kotlin')
|
|
296
|
+
platformSources = project.fileTree('src') { include 'jvmMain/**/*.kt', 'jsMain/**/*.kt', 'androidMain/**/*.kt' }
|
|
297
|
+
destination = output.get().asFile
|
|
298
|
+
compose = composeEnabled
|
|
299
|
+
}
|
|
300
|
+
def kotlin = project.extensions.getByName('kotlin')
|
|
301
|
+
if (kotlin.coreLibrariesVersion != '1.9.23') throw new GradleException('PatchRelay IR interception requires Kotlin 1.9.23; compiler upgrades require a matching plugin')
|
|
302
|
+
kotlin.sourceSets.getByName('commonMain').dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3' }
|
|
303
|
+
if (composeEnabled) {
|
|
304
|
+
kotlin.sourceSets.getByName('commonMain').dependencies { implementation 'org.jetbrains.compose.runtime:runtime:1.6.10' }
|
|
305
|
+
def composeCompiler = project.configurations.create('patchRelayComposeCompiler')
|
|
306
|
+
project.dependencies.add(composeCompiler.name, 'org.jetbrains.compose.compiler:compiler:1.5.13.5')
|
|
307
|
+
project.tasks.matching { it.name.startsWith('compileKotlin') || it.name.startsWith('compileTestKotlin') || it.name.startsWith('compileProductionExecutable') || it.name.startsWith('compileTestDevelopmentExecutable') }.configureEach {
|
|
308
|
+
inputs.files(composeCompiler)
|
|
309
|
+
kotlinOptions.freeCompilerArgs += ['-Xplugin=' + composeCompiler.singleFile.absolutePath]
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
kotlin.sourceSets.getByName('commonMain').kotlin.srcDir(output.map { it.dir('common') })
|
|
313
|
+
// jsMain exists after the consumer declares its JS target.
|
|
314
|
+
project.afterEvaluate {
|
|
315
|
+
kotlin.sourceSets.getByName('jsMain').kotlin.srcDir(output.map { it.dir('js') })
|
|
316
|
+
}
|
|
317
|
+
project.tasks.matching { it.name.startsWith('compileKotlin') || it.name.startsWith('compileProductionExecutable') || it.name.startsWith('compileTestKotlin') }.configureEach { dependsOn(task) }
|
|
318
|
+
kotlin.targets.all { target ->
|
|
319
|
+
if (target.platformType.toString() == 'jvm') target.compilations.matching { it.name == 'main' }.all { compilation ->
|
|
320
|
+
compilation.compileTaskProvider.configure {
|
|
321
|
+
inputs.file(compilerJar)
|
|
322
|
+
kotlinOptions.freeCompilerArgs += ['-Xplugin=' + compilerJar.absolutePath]
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|