Skip to content

Commit

Permalink
feat: add compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
archersado committed Aug 14, 2024
1 parent 29cd5e0 commit 8bfc58d
Show file tree
Hide file tree
Showing 24 changed files with 964 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
lib/
examples/
44 changes: 44 additions & 0 deletions packages/compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# `@hlang-org/compiler`

A runtime compiler for hlang to build the flow graph data struction to executable code,

## Features

- **Optimized Compilation**: Advanced optimization techniques ensure that your code runs efficiently.
- **Cross-Platform Support**: Compile your code for various platforms including desktop, browser and server enviroment.


## Installation
```
npm i @hlang-org/compiler
```

## Usage

```typescript
const compiler = require('@hlang-org/compiler');
const graph = {
graphInfo: {
connections: [{"Input": {"name": "自定义函数", "type": "T", "nodeId": "spider-43039140-7b33-4252-a0f0-29e35defff4b", "portConf": {"id": "Input", "type": "Input"}}, "Output": {"name": "http 请求", "type": "R", "nodeId": "spider-68a448a7-b6f7-46c7-8639-e37ed2c27f47", "portConf": {"id": "Output", "type": "Output"}}}, {"Input": {"name": "http", "type": "T", "nodeId": "spider-1bc19ee8-a5bd-4175-bc91-721d61324eed", "portConf": {"id": "Input", "type": "Input"}}, "Output": {"name": "自定义函数", "type": "T", "nodeId": "spider-43039140-7b33-4252-a0f0-29e35defff4b", "portConf": {"id": "Output", "type": "Output"}}}, {"Input": {"name": "自定义函数", "type": "T", "nodeId": "spider-e62d22a2-478d-4f32-8117-6f782a7ccec0", "portConf": {"id": "Input", "type": "Input"}}, "Output": {"name": "http", "type": "T", "nodeId": "spider-1bc19ee8-a5bd-4175-bc91-721d61324eed", "portConf": {"id": "Output", "type": "Output"}}}],
nodeList: [{"id": 2, "code": "advanced-function", "icon": "", "name": "自定义函数", "type": "T", "nodeId": "spider-e62d22a2-478d-4f32-8117-6f782a7ccec0", "bizData": {"flowId": "1", "nodeId": "spider-e62d22a2-478d-4f32-8117-6f782a7ccec0", "params": {"hackSend": false, "scriptCode": "// Type some code...\ninput.ctx.body = {\n success: true,\n data: input.data\n}\n\nlogger(input.ctx.body, input.ctx)\n\n"}}, "version": 1, "portInfo": {"Input": [{"id": "Input", "dto": {"name": "DTO_Input", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 2, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 3, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "输入", "name": "输入"}], "Output": [{"id": "Output", "dto": {"name": "DTO_Output", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 7, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 8, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "输出", "name": "输出"}]}, "position": {"x": 1202, "y": 269.578125}, "gmtCreate": "2024-05-27T10:32:16.839Z", "groupCode": "advanced", "description": "", "gmtModified": "2024-05-27T10:32:16.839Z", "editorResource": {"styleHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-advanced-function/6/editor.css", "scriptHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-advanced-function/6/editor.js"}, "runtimeResource": "@hset/xlang-advanced-function@latest"}, {"id": 1, "code": "trigger-http-request", "icon": "", "name": "http 请求", "type": "R", "nodeId": "spider-68a448a7-b6f7-46c7-8639-e37ed2c27f47", "bizData": {"flowId": "1", "nodeId": "spider-68a448a7-b6f7-46c7-8639-e37ed2c27f47", "params": {"url": "test", "method": "GET"}}, "version": 1, "portInfo": {"Output": [{"id": "Output", "desc": "输出", "name": "输出"}]}, "position": {"x": 338, "y": 200.578125}, "gmtCreate": "2024-05-27T10:26:37.379Z", "groupCode": "trigger", "description": "", "gmtModified": "2024-05-27T10:26:37.379Z", "editorResource": {"styleHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-trigger-http-request/4/editor.css", "scriptHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-trigger-http-request/4/editor.js"}, "runtimeResource": "@hset/xlang-trigger-http-request@latest"}, {"id": 3, "code": "rpc-http", "icon": "", "name": "http", "type": "T", "nodeId": "spider-1bc19ee8-a5bd-4175-bc91-721d61324eed", "bizData": {"flowId": "1", "nodeId": "spider-1bc19ee8-a5bd-4175-bc91-721d61324eed", "params": {"url": "http://127.0.0.1:7002/flow", "method": "GET"}}, "version": 1, "portInfo": {"Input": [{"id": "Input", "dto": {"name": "DTO_Input", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 2, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 3, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "Input", "name": "Input", "operation": 0}], "Output": [{"id": "Output", "dto": {"name": "DTO_Output", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 7, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 8, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "Output", "name": "Output", "operation": 0}]}, "position": {"x": 880, "y": 210.578125}, "gmtCreate": "2024-05-28T06:32:32.086Z", "groupCode": "rpc", "description": "", "gmtModified": "2024-05-28T06:32:32.086Z", "editorResource": {"styleHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-rpc-http/2/editor.css", "scriptHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-rpc-http/2/editor.js"}, "runtimeResource": "@hset/xlang-rpc-http@latest"}, {"id": 2, "code": "advanced-function", "icon": "", "name": "自定义函数", "type": "T", "nodeId": "spider-43039140-7b33-4252-a0f0-29e35defff4b", "bizData": {"flowId": "1", "nodeId": "spider-43039140-7b33-4252-a0f0-29e35defff4b", "params": {"hackSend": false, "scriptCode": "// Type some code...\n\nconst ctx = input;\n\nlogger(input, 'input');\n\noutput = { ctx }"}}, "version": 2, "portInfo": {"Input": [{"id": "Input", "dto": {"name": "DTO_Input", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 2, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 3, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "输入", "name": "输入"}], "Output": [{"id": "Output", "dto": {"name": "DTO_Output", "children": [{"name": "_traceId", "type": {"name": "string", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 7, "fileName": "src/type/IO.ts", "character": 10}]}, {"name": "payload", "type": {"name": "any", "type": "intrinsic"}, "flags": {"isExported": true}, "sources": [{"line": 8, "fileName": "src/type/IO.ts", "character": 9}]}], "kindString": "Interface"}, "desc": "输出", "name": "输出"}]}, "position": {"x": 598, "y": 128.578125}, "gmtCreate": "2024-05-27T10:32:16.839Z", "groupCode": "advanced", "description": "", "gmtModified": "2024-05-27T14:44:55.000Z", "editorResource": {"styleHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-advanced-function/6/editor.css", "scriptHref": "https://icomestatics.oss-cn-beijing.aliyuncs.com/Elang/node/%40ennew/elang-advanced-function/6/editor.js"}, "runtimeResource": "@hset/xlang-advanced-function@latest"}]
},
id: 1
}
const { graphInfo, id } = graph;
const config = {
basePackageInfo: [
{
code: "Core",
runtimeResource: "@hlang-org/runtime@latest"
},
{
code: "_",
runtimeResource: "lodash@latest"
}
],
dirName: `XLANG_BUILD`
}
compiler({ graphInfo, id, options: config }).then(entry => {
console.log(entry)
});
```
187 changes: 187 additions & 0 deletions packages/compiler/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,
preset: 'ts-jest',
// Stop running tests after `n` failures
// bail: 0,

// Respect "browser" field in package.json when resolving modules
browser: false,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/s7/06196p4x0h75xh7r55lg2qr80000gn/T/jest_dx",

// Automatically clear mock calls and instances between every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: null,

// The directory where Jest should output its coverage files
// coverageDirectory: null,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
moduleFileExtensions: [
"ts",
"js",
],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,

// A path to a custom dependency extractor
// dependencyExtractor: null,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: null,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: null,

// A set of global variables that need to be available in all test environments
// globals: {},

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "json",
// "jsx",
// "ts",
// "tsx",
// "node"
// ],

// A map from regular expressions to module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: null,

// Run tests from one or more projects
// projects: null,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state between every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: null,

// Automatically restore mock state between every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: null,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
// testEnvironment: "browser",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: null,

// This option allows use of a custom test runner
// testRunner: "jasmine2",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",

// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: null,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: null,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};
52 changes: 52 additions & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@hlang-org/compiler",
"version": "1.0.0",
"description": "runtime compiler for hlang",
"author": "archersado <[email protected]>",
"license": "MIT",
"keywords": [
"fbp",
"fbp-runtime",
"javascript",
"flow",
"creative-coding",
"fbp-protocol",
"flow-based-programming",
"visual-programming-language",
"flow-based",
"internal-dsl"
],
"homepage": "https://github.com/hlang-org/hlang#readme",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib"
},
"files": [
"lib"
],
"scripts": {
"test": "jest --coverage --forceExit",
"build": "tsc -p tsconfig.json",
"watch": "tsc -p tsconfig.json -w",
"lint": "tslint --project . -c tslint.json"
},
"dependencies": {
"@hset/hit": "^1.0.4",
"debug": "^4.3.4",
"nunjucks": "^3.2.3"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.18",
"eslint": "^8.9.0",
"jest": "^28.1.3",
"moment": "^2.29.1",
"runscript": "^1.5.2",
"rxjs": "^7.5.4",
"ts-jest": "^29.2.4",
"tslint": "^6.1.3",
"tslint-config-airbnb": "^5.11.2",
"typescript": "^4.5.5"
}
}
55 changes: 55 additions & 0 deletions packages/compiler/src/builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Parser from '../complier';
import * as runScript from 'runscript';
import nunjucks = require('nunjucks');
import { debug } from '../utils';
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';

const DEFAULT_DIR_NAME = 'ELANG_BUILD';

const packageJSONTpl = `
{
"name": "test",
"version": "0.0.0",
"description": "",
"main": "lib/index.js",
"type": "lib/index.d.ts",
"scripts": {
},
"author": "",
"license": "MIT",
"dependencies": {
"@hset/xlang-runtime-middleware": "latest",
{% for item in deps %}
"{{item.name}}": "{{item.version}}" {%if loop.index < loop.length %}, {% endif %}
{% endfor %}
},
"devDependencies": {
}
}
`;

export default async function build(graph): Promise<string> {
const { graphInfo, id, options = {} } = graph;
const { dirName = DEFAULT_DIR_NAME } = options;
const tmpDir = os.tmpdir();
const baseDir = path.join(tmpDir, dirName);

debug('parser product baseDir', baseDir);
if (!fs.existsSync(baseDir)) {
fs.mkdirSync(baseDir);
}
const { target, deps } = Parser(graphInfo, id, options);

console.log('parser output target', target);
console.log('parser output deps', deps);
const packageJSON = nunjucks.renderString(packageJSONTpl, { deps: deps });
const entry = path.resolve(baseDir, 'index.js');
fs.writeFileSync(path.resolve(baseDir, 'package.json'), packageJSON);
fs.writeFileSync(entry, target);
await runScript('npm install', { cwd: baseDir });


return entry;
}
Loading

0 comments on commit 8bfc58d

Please sign in to comment.