From b2fd289792a86e29b62b4472e828c843fd7d5f03 Mon Sep 17 00:00:00 2001 From: Sabeeh Ul Hussnain Date: Wed, 4 Sep 2024 10:48:40 +0500 Subject: [PATCH] Ready for release 0.1.6 (#59) * add micro amper gradle code * clean and minor tweak deps * tweak and refactor code * use SourceReader extension for network * kotlin (2.0.20) and agp (8.5.2) version bump * publish script updated * test script updated * fix linux target * charset support flag updated --- .github/workflows/deploy.yaml | 38 +- .github/workflows/test.yml | 30 +- README.md | 2 +- build.gradle.kts | 369 ++++++++++++++++++ gradle.properties | 10 +- gradle/libs.versions.toml | 7 +- .../com/fleeksoft/ksoup/io/SourceExtJvm.kt | 3 +- ksoup-network-korlibs/build.gradle.kts | 10 +- .../fleeksoft/ksoup/network/KsoupNetwork.kt | 11 +- .../ksoup/network/SourceExtNetwork.kt | 8 + ksoup-network-ktor2/build.gradle.kts | 2 +- .../fleeksoft/ksoup/network/KsoupNetwork.kt | 29 +- .../ksoup/network/SourceExtNetwork.kt | 8 + ksoup-network/build.gradle.kts | 10 +- .../fleeksoft/ksoup/network/KsoupNetwork.kt | 11 +- .../ksoup/network/SourceExtNetwork.kt | 10 + ksoup-test/build.gradle.kts | 15 +- ksoup-test/module.yaml | 28 +- .../test/com/fleeksoft/ksoup/TestHelper.kt | 4 +- .../fleeksoft/ksoup/issues/GithubIssue19.kt | 10 +- .../com/fleeksoft/ksoup/nodes/DocumentTest.kt | 4 - .../com/fleeksoft/ksoup/DataUtilTestJvm.kt | 9 +- .../ksoup/io/InputStreamReader.test.kt | 3 +- ksoup/build.gradle.kts | 12 +- ksoup/module.yaml | 4 +- .../com/fleeksoft/ksoup/KsoupJvmExt.kt | 12 +- .../com/fleeksoft/ksoup/SourceExtJvm.kt | 12 + publishToMaven.sh | 117 +++--- runTests.sh | 26 +- settings.gradle.kts | 24 +- 30 files changed, 604 insertions(+), 234 deletions(-) create mode 100644 build.gradle.kts create mode 100644 ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt create mode 100644 ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt create mode 100644 ksoup-network/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt create mode 100644 ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/SourceExtJvm.kt diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 746bd845..9ff7ce63 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -7,26 +7,14 @@ jobs: build: strategy: matrix: - project: - - "ksoup-engine-common:" - - - "ksoup-engine-kotlinx:kotlinx" - - "ksoup:kotlinx" - - "ksoup-network:kotlinx" - - - "ksoup-engine-korlibs:korlibs" - - "ksoup:korlibs" - - "ksoup-network-korlibs:korlibs" - - - "ksoup-engine-ktor2:ktor2" - - "ksoup:ktor2" - - "ksoup-network-ktor2:ktor2" - - - "ksoup-engine-okio:okio" - - "ksoup:okio" - + buildType: + - "common" + - "kotlinx" + - "korlibs" + - "ktor2" + - "okio" runs-on: macos-latest - name: Publish ${{ matrix.project }} + name: Publish ${{ matrix.buildType }} steps: - uses: actions/checkout@v3 - name: Setup JDK 17 @@ -38,11 +26,17 @@ jobs: - name: Setup gradle uses: gradle/gradle-build-action@v2 - - name: Publish ${{ matrix.project }} + - name: Publish ${{ matrix.buildType }} run: | chmod +x ./publishToMaven.sh - ./publishToMaven.sh --remote ${{ matrix.project }} + ./publishToMaven.sh --remote ${{ matrix.buildType }} working-directory: ${{ github.workspace }} + env: + ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} + ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} + ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_KEY }} + ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.GPG_KEY_ID }} + ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.GPG_KEY_PASSWORD }} - name: Success message - run: echo "Publishing of ${{ matrix.project }} completed successfully." \ No newline at end of file + run: echo "Publishing of ${{ matrix.buildType }} completed successfully." \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 94486bf4..ffff27a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,13 +11,13 @@ jobs: strategy: matrix: config: [ - { target: android, os: ubuntu-latest, tasks: testDebugUnitTest testReleaseUnitTest, continueOnError: false }, - { target: apple, os: macos-latest, tasks: iosX64Test iosSimulatorArm64Test macosX64Test macosArm64Test tvosX64Test tvosSimulatorArm64Test, continueOnError: false }, - { target: js, os: ubuntu-latest, tasks: jsTest wasmTest, continueOnError: false }, - { target: desktop, os: ubuntu-latest, tasks: jvmTest, continueOnError: false }, - { target: windows, os: windows-latest, tasks: mingwX64Test, continueOnError: false }, - { target: linux, os: windows-latest, tasks: linuxX64Test, continueOnError: false }, - ] + { target: android, os: ubuntu-latest, tasks: testDebugUnitTest testReleaseUnitTest, continueOnError: false }, + { target: apple, os: macos-latest, tasks: iosX64Test iosSimulatorArm64Test macosX64Test macosArm64Test tvosX64Test tvosSimulatorArm64Test, continueOnError: false }, + { target: js, os: ubuntu-latest, tasks: jsTest wasmJsTest, continueOnError: false }, + { target: desktop, os: ubuntu-latest, tasks: jvmTest, continueOnError: false }, + { target: windows, os: windows-latest, tasks: mingwX64Test, continueOnError: false }, + { target: linux, os: ubuntu-latest, tasks: linuxX64Test, continueOnError: false }, + ] libBuildType: [ "korlibs", "kotlinx", "okio", "ktor2" ] runs-on: ${{ matrix.config.os }} name: Build ${{ matrix.config.target }} with libBuildType=${{ matrix.libBuildType }} @@ -35,10 +35,22 @@ jobs: - name: Run tests for ${{ matrix.libBuildType }} on ${{ matrix.config.target }} continue-on-error: ${{ matrix.config.continueOnError }} run: | - chmod +x ./runTests.sh - ./runTests.sh ${{ matrix.libBuildType }} ${{ matrix.config.tasks }} + if [ "${{ runner.os }}" == "Windows" ]; then + ./gradlew mingwX64Test -PlibBuildType=${{ matrix.libBuildType }} + else + chmod +x ./runTests.sh + ./runTests.sh ${{ matrix.libBuildType }} ${{ matrix.config.tasks }} + fi + shell: bash working-directory: ${{ github.workspace }} + - name: Upload test results from ksoup-test module if failure + if: failure() + uses: actions/upload-artifact@v3 + with: + name: test-results-${{ matrix.config.target }}-${{ matrix.libBuildType }} + path: ksoup-test/build/reports/tests/**/* + # deploy: # if: github.ref == 'refs/heads/release' # diff --git a/README.md b/README.md index 1b8ea7b9..df3f486e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ **Ksoup** is a Kotlin Multiplatform library for working with real-world HTML and XML. It's a port of the renowned Java library, **jsoup**, and offers an easy-to-use API for URL fetching, data parsing, extraction, and manipulation using DOM and CSS selectors. -[![Kotlin](https://img.shields.io/badge/Kotlin-2.0.10-blue.svg?style=flat&logo=kotlin)](https://kotlinlang.org) +[![Kotlin](https://img.shields.io/badge/Kotlin-2.0.20-blue.svg?style=flat&logo=kotlin)](https://kotlinlang.org) [![Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0) [![Maven Central](https://img.shields.io/maven-central/v/com.fleeksoft.ksoup/ksoup.svg)](https://central.sonatype.com/artifact/com.fleeksoft.ksoup/ksoup) diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 00000000..61da269d --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,369 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet +import kotlin.jvm.optionals.getOrNull + +plugins { + alias(libs.plugins.kmp) + alias(libs.plugins.androidLibrary) +} + +val libFinder = versionCatalogs.find("libs").get() +var REAL_VERSION = libs.versions.libraryVersion.get() +val JVM_TARGET = JvmTarget.JVM_17 +val JDK_VERSION = JavaVersion.VERSION_17 + +val GROUP = "com.fleeksoft" + +kotlin { + jvm() + androidTarget() +} + +allprojects { + repositories { + mavenCentral() + google() + gradlePluginPortal() + } + version = REAL_VERSION + group = GROUP + + project.apply(plugin = "kotlin-multiplatform") + project.apply(plugin = "android-library") + + java.toolchain.languageVersion = JavaLanguageVersion.of(JDK_VERSION.majorVersion) + kotlin.jvmToolchain(JDK_VERSION.majorVersion.toInt()) + afterEvaluate { + tasks.withType(Test::class) { + //this.javaLauncher.set() + this.javaLauncher.set(javaToolchains.launcherFor { + // 17 is latest at the current moment + languageVersion.set(JavaLanguageVersion.of(JDK_VERSION.majorVersion)) + }) + } + } + + android { + compileOptions { + sourceCompatibility = JDK_VERSION + targetCompatibility = JDK_VERSION + } + compileSdk = 34 + namespace = "com.fleeksoft.${project.name.replace("-", ".")}" + defaultConfig { + minSdk = 21 + } + } + MicroAmper(this).configure() +} + +subprojects { + apply(plugin = "kotlin-multiplatform") + + kotlin { + androidTarget { + this.compilerOptions.jvmTarget.set(JVM_TARGET) + publishAllLibraryVariants() + } + } + + tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask::class) { + compilerOptions.suppressWarnings.set(true) + // @TODO: We should actually, convert warnings to errors and start removing warnings + compilerOptions.freeCompilerArgs.add("-nowarn") + } + + tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink::class) { + + val folder = this.outputs.files.toList().firstOrNull() + val fromFolder = File(project.projectDir, "testresources") + + if (folder != null) { + val copyAfterLink = tasks.create("${this.name}CopyResources", Copy::class) + copyAfterLink.from(fromFolder) + copyAfterLink.into(folder) + this.dependsOn(copyAfterLink) + } + } +} + +// Tiny, coupled and limited variant of amper compatible with the current structure, so we can bump to Kotlin 2.0.0 in the meantime, while amper is discarded or evolved. +class MicroAmper(val project: Project) { + private var kotlinPlatforms = mutableListOf() + private var kotlinAliases = LinkedHashMap>() + private var deps = mutableListOf() + + //val kotlinBasePlatforms by lazy { kotlinPlatforms.groupBy { getKotlinBasePlatform(it) }.filter { it.value != listOf(it.key) } } + val kotlinBasePlatforms by lazy { kotlinPlatforms.groupBy { getKotlinBasePlatform(it) } } + + fun getKotlinBasePlatform(platform: String): String = + platform.removeSuffix("X64").removeSuffix("X86").removeSuffix("Arm64").removeSuffix("Arm32").removeSuffix("Simulator").removeSuffix("Device") + .also { + check(it.all { it.isLowerCase() && !it.isDigit() }) + } + + data class Dep(val path: String, val exported: Boolean, val test: Boolean, val platform: String, val compileOnly: Boolean) { + val rplatform = platform.takeIf { it.isNotEmpty() } ?: "common" + val configuration = + "$rplatform${if (test) "Test" else "Main"}${if (exported) "Api" else if (compileOnly) "CompileOnly" else "Implementation"}" + } + + fun parseFile(file: File, lines: List = file.readLines()) { + var mode = "" + + for (line in lines) { + val tline = line.substringBeforeLast('#').trim().takeIf { it.isNotEmpty() } ?: continue + + if (line.startsWith(" ") || line.startsWith("\t") || line.startsWith("-")) { + when { + mode == "product" -> { + //println("product=$tline") + when { + tline.startsWith("platforms:") -> { + val platforms = tline.substringAfter('[').substringBeforeLast(']').split(',').map { it.trim() } + kotlinPlatforms.addAll(platforms) + } + } + } + + mode == "aliases" -> { + //println("aliases=$tline") + if (tline.startsWith("-")) { + val (alias2, platforms2) = tline.split(":", limit = 2) + val alias = alias2.trim('-', ' ') + val platforms = platforms2.trim('[', ']', ' ').split(',').map { it.trim() } + //println(" -> alias=$alias, platforms=$platforms") + kotlinAliases[alias] = platforms + } + } + + mode.contains("dependencies") -> { + val platform = mode.substringAfterLast('@', "") + val test = mode.startsWith("test") + val compileOnly = line.contains(Regex(":\\s*compile-only")) + val exported = line.contains(Regex(":\\s*exported")) + val path = tline.removePrefix("-").removeSuffix(": exported").removeSuffix(":exported").removeSuffix(": compile-only") + .removeSuffix(":compile-only").trim() + if (platform.isBlank() || kotlinBasePlatforms.contains(platform) || kotlinAliases.contains(platform) || platform == "apple") { + deps += Dep(path = path, exported = exported, test = test, platform = platform, compileOnly = compileOnly) + } else { + println("platform not included: $platform in $project") + } + } + } + } else { + if (tline.endsWith(":")) { + mode = tline.trimEnd(':').trim() + } + if (tline.startsWith("apply:")) { + val paths = tline.substringAfter(':').trim('[', ',', ' ', ']').split(",") + for (path in paths) { + parseFile(file.parentFile.resolve(path)) + } + } + } + } + } + + data class SourceSetPair(val main: KotlinSourceSet, val test: KotlinSourceSet) { + fun dependsOn(other: SourceSetPair) { + main.dependsOn(other.main) + test.dependsOn(other.test) + } + } + + val sourceSetPairs = LinkedHashMap() + + // specific depends on more generic + fun NamedDomainObjectContainer.ssDependsOn(base: String, other: String) { + if (base == other) return + //println("$base dependsOn $other") + ssPair(base).dependsOn(ssPair(other)) + } + + val projectFiles: Set = (project.projectDir.list() ?: emptyArray()).toSet() + + fun SourceDirectorySet.srcDirIfExists(path: String) { + //if (path in projectFiles) setSrcDirs(listOf(path)) //else println("file doesn't exist $path") + //srcDir(path) + setSrcDirs(listOf(path)) + } + + fun NamedDomainObjectContainer.ssPair(name: String): SourceSetPair { + return sourceSetPairs.getOrPut(name) { + val atName = if (name == "common") "" else "@$name" + SourceSetPair( + main = maybeCreate("${name}Main").also { + it.kotlin.srcDirIfExists("src$atName") + it.resources.srcDirIfExists("resources$atName") + it.kotlin.srcDir("build/generated/ksp/$name/${name}Main/kotlin") + }, + test = maybeCreate("${name}Test").also { + it.kotlin.srcDirIfExists("test$atName") + it.resources.srcDirIfExists("testResources$atName") + it.kotlin.srcDir("build/generated/ksp/$name/${name}Test/kotlin") + } + ) + } + } + + fun applyTo() = with(project) { + project.kotlin.sourceSets { + ssDependsOn("native", "common") + ssDependsOn("posix", "native") + ssDependsOn("apple", "posix") + ssDependsOn("appleNonWatchos", "apple") + ssDependsOn("appleIosTvos", "apple") + + maybeCreate("commonMain").kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") + + for (platform in kotlinPlatforms) { + val isMacos = platform.startsWith("macos") + val isIos = platform.startsWith("ios") + val isTvos = platform.startsWith("tvos") + val isWatchos = platform.startsWith("watchos") + val isNative = platform.contains("X86") || platform.contains("X64") || platform.contains("Arm") + val isApple = isMacos || isIos || isTvos || isWatchos + val isLinux = platform.startsWith("linux") + val isWindows = platform.startsWith("mingw") + val isPosix = isLinux || isApple + val basePlatform = getKotlinBasePlatform(platform) + if (isIos || isTvos) ssDependsOn(basePlatform, "appleIosTvos") + if (isApple && !isWatchos) ssDependsOn(basePlatform, "appleNonWatchos") + if (isPosix) ssDependsOn(basePlatform, "posix") + if (isApple) ssDependsOn(basePlatform, "apple") + if (isNative) ssDependsOn(basePlatform, "native") + if (platform != basePlatform) ssDependsOn(platform, basePlatform) + } + } + + for (platform in kotlinPlatforms) { + when (platform) { + "jvm" -> kotlin.jvm { + compilerOptions { + this.jvmTarget.set(JVM_TARGET) + } + } + + "js" -> kotlin.js { + browser { + testTask { + useMocha { + timeout = "9s" + } + } + } + nodejs { + testTask { + useMocha { + timeout = "9s" + } + } + } + } + + "wasm" -> { + kotlin.wasmJs { + browser { + testTask { + useMocha { + timeout = "9s" + } + } + } + nodejs { + testTask { + useMocha { + timeout = "9s" + } + } + } + } + kotlin.sourceSets { + ssDependsOn("wasmJs", "wasm") + } + } + + "android" -> kotlin.androidTarget {} + "linuxX64" -> kotlin.linuxX64() + "linuxArm64" -> kotlin.linuxArm64() + "tvosArm64" -> kotlin.tvosArm64() + "tvosX64" -> kotlin.tvosX64() + "tvosSimulatorArm64" -> kotlin.tvosSimulatorArm64() + "macosX64" -> kotlin.macosX64() + "macosArm64" -> kotlin.macosArm64() + "iosArm64" -> kotlin.iosArm64() + "iosSimulatorArm64" -> kotlin.iosSimulatorArm64() + "iosX64" -> kotlin.iosX64() + "watchosArm64" -> kotlin.watchosArm64() + "watchosArm32" -> kotlin.watchosArm32() + "watchosDeviceArm64" -> kotlin.watchosDeviceArm64() + "watchosSimulatorArm64" -> kotlin.watchosSimulatorArm64() + "mingwX64" -> kotlin.mingwX64() + } + } + + //kotlin.applyDefaultHierarchyTemplate() + + kotlin.targets.forEach { + it.compilations.forEach { + it.compileTaskProvider.configure { + compilerOptions { + // apiVersion: Allow to use declarations only from the specified version of bundled libraries + // languageVersion: Provide source compatibility with specified language version + //this.apiVersion.set(KotlinVersion.KOTLIN_2_0) + //this.languageVersion.set(KotlinVersion.KOTLIN_2_0) + } + } + } + } + + kotlin.sourceSets { + // jvm, js, wasm, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, watchosArm64, watchosArm32, watchosDeviceArm64, watchosSimulatorArm64, mingwX64 + + for ((alias, platforms) in (kotlinAliases + kotlinBasePlatforms)) { + //for ((alias, platforms) in kotlinAliases) { + ssDependsOn(alias, "common") + for (platform in platforms) ssDependsOn(platform, alias) + } + } + //println(" -> $platforms") + + dependencies { + for (dep in deps) { + add( + dep.configuration, when { + dep.path.contains('/') -> project(":${File(dep.path).name}") + dep.path.startsWith("\$") -> { + when (dep.path) { + "\$kotlin-test" -> "org.jetbrains.kotlin:kotlin-test" + else -> { + val result = libFinder.findLibrary(dep.path.replace("\$libs.", "").replace(".", "-")).getOrNull()?.get() + result?.toString() ?: TODO("Unknown ${dep.path}, $result") + } + } + } + + else -> dep.path + } + ) + } + } + + for (target in kotlin.targets) { + target.compilations.all { + compileTaskProvider.configure { + compilerOptions { + suppressWarnings.set(true) + } + } + } + } + } + + fun configure() { + val amperFile = File(project.projectDir, "module.yaml").takeIf { it.exists() } ?: return + parseFile(amperFile) + applyTo() + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 1f74c807..470ff79f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,11 @@ android.useAndroidX=true android.nonTransitiveRClass=true kotlin.native.ignoreIncorrectDependencies=true kotlin.mpp.enableCInteropCommonization=true -# dev, kotlinx, korlibs, okio, ktor2 +kotlin.mpp.applyDefaultHierarchyTemplate=false +# common, kotlinx, korlibs, okio, ktor2 # dev will include all modules in settings.gradle.kts but use kotlinx dep for engine -libBuildType=kotlinx -isWasmEnabled=false \ No newline at end of file +libBuildType=common + + +SONATYPE_HOST=CENTRAL_PORTAL +RELEASE_SIGNING_ENABLED=true \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4e31e231..9d67cb8c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,6 @@ [versions] -agp = "8.2.2" #kmp max gradle support 8.2 right now -#kotlin = "1.9.24" -kotlin = "2.0.10" +agp = "8.5.2" +kotlin = "2.0.20" compileSdk = "34" minSdk = "21" libraryVersion = "0.1.6" @@ -48,7 +47,7 @@ okio-nodefilesystem = { module = "com.squareup.okio:okio-nodefilesystem", versio [plugins] androidLibrary = { id = "com.android.library", version.ref = "agp" } -kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +kmp = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" } power-assert = { id = "org.jetbrains.kotlin.plugin.power-assert", version.ref = "kotlin" } mavenPublish = { id = "com.vanniktech.maven.publish", version.ref = "mavenPublish" } diff --git a/ksoup-engine-kotlinx/src@jvmAndAndroid/com/fleeksoft/ksoup/io/SourceExtJvm.kt b/ksoup-engine-kotlinx/src@jvmAndAndroid/com/fleeksoft/ksoup/io/SourceExtJvm.kt index 09ca50f5..5e90f32d 100644 --- a/ksoup-engine-kotlinx/src@jvmAndAndroid/com/fleeksoft/ksoup/io/SourceExtJvm.kt +++ b/ksoup-engine-kotlinx/src@jvmAndAndroid/com/fleeksoft/ksoup/io/SourceExtJvm.kt @@ -2,9 +2,10 @@ package com.fleeksoft.ksoup.io import kotlinx.io.asSource import kotlinx.io.buffered +import kotlinx.io.files.Path import java.io.File import java.io.InputStream -fun FileSource.Companion.from(file: File): FileSource = FileSource.from(file) +fun FileSource.Companion.from(file: File): FileSource = FileSource.from(Path(file.absolutePath)) fun SourceReader.Companion.from(inputStream: InputStream): SourceReader = SourceReader.from(inputStream.asSource().buffered()) \ No newline at end of file diff --git a/ksoup-network-korlibs/build.gradle.kts b/ksoup-network-korlibs/build.gradle.kts index ea1e2783..995de17d 100644 --- a/ksoup-network-korlibs/build.gradle.kts +++ b/ksoup-network-korlibs/build.gradle.kts @@ -1,16 +1,8 @@ plugins { - alias(libs.plugins.dokka) +// alias(libs.plugins.dokka) alias(libs.plugins.mavenPublish) } -val isWasmEnabled = project.findProperty("isWasmEnabled")?.toString()?.toBoolean() ?: false -val libBuildType = project.findProperty("libBuildType")?.toString() -kotlin { - if (isWasmEnabled && libBuildType != "dev") { - wasmJs() - } -} - group = "com.fleeksoft.ksoup" version = libs.versions.libraryVersion.get() diff --git a/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt b/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt index 95b7ca36..30a4d7b0 100644 --- a/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt +++ b/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt @@ -1,8 +1,6 @@ package com.fleeksoft.ksoup.network import com.fleeksoft.ksoup.Ksoup -import com.fleeksoft.ksoup.io.SourceReader -import com.fleeksoft.ksoup.io.from import com.fleeksoft.ksoup.nodes.Document import com.fleeksoft.ksoup.parser.Parser import korlibs.io.async.CIO @@ -35,8 +33,7 @@ public suspend fun Ksoup.parseGetRequest( ) // url can be changed after redirection val finalUrl = httpResponse.headers["location"] ?: url - val sourceReader = SourceReader.from(httpResponse.content) - return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return@withContext parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -65,8 +62,7 @@ public suspend fun Ksoup.parseSubmitRequest( ) // url can be changed after redirection val finalUrl = httpResponse.headers["location"] ?: url - val sourceReader = SourceReader.from(httpResponse.content) - return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return@withContext parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -95,6 +91,5 @@ public suspend fun Ksoup.parsePostRequest( ) // url can be changed after redirection val finalUrl = httpResponse.headers["location"] ?: url - val sourceReader = SourceReader.from(httpResponse.content) - return@withContext parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return@withContext parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } diff --git a/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt b/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt new file mode 100644 index 00000000..91da4834 --- /dev/null +++ b/ksoup-network-korlibs/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt @@ -0,0 +1,8 @@ +package com.fleeksoft.ksoup.network + +import com.fleeksoft.ksoup.io.SourceReader +import com.fleeksoft.ksoup.io.from +import korlibs.io.net.http.HttpClient +import korlibs.io.stream.readAll + +suspend fun HttpClient.Response.asSourceReader() = SourceReader.from(this.content.readAll()) \ No newline at end of file diff --git a/ksoup-network-ktor2/build.gradle.kts b/ksoup-network-ktor2/build.gradle.kts index 71acd901..2a89b7e8 100644 --- a/ksoup-network-ktor2/build.gradle.kts +++ b/ksoup-network-ktor2/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - alias(libs.plugins.dokka) +// alias(libs.plugins.dokka) alias(libs.plugins.mavenPublish) } diff --git a/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt b/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt index a20e2c49..8ff01651 100644 --- a/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt +++ b/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt @@ -25,8 +25,7 @@ public suspend fun Ksoup.parseGetRequest( val httpResponse = NetworkHelperKtor.instance.get(url, httpRequestBuilder = httpRequestBuilder) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val response = httpResponse.bodyAsText() - return parse(html = response, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -46,16 +45,14 @@ public suspend fun Ksoup.parseSubmitRequest( httpRequestBuilder: HttpRequestBuilder.() -> Unit = {}, parser: Parser = Parser.htmlParser(), ): Document { - val httpResponse = - NetworkHelperKtor.instance.submitForm( - url = url, - params = params, - httpRequestBuilder = httpRequestBuilder, - ) + val httpResponse = NetworkHelperKtor.instance.submitForm( + url = url, + params = params, + httpRequestBuilder = httpRequestBuilder, + ) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val result: String = httpResponse.bodyAsText() - return parse(html = result, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -74,13 +71,11 @@ public suspend fun Ksoup.parsePostRequest( httpRequestBuilder: HttpRequestBuilder.() -> Unit = {}, parser: Parser = Parser.htmlParser(), ): Document { - val httpResponse = - NetworkHelperKtor.instance.post( - url = url, - httpRequestBuilder = httpRequestBuilder, - ) + val httpResponse = NetworkHelperKtor.instance.post( + url = url, + httpRequestBuilder = httpRequestBuilder, + ) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val result: String = httpResponse.bodyAsText() - return parse(html = result, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } diff --git a/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt b/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt new file mode 100644 index 00000000..749cb85c --- /dev/null +++ b/ksoup-network-ktor2/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt @@ -0,0 +1,8 @@ +package com.fleeksoft.ksoup.network + +import com.fleeksoft.ksoup.io.SourceReader +import com.fleeksoft.ksoup.io.from +import io.ktor.client.statement.* +import io.ktor.utils.io.* + +suspend fun HttpResponse.asSourceReader() = SourceReader.from(this.bodyAsChannel().toByteArray()) diff --git a/ksoup-network/build.gradle.kts b/ksoup-network/build.gradle.kts index 76b28bb7..b234b8eb 100644 --- a/ksoup-network/build.gradle.kts +++ b/ksoup-network/build.gradle.kts @@ -1,16 +1,8 @@ plugins { - alias(libs.plugins.dokka) +// alias(libs.plugins.dokka) alias(libs.plugins.mavenPublish) } -val isWasmEnabled = project.findProperty("isWasmEnabled")?.toString()?.toBoolean() ?: false -val libBuildType = project.findProperty("libBuildType")?.toString() -kotlin { - if (isWasmEnabled && libBuildType != "dev") { - wasmJs() - } -} - group = "com.fleeksoft.ksoup" version = libs.versions.libraryVersion.get() diff --git a/ksoup-network/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt b/ksoup-network/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt index f9fec9ed..8c0617fc 100644 --- a/ksoup-network/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt +++ b/ksoup-network/src/com/fleeksoft/ksoup/network/KsoupNetwork.kt @@ -1,8 +1,6 @@ package com.fleeksoft.ksoup.network import com.fleeksoft.ksoup.Ksoup -import com.fleeksoft.ksoup.io.SourceReader -import com.fleeksoft.ksoup.io.from import com.fleeksoft.ksoup.nodes.Document import com.fleeksoft.ksoup.parser.Parser import io.ktor.client.request.* @@ -27,8 +25,7 @@ public suspend fun Ksoup.parseGetRequest( val httpResponse = NetworkHelperKtor.instance.get(url, httpRequestBuilder = httpRequestBuilder) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val sourceReader = SourceReader.from(httpResponse.bodyAsChannel()) - return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -56,8 +53,7 @@ public suspend fun Ksoup.parseSubmitRequest( ) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val sourceReader = SourceReader.from(httpResponse.bodyAsChannel()) - return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } /** @@ -82,6 +78,5 @@ public suspend fun Ksoup.parsePostRequest( ) // url can be changed after redirection val finalUrl = httpResponse.request.url.toString() - val sourceReader = SourceReader.from(httpResponse.bodyAsChannel()) - return parse(sourceReader = sourceReader, parser = parser, baseUri = finalUrl) + return parse(sourceReader = httpResponse.asSourceReader(), parser = parser, baseUri = finalUrl) } diff --git a/ksoup-network/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt b/ksoup-network/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt new file mode 100644 index 00000000..1b63888b --- /dev/null +++ b/ksoup-network/src/com/fleeksoft/ksoup/network/SourceExtNetwork.kt @@ -0,0 +1,10 @@ +@file:OptIn(InternalAPI::class) + +package com.fleeksoft.ksoup.network + +import com.fleeksoft.ksoup.io.SourceReader +import com.fleeksoft.ksoup.io.from +import io.ktor.client.statement.* +import io.ktor.utils.io.* + +suspend fun HttpResponse.asSourceReader() = SourceReader.from(this.bodyAsChannel().readBuffer) \ No newline at end of file diff --git a/ksoup-test/build.gradle.kts b/ksoup-test/build.gradle.kts index 95f14014..451600b7 100644 --- a/ksoup-test/build.gradle.kts +++ b/ksoup-test/build.gradle.kts @@ -6,20 +6,7 @@ val rootPath = "generated/kotlin" val isGithubActions: Boolean = System.getenv("GITHUB_ACTIONS")?.toBoolean() == true val libBuildType = project.findProperty("libBuildType")?.toString() -val isWasmEnabled = project.findProperty("isWasmEnabled")?.toString()?.toBoolean() ?: false kotlin { - js(IR) { - browser { - testTask { - useMocha { - timeout = "9s" - } - } - } - } - if (isWasmEnabled && libBuildType != "dev" && (libBuildType == "korlibs" || libBuildType == "kotlinx")) { - wasmJs() - } sourceSets { commonTest { this.kotlin.srcDir(layout.buildDirectory.file(rootPath)) @@ -41,7 +28,7 @@ val generateBuildConfigFile: Task by tasks.creating { const val PROJECT_ROOT: String = "${rootProject.rootDir.absolutePath.replace("\\", "\\\\")}" const val isGithubActions: Boolean = $isGithubActions const val libBuildType: String = "$libBuildType" - const val isKotlinx: Boolean = ${libBuildType == "kotlinx" || libBuildType == "dev"} + const val isKotlinx: Boolean = ${libBuildType == "kotlinx" || libBuildType == "common"} const val isKorlibs: Boolean = ${libBuildType == "korlibs"} const val isOkio: Boolean = ${libBuildType == "okio"} const val isKtor2: Boolean = ${libBuildType == "ktor2"} diff --git a/ksoup-test/module.yaml b/ksoup-test/module.yaml index 8303702e..13af50ff 100644 --- a/ksoup-test/module.yaml +++ b/ksoup-test/module.yaml @@ -1,27 +1,27 @@ product: - type: lib - platforms: [ jvm, js, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64 ] + type: lib + platforms: [ jvm, js, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64 ] apply: [ ../common.module-template.yaml ] aliases: - - jvmAndAndroid: [ jvm, android ] + - jvmAndAndroid: [ jvm, android ] repositories: - - mavenLocal + - mavenLocal test-dependencies: - - ../ksoup - - $libs.korlibs.io - - $libs.kotlinx.io - - $libs.codepoints - - $libs.kotlinx.coroutines.test - - $libs.kotlinx.datetime - - $libs.stately.concurrent + - ../ksoup + - $libs.korlibs.io + - $libs.kotlinx.io + - $libs.codepoints + - $libs.kotlinx.coroutines.test + - $libs.kotlinx.datetime + - $libs.stately.concurrent test-dependencies@jvmAndAndroid: - - $libs.jsoup + - $libs.jsoup settings: - kotlin: - optIns: [ kotlinx.cinterop.BetaInteropApi, kotlinx.cinterop.UnsafeNumber, kotlinx.cinterop.ExperimentalForeignApi, kotlin.experimental.ExperimentalNativeApi ] \ No newline at end of file + kotlin: + optIns: [ kotlinx.cinterop.BetaInteropApi, kotlinx.cinterop.UnsafeNumber, kotlinx.cinterop.ExperimentalForeignApi, kotlin.experimental.ExperimentalNativeApi ] diff --git a/ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt b/ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt index 4f583942..22e5556c 100644 --- a/ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt +++ b/ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt @@ -72,7 +72,7 @@ object TestHelper { fun isGzipSupported(): Boolean = BuildConfig.isKorlibs fun isUtf16Supported(): Boolean = !((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm()) fun isUtf32Supported(): Boolean = !(Platform.isJsOrWasm() || Platform.isWindows() || Platform.isLinux()) - fun isEUCKRSupported(): Boolean = !(Platform.isJsOrWasm() || Platform.isApple() || Platform.isWindows()) - fun isGB2312Supported(): Boolean = !(Platform.isApple() || Platform.isWindows() || ((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm())) + fun isEUCKRSupported(): Boolean = !(Platform.isJsOrWasm() || Platform.isApple() || Platform.isWindows() || (BuildConfig.isKorlibs && Platform.isLinux())) + fun isGB2312Supported(): Boolean = !(Platform.isApple() || Platform.isWindows() || ((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm()) || (BuildConfig.isKorlibs && Platform.isLinux())) fun canReadResourceFile(): Boolean = !Platform.isWasmJs() || BuildConfig.isKorlibs } \ No newline at end of file diff --git a/ksoup-test/test/com/fleeksoft/ksoup/issues/GithubIssue19.kt b/ksoup-test/test/com/fleeksoft/ksoup/issues/GithubIssue19.kt index 08ecee29..fe7444b2 100644 --- a/ksoup-test/test/com/fleeksoft/ksoup/issues/GithubIssue19.kt +++ b/ksoup-test/test/com/fleeksoft/ksoup/issues/GithubIssue19.kt @@ -14,11 +14,13 @@ class GithubIssue19 { @Test fun testAttributeIncorrectMixCharsetIssue() = runTest { - if (Platform.isJsOrWasm()) { -// timeout issue - return@runTest + val resourceName = "htmltests/issue19.html.gz" + val document: Document = if (!TestHelper.isGzipSupported()) { + val source = TestHelper.readResource(resourceName) + Ksoup.parse(sourceReader = source, baseUri = "http://example.com") + } else { + Ksoup.parseFile(filePath = TestHelper.getResourceAbsolutePath(resourceName), baseUri = "http://example.com") } - val document: Document = Ksoup.parseFile(TestHelper.getResourceAbsolutePath("htmltests/issue19.html.gz")) val imagesEls: Elements = document.select("img") for (imagesEl in imagesEls) { val attr: String = imagesEl.attr("src") diff --git a/ksoup-test/test/com/fleeksoft/ksoup/nodes/DocumentTest.kt b/ksoup-test/test/com/fleeksoft/ksoup/nodes/DocumentTest.kt index 3ae75050..d514e329 100644 --- a/ksoup-test/test/com/fleeksoft/ksoup/nodes/DocumentTest.kt +++ b/ksoup-test/test/com/fleeksoft/ksoup/nodes/DocumentTest.kt @@ -221,10 +221,6 @@ class DocumentTest { @Test fun testOverflowClone() { - if (Platform.isWasmJs()) { - // FIXME: timeout error for wasmjs - return - } val sb = StringBuilder() sb.append("") diff --git a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/DataUtilTestJvm.kt b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/DataUtilTestJvm.kt index 4f653609..2c1bddac 100644 --- a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/DataUtilTestJvm.kt +++ b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/DataUtilTestJvm.kt @@ -4,7 +4,6 @@ import com.fleeksoft.ksoup.helper.DataUtil import com.fleeksoft.ksoup.nodes.Document import com.fleeksoft.ksoup.parser.Parser import com.fleeksoft.ksoup.ported.io.Charsets -import com.fleeksoft.ksoup.ported.openSourceReader import com.fleeksoft.ksoup.ported.toByteArray import kotlinx.coroutines.test.runTest import java.io.* @@ -71,7 +70,7 @@ class DataUtilTestJvm { inputStream(secondPart), ) val doc: Document = DataUtil.parseInputSource( - sourceReader = sequenceStream.readAllBytes().openSourceReader(), + sourceReader = sequenceStream.toSourceReader(), charsetName = null, baseUri = "", parser = Parser.htmlParser(), @@ -95,7 +94,7 @@ class DataUtilTestJvm { val input = getFileAsString(file) val expected = Ksoup.parse(html = input, baseUri = "https://example.com") val doc: Document = Ksoup.parse( - sourceReader = GZIPInputStream(FileInputStream(file)).readAllBytes().openSourceReader(), + sourceReader = GZIPInputStream(FileInputStream(file)).toSourceReader(), charsetName = null, baseUri = "https://example.com", ) @@ -129,7 +128,7 @@ class DataUtilTestJvm { baseUri = "https://example.com", ) val docThree: Document = Ksoup.parse( - sourceReader = GZIPInputStream(FileInputStream(file)).readAllBytes().openSourceReader(), + sourceReader = GZIPInputStream(FileInputStream(file)).toSourceReader(), charsetName = null, baseUri = "https://example.com", ) @@ -157,7 +156,7 @@ class DataUtilTestJvm { val bytes: ByteArray = if (file.getName().endsWith(".gz")) { val stream: InputStream = GZIPInputStream(FileInputStream(file)) - val byteBuffer: ByteArray = DataUtil.readToByteBuffer(stream.readAllBytes().openSourceReader(), 0) + val byteBuffer: ByteArray = DataUtil.readToByteBuffer(stream.toSourceReader(), 0) byteBuffer } else { file.readBytes() diff --git a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt index 936d38d4..7d82a031 100644 --- a/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt +++ b/ksoup-test/test@jvmAndAndroid/com/fleeksoft/ksoup/io/InputStreamReader.test.kt @@ -5,6 +5,7 @@ import com.fleeksoft.ksoup.ported.io.BufferedReader import com.fleeksoft.ksoup.ported.io.InputSourceReader import com.fleeksoft.ksoup.ported.io.Reader import com.fleeksoft.ksoup.ported.io.StringReader +import com.fleeksoft.ksoup.toSourceReader import korlibs.io.lang.substr import java.io.ByteArrayInputStream import kotlin.test.Test @@ -13,7 +14,7 @@ import kotlin.test.assertEquals class InputStreamReader { private fun String.toInputStreamSourceReader(): SourceReader { - return SourceReader.from(ByteArrayInputStream(this.encodeToByteArray())) + return ByteArrayInputStream(this.encodeToByteArray()).toSourceReader() } @Test diff --git a/ksoup/build.gradle.kts b/ksoup/build.gradle.kts index 256302ef..bba959a8 100644 --- a/ksoup/build.gradle.kts +++ b/ksoup/build.gradle.kts @@ -1,5 +1,7 @@ +import com.vanniktech.maven.publish.SonatypeHost + plugins { - alias(libs.plugins.dokka) +// alias(libs.plugins.dokka) alias(libs.plugins.mavenPublish) } @@ -33,7 +35,13 @@ kotlin { } } -val artifactId = if (libBuildType == "korlibs") "ksoup-korlibs" else if (libBuildType == "okio") "ksoup-okio" else "ksoup" +val artifactId = when (libBuildType) { + "korlibs" -> "ksoup-korlibs" + "okio" -> "ksoup-okio" + "ktor2" -> "ksoup-ktor2" + else -> "ksoup" +} + mavenPublishing { coordinates("com.fleeksoft.ksoup", artifactId, libs.versions.libraryVersion.get()) pom { diff --git a/ksoup/module.yaml b/ksoup/module.yaml index 3758dc60..9c79b217 100644 --- a/ksoup/module.yaml +++ b/ksoup/module.yaml @@ -1,11 +1,11 @@ product: type: lib - platforms: [jvm, js, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64] + platforms: [ jvm, js, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64 ] apply: [ ../common.module-template.yaml ] aliases: - - jvmAndAndroid: [jvm, android] + - jvmAndAndroid: [ jvm, android ] repositories: - mavenLocal diff --git a/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/KsoupJvmExt.kt b/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/KsoupJvmExt.kt index 35bd5d79..ce3cd2d7 100644 --- a/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/KsoupJvmExt.kt +++ b/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/KsoupJvmExt.kt @@ -1,11 +1,7 @@ package com.fleeksoft.ksoup -import com.fleeksoft.ksoup.io.FileSource -import com.fleeksoft.ksoup.io.SourceReader -import com.fleeksoft.ksoup.io.from import com.fleeksoft.ksoup.nodes.Document import com.fleeksoft.ksoup.parser.Parser -import com.fleeksoft.ksoup.ported.toSourceFile import java.io.File import java.io.IOException import java.io.InputStream @@ -30,7 +26,7 @@ public fun Ksoup.parseInputStream( parser: Parser = Parser.htmlParser(), ): Document { return parse( - sourceReader = SourceReader.from(inputStream), + sourceReader = inputStream.toSourceReader(), charsetName = charsetName, baseUri = baseUri, parser = parser, @@ -54,7 +50,7 @@ public suspend fun Ksoup.parseFile( parser: Parser = Parser.htmlParser(), ): Document { return parseFile( - file = FileSource.from(file), + file = file.toFileSource(), charsetName = charsetName, baseUri = baseUri, parser = parser, @@ -80,8 +76,8 @@ suspend fun Ksoup.parsePath( charsetName: String? = null, parser: Parser = Parser.htmlParser() ): Document { - return Ksoup.parse( - sourceReader = path.absolutePathString().toSourceFile().toSourceReader(), + return parseFile( + file = path.toFileSource(), baseUri = baseUri, charsetName = charsetName, parser = parser diff --git a/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/SourceExtJvm.kt b/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/SourceExtJvm.kt new file mode 100644 index 00000000..4946898a --- /dev/null +++ b/ksoup/src@jvmAndAndroid/com/fleeksoft/ksoup/SourceExtJvm.kt @@ -0,0 +1,12 @@ +package com.fleeksoft.ksoup + +import com.fleeksoft.ksoup.io.FileSource +import com.fleeksoft.ksoup.io.SourceReader +import com.fleeksoft.ksoup.io.from +import java.io.File +import java.io.InputStream +import kotlin.io.path.absolutePathString + +fun File.toFileSource(): FileSource = FileSource.from(this) +fun java.nio.file.Path.toFileSource(): FileSource = FileSource.from(this.absolutePathString()) +fun InputStream.toSourceReader(): SourceReader = SourceReader.from(this) \ No newline at end of file diff --git a/publishToMaven.sh b/publishToMaven.sh index 1d58c0b3..2e22af6a 100755 --- a/publishToMaven.sh +++ b/publishToMaven.sh @@ -12,35 +12,45 @@ PUBLISH_TASK="publishToMavenLocal" # Check for the --remote flag if [ "$1" == "--remote" ]; then PUBLISH_TASK="publishAllPublicationsToMavenCentralRepository" - shift # Remove the --remote argument + shift fi -# projectModule:libBuildType -default_projects=( - "ksoup-engine-common:" +# Default build types if none are passed +default_build_types=("kotlinx" "korlibs" "ktor2" "okio") - "ksoup-engine-kotlinx:kotlinx" - "ksoup:kotlinx" - "ksoup-network:kotlinx" - - "ksoup-engine-korlibs:korlibs" - "ksoup:korlibs" - "ksoup-network-korlibs:korlibs" - - "ksoup-engine-ktor2:ktor2" - "ksoup:ktor2" - "ksoup-network-ktor2:ktor2" +# If build types are passed, use them; otherwise, use the default list +if [ "$#" -ge 1 ]; then + build_types=("$@") +else + build_types=("${default_build_types[@]}") +fi - "ksoup-engine-okio:okio" - "ksoup:okio" -) +# Function to add projects based on the key +add_projects_based_on_key() { + local key="$1" + case "$key" in + "common") + projects=("ksoup-engine-common") + ;; + "kotlinx") + projects=("ksoup-engine-kotlinx" "ksoup" "ksoup-network") + ;; + "korlibs") + projects=("ksoup-engine-korlibs" "ksoup" "ksoup-network-korlibs") + ;; + "ktor2") + projects=("ksoup-engine-ktor2" "ksoup" "ksoup-network-ktor2") + ;; + "okio") + projects=("ksoup-engine-okio" "ksoup") + ;; + *) + echo "Unknown key: $key" + exit 1 + ;; + esac +} - # Check if projects were passed as arguments - if [ "$#" -ne 0 ]; then - projects=("$@") - else - projects=("${default_projects[@]}") - fi # Function to add wasm to platforms list if not already present add_wasm_platform() { @@ -49,20 +59,20 @@ add_wasm_platform() { # Check if 'platforms:' line already contains 'wasm' if grep -q 'platforms: \[.*wasm' "$module_file"; then echo "wasm is already in the platforms list in $module_file" - return 0 # Return 0 (indicating wasm was not added) else echo "Adding wasm to platforms list in $module_file" cp "$module_file" "$module_file.bak" # Add 'wasm' to the beginning of the platforms list sed -i.bak 's/\(platforms: \[\)/\1wasm, /' "$module_file" - return 1 # Return 1 (indicating wasm was added) fi } # Function to restore the original module.yaml file restore_module_yaml() { - echo "Restoring original module.yaml in $1" - mv "$1/module.yaml.bak" "$1/module.yaml" + if [ -f "$1/module.yaml.bak" ]; then + echo "Restoring original module.yaml in $1" + mv "$1/module.yaml.bak" "$1/module.yaml" + fi } # Function to handle errors and restore the original file if needed @@ -86,46 +96,35 @@ safe_remove_dir() { trap 'error_handler' ERR # Loop through all projects and publish them -for project in "${projects[@]}"; do - # Split the project name and build type - IFS=":" read -r projectName buildType <<< "$project" +for buildType in "${build_types[@]}"; do + add_projects_based_on_key "$buildType" - wasm_added=0 + # Remove build directories if they exist + echo "remove build dirs if exists" + safe_remove_dir ".kotlin" + safe_remove_dir "build" + safe_remove_dir ".gradle" + safe_remove_dir "kotlin-js-store" if [ "$ADD_WASM" = true ] && [[ "$buildType" == "kotlinx" || "$buildType" == "korlibs" ]]; then - trap - ERR # Temporarily disable the trap - set +e # Disable exit on error - # Add wasm to platforms list if buildType is kotlinx or korlibs - add_wasm_platform "$projectName" - wasm_added=$? # Capture the return value indicate if wasm added - set -e # Re-enable exit on error - trap 'error_handler' ERR # Re-enable the trap - else - wasm_added=0 # Set to false if wasm wasn't added + echo "check and add wasm to projects" + for projectName in "${projects[@]}"; do + add_wasm_platform "$projectName" + done fi - if [ -n "$buildType" ]; then - - # Remove build directories if they exist - echo "remove build dirs if exists" - safe_remove_dir ".kotlin" - safe_remove_dir "build" - safe_remove_dir ".gradle" - safe_remove_dir "kotlin-js-store" + ./gradlew clean -PlibBuildType="$buildType" --quiet --warning-mode=none - ./gradlew clean -PlibBuildType="$buildType" --quiet --warning-mode=none + for projectName in "${projects[@]}"; do + echo "*****buildType: $buildType, project: $projectName" echo "Publishing $projectName with libBuildType=$buildType" - ./gradlew ":$projectName:$PUBLISH_TASK" -PlibBuildType="$buildType" --quiet --warning-mode=none - else - ./gradlew clean --quiet --warning-mode=none - echo "Publishing $projectName" - ./gradlew ":$projectName:$PUBLISH_TASK" --quiet --warning-mode=none - fi + ./gradlew ":$projectName:$PUBLISH_TASK" -PlibBuildType="$buildType" --quiet --warning-mode=none --no-configuration-cache + done - # Restore the original module.yaml file after publishing if wasm was added - if [ "$wasm_added" -eq 1 ]; then + echo "check and restore module.yaml if required" + for projectName in "${projects[@]}"; do restore_module_yaml "$projectName" - fi + done done echo "Publishing completed successfully." diff --git a/runTests.sh b/runTests.sh index 97125666..279b388e 100755 --- a/runTests.sh +++ b/runTests.sh @@ -8,8 +8,6 @@ add_wasm_platform() { local module_file="ksoup/module.yaml" local test_module_file="ksoup-test/module.yaml" - wasm_added=0 - if grep -q 'platforms: \[.*wasm' "$module_file"; then echo "wasm is already in the platforms list in $module_file" @@ -17,7 +15,6 @@ add_wasm_platform() { echo "Adding wasm to platforms list in $module_file" cp "$module_file" "$module_file.bak" sed -i.bak 's/\(platforms: \[\)/\1wasm, /' "$module_file" - wasm_added=1 fi if grep -q 'platforms: \[.*wasm' "$test_module_file"; then @@ -26,17 +23,17 @@ add_wasm_platform() { echo "Adding wasm to platforms list in $test_module_file" cp "$test_module_file" "$test_module_file.bak" sed -i.bak 's/\(platforms: \[\)/\1wasm, /' "$test_module_file" - wasm_added=1 fi } # Function to restore the original module.yaml files restore_module_yaml() { - echo "Restoring original module.yaml files..." if [ -f "ksoup/module.yaml.bak" ]; then + echo "ksoup/module.yaml restored" mv "ksoup/module.yaml.bak" "ksoup/module.yaml" fi if [ -f "ksoup-test/module.yaml.bak" ]; then + echo "ksoup-test/module.yaml restored" mv "ksoup-test/module.yaml.bak" "ksoup-test/module.yaml" fi } @@ -44,9 +41,7 @@ restore_module_yaml() { # Error handler function to restore module.yaml on failure error_handler() { echo "Error detected, restoring module.yaml if necessary..." - if [ -f "ksoup/module.yaml.bak" ]; then - restore_module_yaml - fi + restore_module_yaml exit 1 } @@ -69,20 +64,15 @@ run_tests() { if [ ${#tasks[@]} -eq 0 ]; then echo "No specific tasks provided, running all default tests..." - tasks=("jvmTest" "testDebugUnitTest" "testReleaseUnitTest" "jsTest" "wasmTest" "iosX64Test" "iosSimulatorArm64Test" "macosX64Test" "macosArm64Test" "tvosX64Test" "tvosSimulatorArm64Test") - fi + # tasks=("jvmTest" "testDebugUnitTest" "testReleaseUnitTest" "jsTest" "wasmJsTest" "iosX64Test" "iosSimulatorArm64Test" "macosX64Test" "macosArm64Test" "tvosX64Test" "tvosSimulatorArm64Test") + tasks=("jvmTest" "jsTest" "wasmJsTest") + fi echo "Running tests with libBuildType=$libBuildType and tasks=${tasks[*]}..." # Only add/remove wasm for kotlinx and korlibs - local wasm_added=0 if [[ "$libBuildType" == "kotlinx" || "$libBuildType" == "korlibs" ]]; then -# trap - ERR # Temporarily disable the trap -# set +e # Disable exit on error add_wasm_platform -# wasm_added=$? -# set -e # Re-enable exit on error -# trap 'error_handler' ERR # Re-enable the trap fi # Remove build directories if they exist @@ -104,9 +94,7 @@ run_tests() { done # Restore original module.yaml if wasm was added - if [ "$wasm_added" -eq 1 ]; then - restore_module_yaml - fi + restore_module_yaml } # Supported parameters diff --git a/settings.gradle.kts b/settings.gradle.kts index bd5b1813..4e9ddbc2 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,32 +3,40 @@ pluginManagement { mavenCentral() google() gradlePluginPortal() - maven("https://packages.jetbrains.team/maven/p/amper/amper") - maven("https://www.jetbrains.com/intellij-repository/releases") - maven("https://packages.jetbrains.team/maven/p/ij/intellij-dependencies") +// maven("https://packages.jetbrains.team/maven/p/amper/amper") +// maven("https://www.jetbrains.com/intellij-repository/releases") +// maven("https://packages.jetbrains.team/maven/p/ij/intellij-dependencies") } } -plugins { +/*plugins { id("org.jetbrains.amper.settings.plugin").version("0.5.0-dev-992") +}*/ + +dependencyResolutionManagement { + repositories { + google() + mavenCentral() + mavenLocal() + } } val libBuildType = settings.providers.gradleProperty("libBuildType").get() include("ksoup-engine-common") -if (libBuildType == "korlibs" || libBuildType == "dev") { +if (libBuildType == "korlibs" || libBuildType == "common") { include("ksoup-engine-korlibs", "ksoup-network-korlibs") } -if (libBuildType == "kotlinx" || libBuildType == "dev") { +if (libBuildType == "kotlinx" || libBuildType == "common") { include("ksoup-engine-kotlinx", "ksoup-network") } -if (libBuildType == "okio" || libBuildType == "dev") { +if (libBuildType == "okio" || libBuildType == "common") { include("ksoup-engine-okio", "ksoup-network-ktor2") } -if (libBuildType == "ktor2" || libBuildType == "dev") { +if (libBuildType == "ktor2" || libBuildType == "common") { include("ksoup-engine-ktor2", "ksoup-network-ktor2") }