Repository: tipsy/javalin
Pull request #1238 (2021-05-04 20:44:26)

[javalinvue] Rework configuration

tipsy: Not sure why the java test was messing up various tests, but I just removed it...

Patch:
diff --git a/javalin/src/main/java/io/javalin/plugin/rendering/vue/Configs.kt b/javalin/src/main/java/io/javalin/plugin/rendering/vue/Configs.kt
new file mode 100644
index 00000000..e4c55860
--- /dev/null
+++ b/javalin/src/main/java/io/javalin/plugin/rendering/vue/Configs.kt
@@ -0,0 +1,29 @@
+package io.javalin.plugin.rendering.vue
+
+import java.nio.file.Path
+import java.nio.file.Paths
+
+class VueDirConfig {
+    fun externalPath(path: String) {
+        JavalinVue.rootDirectory = Paths.get(path)
+    }
+    @JvmOverloads
+    fun classpathPath(path: String, resourcesJarClass: Class<*> = PathMaster::class.java) {
+        JavalinVue.resourcesJarClass = resourcesJarClass // used by line below (global import...)
+        JavalinVue.rootDirectory = PathMaster.classpathPath(path)
+    }
+    fun explicitPath(path: Path) {
+        JavalinVue.rootDirectory = path
+    }
+}
+
+class VueVersionConfig {
+    fun vue2() {
+        JavalinVue.vueVersion = VueVersion.VUE_2
+        JavalinVue.vueAppName = "Vue"
+    }
+    fun vue3(appName: String) {
+        JavalinVue.vueVersion = VueVersion.VUE_3
+        JavalinVue.vueAppName = appName
+    }
+}
diff --git a/javalin/src/main/java/io/javalin/plugin/rendering/vue/JavalinVue.kt b/javalin/src/main/java/io/javalin/plugin/rendering/vue/JavalinVue.kt
index 54286b49..31eaf85a 100644
--- a/javalin/src/main/java/io/javalin/plugin/rendering/vue/JavalinVue.kt
+++ b/javalin/src/main/java/io/javalin/plugin/rendering/vue/JavalinVue.kt
@@ -7,12 +7,12 @@
 package io.javalin.plugin.rendering.vue
 
 import io.javalin.http.Context
-import io.javalin.http.staticfiles.Location
 import io.javalin.http.util.ContextUtil.isLocalhost
 import java.nio.file.FileSystems
 import java.nio.file.Files
 import java.nio.file.Path
 import java.nio.file.Paths
+import java.util.function.Consumer
 import java.util.stream.Collectors
 import io.javalin.plugin.rendering.vue.JavalinVue.resourcesJarClass as jarClass
 
