From d1b37074a64d67fb65c76498baebb48586e9a243 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:49:27 +0300 Subject: [PATCH] fix: resturcture build pipeline --- .gitattributes | 9 -- .github/workflows/tagged-release.yml | 8 +- .gitignore | 3 +- build.js | 65 ++++++++++ lib/tsconfig.json | 33 +++++ meta.json | 5 + src/me/topchetoeu/jscript/Main.java | 1 + src/me/topchetoeu/jscript/Metadata.java | 7 + .../jscript/polyfills/TypescriptEngine.java- | 122 +++++++++--------- tsconfig.json | 33 ----- 10 files changed, 175 insertions(+), 111 deletions(-) delete mode 100644 .gitattributes create mode 100644 build.js create mode 100644 lib/tsconfig.json create mode 100644 meta.json create mode 100644 src/me/topchetoeu/jscript/Metadata.java delete mode 100644 tsconfig.json diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index c53344c..0000000 --- a/.gitattributes +++ /dev/null @@ -1,9 +0,0 @@ -# -# https://help.github.com/articles/dealing-with-line-endings/ -# -# Linux start script should use lf -/gradlew text eol=lf - -# These are Windows script files and should use crlf -*.bat text eol=crlf - diff --git a/.github/workflows/tagged-release.yml b/.github/workflows/tagged-release.yml index 3a43384..7983a85 100644 --- a/.github/workflows/tagged-release.yml +++ b/.github/workflows/tagged-release.yml @@ -18,13 +18,7 @@ jobs: repository: 'java-jscript' - name: "Build" run: | - cd java-jscript; - ls; - mkdir -p dst/classes; - rsync -av --exclude='*.java' src/ dst/classes; - tsc --outDir dst/classes/me/topchetoeu/jscript/js --declarationDir dst/classes/me/topchetoeu/jscript/dts; - find src -name "*.java" | xargs javac -d dst/classes; - jar -c -f dst/jscript.jar -e me.topchetoeu.jscript.Main -C dst/classes . + cd java-jscript; node ./build.js - uses: "marvinpinto/action-automatic-releases@latest" with: diff --git a/.gitignore b/.gitignore index 9297058..9e46f3f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ out build bin dst -/*.js \ No newline at end of file +/*.js +!/build.js \ No newline at end of file diff --git a/build.js b/build.js new file mode 100644 index 0000000..718963f --- /dev/null +++ b/build.js @@ -0,0 +1,65 @@ +const { spawn } = require('child_process'); +const fs = require('fs/promises'); +const pt = require('path'); +const conf = require('./meta'); +const { argv } = require('process'); + +async function* find(src, dst, wildcard) { + const stat = await fs.stat(src); + + if (stat.isDirectory()) { + for (const el of await fs.readdir(src)) { + for await (const res of find(pt.join(src, el), dst ? pt.join(dst, el) : undefined, wildcard)) yield res; + } + } + else if (stat.isFile() && wildcard(src)) yield dst ? { src, dst } : src; +} +async function copy(src, dst, wildcard) { + const promises = []; + + for await (const el of find(src, dst, wildcard)) { + promises.push((async () => { + await fs.mkdir(pt.dirname(el.dst), { recursive: true }); + await fs.copyFile(el.src, el.dst); + })()); + } + + await Promise.all(promises); +} + +function run(cmd, ...args) { + return new Promise((res, rej) => { + const proc = spawn(cmd, args, { stdio: 'inherit' }); + proc.once('exit', code => { + if (code === 0) res(code); + else rej(new Error(`Process ${cmd} exited with code ${code}.`)); + }); + }) +} + +async function compileJava() { + await fs.writeFile('Metadata.java', (await fs.readFile('src/me/topchetoeu/jscript/Metadata.java')).toString() + .replace('${VERSION}', conf.version) + .replace('${NAME}', conf.name) + .replace('${AUTHOR}', conf.author) + ); + + const args = ['-d', 'dst/classes', 'Metadata.java']; + for await (const path of find('src', undefined, v => v.endsWith('.java') && !v.endsWith('Metadata.java'))) args.push(path); + await run('javac', ...args); + await fs.rm('Metadata.java'); +} + +(async () => { + try { + fs.rm('dst', { recursive: true }); + await copy('src', 'dst/classes', v => !v.endsWith('.java')); + await run('tsc', '-p', 'lib/tsconfig.json', '--outFile', 'dst/classes/me/topchetoeu/jscript/js/core.js'), + await compileJava(); + await run('jar', '-c', '-f', 'dst/jscript.jar', '-e', 'me.topchetoeu.jscript.Main', '-C', 'dst/classes', '.'); + } + catch (e) { + if (argv.includes('debug')) throw e; + else console.log(e.toString()); + } +})(); diff --git a/lib/tsconfig.json b/lib/tsconfig.json new file mode 100644 index 0000000..5172e2d --- /dev/null +++ b/lib/tsconfig.json @@ -0,0 +1,33 @@ +{ + "files": [ + "lib.d.ts", + "modules.ts", + "utils.ts", + "values/object.ts", + "values/symbol.ts", + "values/function.ts", + "values/errors.ts", + "values/string.ts", + "values/number.ts", + "values/boolean.ts", + "values/array.ts", + "promise.ts", + "map.ts", + "set.ts", + "regex.ts", + "core.ts" + ], + "compilerOptions": { + "outFile": "../bin/me/topchetoeu/jscript/js/core.js", + // "declarationDir": "", + // "declarationDir": "bin/me/topchetoeu/jscript/dts", + "target": "ES5", + "lib": [], + "module": "None", + "stripInternal": true, + "downlevelIteration": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + } +} diff --git a/meta.json b/meta.json new file mode 100644 index 0000000..d888310 --- /dev/null +++ b/meta.json @@ -0,0 +1,5 @@ +{ + "version": "0.0.1-alpha", + "name": "java-jscript", + "author": "TopchetoEU" +} \ No newline at end of file diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index 95f00f2..b72f080 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -50,6 +50,7 @@ public class Main { }; public static void main(String args[]) { + System.out.println(String.format("Running %s v%s by %s", Metadata.NAME, Metadata.VERSION, Metadata.AUTHOR)); var in = new BufferedReader(new InputStreamReader(System.in)); engine = new Engine(); var scope = engine.global().globalChild(); diff --git a/src/me/topchetoeu/jscript/Metadata.java b/src/me/topchetoeu/jscript/Metadata.java new file mode 100644 index 0000000..5a0bf0b --- /dev/null +++ b/src/me/topchetoeu/jscript/Metadata.java @@ -0,0 +1,7 @@ +package me.topchetoeu.jscript; + +public class Metadata { + public static final String VERSION = "${VERSION}"; + public static final String AUTHOR = "${AUTHOR}"; + public static final String NAME = "${NAME}"; +} diff --git a/src/me/topchetoeu/jscript/polyfills/TypescriptEngine.java- b/src/me/topchetoeu/jscript/polyfills/TypescriptEngine.java- index 3d379ce..54b677e 100644 --- a/src/me/topchetoeu/jscript/polyfills/TypescriptEngine.java- +++ b/src/me/topchetoeu/jscript/polyfills/TypescriptEngine.java- @@ -1,61 +1,61 @@ -package me.topchetoeu.jscript.polyfills; - -import java.io.File; -import java.util.ArrayList; -import java.util.Map; - -import me.topchetoeu.jscript.engine.scope.GlobalScope; -import me.topchetoeu.jscript.engine.values.ArrayValue; -import me.topchetoeu.jscript.engine.values.FunctionValue; -import me.topchetoeu.jscript.engine.values.NativeFunction; -import me.topchetoeu.jscript.engine.values.Values; - -public class TypescriptEngine extends PolyfillEngine { - private FunctionValue ts; - - @Override - public FunctionValue compile(GlobalScope scope, String filename, String raw) throws InterruptedException { - if (ts != null) { - var res = Values.array(ts.call(context(), null, filename, raw)); - var src = Values.toString(context(), res.get(0)); - var func = Values.function(res.get(1)); - - var compiled = super.compile(scope, filename, src); - - return new NativeFunction(null, (ctx, thisArg, args) -> { - return func.call(context(), null, compiled, thisArg, new ArrayValue(args)); - }); - } - return super.compile(scope, filename, raw); - } - - public TypescriptEngine(File root) { - super(root); - var scope = global().globalChild(); - - var decls = new ArrayList(); - decls.add(resourceToString("dts/core.d.ts")); - decls.add(resourceToString("dts/iterators.d.ts")); - decls.add(resourceToString("dts/map.d.ts")); - decls.add(resourceToString("dts/promise.d.ts")); - decls.add(resourceToString("dts/regex.d.ts")); - decls.add(resourceToString("dts/require.d.ts")); - decls.add(resourceToString("dts/set.d.ts")); - decls.add(resourceToString("dts/values/array.d.ts")); - decls.add(resourceToString("dts/values/boolean.d.ts")); - decls.add(resourceToString("dts/values/number.d.ts")); - decls.add(resourceToString("dts/values/errors.d.ts")); - decls.add(resourceToString("dts/values/function.d.ts")); - decls.add(resourceToString("dts/values/object.d.ts")); - decls.add(resourceToString("dts/values/string.d.ts")); - decls.add(resourceToString("dts/values/symbol.d.ts")); - - scope.define("libs", true, ArrayValue.of(decls)); - scope.define(true, new NativeFunction("init", (el, t, args) -> { - ts = Values.function(args[0]); - return null; - })); - - pushMsg(false, scope, Map.of(), "bootstrap.js", resourceToString("js/bootstrap.js"), null); - } -} +package me.topchetoeu.jscript.polyfills; + +import java.io.File; +import java.util.ArrayList; +import java.util.Map; + +import me.topchetoeu.jscript.engine.scope.GlobalScope; +import me.topchetoeu.jscript.engine.values.ArrayValue; +import me.topchetoeu.jscript.engine.values.FunctionValue; +import me.topchetoeu.jscript.engine.values.NativeFunction; +import me.topchetoeu.jscript.engine.values.Values; + +public class TypescriptEngine extends PolyfillEngine { + private FunctionValue ts; + + @Override + public FunctionValue compile(GlobalScope scope, String filename, String raw) throws InterruptedException { + if (ts != null) { + var res = Values.array(ts.call(context(), null, filename, raw)); + var src = Values.toString(context(), res.get(0)); + var func = Values.function(res.get(1)); + + var compiled = super.compile(scope, filename, src); + + return new NativeFunction(null, (ctx, thisArg, args) -> { + return func.call(context(), null, compiled, thisArg, new ArrayValue(args)); + }); + } + return super.compile(scope, filename, raw); + } + + public TypescriptEngine(File root) { + super(root); + var scope = global().globalChild(); + + var decls = new ArrayList(); + decls.add(resourceToString("dts/core.d.ts")); + decls.add(resourceToString("dts/iterators.d.ts")); + decls.add(resourceToString("dts/map.d.ts")); + decls.add(resourceToString("dts/promise.d.ts")); + decls.add(resourceToString("dts/regex.d.ts")); + decls.add(resourceToString("dts/require.d.ts")); + decls.add(resourceToString("dts/set.d.ts")); + decls.add(resourceToString("dts/values/array.d.ts")); + decls.add(resourceToString("dts/values/boolean.d.ts")); + decls.add(resourceToString("dts/values/number.d.ts")); + decls.add(resourceToString("dts/values/errors.d.ts")); + decls.add(resourceToString("dts/values/function.d.ts")); + decls.add(resourceToString("dts/values/object.d.ts")); + decls.add(resourceToString("dts/values/string.d.ts")); + decls.add(resourceToString("dts/values/symbol.d.ts")); + + scope.define("libs", true, ArrayValue.of(decls)); + scope.define(true, new NativeFunction("init", (el, t, args) -> { + ts = Values.function(args[0]); + return null; + })); + + pushMsg(false, scope, Map.of(), "bootstrap.js", resourceToString("js/bootstrap.js"), null); + } +} diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 716b087..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "files": [ - "lib/lib.d.ts", - "lib/modules.ts", - "lib/utils.ts", - "lib/values/object.ts", - "lib/values/symbol.ts", - "lib/values/function.ts", - "lib/values/errors.ts", - "lib/values/string.ts", - "lib/values/number.ts", - "lib/values/boolean.ts", - "lib/values/array.ts", - "lib/promise.ts", - "lib/map.ts", - "lib/set.ts", - "lib/regex.ts", - "lib/core.ts" - ], - "compilerOptions": { - "outFile": "bin/me/topchetoeu/jscript/js/core.js", - // "declarationDir": "", - // "declarationDir": "bin/me/topchetoeu/jscript/dts", - "target": "ES5", - "lib": [], - "module": "None", - "stripInternal": true, - "downlevelIteration": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - } -}