bridge-core / editor

Next generation of bridge., the Minecraft Add-On editor
https://editor.bridge-core.app
GNU General Public License v3.0
133 stars 41 forks source link

Generator scripts #132

Open solvedDev opened 2 years ago

solvedDev commented 2 years ago

Generate sets of items, entities or blocks with JavaScript

solvedDev commented 2 years ago

Concept:

Allow any file to be written as a JavaScript/TypeScript file which returns the file's content or a collection of files.

Generate a single file

If the generator script just returns a single file, the output will be written to the file name of the generator script: myGenerator.js-> myGenerator.json

For some files, we will provide helper methods to make it more convenient to build the JSON. It should also be possible to import and use custom components where applicable.

import { defineEntity } from "@bridge/generate"
import { Pathfinder } from "../components/Pathfinder.js"

export default defineEntity(({ description, components, use }) => {
  // The exact shape of these helper functions still needs to be discussed.
  // Generally supporting this way of writing an entity/item or block is necessary
  // to make custom components work flawlessly
  description({...})
  use(Pathfinder)

  components({...})
})

The usage of the helper methods is not required; it's also possible to just return raw JSON.

export default {
  "minecraft:entity": {...}
}

This also works for mcfunction files:

export default `
/say Hello World!
/tag @s init
`

Generate multiple files

To get more control over the output of your generator script, export a file collection

import { FileCollection, defineEntity } from "@bridge/generate"

const files = new FileCollection()
files.add('dummy/file_1.json', defineEntity(...))
files.add('dummy/file_2.json', defineEntity(...))

export default files

Auto-completions & Developer experience

We can provide auto-completions for the helper functions using #463. Additionally, it's worth considering whether to add a "template" API

import { useTemplate } from "@bridge/generate"

// The useTemplate function returns the file contents and omits the template file from the compilation output
// Function signature: useTemplate<T=any>(templatePath: string, omitFromOutput=true): Promise<T>
const template = await useTemplate('./myEntity.json')

return template
solvedDev commented 2 years ago

Edit: Done

Dash cache file should store files generated by script and unlink them when... - Generator script is unlinked - Before running script again