Skip to content

Commit

Permalink
use json5 for user input
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Jan 25, 2021
1 parent 2df2086 commit 4bafb12
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .run/CLI build.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration default="false" name="CLI compile fabricPack" type="JetRunConfigurationType">
<module name="voodoo-parent.voodoo.main" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="--log TRACE compile fabricPack/152Version.voodoo.json fabricPack/164Version.voodoo.json" />
<option name="PROGRAM_PARAMETERS" value="--log TRACE compile fabricPack/152Version.voodoo.json5 fabricPack/164Version.voodoo.json5" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ java -jar voodoo.jar init project
./voodoo init pack --id newPack --mcVersion 1.16.2

# builds a pack
./voodoo compile newPack/v0.0.1.voodoo.json
./voodoo compile newPack/v0.0.1.voodoo.json5

# packages for upload
./voodoo package --id mypack -p voodoo -p mmc-voodoo -p curse
Expand Down Expand Up @@ -135,10 +135,9 @@ Developing
Support
-------

Feel welcome to post ideas and suggestions to our [tracker](https://github.com/DaemonicLabs/Voodoo/issues).
Feel welcome to post ideas and suggestions to our [issue tracker](https://github.com/DaemonicLabs/Voodoo/issues).

contact me directly in chat [![Discord](https://img.shields.io/discord/342696338556977153.svg?style=flat-square&label=%23ai-lab&logo=discord)](https://discord.gg/SRFkHfp)
or on irc `#unascribed` @ `irc.esper.net`
contact me directly in chat [![Discord](https://img.shields.io/discord/342696338556977153.svg?style=flat-square&label=%23ai-lab&logo=discord)](https://discord.gg/SRFkHfp)

How to contribute?
------------------
Expand Down
7 changes: 3 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ java -jar voodoo.jar init project
./voodoo init pack --id newPack --mcVersion 1.16.2

# builds a pack
./voodoo compile newPack/v0.0.1.voodoo.json
./voodoo compile newPack/v0.0.1.voodoo.json5

# packages for upload
./voodoo package --id mypack -p voodoo -p mmc-voodoo -p curse
Expand Down Expand Up @@ -122,10 +122,9 @@ Developing
Support
=======

Feel welcome to post ideas and suggestions to our [tracker](https://github.com/DaemonicLabs/Voodoo/issues).
Feel welcome to post ideas and suggestions to our [issue tracker](https://github.com/DaemonicLabs/Voodoo/issues).

contact me directly in chat [![Discord](https://img.shields.io/discord/342696338556977153.svg?style=flat-square&label=%23ai-lab&logo=discord)](https://discord.gg/SRFkHfp)
or on irc `#unascribed` @ `irc.esper.net`
contact me directly in chat [![Discord](https://img.shields.io/discord/342696338556977153.svg?style=flat-square&label=%23ai-lab&logo=discord)](https://discord.gg/SRFkHfp)

How to contribute?
==================
Expand Down
28 changes: 14 additions & 14 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ run the `generateSchema` command once to make it generate a `config.json` and `.
./voodoo generateSchema
```

now you should see `config.json`
now you should see `config.json5`

`/config.json`
```json
`/config.json5`
```json5
{
"$schema": "./schema/config.schema.json",
"curseforgeGenerators": {
Expand Down Expand Up @@ -73,8 +73,8 @@ so `init` created a gitignore file

we are interested in fabric and fabric mods on curseforge

`/config.json`
```json
`/config.json5`
```json5
{
"$schema": "./schema/config.schema.json",
"curseforgeGenerators": {
Expand Down Expand Up @@ -132,8 +132,8 @@ try to keep them separate when maintaining multiple versions

by default the modloader is set to `None`

??? note "`/magicpack/v0.0.1.voodoo.json`"
```json hl_lines="6 7 8"
??? note "`/magicpack/v0.0.1.voodoo.json5`"
```json5 hl_lines="6 7 8"
{
"$schema": "../schema/versionPack.schema.json",
"version": "0.0.1",
Expand All @@ -149,8 +149,8 @@ by default the modloader is set to `None`

lets replace with with fabric

!!! note "`/magicpack/v0.0.1.voodoo.json`"
```json hl_lines="6 7 8 9"
!!! note "`/magicpack/v0.0.1.voodoo.json5`"
```json5 hl_lines="6 7 8 9"
{
"$schema": "../schema/versionPack.schema.json",
"version": "0.0.1",
Expand Down Expand Up @@ -202,8 +202,8 @@ there is multiple entry types that you can
since we setup mods from curseforge with the name `Fabric` before, we can use them
so lets add some mods

???+ note "`/magicpack/v0.0.1.voodoo.json`"
```json hl_lines="10 11 12 13 14 15 16 17 18 19"
???+ note "`/magicpack/v0.0.1.voodoo.json5`"
```json5 hl_lines="10 11 12 13 14 15 16 17 18 19"
{
"$schema": "../schema/versionPack.schema.json",
"version": "0.0.1",
Expand Down Expand Up @@ -233,7 +233,7 @@ so lets add some mods
`compiling` the pack does resolve all dependencies of entries and pins versions

```bash
voodoo compile magicpack/v0.0.1.voodoo.json
voodoo compile magicpack/v0.0.1.voodoo.json5
```

this created `/magicpack/lock/` folder containing the output artifacts
Expand All @@ -247,13 +247,13 @@ before deploying the pack, lets make sure it runs first
before this make sure multimc is in your `PATH`

```bash
voodoo launch multimc magicpack/v0.0.1.voodoo.json
voodoo launch multimc magicpack/v0.0.1.voodoo.json5
```

## Packaging and Upload

```bash
voodoo package magicpack/modpack/meta.json voodoo mmc-voodoo server
voodoo package magicpack/modpack/meta.json5 voodoo mmc-voodoo server
```

this created `/_upload/voodoo/`, `/_upload/multimc-voodoo` and `/_upload/server/`
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"curse=Fabric16/fabric-api",
"curse=Fabric16/betternether",
"curse=Fabric16/betternether",
// comment
{
"type": "curse",
"projectName": "Fabric16/tab-inventory-fabric",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions util/src/main/kotlin/voodoo/util/JsonExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ val json = Json {
prettyPrint = true
encodeDefaults = false
}
val lenientJson = Json {
prettyPrint = true
encodeDefaults = false
isLenient = true
// allowSpecialFloatingPointValues = true

}

inline fun <reified T : Any> T.toJson(serializer: SerializationStrategy<T>): String = json.encodeToString(serializer, this)
2 changes: 1 addition & 1 deletion util/src/main/kotlin/voodoo/util/ZipExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream


val logger = KotlinLogging.logger{}
private val logger = KotlinLogging.logger {}

fun packToZip(sourceDir: File, zipFile: File, preserveTimestamps: Boolean = true) {
zipFile.let { if (it.exists()) it.delete() }
Expand Down
2 changes: 2 additions & 0 deletions versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ version.logback-classic=1.2.3
version.org.junit.jupiter..junit-jupiter-api=5.7.0

version.org.junit.jupiter..junit-jupiter-engine=5.7.0

version.blue.endless..jankson=1.2.0
2 changes: 2 additions & 0 deletions voodoo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies {
api(project(":pack"))
api(project(":pack:pack-tester"))

implementation("blue.endless:jankson:_")

// implementation(KotlinX.coroutines.debug)

implementation(group = "ch.qos.logback", name = "logback-classic", version = "_") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class EvalScriptCommand(
val rootDir by requireObject<File>()
val scriptFile by argument(
"script",
"pack .voodoo.json file"
"pack .voodoo.json5 file"
).file(mustExist = true, canBeFile = true, canBeDir = false, mustBeReadable = true, mustBeWritable = false, canBeSymlink = false)

val outputFile by argument(
"output",
"output pack .voodoo.json file"
"output pack .voodoo.json5 file"
).file(mustExist = false, canBeFile = true, canBeDir = false, mustBeReadable = false, mustBeWritable = true, canBeSymlink = false)
.validate { file ->
require(file.name.endsWith(".voodoo.json")) { "output file must be of type '.voodoo.json'" }
Expand Down
5 changes: 2 additions & 3 deletions voodoo/src/main/kotlin/voodoo/cli/CompileCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CompileCommand() : CliktCommand(

val packFiles by argument(
"PACK_FILE",
"path to .voodoo.json file"
"path to .${VersionPack.extension} file"
).file(mustExist = true, canBeFile = true, canBeDir = false)
.multiple()
.validate { files ->
Expand All @@ -55,10 +55,9 @@ class CompileCommand() : CliktCommand(
val packs: Map<Pair<String, MetaPack>, List<VersionPack>> = packFiles.map { packFile ->
val baseDir = rootDir.resolve(packFile.absoluteFile.parentFile)
val id = baseDir.name
val metaPackFile = baseDir.resolve(MetaPack.FILENAME)
val versionPack = VersionPack.parse(packFile = packFile)

val metaPack = json.decodeFromString(MetaPack.serializer(), metaPackFile.readText())
val metaPack = MetaPack.parse(baseDir.resolve(MetaPack.FILENAME))
Triple(id, metaPack, versionPack)
}
.groupBy(
Expand Down
2 changes: 1 addition & 1 deletion voodoo/src/main/kotlin/voodoo/cli/GenerateSchemaCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GenerateSchemaCommand : CliktCommand(
writeText(json.encodeToSchema(MetaPack.serializer()))
}

Autocompletions.generate(configFile = configFile)
Autocompletions.generate(config = config)


// rootDir.resolve("packIdPlaceholder").resolve(Modpack.defaultSchema).apply {
Expand Down
11 changes: 1 addition & 10 deletions voodoo/src/main/kotlin/voodoo/cli/PackageCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ class PackageCommand(): CliktCommand(
}
}

// val id by option(
// "--id",
// help = "pack id"
// ).required()
// .validate {
// require(it.isNotBlank()) { "id must not be blank" }
// require(it.matches("""[\w_]+""".toRegex())) { "modpack id must not contain special characters" }
// }

val packTargets by argument(
"TARGET", "pack targets"
).choice(Pack.packMap)
Expand All @@ -73,7 +64,7 @@ class PackageCommand(): CliktCommand(
val id = baseDir.name

stopwatch {
val metaPack = json.decodeFromString(MetaPack.serializer(), metaPackFile.readText())
val metaPack = MetaPack.parse(metaPackFile)
val uploadDir = uploadDirOption ?: SharedFolders.UploadDir.get(id)

packTargets.toSet().forEach { packTarget ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LaunchMultiMCCommand(): CliktCommand(

val packFile by argument(
"PACK_FILE",
"path to .voodoo.json file"
"path to .${VersionPack.extension} file"
).file(mustExist = true, canBeFile = true, canBeDir = false)
.validate { file ->
require(file.name.endsWith("." + VersionPack.extension)) {
Expand Down
20 changes: 2 additions & 18 deletions voodoo/src/main/kotlin/voodoo/config/Autocompletions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package voodoo.config

import com.github.ricky12awesome.jss.encodeToSchema
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -19,9 +18,6 @@ object Autocompletions {
private val rootDir by lazy {
SharedFolders.RootDir.get().absoluteFile
}
private val configFile by lazy {
rootDir.resolve("config.json")
}
private val cacheDir by lazy {
rootDir.resolve(".completions").apply { mkdirs() }
}
Expand All @@ -34,12 +30,7 @@ object Autocompletions {
private val fabricLoadersFile: File = cacheDir.resolve("fabric_loaders.json")

private val config by lazy {
if (configFile.exists()) {
json.decodeFromString(
Configuration.serializer(),
configFile.readText()
)
} else {
Configuration.parseOrDefault(rootDir) {
Configuration()
}
}
Expand Down Expand Up @@ -140,17 +131,10 @@ object Autocompletions {
}


suspend fun generate(configFile: File) {
suspend fun generate(config: Configuration) {

logger.info { "generating autocompletions into $cacheDir" }

val config = if (configFile.exists()) {
json.decodeFromString(
Configuration.serializer(),
configFile.readText()
)
} else Configuration()

coroutineScope {
launch(MDCContext()) {
generateCurse(config.curseforgeGenerators)
Expand Down
31 changes: 26 additions & 5 deletions voodoo/src/main/kotlin/voodoo/config/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package voodoo.config

import blue.endless.jankson.Jankson
import com.github.ricky12awesome.jss.JsonSchema
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import mu.KotlinLogging
import voodoo.pack.EntryOverride
import voodoo.util.json
import java.io.File
Expand All @@ -24,17 +26,36 @@ data class Configuration(
val overrides: Map<String, EntryOverride> = mapOf()
) {
companion object {
private val logger = KotlinLogging.logger {}
const val defaultSchema = "./schema/config.schema.json"
private const val CONFIG_PATH = "config.json"
private const val CONFIG_PATH = "config.json5"
fun parse(rootDir: File): Configuration {
val configFile = rootDir.resolve(CONFIG_PATH)
return json.decodeFromString(serializer(), configFile.readText())

val cleanedString = Jankson
.builder()
.build()
.load(configFile.readText()).let { jsonObject ->
jsonObject.toJson(false, true);
}

return json.decodeFromString(serializer(), cleanedString)
}
fun parseOrElse(rootDir: File, generateDefault: () -> Configuration = { Configuration() }): Configuration {
fun parseOrDefault(rootDir: File, generateDefault: () -> Configuration = { Configuration() }): Configuration {
val configFile = rootDir.resolve(CONFIG_PATH)
return if(configFile.exists()) {
json.decodeFromString(serializer(), configFile.readText())
} else generateDefault()
val cleanedString = Jankson
.builder()
.build()
.load(configFile.readText()).let { jsonObject ->
jsonObject.toJson(false, true);
}

json.decodeFromString(serializer(), cleanedString)
} else {
logger.trace { "generate default configuration" }
generateDefault()
}
}
fun save(rootDir: File, configuration: Configuration) {
val configFile = rootDir.resolve(CONFIG_PATH)
Expand Down
3 changes: 3 additions & 0 deletions voodoo/src/main/kotlin/voodoo/config/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package voodoo.config
import com.github.ricky12awesome.jss.buildJsonSchema
import com.github.ricky12awesome.jss.encodeToSchema
import kotlinx.serialization.json.*
import mu.KotlinLogging
import voodoo.data.nested.NestedPack
import voodoo.pack.FileEntry
import voodoo.pack.VersionPack
import voodoo.util.json

private val logger = KotlinLogging.logger {}

fun NestedPack.Companion.generateSchema() = json.encodeToSchema(serializer())
.replace("\"replace_with_curseforge_projects\"",
Autocompletions.curseforge.keys.joinToString(",") { "\"$it\"" }
Expand Down
Loading

0 comments on commit 4bafb12

Please sign in to comment.