This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ByteArray return implemented. To lower overhead and provide greater abstraction on what can be returned from an extension - IExtension.kt; getPassage return changed to ByteArray - LuaExtension.kt; use checkstring to get the byte array of that string (which can be easily decoded to a string). Lua strings are just bytearrays. So this works fine. - Test.kt; Implemented bytearray decoding * KTS Scripting implemented This implements KTS extension support. Included are additional libraries, some minor changes to the way libs are handled - build.gradle.kts; Updated kotlin, Updated dokka, Implemented scripting support - ExtensionType.kt; Defines the type of extension - javax.script.ScriptEngineFactory; Definition to use the kts script engine - KtsExtension.kt; Delegated class that redirects to the parsed kts script - names.kt; Added json type parameter for extension type - RepoData.kt; RepoExtension now has a type parameter - ShosetsuKtsLib.kt; translation of ShosetsuLuaLib.kt for KTS - ShosetsuSharedLib.kt; httpClient is now located here to be shared - ShosetsuLuaLib.kt; now uses ShosetsuSharedLib.kt, old httpClient is deprecated - Test.kt; Modified to use path values instead and patched for KTS support using ExtensionType.kt
- Loading branch information
1 parent
f2f66eb
commit 87dc4a7
Showing
11 changed files
with
168 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.shosetsu.lib | ||
|
||
/** | ||
* Type of extension | ||
*/ | ||
enum class ExtensionType { | ||
/** .lua */ | ||
LuaScript, | ||
|
||
/** .kts */ | ||
KotlinScript | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.shosetsu.lib | ||
|
||
import okhttp3.OkHttpClient | ||
|
||
/** | ||
* shosetsu-kotlin-lib | ||
* 06 / 10 / 2020 | ||
*/ | ||
object ShosetsuSharedLib { | ||
/** okhttp HTTP Client used by lib functions. */ | ||
lateinit var httpClient: OkHttpClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package app.shosetsu.lib.kts | ||
|
||
import app.shosetsu.lib.IExtension | ||
import java.io.File | ||
import kotlin.time.ExperimentalTime | ||
|
||
/** | ||
* shosetsu-services | ||
* 06 / 10 / 2020 | ||
*/ | ||
@ExperimentalTime | ||
class KtsExtension( | ||
private val content: String, | ||
private val _kts: IExtension = KtsObjectLoader().load(content) | ||
) : IExtension by _kts { | ||
constructor(file: File) : this(file.readText()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app.shosetsu.lib.kts | ||
|
||
import javax.script.ScriptEngine | ||
import javax.script.ScriptEngineManager | ||
|
||
/** | ||
* This class is not thread-safe, don't use it for parallel executions and create new instances instead. | ||
*/ | ||
class KtsObjectLoader(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) { | ||
|
||
val engine: ScriptEngine = ScriptEngineManager(classLoader).getEngineByExtension("kts") | ||
|
||
@Throws(IllegalArgumentException::class) | ||
inline fun <reified T> Any?.castOrError(): T = takeIf { it is T }?.let { it as T } | ||
?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}") | ||
|
||
@Throws(RuntimeException::class) | ||
inline fun <reified T> load(script: String): T = | ||
kotlin.runCatching { engine.eval(script) } | ||
.getOrElse @Throws(RuntimeException::class) { throw RuntimeException("Cannot load script", it) } | ||
.castOrError() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package app.shosetsu.lib.kts | ||
|
||
import app.shosetsu.lib.ShosetsuSharedLib.httpClient | ||
import app.shosetsu.lib.exceptions.HTTPException | ||
import app.shosetsu.lib.lua.ShosetsuLuaLib.LibFunctions.RequestDocument | ||
import okhttp3.* | ||
import okhttp3.internal.closeQuietly | ||
import org.jsoup.Jsoup | ||
import org.jsoup.nodes.Document | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* shosetsu-kotlin-lib | ||
* 06 / 10 / 2020 | ||
*/ | ||
object ShosetsuKtsLib { | ||
val defaultCacheControl: CacheControl | ||
get() = CacheControl.Builder().maxAge(10, TimeUnit.MINUTES).build() | ||
|
||
val defaultHeaders: Headers | ||
get() = Headers.Builder().build() | ||
|
||
val defaultBody: RequestBody | ||
get() = FormBody.Builder().build() | ||
|
||
|
||
// For normal extensions, these simple functions are sufficient. | ||
fun get(url: String, headers: Headers, cacheControl: CacheControl): Request = | ||
Request.Builder().url(url).headers(headers).cacheControl(cacheControl).build() | ||
|
||
fun post(url: String, headers: Headers, body: RequestBody, cacheControl: CacheControl): Request = | ||
Request.Builder().url(url).post(body).headers(headers).cacheControl(cacheControl).build() | ||
|
||
|
||
fun parseDocument(content: String): Document = Jsoup.parse(content)!! | ||
|
||
fun requestResponse(request: Request): Response = httpClient.newCall(request).execute() | ||
|
||
fun requestDocument(request: Request): Document = Document( | ||
requestResponse(request).let { r -> | ||
r.takeIf { it.code == 200 }?.body?.string() ?: run { | ||
r.closeQuietly() | ||
throw HTTPException(r.code) | ||
} | ||
} | ||
) | ||
|
||
fun getDocument(url: String): Document = RequestDocument(get(url, defaultHeaders, defaultCacheControl)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/javax.script.ScriptEngineFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters