Skip to content

Commit

Permalink
Merge pull request #45 from InkApplications/module-refactor
Browse files Browse the repository at this point in the history
Break CLI into seperate module
  • Loading branch information
ReneeVandervelde authored Sep 28, 2024
2 parents 6081145 + 43a7913 commit a550a51
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/render-ui
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
done
SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

$SCRIPTDIR/../gradlew render-static-html:installDist --quiet --build-file $SCRIPTDIR/../build.gradle.kts && $SCRIPTDIR/../render-static-html/build/install/render-ui/bin/render-ui "$@"
$SCRIPTDIR/../gradlew cli:installDist --quiet --build-file $SCRIPTDIR/../build.gradle.kts && $SCRIPTDIR/../cli/build/install/inkui/bin/inkui "$@"
13 changes: 13 additions & 0 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
application
kotlin("jvm")
}

application {
applicationName = "inkui"
mainClass.set("ink.ui.cli.MainKt")
}

dependencies {
implementation(projects.renderStaticHtml)
}
34 changes: 34 additions & 0 deletions cli/src/main/java/ink/ui/cli/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ink.ui.cli

import ink.ui.render.statichtml.InkUiScript
import java.io.File
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.onFailure
import kotlin.script.experimental.api.valueOrThrow
import kotlin.system.exitProcess

fun main(args: Array<String>) {
val file = args.firstOrNull()
?.let(::File)
?: run {
println("Usage: render-ui <file>")
exitProcess(1)
}
InkUiScript.evalFile(file)
.onFailure { result ->
result.reports
.filter { it.severity > ScriptDiagnostic.Severity.INFO }
.forEach {
println(it.message)
it.exception?.printStackTrace()
}
exitProcess(1)
}
.valueOrThrow()
.returnValue
.scriptInstance
.let { it as InkUiScript }
.run {
print(getHtml())
}
}
6 changes: 5 additions & 1 deletion render-static-html/api/render-static-html.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public final class ink/ui/render/statichtml/HtmlRenderer {
public final fun renderElement (Link/ui/structures/elements/UiElement;)Lkotlin/jvm/functions/Function1;
public final fun renderLayout (Link/ui/structures/layouts/UiLayout;)Lkotlin/jvm/functions/Function1;
public final fun withRenderer (Link/ui/render/statichtml/renderer/ElementRenderer;)Link/ui/render/statichtml/HtmlRenderer;
public final fun withResourceBaseUrl (Ljava/lang/String;)Link/ui/render/statichtml/HtmlRenderer;
}

public abstract interface class ink/ui/render/statichtml/InkUiBuilder {
Expand All @@ -27,7 +28,7 @@ public final class ink/ui/render/statichtml/InkUiConfig : kotlin/script/experime

public abstract class ink/ui/render/statichtml/InkUiScript : ink/ui/render/statichtml/InkUiBuilder {
public static final field Companion Link/ui/render/statichtml/InkUiScript$Companion;
public fun <init> (Ljava/io/File;)V
public fun <init> (Ljava/io/File;[Link/ui/render/statichtml/renderer/ElementRenderer;)V
public fun addBody (Link/ui/structures/layouts/UiLayout;)V
public fun addBody (Lkotlin/jvm/functions/Function1;)V
public fun addPageFooter (Link/ui/structures/elements/UiElement;)V
Expand All @@ -38,6 +39,7 @@ public abstract class ink/ui/render/statichtml/InkUiScript : ink/ui/render/stati
public fun elementRenderer (Link/ui/render/statichtml/renderer/ElementRenderer;)V
public final fun getCodeBlocks ()Z
public final fun getContentBreak ()Z
public final fun getHtml ()Ljava/lang/String;
public final fun getInkFooter ()Z
public final fun getResourceBaseUrl ()Ljava/lang/String;
public final fun getSectioned ()Z
Expand All @@ -52,6 +54,8 @@ public abstract class ink/ui/render/statichtml/InkUiScript : ink/ui/render/stati
}

public final class ink/ui/render/statichtml/InkUiScript$Companion {
public final fun evalFile (Ljava/io/File;Ljava/util/List;)Lkotlin/script/experimental/api/ResultWithDiagnostics;
public static synthetic fun evalFile$default (Link/ui/render/statichtml/InkUiScript$Companion;Ljava/io/File;Ljava/util/List;ILjava/lang/Object;)Lkotlin/script/experimental/api/ResultWithDiagnostics;
}

public final class ink/ui/render/statichtml/MainKt {
Expand Down
30 changes: 15 additions & 15 deletions render-static-html/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
plugins {
application
kotlin("jvm")
kotlin("multiplatform")
id("ink.publishing")
}

application {
applicationName = "render-ui"
mainClass.set("ink.ui.render.statichtml.MainKt")
}

dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlin.scripting.common)
implementation(libs.kotlin.scripting.jvm.core)
implementation(libs.kotlin.scripting.jvm.host)
implementation(libs.kotlinx.html)
api(projects.structures)
implementation(projects.renderWebCommon)
kotlin {
jvmToolchain(11)
jvm()
sourceSets {
jvmMain.dependencies {
implementation(libs.kotlinx.coroutines.core)
api(libs.kotlin.scripting.common)
implementation(libs.kotlin.scripting.jvm.core)
implementation(libs.kotlin.scripting.jvm.host)
implementation(libs.kotlinx.html)
api(projects.structures)
implementation(projects.renderWebCommon)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,8 @@ class HtmlRenderer(
fun withRenderer(renderer: ElementRenderer): HtmlRenderer {
return HtmlRenderer(resourceBaseUrl, arrayOf(renderer, *customRenderers))
}

fun withResourceBaseUrl(url: String): HtmlRenderer {
return HtmlRenderer(url, customRenderers)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT
@Suppress("unused")
abstract class InkUiScript(
private val scriptFile: File,
customRenderers: Array<ElementRenderer>,
): InkUiBuilder {
private var pageHeaders: MutableList<TagConsumer<*>.() -> Unit> = mutableListOf()
private var pageFooters: MutableList<TagConsumer<*>.() -> Unit> = mutableListOf()
Expand All @@ -34,9 +35,9 @@ abstract class InkUiScript(
final override var resourceBaseUrl: String = "https://ui.inkapplications.com/res"
set(value) {
field = value
renderer = HtmlRenderer(value)
renderer = renderer.withResourceBaseUrl(value)
}
private var renderer = HtmlRenderer(resourceBaseUrl)
private var renderer = HtmlRenderer(resourceBaseUrl, customRenderers)

override fun addPageHeader(element: UiElement) {
pageHeaders.add(renderer.renderElement(element))
Expand Down Expand Up @@ -80,7 +81,7 @@ abstract class InkUiScript(
) + styles
}

internal fun getHtml(): String {
fun getHtml(): String {
return renderer.renderDocument(
pageTitle = title ?: scriptFile.name.replace(Regex("\\.inkui\\.kts$", RegexOption.IGNORE_CASE), ""),
pageHeaders = pageHeaders,
Expand All @@ -99,11 +100,14 @@ abstract class InkUiScript(
}

companion object {
internal fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
fun evalFile(
scriptFile: File,
customRenderers: List<ElementRenderer> = emptyList(),
): ResultWithDiagnostics<EvaluationResult> {
val source = scriptFile.toScriptSource()
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<InkUiScript>()
val evaluationConfiguration = ScriptEvaluationConfiguration {
constructorArgs(scriptFile)
constructorArgs(scriptFile, customRenderers.toTypedArray())
}
return BasicJvmScriptingHost().eval(
script = source,
Expand Down
4 changes: 2 additions & 2 deletions sample-web/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ val webDistDir = project.layout.buildDirectory.get().dir("dist/web").asFile
val webDistComposeDir = project.layout.buildDirectory.get().dir("dist/web/compose").asFile

tasks.register("buildStatic") {
dependsOn(projects.renderStaticHtml.dependencyProject.tasks.named("installDist"))
dependsOn(projects.cli.dependencyProject.tasks.named("installDist"))
doLast {
staticOutputDir.createDirectory()
exec {
val app = projects.renderStaticHtml.dependencyProject.layout.buildDirectory.get().file("install/render-ui/bin/render-ui")
val app = projects.cli.dependencyProject.layout.buildDirectory.get().file("install/inkui/bin/inkui")
val script = project.layout.projectDirectory.file("src/staticMain/Sample.inkui.kts")
val output = "${staticOutputDir.path}/index.html"
commandLine("sh", "-c", "$app $script > $output")
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

rootProject.name = "ink-ui"

include("cli")
include("render-compose")
include("render-compose-html")
include("render-static-html")
Expand Down

0 comments on commit a550a51

Please sign in to comment.