Skip to content

Commit c718768

Browse files
committed
add JS platform
1 parent ca813a5 commit c718768

9 files changed

Lines changed: 145 additions & 56 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ xcuserdata
1313
**.podspec
1414
Podfile.lock
1515
Pods
16+
17+
kotlin-js-store

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ fun initializeMarsXLog(
4545
logToConsole: Boolean
4646
)
4747

48+
// JS initialize
49+
fun initializeConsoleLog()
50+
4851
// logging
4952
object Logging {
5053
fun debug(tag: String, content: String)

example/shared/build.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66

77
kotlin {
88
android()
9+
910
iosX64()
1011
iosArm64()
1112
iosSimulatorArm64()
@@ -22,19 +23,27 @@ kotlin {
2223
}
2324
}
2425

26+
js(IR) {
27+
browser {
28+
}
29+
binaries.executable()
30+
}
31+
2532
sourceSets {
2633
val commonMain by getting {
2734
dependencies {
28-
implementation("${Consts.releaseGroup}:${Consts.releaseName}:${Consts.releaseVersion}")
35+
//implementation("${Consts.releaseGroup}:${Consts.releaseName}:${Consts.releaseVersion}")
2936
}
3037
}
3138
val commonTest by getting {
3239
dependencies {
3340
implementation(kotlin("test"))
3441
}
3542
}
43+
3644
val androidMain by getting
3745
val androidTest by getting
46+
3847
val iosX64Main by getting
3948
val iosArm64Main by getting
4049
val iosSimulatorArm64Main by getting
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.piasy.kmp.xlog.example
2+
3+
import com.piasy.kmp.xlog.initializeConsoleLog
4+
import kotlinx.browser.document
5+
6+
class JsPlatform : Platform {
7+
override val name: String = "JS"
8+
}
9+
10+
actual fun getPlatform(): Platform = JsPlatform()
11+
12+
fun main() {
13+
initializeConsoleLog()
14+
15+
document.getElementById("text")?.innerHTML = Greeting().greeting()
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>kmp-xlog</title>
6+
</head>
7+
<body>
8+
<h1 id="text"></h1>
9+
<script src="shared.js"></script>
10+
</body>
11+
</html>

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ android.nonTransitiveRClass=true
1212
kotlin.mpp.enableCInteropCommonization=true
1313
kotlin.mpp.androidSourceSetLayoutVersion=2
1414
kotlin.native.binary.memoryModel=experimental
15+
kotlin.js.generate.executable.default=false
1516

1617
#Publish
1718
artifact.name="kmp-xlog"

kmp-xlog/build.gradle.kts

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
plugins {
22
kotlin("multiplatform")
33
kotlin("native.cocoapods")
4+
45
id("com.android.library")
56

67
id("convention.publication")
78
}
89

10+
val hostOs: String = System.getProperty("os.name")
11+
val isMacOS = hostOs == "Mac OS X"
12+
val isWindows = hostOs.startsWith("Windows")
13+
914
kotlin {
1015
android() {
1116
publishLibraryVariants("release", "debug")
1217
}
1318

14-
listOf(iosArm64(), iosSimulatorArm64(), iosX64()).forEach { t ->
15-
t.compilations.getByName("main") {
16-
val xlog by cinterops.creating {
17-
defFile("src/iosMain/cinterop/xlog.def")
19+
if (isMacOS) {
20+
listOf(iosArm64(), iosSimulatorArm64(), iosX64()).forEach { t ->
21+
t.compilations.getByName("main") {
22+
val xlog by cinterops.creating {
23+
defFile("src/iosMain/cinterop/xlog.def")
1824

19-
includeDirs("${project.projectDir}/src/iosMain/objc")
25+
includeDirs("${project.projectDir}/src/iosMain/objc")
26+
}
2027
}
21-
}
2228

23-
// kmp doesn't support link to xcframework now,
24-
// but actually we don't need to link it when build static framework
25-
/*val libPath = hashMapOf(
29+
// kmp doesn't support link to xcframework now,
30+
// but actually we don't need to link it when build static framework
31+
/*val libPath = hashMapOf(
2632
"iosArm64" to "os-arm64",
2733
"iosSimulatorArm64" to "sim-arm64",
2834
"iosX64" to "sim-x64",
@@ -38,24 +44,31 @@ kotlin {
3844
)
3945
}
4046
}*/
41-
}
47+
}
48+
49+
cocoapods {
50+
summary = "KMP wrapper for tencent mars xlog"
51+
homepage = "https://github.com/HackWebRTC/kmp-xlog"
52+
version = Consts.releaseVersion
53+
ios.deploymentTarget = Consts.iosDeploymentTarget
54+
55+
license = "{ :type => 'MIT', :file => 'LICENSE'}"
56+
source = "{ :http => '$homepage/releases/download/v$version/kmp_xlog.xcframework.zip' }"
57+
authors = "Piasy Xu"
58+
59+
extraSpecAttributes["libraries"] = "'z'"
60+
extraSpecAttributes["framework"] = "'SystemConfiguration'"
61+
framework {
62+
baseName = "kmp_xlog"
63+
isStatic = true
64+
embedBitcode("disable")
65+
}
66+
}
4267

43-
cocoapods {
44-
summary = "KMP wrapper for tencent mars xlog"
45-
homepage = "https://github.com/HackWebRTC/kmp-xlog"
46-
version = Consts.releaseVersion
47-
ios.deploymentTarget = Consts.iosDeploymentTarget
48-
49-
license = "{ :type => 'MIT', :file => 'LICENSE'}"
50-
source = "{ :http => '$homepage/releases/download/v$version/kmp_xlog.xcframework.zip' }"
51-
authors = "Piasy Xu"
52-
53-
extraSpecAttributes["libraries"] = "'z'"
54-
extraSpecAttributes["framework"] = "'SystemConfiguration'"
55-
framework {
56-
baseName = "kmp_xlog"
57-
isStatic = true
58-
embedBitcode("disable")
68+
js(IR) {
69+
browser {
70+
}
71+
binaries.executable()
5972
}
6073
}
6174

@@ -67,29 +80,39 @@ kotlin {
6780
implementation("io.mockk:mockk-common:${Consts.mockk}")
6881
}
6982
}
83+
7084
val androidMain by getting
7185
val androidTest by getting {
7286
dependencies {
7387
implementation("io.mockk:mockk:${Consts.mockk}")
7488
}
7589
}
76-
val iosArm64Main by getting
77-
val iosSimulatorArm64Main by getting
78-
val iosX64Main by getting
79-
val iosMain by creating {
80-
dependsOn(commonMain)
81-
iosArm64Main.dependsOn(this)
82-
iosSimulatorArm64Main.dependsOn(this)
83-
iosX64Main.dependsOn(this)
84-
}
85-
val iosArm64Test by getting
86-
val iosSimulatorArm64Test by getting
87-
val iosX64Test by getting
88-
val iosTest by creating {
89-
dependsOn(commonTest)
90-
iosArm64Test.dependsOn(this)
91-
iosSimulatorArm64Test.dependsOn(this)
92-
iosX64Test.dependsOn(this)
90+
91+
if (isMacOS) {
92+
val iosArm64Main by getting
93+
val iosSimulatorArm64Main by getting
94+
val iosX64Main by getting
95+
val iosMain by creating {
96+
dependsOn(commonMain)
97+
iosArm64Main.dependsOn(this)
98+
iosSimulatorArm64Main.dependsOn(this)
99+
iosX64Main.dependsOn(this)
100+
}
101+
val iosArm64Test by getting
102+
val iosSimulatorArm64Test by getting
103+
val iosX64Test by getting
104+
val iosTest by creating {
105+
dependsOn(commonTest)
106+
iosArm64Test.dependsOn(this)
107+
iosSimulatorArm64Test.dependsOn(this)
108+
iosX64Test.dependsOn(this)
109+
}
110+
111+
val jsMain by getting {
112+
dependencies {
113+
implementation(kotlin("stdlib-js"))
114+
}
115+
}
93116
}
94117
}
95118
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.piasy.kmp.xlog
2+
3+
/**
4+
* Created by Piasy{github.com/Piasy} on 2022/12/15.
5+
*/
6+
fun initializeConsoleLog() {
7+
Logging.init(JsLogging)
8+
}
9+
10+
private object JsLogging : LoggingImpl {
11+
override fun debug(tag: String, content: String) {
12+
console.log(tag, content)
13+
}
14+
15+
override fun info(tag: String, content: String) {
16+
console.info(tag, content)
17+
}
18+
19+
override fun error(tag: String, content: String) {
20+
console.error(tag, content)
21+
}
22+
}

settings.gradle.kts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
pluginManagement {
2-
repositories {
3-
google()
4-
gradlePluginPortal()
5-
mavenCentral()
6-
}
2+
repositories {
3+
google()
4+
gradlePluginPortal()
5+
mavenCentral()
6+
}
77
}
88

99
dependencyResolutionManagement {
10-
repositories {
11-
google()
12-
mavenCentral()
13-
mavenLocal()
14-
}
10+
repositories {
11+
google()
12+
mavenCentral()
13+
mavenLocal()
14+
}
1515
}
1616

1717
rootProject.name = "kmp-xlog"
1818
include(":kmp-xlog")
1919

20-
include(":example:androidApp")
21-
include(":example:shared")
20+
if (System.getProperty("os.name") == "Mac OS X") {
21+
include(":example:androidApp")
22+
include(":example:shared")
23+
}

0 commit comments

Comments
 (0)