Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the template generator via a REST api #80

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"remoteUser": "node",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, I changed the Dev Container to use the node user instead of root as it messes with the file permission on Linux host.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I thought it was to solve the warning that jekyll prints :) Solved 2 things at once 👍

"build": { "dockerfile": "../Dockerfile" },
"forwardPorts": [
4000,
Expand Down
31 changes: 27 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ jobs:
env:
ImageOS: ubuntu22
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
# Maintain timestamps of previously published files
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
ref: static
path: _site
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
- run: rm -rf ./_site/cli && cp ./cli/bundled.ts ./_site/cli
# Upload the static content as a workflow artifact when not publishing the site (non-main branches)
- name: Archive site
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: site
path: _site
Expand All @@ -49,7 +49,7 @@ jobs:
runs-on: ubuntu-22.04
if: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }}
steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: site
path: _site
Expand All @@ -61,3 +61,26 @@ jobs:
BRANCH: static
FOLDER: _site
CLEAN: true

build-api:
runs-on: ubuntu-22.04
env:
ImageOS: ubuntu22
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm i
working-directory: ./scripts
- run: npm i
working-directory: ./template-api
- run: npm run buildLib
working-directory: ./scripts
- run: npm run build
working-directory: ./template-api
- uses: cloudflare/wrangler-action@v3
if: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }}
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
workingDirectory: "template-api"
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
FROM node:18-bookworm

ENV DEBIAN_FRONTEND=noninteractive
ENV PATH="${PATH}:$HOME/gems/bin"
ENV GEM_HOME="$HOME/gems"

RUN apt update && apt install -y ruby-full build-essential zlib1g-dev

USER node
ENV HOME="/home/node"

ENV PATH="${PATH}:$HOME/gems/bin"
ENV GEM_HOME="$HOME/gems"
RUN gem install jekyll bundler

ENV DENO_INSTALL="$HOME/.deno"
Expand Down
40 changes: 20 additions & 20 deletions scripts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"vite": "^4.3.9"
},
"dependencies": {
"eta": "^3.0.3",
"eta": "^3.4.0",
"file-saver": "^2.0.5",
"jszip": "^3.10.1",
"vite-plugin-dts": "^2.3.0"
Expand Down
10 changes: 3 additions & 7 deletions scripts/src/lib/template/eta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Eta } from "eta"
import { Eta } from "eta";

const eta = new Eta({
export const eta = new Eta({
autoTrim: false
})

export function renderTemplate(template: string, options: object): string {
return eta.renderString(template, options);
}
});
10 changes: 5 additions & 5 deletions scripts/src/lib/template/gradlegroovy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { ComputedConfiguration, TemplateWriter } from './template';
import { renderTemplate } from './eta';

import gradlePropertiesTemplate from './templates/gradle/groovy/gradle.properties.eta?raw';
import buildGradleTemplate from './templates/gradle/groovy/build.gradle.eta?raw';
import gradlePropertiesTemplate from './templates/gradle/groovy/gradle.properties.eta';
import buildGradleTemplate from './templates/gradle/groovy/build.gradle.eta';
import settingsGradle from './templates/gradle/groovy/settings.gradle?raw';
import { getJavaVersion } from './java';
import { eta } from './eta';

export async function addGroovyGradle(writer: TemplateWriter, config: ComputedConfiguration) {
await writer.write('gradle.properties', renderTemplate(gradlePropertiesTemplate, config));
await writer.write('build.gradle', renderTemplate(buildGradleTemplate, {...config, java: getJavaVersion(config.minecraftVersion)}));
await writer.write('gradle.properties', eta.render(gradlePropertiesTemplate, config));
await writer.write('build.gradle', eta.render(buildGradleTemplate, {...config, java: getJavaVersion(config.minecraftVersion)}));
await writer.write('settings.gradle', settingsGradle);
}
10 changes: 5 additions & 5 deletions scripts/src/lib/template/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { renderTemplate } from "./eta";
import type { ComputedConfiguration, TemplateWriter } from "./template";

import mixinTemplate from './templates/mixin/Mixin.java.eta?raw';
import mixinClientTemplate from './templates/mixin/ClientMixin.java.eta?raw';
import mixinTemplate from './templates/mixin/Mixin.java.eta';
import mixinClientTemplate from './templates/mixin/ClientMixin.java.eta';
import { getJavaVersion } from "./java";
import { eta } from "./eta";

