|
16 | 16 |
|
17 | 17 | package com.haulmont.cuba.cli |
18 | 18 |
|
| 19 | +import com.haulmont.cuba.cli.commands.CliCommand |
| 20 | +import org.kodein.di.direct |
| 21 | +import org.kodein.di.generic.instance |
19 | 22 | import java.net.URI |
20 | 23 | import java.nio.file.* |
| 24 | +import kotlin.properties.ReadOnlyProperty |
| 25 | +import kotlin.reflect.KProperty |
21 | 26 |
|
22 | | -class Resources { |
| 27 | +class Resources(private val cliPlugin: CliPlugin) { |
23 | 28 |
|
24 | | - fun getResourcePath(resourceName: String, clazz: Class<Any>): Path? { |
25 | | - if (jrtFileSystem != null) { |
26 | | - val moduleName = clazz.module.name |
27 | | - val jrtPath = jrtFileSystem.getPath("/modules", moduleName, resourceName) |
28 | | - if (Files.exists(jrtPath)) { |
29 | | - return jrtPath |
30 | | - } |
31 | | - } |
| 29 | + private val resourcesBasePath: String = cliPlugin.resources.let { |
| 30 | + (it as? HasResources)?.resourcesBasePath |
| 31 | + ?: throw RuntimeException("Plugin ${cliPlugin.javaClass} doesn't support resources") |
| 32 | + } |
32 | 33 |
|
33 | | - val uri = clazz.getResource(resourceName)?.toURI() |
34 | 34 |
|
35 | | - return if (uri != null) { |
36 | | - if (uri.scheme == "jar") { |
37 | | - val fileSystem = getFileSystem(uri) |
38 | | - fileSystem.getPath(resourceName) |
39 | | - } else { |
40 | | - Paths.get(uri) |
41 | | - } |
42 | | - } else null |
| 35 | + fun getTemplate(templateName: String): Path { |
| 36 | + return getResourcePath(resourcesBasePath + "templates/" + templateName) |
| 37 | + ?: throw RuntimeException("Template $templateName not found in ${cliPlugin.javaClass} plugin") |
43 | 38 | } |
44 | 39 |
|
45 | | - private fun getFileSystem(templateUri: URI?) = try { |
46 | | - FileSystems.getFileSystem(templateUri) |
47 | | - } catch (e: FileSystemNotFoundException) { |
48 | | - FileSystems.newFileSystem(templateUri, mutableMapOf<String, Any>()) |
| 40 | + fun getSnippets(snippetsBasePath: String): Path { |
| 41 | + return getResourcePath(resourcesBasePath + "snippets/" + snippetsBasePath) |
| 42 | + ?: throw RuntimeException("Snippets $snippetsBasePath not found in ${cliPlugin.javaClass} plugin") |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + fun getResourcePath(resourceName: String): Path? { |
| 47 | + return getResourcePath(resourceName, cliPlugin.javaClass) |
49 | 48 | } |
50 | 49 |
|
51 | 50 | companion object { |
| 51 | + fun fromMyPlugin(): ReadOnlyProperty<CliCommand, Resources> = object : ReadOnlyProperty<CliCommand, Resources> { |
| 52 | + override fun getValue(thisRef: CliCommand, property: KProperty<*>): Resources { |
| 53 | + val context = kodein.direct.instance<CliContext>() |
| 54 | + |
| 55 | + val plugin = context.plugins.find { |
| 56 | + it.javaClass.module == thisRef.javaClass.module |
| 57 | + }!! |
| 58 | + |
| 59 | + return Resources(plugin) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fun getResourcePath(resourceName: String, clazz: Class<Any>): Path? { |
| 64 | + if (jrtFileSystem != null) { |
| 65 | + val moduleName = clazz.module.name |
| 66 | + val jrtPath = jrtFileSystem.getPath("/modules", moduleName, resourceName) |
| 67 | + if (Files.exists(jrtPath)) { |
| 68 | + return jrtPath |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return clazz.getResource(resourceName) |
| 73 | + ?.toURI() |
| 74 | + ?.let { |
| 75 | + if (it.scheme == "jar") { |
| 76 | + val fileSystem = getFileSystem(it) |
| 77 | + fileSystem.getPath(resourceName) |
| 78 | + } else { |
| 79 | + Paths.get(it) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun getFileSystem(templateUri: URI?) = try { |
| 85 | + FileSystems.getFileSystem(templateUri) |
| 86 | + } catch (e: FileSystemNotFoundException) { |
| 87 | + FileSystems.newFileSystem(templateUri, mutableMapOf<String, Any>()) |
| 88 | + } |
| 89 | + |
52 | 90 | private val jrtFileSystem: FileSystem? = try { |
53 | 91 | FileSystems.getFileSystem(URI.create("jrt:/")) |
54 | 92 | } catch (e: Exception) { |
|
0 commit comments