Skip to content

Commit

Permalink
Fix issues blocking Windows support
Browse files Browse the repository at this point in the history
Specifically...

- gradlew vs gradlew.bat
- Psi parsing confused by \r
- Gradle daemon using a different working directory than *nix (so we
  have to be explicit about the roots that we use)

Fixes #46
  • Loading branch information
bitspittle committed Oct 28, 2021
1 parent 223ba88 commit a299672
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.varabyte.kobweb.cli.common

object Os {
fun isWindows() = System.getProperty("os.name").lowercase().contains("windows")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.varabyte.kobweb.cli.common

fun Runtime.gradlew(vararg args: String): Process {
val finalArgs = mutableListOf(
if (!Os.isWindows()) "./gradlew" else "./gradlew.bat"
)

finalArgs.addAll(args)
return exec(finalArgs.toTypedArray())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.varabyte.kobweb.cli.export

import com.varabyte.kobweb.cli.common.Anims
import com.varabyte.kobweb.cli.common.consumeProcessOutput
import com.varabyte.kobweb.cli.common.gradlew
import com.varabyte.kobweb.cli.common.kobwebProject
import com.varabyte.kobweb.cli.common.newline
import com.varabyte.konsole.foundation.anim.konsoleAnimOf
Expand Down Expand Up @@ -43,8 +44,8 @@ fun handleExport() = konsoleApp {
ExportState.CANCELLED -> yellow { textLine("Export cancelled: $cancelReason") }
}
}.run {
val exportProcess = Runtime.getRuntime()
.exec(arrayOf("./gradlew", "-PkobwebReuseServer=false", "-PkobwebBuildTarget=RELEASE", "kobwebExport"))
val exportProcess =
Runtime.getRuntime().gradlew("-PkobwebReuseServer=false", "-PkobwebBuildTarget=RELEASE", "kobwebExport")
consumeProcessOutput(exportProcess) { shouldAddNewline = true }

onKeyPressed {
Expand Down Expand Up @@ -72,7 +73,7 @@ fun handleExport() = konsoleApp {
}
check(exportState in listOf(ExportState.FINISHING, ExportState.CANCELLING))

val stopProcess = Runtime.getRuntime().exec(arrayOf("./gradlew", "kobwebStop"))
val stopProcess = Runtime.getRuntime().gradlew("kobwebStop")
consumeProcessOutput(stopProcess) { shouldAddNewline = true }
stopProcess.waitFor()

Expand Down
7 changes: 4 additions & 3 deletions cli/kobweb/src/main/kotlin/com/varabyte/kobweb/cli/run/Run.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.varabyte.kobweb.cli.run

import com.varabyte.kobweb.cli.common.Anims
import com.varabyte.kobweb.cli.common.consumeProcessOutput
import com.varabyte.kobweb.cli.common.gradlew
import com.varabyte.kobweb.cli.common.kobwebFolder
import com.varabyte.kobweb.cli.common.newline
import com.varabyte.kobweb.server.api.ServerEnvironment
Expand Down Expand Up @@ -97,11 +98,11 @@ fun handleRun(env: ServerEnvironment) = konsoleApp {
}
}.runUntilSignal {
@Suppress("BlockingMethodInNonBlockingContext")
val args = mutableListOf("./gradlew", "-PkobwebEnv=$env", "kobwebStart")
val args = mutableListOf("-PkobwebEnv=$env", "kobwebStart")
if (env == ServerEnvironment.DEV) {
args.add("-t") // Enable live reloading only while in dev mode
}
val startServerProcess = Runtime.getRuntime().exec(args.toTypedArray())
val startServerProcess = Runtime.getRuntime().gradlew(*args.toTypedArray())
consumeProcessOutput(startServerProcess) { addOutputSeparator = true }

Runtime.getRuntime().addShutdownHook(Thread {
Expand Down Expand Up @@ -136,7 +137,7 @@ fun handleRun(env: ServerEnvironment) = konsoleApp {
startServerProcess.destroy()
startServerProcess.waitFor()

val stopServerProcess = Runtime.getRuntime().exec(arrayOf("./gradlew", "kobwebStop"))
val stopServerProcess = Runtime.getRuntime().gradlew("kobwebStop")
consumeProcessOutput(stopServerProcess) { addOutputSeparator = true }
stopServerProcess.waitFor()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class KobwebProject(
val path: Path = Path.of("")
) {
val kobwebFolder = KobwebFolder.inPath(path)
?: throw KobwebException("Not a valid path to a Kobweb project (no .kobweb folder found): $path")
?: throw KobwebException("Not a valid path to a Kobweb project (no .kobweb folder found): ${path.toAbsolutePath()}")
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.varabyte.kobweb.gradle.application.project

import com.varabyte.kobweb.gradle.application.extensions.visitAllChildren
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtPackageDirective
Expand All @@ -23,11 +19,10 @@ class ApiData {
apiPackage: String,
apiSources: List<File>
): ApiData {
val kotlinProject = createKotlinProject()
val kotlinProject = PsiUtils.createKotlinProject()
val apiData = ApiData()
apiSources.forEach { file ->
val ktFile = PsiManager.getInstance(kotlinProject)
.findFile(LightVirtualFile(file.name, KotlinFileType.INSTANCE, file.readText())) as KtFile
val ktFile = kotlinProject.parseKotlinFile(file)

var currPackage = ""
var initSimpleName = INIT_SIMPLE_NAME
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.varabyte.kobweb.gradle.application.project

import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtFile
import java.io.File

object PsiUtils {
// For now, we're directly parsing Kotlin code using the embedded Kotlin compiler. This is a temporary approach.
// In the future, this should use KSP to navigate through source files. See also: Bug #4
fun createKotlinProject(): Project {
return KotlinCoreEnvironment.createForProduction(
Disposer.newDisposable(),
CompilerConfiguration(),
EnvironmentConfigFiles.JS_CONFIG_FILES
).project
}
}

fun Project.parseKotlinFile(file: File): KtFile {
return PsiManager.getInstance(this)
.findFile(
LightVirtualFile(
file.name,
KotlinFileType.INSTANCE,
// Standardize newlines or else PSI parsing gets confused (e.g. by \r on Windows)
file.readText().replace(System.lineSeparator(), "\n"),
)
) as KtFile
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.varabyte.kobweb.gradle.application.project

import com.varabyte.kobweb.gradle.application.extensions.visitAllChildren
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtPackageDirective
Expand All @@ -23,10 +19,9 @@ class SiteData {
siteSources: List<File>,
): SiteData {
val siteData = SiteData()
val kotlinProject = createKotlinProject()
val kotlinProject = PsiUtils.createKotlinProject()
siteSources.forEach { file ->
val ktFile = PsiManager.getInstance(kotlinProject)
.findFile(LightVirtualFile(file.name, KotlinFileType.INSTANCE, file.readText())) as KtFile
val ktFile = kotlinProject.parseKotlinFile(file)

var currPackage = ""
var pageSimpleName = PAGE_SIMPLE_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ fun ServerState.toDisplayText(): String {
* project.
*/
abstract class KobwebProjectTask(@get:Internal val config: KobwebConfig, desc: String) : KobwebTask(desc) {
@get:Internal val kobwebProject = KobwebProject()

@get:Internal
protected val kobwebConfFile = KobwebConfFile(kobwebProject.kobwebFolder)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.varabyte.kobweb.gradle.application.tasks

import com.varabyte.kobweb.project.KobwebFolder
import com.varabyte.kobweb.server.api.ServerEnvironment
import com.varabyte.kobweb.server.api.ServerStateFile
import org.gradle.api.GradleException
Expand All @@ -26,10 +25,7 @@ abstract class KobwebStartTask @Inject constructor(

@TaskAction
fun execute() {
val kobwebFolder = KobwebFolder.inWorkingDirectory()
?: throw GradleException("This project is missing a Kobweb root folder")

val stateFile = ServerStateFile(kobwebFolder)
val stateFile = ServerStateFile(kobwebProject.kobwebFolder)
stateFile.content?.let { serverState ->
val alreadyRunningMessage = "A Kobweb server is already running at ${serverState.toDisplayText()}"
if (serverState.isRunning()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import java.nio.file.Files
abstract class KobwebStopTask : KobwebTask("Stop a Kobweb server if one is running") {
@TaskAction
fun execute() {
val kobwebFolder = KobwebFolder.inWorkingDirectory()
?: throw GradleException("This project is missing a Kobweb root folder")

val kobwebFolder = kobwebProject.kobwebFolder
val stateFile = ServerStateFile(kobwebFolder)
stateFile.content?.let { serverState ->
if (ProcessHandle.of(serverState.pid).isPresent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.varabyte.kobweb.gradle.application.tasks

import com.varabyte.kobweb.project.KobwebProject
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Internal

abstract class KobwebTask(desc: String) : DefaultTask() {
@get:Internal
val kobwebProject = KobwebProject(project.layout.projectDirectory.asFile.toPath())

init {
group = "kobweb"
description = desc
Expand Down

0 comments on commit a299672

Please sign in to comment.