Compare commits
5 Commits
1.0
..
10af871d2b
| Author | SHA1 | Date | |
|---|---|---|---|
| 10af871d2b | |||
| 907fdb4f10 | |||
| f037c6a724 | |||
| 6ed31db7dc | |||
| 6e1e82c2c5 |
@@ -12,9 +12,8 @@ Move the executable somewhere in your $PATH
|
|||||||
Or build a native image with GraalVM
|
Or build a native image with GraalVM
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./gradlew installShadowDist
|
./gradlew buildNative
|
||||||
cd app/build/install/app-shadow/lib
|
cd app/build/native
|
||||||
native-image --no-fallback -R:MaxNewSize=32 -jar scaffold.jar
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -28,7 +27,7 @@ cd generator-directory
|
|||||||
|
|
||||||
You can then add [Pebble templates](https://pebbletemplates.io/) and files in the tree directory
|
You can then add [Pebble templates](https://pebbletemplates.io/) and files in the tree directory
|
||||||
|
|
||||||
An example is present in this repo
|
An kotlin example is present [here](https://git.simplenotes.be/hubert/Kotlin-scaffold)
|
||||||
|
|
||||||
### List generators
|
### List generators
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("kotlin-application")
|
id("kotlin-application")
|
||||||
id("com.github.johnrengelman.shadow") version "6.1.0"
|
id("shadow")
|
||||||
|
id("native-image")
|
||||||
|
id("release")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version = "0.0.1-SNAPSHOT"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
|
implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
|
||||||
runtimeOnly("org.slf4j:slf4j-simple:2.0.0-alpha1")
|
runtimeOnly("org.slf4j:slf4j-simple:2.0.0-alpha1")
|
||||||
implementation("io.pebbletemplates:pebble:3.1.5")
|
implementation("io.pebbletemplates:pebble:3.1.5")
|
||||||
implementation("com.github.ajalt.clikt:clikt:3.1.0")
|
implementation("com.github.ajalt.clikt:clikt:3.1.0")
|
||||||
implementation("com.electronwill.night-config:toml:3.6.3")
|
implementation("org.graalvm.js:js:21.0.0.2")
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
@@ -16,9 +20,3 @@ application {
|
|||||||
mainClass.set("scaffold.ScaffoldKt")
|
mainClass.set("scaffold.ScaffoldKt")
|
||||||
applicationName = "scaffold"
|
applicationName = "scaffold"
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
|
|
||||||
archiveBaseName.set("scaffold")
|
|
||||||
archiveClassifier.set("")
|
|
||||||
archiveVersion.set("")
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name":"com.electronwill.nightconfig.toml.TomlFormat"
|
"name":"java.util.Map$Entry",
|
||||||
|
"allPublicMethods":true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
{
|
{
|
||||||
"resources":{
|
"resources": {
|
||||||
"includes":[{"pattern":"\\QMETA-INF/services/org.slf4j.spi.SLF4JServiceProvider\\E"}]},
|
"includes": [
|
||||||
"bundles":[]
|
{"pattern": "\\QMETA-INF/services/org.slf4j.spi.SLF4JServiceProvider\\E"},
|
||||||
|
{"pattern": "\\Qinit/index.d.ts\\E"},
|
||||||
|
{"pattern": "\\Qinit/index.js\\E"},
|
||||||
|
{"pattern": "\\Qinit/README.md\\E"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"bundles": []
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
# {{ name }}
|
||||||
Vendored
+43
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Print a message to the console
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
declare function echo(message: string): void
|
||||||
|
|
||||||
|
declare interface prompt {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prompt a user for text input
|
||||||
|
*
|
||||||
|
* @param text - The text to display for the prompt
|
||||||
|
* @param def - A default value
|
||||||
|
*/
|
||||||
|
string(text: string, def?: string): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prompt a user for text input
|
||||||
|
*
|
||||||
|
* @param text - The text to display for the prompt
|
||||||
|
* @param def - A default value
|
||||||
|
*/
|
||||||
|
boolean(text: string, def?: boolean): boolean
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a template
|
||||||
|
*
|
||||||
|
* @param name - The template name
|
||||||
|
* @param context - The template context
|
||||||
|
* @param output - The output path, if absent, default to the template name
|
||||||
|
*/
|
||||||
|
declare function render(name: string, context: any, output?: string): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy a file
|
||||||
|
*
|
||||||
|
* @param input - The input path
|
||||||
|
* @param output - The output path, if absent, default to the input path
|
||||||
|
*/
|
||||||
|
declare function copy(input: string, output?: string): void
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
echo("Example")
|
||||||
|
|
||||||
|
const name = prompt.string("Project name")
|
||||||
|
|
||||||
|
const ctx = {name: name}
|
||||||
|
|
||||||
|
render("README.md", ctx)
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package scaffold
|
|
||||||
|
|
||||||
import com.electronwill.nightconfig.core.Config
|
|
||||||
import com.electronwill.nightconfig.core.UnmodifiableConfig
|
|
||||||
import com.electronwill.nightconfig.core.file.FileConfig
|
|
||||||
import java.nio.file.Path
|
|
||||||
|
|
||||||
data class GeneratorConfig(
|
|
||||||
val templates: List<String>,
|
|
||||||
val files: List<Path>,
|
|
||||||
val prompts: Map<String, List<PromptValue>>
|
|
||||||
)
|
|
||||||
|
|
||||||
fun Generator.loadConfig(): GeneratorConfig {
|
|
||||||
val config: UnmodifiableConfig = FileConfig.of(rootPath.resolve("config.toml")).apply { load() }
|
|
||||||
val templates = config.get<List<String>>("templates")
|
|
||||||
|
|
||||||
val prompts = mutableMapOf<String, MutableList<PromptValue>>()
|
|
||||||
val section = config.get<Any?>("prompt") ?: error("Missing prompt section")
|
|
||||||
if (section !is Config) error("Prompt section is not an object")
|
|
||||||
|
|
||||||
for ((subsectionName, subsection) in section.valueMap()) {
|
|
||||||
check(subsection is List<*>) { "prompts.$subsectionName is not a list of prompts" }
|
|
||||||
|
|
||||||
for (prompt in subsection) {
|
|
||||||
check(prompt is Config)
|
|
||||||
|
|
||||||
val promptValue = PromptValue(
|
|
||||||
name = prompt.get("name") ?: error("Missing name"),
|
|
||||||
info = prompt.get("info"),
|
|
||||||
type = prompt.getEnum("type", PromptType::class.java) ?: PromptType.String
|
|
||||||
)
|
|
||||||
|
|
||||||
val current = prompts.computeIfAbsent(subsectionName) { ArrayList() }
|
|
||||||
current += promptValue
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
val files = config.get<List<String>>("files").flatMap { path ->
|
|
||||||
if ("*" in path) FileGlob.listFiles(treeRoot, path)
|
|
||||||
else listOf(treeRoot.resolve(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
return GeneratorConfig(templates, files, prompts)
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package scaffold
|
|
||||||
|
|
||||||
import java.nio.file.*
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes
|
|
||||||
|
|
||||||
object FileGlob {
|
|
||||||
|
|
||||||
fun listFiles(root: Path, glob: String): MutableList<Path> {
|
|
||||||
val matcher = FileSystems.getDefault().getPathMatcher("glob:$root/$glob")
|
|
||||||
|
|
||||||
val matches = mutableListOf<Path>()
|
|
||||||
|
|
||||||
Files.walkFileTree(root, object : SimpleFileVisitor<Path>() {
|
|
||||||
override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult {
|
|
||||||
if (matcher.matches(file)) matches.add(file)
|
|
||||||
return FileVisitResult.CONTINUE
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return matches
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,13 +5,12 @@ import java.nio.file.Path
|
|||||||
import kotlin.streams.toList
|
import kotlin.streams.toList
|
||||||
|
|
||||||
class Generators(val configDirectory: Path) {
|
class Generators(val configDirectory: Path) {
|
||||||
fun isValid(name: String): Boolean {
|
fun isValid(name: String) = isValid(configDirectory.resolve(name))
|
||||||
val generatorRoot = configDirectory.resolve(name)
|
|
||||||
return Files.isDirectory(generatorRoot) && Files.isRegularFile(generatorRoot.resolve("config.toml"))
|
private fun isValid(path: Path) = Files.isDirectory(path) && Files.isRegularFile(path.resolve("index.js"))
|
||||||
}
|
|
||||||
|
|
||||||
fun findAll(): List<Path> = Files
|
fun findAll(): List<Path> = Files
|
||||||
.list(configDirectory)
|
.list(configDirectory)
|
||||||
.filter { Files.exists(it.resolve("config.toml")) }
|
.filter(::isValid)
|
||||||
.toList()
|
.toList()
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package scaffold
|
||||||
|
|
||||||
|
import com.github.ajalt.clikt.core.UsageError
|
||||||
|
import com.github.ajalt.clikt.output.CliktConsole
|
||||||
|
import com.github.ajalt.clikt.output.TermUi
|
||||||
|
import com.github.ajalt.clikt.output.defaultCliktConsole
|
||||||
|
|
||||||
|
class Prompt(private val console: CliktConsole = defaultCliktConsole()) {
|
||||||
|
|
||||||
|
fun string(text: String, default: String?): String = TermUi.prompt(
|
||||||
|
text = text,
|
||||||
|
default = default,
|
||||||
|
console = console
|
||||||
|
) { it }!!
|
||||||
|
|
||||||
|
fun boolean(text: String, default: Boolean?): Boolean {
|
||||||
|
val (defaultString, suffix) = when (default) {
|
||||||
|
true -> "y" to "[Y/n]"
|
||||||
|
false -> "n" to "[y/N]"
|
||||||
|
null -> null to "[y/n]"
|
||||||
|
}
|
||||||
|
|
||||||
|
return TermUi.prompt(
|
||||||
|
"$text $suffix",
|
||||||
|
default = defaultString,
|
||||||
|
showDefault = false,
|
||||||
|
console = console
|
||||||
|
) {
|
||||||
|
when (it.toLowerCase()) {
|
||||||
|
"y" -> true
|
||||||
|
"n" -> false
|
||||||
|
else -> throw UsageError("Can only be [y/n]")
|
||||||
|
}
|
||||||
|
}!!
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package scaffold
|
|
||||||
|
|
||||||
enum class PromptType { String }
|
|
||||||
|
|
||||||
data class PromptValue(
|
|
||||||
val name: String,
|
|
||||||
val info: String?,
|
|
||||||
val type: PromptType,
|
|
||||||
)
|
|
||||||
+4
-4
@@ -19,11 +19,11 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val scaffoldConfigDirectory = configDirectory.resolve("scaffold")
|
val scaffoldConfigDirectory = configDirectory.resolve("scaffold")
|
||||||
val generatorService = Generators(scaffoldConfigDirectory)
|
val generators = Generators(scaffoldConfigDirectory)
|
||||||
|
|
||||||
Scaffold().subcommands(
|
Scaffold().subcommands(
|
||||||
ListCommand(generatorService),
|
ListCommand(generators),
|
||||||
GenerateCommand(generatorService),
|
GenerateCommand(generators),
|
||||||
NewCommand(generatorService)
|
NewCommand(generators),
|
||||||
).main(args)
|
).main(args)
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package scaffold.commands
|
|||||||
|
|
||||||
import com.github.ajalt.clikt.core.CliktCommand
|
import com.github.ajalt.clikt.core.CliktCommand
|
||||||
import com.github.ajalt.clikt.core.ProgramResult
|
import com.github.ajalt.clikt.core.ProgramResult
|
||||||
|
import com.github.ajalt.clikt.core.UsageError
|
||||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||||
import com.github.ajalt.clikt.parameters.options.convert
|
import com.github.ajalt.clikt.parameters.options.convert
|
||||||
import com.github.ajalt.clikt.parameters.options.option
|
import com.github.ajalt.clikt.parameters.options.option
|
||||||
@@ -11,34 +12,21 @@ import com.mitchellbosecke.pebble.loader.FileLoader
|
|||||||
import com.mitchellbosecke.pebble.template.PebbleTemplate
|
import com.mitchellbosecke.pebble.template.PebbleTemplate
|
||||||
import scaffold.Generator
|
import scaffold.Generator
|
||||||
import scaffold.Generators
|
import scaffold.Generators
|
||||||
import scaffold.PromptType
|
import scaffold.Prompt
|
||||||
import scaffold.loadConfig
|
import scaffold.scripting.ScriptContext
|
||||||
|
import scaffold.scripting.ScriptEngine
|
||||||
import java.io.StringWriter
|
import java.io.StringWriter
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
|
||||||
class GenerateCommand(private val generators: Generators) : CliktCommand("generate") {
|
class GenerateCommand(private val generators: Generators) : CliktCommand("generate") {
|
||||||
private val name by argument(help = "generator name")
|
private val name by argument(help = "generator name")
|
||||||
private val output by option().convert { Path.of(it) }.required()
|
private val outputPathRoot by option("-o", "--output").convert { Path.of(it) }.required()
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
checkPreconditions()
|
checkPreconditions()
|
||||||
|
|
||||||
val generator = Generator(generators.configDirectory, name)
|
val generator = Generator(generators.configDirectory, name)
|
||||||
val config = generator.loadConfig()
|
|
||||||
|
|
||||||
val templateContext = mutableMapOf<String, Any?>()
|
|
||||||
|
|
||||||
for ((path, prompts) in config.prompts) {
|
|
||||||
val map = mutableMapOf<String, Any>()
|
|
||||||
templateContext[path] = map
|
|
||||||
for ((name, info, type) in prompts) {
|
|
||||||
if (type != PromptType.String) TODO()
|
|
||||||
|
|
||||||
val value = prompt(info ?: name)!!
|
|
||||||
map[name] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val pebble = PebbleEngine.Builder()
|
val pebble = PebbleEngine.Builder()
|
||||||
.autoEscaping(false)
|
.autoEscaping(false)
|
||||||
@@ -46,29 +34,36 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera
|
|||||||
.loader(FileLoader().apply { prefix = generator.treeRoot.toString() })
|
.loader(FileLoader().apply { prefix = generator.treeRoot.toString() })
|
||||||
.build()!!
|
.build()!!
|
||||||
|
|
||||||
for (template in config.templates) {
|
val scriptContext = object : ScriptContext {
|
||||||
val renderedTemplate = pebble.getTemplate(template)(templateContext)
|
override fun echo(message: String) = this@GenerateCommand.echo(message)
|
||||||
val outputPath = output.resolve(template)
|
|
||||||
|
|
||||||
|
override val prompt = Prompt()
|
||||||
|
|
||||||
|
override fun render(template: String, ctx: Map<String, Any?>, output: String?) {
|
||||||
|
val renderedTemplate = pebble.getTemplate(template)(ctx)
|
||||||
|
val outputPath = output?.let { outputPathRoot.resolve(it) } ?: outputPathRoot.resolve(template)
|
||||||
Files.createDirectories(outputPath.parent)
|
Files.createDirectories(outputPath.parent)
|
||||||
Files.writeString(outputPath, renderedTemplate)
|
Files.writeString(outputPath, renderedTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (inputFile in config.files) {
|
override fun copy(input: String, output: String?) {
|
||||||
val relativeFile = generator.treeRoot.relativize(inputFile)
|
val inputPath = generator.treeRoot.resolve(input)
|
||||||
val outputFile = output.resolve(relativeFile)
|
val outputPath = output?.let { outputPathRoot.resolve(it) } ?: outputPathRoot.resolve(inputPath)
|
||||||
|
Files.createDirectories(outputPath.parent)
|
||||||
Files.createDirectories(outputFile.parent)
|
Files.copy(inputPath, outputPath)
|
||||||
Files.copy(inputFile, outputFile)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
echo("Generated project in $output")
|
val engine = ScriptEngine(scriptContext)
|
||||||
|
val script = Files.readString(generator.rootPath.resolve("index.js"))
|
||||||
|
|
||||||
|
engine.eval(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkPreconditions() {
|
private fun checkPreconditions() {
|
||||||
if (Files.exists(output)) {
|
if (Files.exists(outputPathRoot)) {
|
||||||
if (Files.list(output).findAny().isPresent) {
|
if (Files.list(outputPathRoot).findAny().isPresent) {
|
||||||
echo("Output path `$output` is not empty")
|
echo("Output path `$outputPathRoot` is not empty")
|
||||||
throw ProgramResult(1)
|
throw ProgramResult(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,15 @@ import com.github.ajalt.clikt.core.CliktCommand
|
|||||||
import scaffold.Generators
|
import scaffold.Generators
|
||||||
|
|
||||||
class ListCommand(private val generators: Generators) : CliktCommand("list") {
|
class ListCommand(private val generators: Generators) : CliktCommand("list") {
|
||||||
override fun run() = echo(generators.findAll().joinToString(" ") { it.last().toString() })
|
override fun run() {
|
||||||
|
val generators = generators.findAll()
|
||||||
|
|
||||||
|
if (generators.isEmpty()) {
|
||||||
|
echo("No generators yet, you can create one with `scaffold new`")
|
||||||
|
} else {
|
||||||
|
generators.forEach {
|
||||||
|
echo(it.last())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,29 +19,15 @@ class NewCommand(private val generators: Generators) : CliktCommand("new") {
|
|||||||
|
|
||||||
Files.createDirectories(root)
|
Files.createDirectories(root)
|
||||||
|
|
||||||
val configFile = """
|
fun resource(path: String) = javaClass.getResource("/$path").readText()
|
||||||
templates = [
|
|
||||||
"README.md"
|
|
||||||
]
|
|
||||||
|
|
||||||
files = [
|
Files.writeString(root.resolve("index.js"), resource("init/index.js"))
|
||||||
".gitignore"
|
Files.writeString(root.resolve("index.d.ts"), resource("init/index.d.ts"))
|
||||||
]
|
|
||||||
|
|
||||||
[prompt]
|
|
||||||
|
|
||||||
[[prompt.project]]
|
|
||||||
type = "String"
|
|
||||||
name = "name"
|
|
||||||
info = "Project name"
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
Files.writeString(root.resolve("config.toml"), configFile)
|
|
||||||
val tree = root.resolve("tree")
|
val tree = root.resolve("tree")
|
||||||
Files.createDirectory(tree)
|
Files.createDirectory(tree)
|
||||||
|
|
||||||
Files.writeString(tree.resolve("README.md"), "# {{ project.name }}")
|
Files.writeString(tree.resolve("README.md"), resource("init/README.md"))
|
||||||
Files.writeString(tree.resolve(".gitignore"), "# https://git-scm.com/docs/gitignore")
|
|
||||||
|
|
||||||
echo("Generator created in $root")
|
echo("Generator created in $root")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package scaffold.scripting
|
||||||
|
|
||||||
|
import org.graalvm.polyglot.Value
|
||||||
|
import org.graalvm.polyglot.proxy.ProxyExecutable
|
||||||
|
import org.graalvm.polyglot.proxy.ProxyObject
|
||||||
|
import scaffold.Prompt
|
||||||
|
|
||||||
|
class PromptProxyAdapter(private val prompt: Prompt) : ProxyObject {
|
||||||
|
|
||||||
|
override fun getMember(key: String?) = when (key) {
|
||||||
|
"string" -> ProxyExecutable { args ->
|
||||||
|
prompt.string(args[0].asString(), args.getOrNull(1)?.asString())
|
||||||
|
}
|
||||||
|
"boolean" -> ProxyExecutable { args ->
|
||||||
|
prompt.boolean(args[0].asString(), args.getOrNull(1)?.asBoolean())
|
||||||
|
}
|
||||||
|
else -> throw UnsupportedOperationException()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getMemberKeys() = arrayOf("string", "boolean")
|
||||||
|
|
||||||
|
override fun hasMember(key: String?): Boolean = key in memberKeys
|
||||||
|
|
||||||
|
override fun putMember(key: String?, value: Value?) = throw UnsupportedOperationException()
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package scaffold.scripting
|
||||||
|
|
||||||
|
import scaffold.Prompt
|
||||||
|
|
||||||
|
interface ScriptContext {
|
||||||
|
fun echo(message: String)
|
||||||
|
|
||||||
|
val prompt: Prompt
|
||||||
|
|
||||||
|
fun render(template: String, ctx: Map<String, Any?>, output: String?)
|
||||||
|
|
||||||
|
fun copy(input: String, output: String?)
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package scaffold.scripting
|
||||||
|
|
||||||
|
import org.graalvm.polyglot.Context
|
||||||
|
import org.graalvm.polyglot.proxy.ProxyExecutable
|
||||||
|
import org.intellij.lang.annotations.Language
|
||||||
|
|
||||||
|
class ScriptEngine(private val scriptContext: ScriptContext) {
|
||||||
|
|
||||||
|
private val context = Context
|
||||||
|
.newBuilder("js")
|
||||||
|
.allowIO(true)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
init {
|
||||||
|
with(context.getBindings("js")) {
|
||||||
|
putMember("echo", ProxyExecutable { (message) ->
|
||||||
|
scriptContext.echo(message.asString())
|
||||||
|
null
|
||||||
|
})
|
||||||
|
|
||||||
|
putMember("prompt", PromptProxyAdapter(scriptContext.prompt))
|
||||||
|
|
||||||
|
putMember("render", ProxyExecutable { args ->
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
scriptContext.render(
|
||||||
|
args[0].asString(),
|
||||||
|
args[1].`as`(Map::class.java) as Map<String, Any?>,
|
||||||
|
args.getOrNull(2)?.asString()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
putMember("copy", ProxyExecutable { args ->
|
||||||
|
scriptContext.copy(args[0].asString(), args.getOrNull(1)?.asString())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun eval(@Language("js") script: CharSequence) {
|
||||||
|
context.eval("js", script)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
|
package scaffold
|
||||||
|
|
||||||
|
import com.github.ajalt.clikt.output.CliktConsole
|
||||||
|
import io.mockk.clearMocks
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
|
import strikt.api.expectThat
|
||||||
|
import strikt.assertions.isEqualTo
|
||||||
|
|
||||||
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
class PromptTest {
|
||||||
|
|
||||||
|
val console = mockk<CliktConsole>()
|
||||||
|
val prompt = Prompt(console)
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun beforeEach() = clearMocks(console)
|
||||||
|
|
||||||
|
@CsvSource(
|
||||||
|
value = [
|
||||||
|
"null, answer, answer",
|
||||||
|
"def, answer, answer",
|
||||||
|
"def, '', def"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
@ParameterizedTest(name = "prompt string({argumentsWithNames})")
|
||||||
|
fun `prompt string`(default: String?, answer: String, expectedValue: String) {
|
||||||
|
every { console.promptForLine(any(), any()) } returns answer
|
||||||
|
expectThat(prompt.string("test", default)).isEqualTo(expectedValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
@CsvSource(
|
||||||
|
value = [
|
||||||
|
"null, y, true",
|
||||||
|
"null, n, false",
|
||||||
|
|
||||||
|
"true, '', true",
|
||||||
|
"true, y, true",
|
||||||
|
"true, n, false",
|
||||||
|
|
||||||
|
"false, '', false",
|
||||||
|
"false, y, true",
|
||||||
|
"false, n, false",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
@ParameterizedTest(name = "prompt boolean({argumentsWithNames})")
|
||||||
|
fun `prompt boolean`(default: Boolean?, answer: String, expectedValue: Boolean) {
|
||||||
|
every { console.promptForLine(any(), any()) } returns answer
|
||||||
|
expectThat(prompt.boolean("test", default)).isEqualTo(expectedValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
|
package scaffold.scripting
|
||||||
|
|
||||||
|
import io.mockk.clearMocks
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
import org.junit.jupiter.api.TestInstance.Lifecycle
|
||||||
|
import scaffold.Prompt
|
||||||
|
|
||||||
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class ScriptEngineTest {
|
||||||
|
|
||||||
|
val scriptContext = mockk<ScriptContext>().also {
|
||||||
|
every { it.prompt } returns Prompt()
|
||||||
|
}
|
||||||
|
|
||||||
|
val scriptEngine = ScriptEngine(scriptContext)
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun beforeEach() = clearMocks(scriptContext)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun echo() {
|
||||||
|
every { scriptContext.echo("test") } returns Unit
|
||||||
|
|
||||||
|
val script = "echo('test')"
|
||||||
|
scriptEngine.eval(script)
|
||||||
|
|
||||||
|
verify { scriptContext.echo(any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun render() {
|
||||||
|
every { scriptContext.render("test", match { it["something"] == "hello" }, null) } returns Unit
|
||||||
|
|
||||||
|
val script = """
|
||||||
|
const ctx = {
|
||||||
|
"something": "hello"
|
||||||
|
}
|
||||||
|
|
||||||
|
render("test", ctx)
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
scriptEngine.eval(script)
|
||||||
|
|
||||||
|
verify { scriptContext.render(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,4 +13,5 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.4.31"))
|
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.4.31"))
|
||||||
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
|
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
|
||||||
|
implementation("com.github.jengelman.gradle.plugins:shadow:6.1.0")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,6 @@ plugins {
|
|||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
maven {
|
|
||||||
url = uri("https://kotlin.bintray.com/kotlinx")
|
|
||||||
// https://github.com/Kotlin/kotlinx.html/issues/173
|
|
||||||
content { includeModule("org.jetbrains.kotlinx", "kotlinx-html-jvm") }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ plugins {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(kotlin("stdlib-jdk8"))
|
implementation(kotlin("stdlib-jdk8"))
|
||||||
implementation(platform(kotlin("bom")))
|
implementation(platform(kotlin("bom")))
|
||||||
testImplementation("io.kotest:kotest-runner-junit5:4.4.1")
|
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
|
||||||
|
testImplementation("io.strikt:strikt-core:0.30.0")
|
||||||
|
testImplementation("io.mockk:mockk:1.10.6")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<Test> {
|
tasks.withType<Test> {
|
||||||
@@ -19,14 +21,10 @@ tasks.withType<KotlinCompile> {
|
|||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = "11"
|
jvmTarget = "11"
|
||||||
javaParameters = true
|
javaParameters = true
|
||||||
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
sourceSets.all {
|
|
||||||
languageSettings.enableLanguageFeature("InlineClasses")
|
|
||||||
}
|
|
||||||
sourceSets["main"].kotlin.setSrcDirs(listOf("src"))
|
sourceSets["main"].kotlin.setSrcDirs(listOf("src"))
|
||||||
sourceSets["test"].kotlin.setSrcDirs(listOf("test"))
|
sourceSets["test"].kotlin.setSrcDirs(listOf("test"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
task("buildNative") {
|
||||||
|
dependsOn("installShadowDist")
|
||||||
|
|
||||||
|
outputs.file("${buildDir}/native/scaffold")
|
||||||
|
|
||||||
|
doLast {
|
||||||
|
val graalvmHome = System.getenv("GRAALVM_HOME") ?: error("GRAALVM_HOME is not set")
|
||||||
|
|
||||||
|
exec {
|
||||||
|
commandLine(
|
||||||
|
"${graalvmHome}/bin/native-image",
|
||||||
|
"--no-fallback",
|
||||||
|
"-R:MaxNewSize=32",
|
||||||
|
"--language:js",
|
||||||
|
"-jar",
|
||||||
|
"${buildDir}/install/app-shadow/lib/scaffold.jar",
|
||||||
|
"${buildDir}/native/scaffold"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
tasks.register<Zip>("release") {
|
||||||
|
dependsOn("buildNative")
|
||||||
|
|
||||||
|
archiveFileName.set("scaffold-${archiveVersion.get()}-linux.zip")
|
||||||
|
destinationDirectory.set(file("$buildDir/release"))
|
||||||
|
from("$buildDir/native/scaffold")
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id("com.github.johnrengelman.shadow")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<ShadowJar> {
|
||||||
|
archiveBaseName.set("scaffold")
|
||||||
|
archiveClassifier.set("")
|
||||||
|
archiveVersion.set("")
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
templates = [
|
|
||||||
"README.md"
|
|
||||||
]
|
|
||||||
|
|
||||||
files = [
|
|
||||||
".gitignore",
|
|
||||||
".gitattributes"
|
|
||||||
]
|
|
||||||
|
|
||||||
[prompt]
|
|
||||||
|
|
||||||
[[prompt.project]]
|
|
||||||
type = "String"
|
|
||||||
name = "name"
|
|
||||||
info = "Project name"
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#
|
|
||||||
# https://help.github.com/articles/dealing-with-line-endings/
|
|
||||||
#
|
|
||||||
# These are explicitly windows files and should use crlf
|
|
||||||
*.bat text eol=crlf
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
.gradle
|
|
||||||
build
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
# {{ project.name }}
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```bash
|
|
||||||
./gradlew run
|
|
||||||
```
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
org.gradle.jvmargs=-Xmx2048M -XX:MaxPermSize=512m -Dfile.encoding=UTF-8
|
||||||
|
org.gradle.caching=true
|
||||||
|
org.gradle.parallel=true
|
||||||
Reference in New Issue
Block a user