export async function generateMixin(writer: TemplateWriter, options: ComputedConfiguration) : Promise<unknown[]> {
const packageName = options.packageName + ".mixin";
Expand All @@ -23,7 +23,7 @@ export async function generateMixin(writer: TemplateWriter, options: ComputedCon

const mixinJsonName = `${options.modid}.mixins.json`;
await writer.write(`src/main/resources/${mixinJsonName}`, JSON.stringify(mixinJson, null, "\t"));
await writer.write(`src/main/java/${packageName.replaceAll("\.", "/")}/${className}.java`, renderTemplate(mixinTemplate, {
await writer.write(`src/main/java/${packageName.replaceAll("\.", "/")}/${className}.java`, eta.render(mixinTemplate, {
className,
packageName
}));
Expand All @@ -49,7 +49,7 @@ export async function generateClientMixin(writer: TemplateWriter, options: Compu

const mixinJsonName = `${options.modid}.client.mixins.json`;
await writer.write(`src/client/resources/${mixinJsonName}`, JSON.stringify(mixinJson, null, "\t"));
await writer.write(`src/client/java/${packageName.replaceAll("\.", "/")}/${className}.java`, renderTemplate(mixinClientTemplate, {
await writer.write(`src/client/java/${packageName.replaceAll("\.", "/")}/${className}.java`, eta.render(mixinClientTemplate, {
className,
packageName
}));
Expand Down
26 changes: 13 additions & 13 deletions scripts/src/lib/template/modentrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { renderTemplate } from "./eta";
import type { ComputedConfiguration, TemplateWriter } from "./template";

import javaEntrypointTemplate from './templates/entrypoint/Entrypoint.java.eta?raw';
import kotlinEntrypointTemplate from './templates/entrypoint/Entrypoint.kt.eta?raw';
import javaEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.java.eta?raw';
import kotlinEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.kt.eta?raw';
import javaEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.java.eta?raw';
import kotlinEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.kt.eta?raw';
import javaEntrypointTemplate from './templates/entrypoint/Entrypoint.java.eta';
import kotlinEntrypointTemplate from './templates/entrypoint/Entrypoint.kt.eta';
import javaEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.java.eta';
import kotlinEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.kt.eta';
import javaEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.java.eta';
import kotlinEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.kt.eta';
import { getMinorMinecraftVersion } from "./minecraft";
import { eta } from "./eta";

interface ClassOptions {
package: string, // com.example
Expand Down Expand Up @@ -55,10 +55,10 @@ async function generateJavaEntrypoint(writer: TemplateWriter, options: ClassOpti
]
};

await writer.write(`src/main/java/${options.path}.java`, renderTemplate(javaEntrypointTemplate, options))
await writer.write(`src/main/java/${options.path}.java`, eta.render(javaEntrypointTemplate, options))

if (options.clientEntrypoint) {
await writer.write(`src/client/java/${options.path}Client.java`, renderTemplate(javaEntrypointClientTemplate, {...options, className: options.className + "Client"}));
await writer.write(`src/client/java/${options.path}Client.java`, eta.render(javaEntrypointClientTemplate, {...options, className: options.className + "Client"}));

entrypoints = {
...entrypoints,
Expand All @@ -69,7 +69,7 @@ async function generateJavaEntrypoint(writer: TemplateWriter, options: ClassOpti
}

if (options.dataEntrypoint) {
await writer.write(`src/main/java/${options.path}DataGenerator.java`, renderTemplate(javaEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}));
await writer.write(`src/main/java/${options.path}DataGenerator.java`, eta.render(javaEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}));

entrypoints = {
...entrypoints,
Expand All @@ -92,10 +92,10 @@ async function generateKotlinEntrypoint(writer: TemplateWriter, options: ClassOp
]
};

await writer.write(`src/main/kotlin/${options.path}.kt`, renderTemplate(kotlinEntrypointTemplate, options))
await writer.write(`src/main/kotlin/${options.path}.kt`, eta.render(kotlinEntrypointTemplate, options))

if (options.clientEntrypoint) {
await writer.write(`src/client/kotlin/${options.path}Client.kt`, renderTemplate(kotlinEntrypointClientTemplate, {...options, className: options.className + "Client"}))
await writer.write(`src/client/kotlin/${options.path}Client.kt`, eta.render(kotlinEntrypointClientTemplate, {...options, className: options.className + "Client"}))

entrypoints = {
...entrypoints,
Expand All @@ -109,7 +109,7 @@ async function generateKotlinEntrypoint(writer: TemplateWriter, options: ClassOp
}

if (options.dataEntrypoint) {
await writer.write(`src/main/kotlin/${options.path}DataGenerator.kt`, renderTemplate(kotlinEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}))
await writer.write(`src/main/kotlin/${options.path}DataGenerator.kt`, eta.render(kotlinEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}))

entrypoints = {
...entrypoints,
Expand Down
2 changes: 1 addition & 1 deletion scripts/src/lib/template/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function decode64(base64: string): ArrayBufferLike {
var binary_string = window.atob(base64);
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
Expand Down
5 changes: 5 additions & 0 deletions scripts/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

declare module "*.eta" {
const template: import("eta/dist/types/compile").TemplateFunction;
export default template;
}
38 changes: 32 additions & 6 deletions scripts/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import dts from 'vite-plugin-dts'
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import dts from 'vite-plugin-dts';
import { Eta } from 'eta';

const buildLib = process.env.BUILD_LIB;

/**
* @returns {import('vite').Plugin}
*/
function eta() {
const eta = new Eta({
autoTrim: false
});

return {
transform(template, id) {
if (id.match(/\.eta$/)) return {
code: `
export default ${eta.compile(template)};
`
};
}
};
}

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => buildLib ? ({
// Library for CLI
plugins: [dts({ rollupTypes: true })],
plugins: [
eta(),
dts({ rollupTypes: true })
],
build: {
sourcemap: false,
minify: false,
minify: true,
outDir: "./dist",
emptyOutDir: true,
lib: {
Expand All @@ -22,7 +45,10 @@ export default defineConfig(({ mode }) => buildLib ? ({
}
}) : ({
// Web build
plugins: [svelte()],
plugins: [
eta(),
svelte()
],
build: {
sourcemap: mode === "development",
// Build directly into the Jekyll output directory
Expand Down
12 changes: 12 additions & 0 deletions template-api/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
Loading
Loading