skrape.it icon indicating copy to clipboard operation
skrape.it copied to clipboard

[FEATURE] Kotlin Multiplatform support

Open mykola-dev opened this issue 2 years ago • 11 comments

Hey guys! Is there any reason why this lib is jvm only? I would like to use it in my KMM project (android, web, desktop, ios) How much effort is required to migrate the lib to KMM?

mykola-dev avatar Jul 09 '22 10:07 mykola-dev

Hey, I would definatly like to have the library support multiplattform. I think especially in the js/web world it could have great benefit. But so far I have zero experience with creating multiplatform libraries in kotlin. If someone would be willing to investigate and provide a pull request it would be highly appreciated.

Some of the modules are plain kotlin anyway or use libraries that are also multiplatform (like ktor client) and should thereby (I guess) be easily convertable to multi platform

christian-draeger avatar Jul 12 '22 18:07 christian-draeger

as i see the lib uses jsoup under the hood. so the main effort - migrate/rewrite it with KMP with preserving the public api.

mykola-dev avatar Jul 13 '22 09:07 mykola-dev

Following modules are pure kotlin:

  • assertions
  • base-fetcher
  • dsl

Following modules have jvm specific dependencies which would need to be replaced on none jvm plattforms:

  • html-parser (uses jsoup)
    • regarding JS it could maybe be possible to use document api directly?
    • on native maybe something like xsoup?
  • http-fetcher (uses ktor client over apache-client)
  • async-fetcher (uses ktor client over apache-client)
  • browser-fetcher (uses html-unit to execute Javascript that is included in html)
    • on JS this should be possible somehow
    • on native don't know about an appropriate equivalent

Since I never wrote a KMM Lib and running short on time currently because of job and kid, would you mind to provide a PR @mykola-dev? This would really help 🙏

Maybe at least for 1 or 2 modules (if that is possible) to see how things work.

Another question would be how to publish it then.

christian-draeger avatar Jul 21 '22 20:07 christian-draeger

xsoup is a java lib too. i tried to migrate jsoup to kotlin (with further kotlin native migration), but it uses lot of jvm specific apis. estimated efforts are huge, so i abandoned this idea.

mykola-dev avatar Jul 23 '22 12:07 mykola-dev

I'm interested in making a PR for this. I've had a look at the code and I think some groundwork needs to be done first, as the Gradle config is applying the Kotlin/JVM plugin to all projects.

I can make a PR to create some convention plugins for the different Kotlin platforms, as well as other shared build config. The end result will be that the root build.gradle.kts file will be language-neutral, and the subprojects will have a Kotlin/JVM convention plugin applied.

I won't touch any of the source code, just the Gradle config.

aSemy avatar Jul 23 '22 15:07 aSemy

@aSemy this is awesome news. I really appreciate 💯🙏

christian-draeger avatar Jul 23 '22 17:07 christian-draeger

At first glance it looks like migrating to multiplatform will be pretty easy!

  1. Make DomTreeElement a common class - different languages will have to implement their own versions. JVM can use JSoup, and as you noted, JS can use a native implementation
  2. Ktor is multiplatform - so that shouldn't be hard to change
  3. HtmlUnit is only used in one file. I expect that will be pretty easy to make multiplatform.

Something that might be difficult to change is that all the tests are using JUnit, which of course is JVM only. But migrating to something like Kotlin Test or Kotest should be reasonably easy.

aSemy avatar Jul 23 '22 17:07 aSemy

@aSemy how are you going to port the jsoup stuff to the native platforms?

mykola-dev avatar Jul 24 '22 07:07 mykola-dev

I'm planning to only keep the JVM target, but migrate the common Skrape code to commonMain. I'll make a commonMain expect class that any JVM native functionality will have to provide. So no, I'm not going to touch any of the JSoup library.

aSemy avatar Jul 24 '22 08:07 aSemy

Would love support for this.

Shabinder avatar Sep 10 '23 20:09 Shabinder

Hey @Shabinder, Haven't been available for a while due to private responsibilities.

Would you still be open to help on this topic?

christian-draeger avatar Nov 26 '23 07:11 christian-draeger