@@ -20,21 +20,22 @@ enum class VueVersion { VUE_2, VUE_3 }
 
 object JavalinVue {
     // @formatter:off
+    internal var resourcesJarClass: Class<*> = PathMaster::class.java // can be any class in the jar to look for resources in
+    internal var rootDirectory: Path? = null // is set on first request (if not configured)
+    @JvmStatic fun rootDirectory(config: Consumer<VueDirConfig>) = config.accept(VueDirConfig()) // configures the two internal variables above
+
+    internal var vueVersion = VueVersion.VUE_2
+    internal var vueAppName = "Vue" // only relevant for Vue 3 apps
+    @JvmStatic fun vueVersion(config: Consumer<VueVersionConfig>) = config.accept(VueVersionConfig()) // configures the two internal variables above
+
     internal var isDev: Boolean? = null // cached and easily accessible, is set on first request (can't be configured directly by end user)
     @JvmField var isDevFunction: (Context) -> Boolean = { it.isLocalhost() } // used to set isDev, will be called once
+
     @JvmField var optimizeDependencies = true // only include required components for the route component
-    @JvmField var resourcesJarClass: Class<*> = PathMaster::class.java // can be any class in the jar to look for resources in
+
     @JvmField var stateFunction: (Context) -> Any = { mapOf<String, String>() } // global state that is injected into all VueComponents
+
     @JvmField var cacheControl = "no-cache, no-store, must-revalidate"
-    @JvmField var rootDirectory: Path? = null // is set on first request (if not configured)
-    @JvmStatic fun rootDirectory(path: String, location: Location) {
-        rootDirectory = if (location == Location.CLASSPATH) PathMaster.classpathPath(path) else Paths.get(path)
-    }
-    @JvmField var vueVersion = VueVersion.VUE_2
-    internal var vueAppName = "Vue" // only relevant for Vue 3 apps
-    @JvmStatic fun vueAppName(appName: String) {
-        vueAppName = if (vueVersion == VueVersion.VUE_3) appName else throw IllegalStateException("Only supported when 'vueVersion' is 'VUE_3'")
-    }
     // @formatter:on
     fun walkPaths(): Set<Path> = Files.walk(rootDirectory, 20).collect(Collectors.toSet())
     internal val cachedPaths by lazy { walkPaths() }


Test patch:
diff --git a/javalin/src/test/java/io/javalin/TestJavalinVue.kt b/javalin/src/test/java/io/javalin/TestJavalinVue.kt
index 9170dba0..013dd1ca 100644
--- a/javalin/src/test/java/io/javalin/TestJavalinVue.kt
+++ b/javalin/src/test/java/io/javalin/TestJavalinVue.kt
@@ -7,10 +7,8 @@
 package io.javalin
 
 import io.javalin.http.Context
-import io.javalin.http.staticfiles.Location
 import io.javalin.plugin.rendering.vue.JavalinVue
 import io.javalin.plugin.rendering.vue.VueComponent
-import io.javalin.plugin.rendering.vue.VueVersion
 import io.javalin.testing.TestUtil
 import io.mockk.every
 import io.mockk.mockk
@@ -31,12 +29,13 @@ class TestJavalinVue {
 
     companion object {
         fun before() {
-            JavalinVue.vueAppName = "Vue"
-            JavalinVue.vueVersion = VueVersion.VUE_2
-            JavalinVue.isDev = null // reset
-            JavalinVue.stateFunction = { ctx -> mapOf<String, String>() } // reset
-            JavalinVue.rootDirectory("src/test/resources/vue", Location.EXTERNAL) // src/main -> src/test
-            JavalinVue.optimizeDependencies = false
+            with(JavalinVue) {
+                vueVersion { it.vue2() }
+                isDev = null // reset
+                stateFunction = { ctx -> mapOf<String, String>() } // reset
+                rootDirectory { it.externalPath("src/test/resources/vue") } // src/main ->
+                optimizeDependencies = false
+            }
         }
     }
 
@@ -74,8 +73,7 @@ class TestJavalinVue {
 
     @Test
     fun `vue3 component without state`() = TestUtil.test { app, http ->
-        JavalinVue.vueVersion = VueVersion.VUE_3
-        JavalinVue.vueAppName("app")
+        JavalinVue.vueVersion { it.vue3("app") }
         val encodedEmptyState = """{"pathParams":{},"queryParams":{},"state":{}}""".uriEncodeForJavascript()
 
         app.get("/no-state", VueComponent("<test-component-3></test-component-3>"))
@@ -88,8 +86,7 @@ class TestJavalinVue {
 
     @Test
     fun `vue3 component with state`() = TestUtil.test { app, http ->
-        JavalinVue.vueVersion = VueVersion.VUE_3
-        JavalinVue.vueAppName("app")
+        JavalinVue.vueVersion { it.vue3("app") }
         val encodedState = """{"pathParams":{"my-param":"test-path-param"},"queryParams":{"qp":["test-query-param"]},"state":{"user":{"name":"tipsy","email":"tipsy@tipsy.tipsy"},"role":{"name":"Maintainer"}}}""".uriEncodeForJavascript()
         JavalinVue.stateFunction = { ctx -> state }
         app.get("/vue/:my-param", VueComponent("<test-component-3></test-component-3>"))
@@ -167,7 +164,7 @@ class TestJavalinVue {
 
     @Test
     fun `classpath rootDirectory works`() = TestUtil.test { app, http ->
-        JavalinVue.rootDirectory("/vue", Location.CLASSPATH)
+        JavalinVue.rootDirectory { it.classpathPath("/vue") }
         app.get("/classpath", VueComponent("test-component"))
         assertThat(http.getBody("/classpath")).contains("<test-component></test-component>")
     }
@@ -182,7 +179,7 @@ class TestJavalinVue {
     @Test
     fun `non-existent folder fails`() = TestUtil.test { app, http ->
         JavalinVue.isDev = true // reset
-        JavalinVue.rootDirectory("/vue", Location.EXTERNAL)
+        JavalinVue.rootDirectory { it.externalPath("/vue") }
         app.get("/fail", VueComponent("test-component"))
         assertThat(http.get("/fail").status).isEqualTo(500)
     }
diff --git a/javalin/src/test/java/io/javalin/TestJavalinVueResolution.java b/javalin/src/test/java/io/javalin/TestJavalinVueResolution.java
index 6d934af1..30496156 100644
--- a/javalin/src/test/java/io/javalin/TestJavalinVueResolution.java
+++ b/javalin/src/test/java/io/javalin/TestJavalinVueResolution.java
@@ -74,8 +74,7 @@ public class TestJavalinVueResolution {
     @Test
     public void resolveVue3DependencyTest() {
         TestUtil.test((server, httpUtil) -> {
-            JavalinVue.vueVersion = VueVersion.VUE_3;
-            JavalinVue.vueAppName("app");
+            JavalinVue.vueVersion(vueVersion -> vueVersion.vue3("app"));
             server.get("/single-view", new VueComponent("<view-one-3></view-one-3>"));
             String body = httpUtil.getBody("/single-view");
             assertThat(body).contains("<body><view-one-3></view-one-3></body>");