https://github.com/devsrsouza/kmp-swift-reveal
Gradle plugin that reveal the Swift code that you should expect from a Kotlin iOS module.
https://github.com/devsrsouza/kmp-swift-reveal
gradle-plugin kmp kotlin kotlin-multiplatform kotlin-multiplatform-mobile
Last synced: about 1 month ago
JSON representation
Gradle plugin that reveal the Swift code that you should expect from a Kotlin iOS module.
- Host: GitHub
- URL: https://github.com/devsrsouza/kmp-swift-reveal
- Owner: DevSrSouza
- License: mit
- Created: 2023-10-04T01:04:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-13T11:33:52.000Z (over 1 year ago)
- Last Synced: 2024-10-12T03:44:31.028Z (7 months ago)
- Topics: gradle-plugin, kmp, kotlin, kotlin-multiplatform, kotlin-multiplatform-mobile
- Language: Kotlin
- Homepage: https://plugins.gradle.org/plugin/dev.srsouza.gradle.kmp-swift-reveal
- Size: 69.3 KB
- Stars: 49
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kmp-swift-reveal 🐘
Gradle plugin that reveal the Swift code that you should expect from a Kotlin iOS module.
:arrow_right: `gradlew swiftReveal`
```kotlin
import kotlin.experimental.ExperimentalObjCNameclass ExampleClass {
fun mySuperFun(): Int = 10
fun withCallback(myCallback: () -> String) {}
fun myFunctionReturnMap(): Map = mapOf("Example" to "Value")
@OptIn(ExperimentalObjCName::class)
@ObjCName(swiftName = "mySwiftNameFunction")
fun withObjcName() {}
}fun ExampleClass.myExtensionFunction() {}
fun myTopLevelFunction(): String = "Example return string"
```Generated Swift representation
```swift
class ExampleClass : KotlinBase {
public init()
open func myFunctionReturnMap() -> [String : String]open func mySuperFun() -> Int32
open func withCallback(myCallback: @escaping () -> String)
open func mySwiftNameFunction()
}extension ExampleClass {
open func myExtensionFunction()
}class ExampleClassKt : KotlinBase {
open class func myTopLevelFunction() -> String
}
```## Roadmap
- [ ] Support generate swift file from `binaries` configuration of the module.
- [ ] More examples
- [X] CI usage examples
- [ ] Filter classes and definitions from module dependencies.
- [ ] Validate and support [SKIE](https://github.com/touchlab/SKIE)## Motivation
First motivation: When writing a Kotlin Multiplatform module targeting iOS is usually hard to know what to expect when going to Swift, with the time we can get a little better at this but we usually miss things.
Second motivation: When working with iOS developers it usually much harder for then to predict what is the output of a Kotlin Multiplatform module. This tool can be hooked into the CI pipeline for example to show ahead of time what the iOS developer should expect.
This project aims to fill this unknown gap before going to XCode and implement using new Kotlin Multiplatform code by generating a Swift representation of the output OBJC Header of your module.
## How to use
- Add the plugin to your module build script
```kotlin
plugins {
...
id("dev.srsouza.gradle.kmp-swift-reveal") version "0.1.1"
}
```- Call: `gradlew :yourModule:swiftReveal`
- The file should be located by default in `build/kmp-swift-reveal/out/module/module.swift`
## Configuring
```kotlin
swiftReveal {
directoryForSwiftGeneratedSourceFromModule.set(layout.projectDirectory.dir("swift-reveal")) // default: build/kmp-swift-reveal/out/module/
}
```## How it works
The project was inspired by usage of [Source Kitten](https://github.com/jpsim/SourceKitten/) in the talk [Kotlin/Multiplatform for iOS developers : state & future by Salomon Brys](https://www.youtube.com/watch?v=j-zEAMcMcjA) at Kotlin Conf 23.
Basically, the projects creates a Source Kitten Request for a Swift source code and filter the source code with only your module declarations.
## Integration example
You can do a bunch of custom integration with the generated file to make it visible to the iOS Developers the changes
that are being taking place in the modules that expose code to Swift.A simple **CI** example would be this Github Action Jobs
```yaml
# Put the Swift generated file to the `GITHUB_OUTPUT` of the Step
- name: Get Swift Reveal Output
id: build_swift_reveal_comment
run: |
FILE_CONTENT=$(cat example/swift-reveal/module.swift)
delimiter="$(openssl rand -hex 8)"
echo "content<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "$FILE_CONTENT" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
if: success()
# Comment the Swift file to the Github PR using 'maintain-one-comment' Action
- name: Swift Reveal Comment
uses: actions-cool/maintain-one-comment@v3
if: success()
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
## Swift Reveal result
```swift
${{ steps.build_swift_reveal_comment.outputs.content }}
```
body-include: ''
```You can get the full example Github Action Workflow [here](https://github.com/DevSrSouza/kmp-swift-reveal/blob/main/.github/workflows/swift-reveal.yaml).
## Thanks
- @cortinico for the amazing [kotlin-gradle-plugin-template](https://github.com/cortinico/kotlin-gradle-plugin-template)
- @SalomonBrys for the [Kotlin Conf talk](https://www.youtube.com/watch?v=j-zEAMcMcjA) that inspired this project
- Source kitten contributors
- Kotlin Gradle Plugin and Compose Multiplatform developers for a bunch of Gradle Extensions.