acp-gencode
v0.1.0
Published
Hệ thống tạo mã tự động cho lowcoder
Downloads
30
Readme
Lowcoder Gencode
Hệ thống tạo mã tự động cho lowcoder dựa trên mô hình của Plasmic.
Tích hợp với Editor
Gencode đã được tích hợp trực tiếp vào giao diện editor của Lowcoder thông qua nút "Tạo mã" trên thanh công cụ. Khi nhấn vào nút này, hệ thống sẽ lấy DSL của ứng dụng hiện tại và tạo mã tương ứng.
Tích hợp thư viện từ npm
Thư viện acp-gencode có thể được cài đặt và sử dụng trong Lowcoder như một dependency bình thường:
- Cài đặt thư viện:
cd client
yarn add acp-gencode- Import các hàm từ thư viện trong component GenCodeButton:
import { generateCodeFromDSL } from "acp-gencode/dist/integrations/editorIntegration";- Sử dụng hàm trực tiếp để tạo mã:
const code = await generateCodeFromDSL(dsl, { template, designSystem });Cách tiếp cận này đơn giản hóa quá trình tích hợp và loại bỏ nhu cầu phải chạy một service API riêng biệt.
Cài đặt
Cài đặt từ npm
npm install acp-gencode
# hoặc
yarn add acp-gencodeCài đặt từ source
git clone https://github.com/vnpt/acp-gencode.git
cd acp-gencode
npm install
npm run buildSử dụng
Tạo một component schema
import { createComponentBuilder, createPropSchema } from 'acp-gencode';
// Tạo một component builder
const buttonBuilder = createComponentBuilder('button', 'Button')
.addProp('text', createPropSchema('string', {
defaultValue: 'Button',
description: 'Nội dung của button'
}))
.addProp('type', createPropSchema('string', {
defaultValue: 'primary',
description: 'Kiểu của button'
}))
.addProp('disabled', createPropSchema('boolean', {
defaultValue: false,
description: 'Trạng thái disabled của button'
}))
.exposeState('disabled')
.exposeMethod({
name: 'click',
description: 'Kích hoạt sự kiện click của button'
});
// Xây dựng schema
const buttonSchema = buttonBuilder.build();Tạo mã từ schema
import { generateComponentCode } from 'acp-gencode';
// Tạo mã từ schema
const code = await generateComponentCode(buttonSchema, {
template: 'default',
designSystem: 'lowcoder'
});
console.log(code.imports);
console.log(code.props);
console.log(code.body);
console.log(code.exposing);API
SchemaGenerator
createComponentSchema(type, name, props): Tạo một component schema mớiaddChildToSchema(parent, child): Thêm một component con vào component chaaddPropToSchema(schema, propName, propSchema): Thêm một thuộc tính vào component schemaaddStylesToSchema(schema, styles): Thêm style vào component schemaaddEventToSchema(schema, event): Thêm một event vào component schemaaddExposedState(schema, stateName): Thêm một state được expose vào component schemaaddExposedMethod(schema, method): Thêm một method được expose vào component schemavalidateSchema(schema): Kiểm tra tính hợp lệ của component schemaschemaToJson(schema): Chuyển đổi component schema thành JSONjsonToSchema(json): Chuyển đổi JSON thành component schema
CodeGenerator
generateComponentCode(schema, options): Tạo mã cho component từ schemagenerateImports(schema, designSystem): Tạo imports cho componentgenerateProps(schema): Tạo props interface cho componentgenerateComponentBody(schema, template): Tạo body cho componentgenerateExposing(schema): Tạo mã cho exposed state và methodsassembleComponentFile(code): Tạo file component hoàn chỉnh
ComponentBuilder
new GenComponentBuilder(type, name, options): Khởi tạo một component builder mớiaddProp(name, propSchema): Thêm một prop vào componentaddProps(props): Thêm nhiều props vào componentaddChild(childSchema): Thêm một component con vào componentaddChildren(childrenSchemas): Thêm nhiều component con vào componentaddStyle(styleKey, styleValue): Thêm một style vào componentaddStyles(styles): Thêm nhiều styles vào componentaddEvent(event): Thêm một event vào componentaddEvents(events): Thêm nhiều events vào componentexposeState(stateName): Expose một state của componentexposeStates(stateNames): Expose nhiều states của componentexposeMethod(method): Expose một method của componentexposeMethods(methods): Expose nhiều methods của componentsetOptions(options): Cập nhật tùy chọn cho builderbuild(): Xây dựng và trả về component schemagenerateCode(): Tạo mã từ component schemaGenComponentBuilder.fromSchema(schema, options): Tạo một component builder từ schema có sẵn
Tích hợp với Lowcoder
Cách thêm GenCodeButton vào editor
- Thêm component GenCodeButton vào thư mục
client/packages/lowcoder/src/components/ - Cập nhật file
editorView.tsxđể thêm nút GenCodeButton vào header - Cập nhật file
header.tsxđể hỗ trợ rightContent - Thêm các chuỗi dịch vào file i18n
Cách sử dụng
Cài đặt dependencies:
cd gencode npm install cd ../client yarn installKhởi động Lowcoder:
cd client yarn startTruy cập vào editor và nhấn vào nút "Tạo mã" trên thanh công cụ
Đóng góp
Vui lòng đọc CONTRIBUTING.md để biết chi tiết về quy trình đóng góp.
Giấy phép
Dự án này được cấp phép theo giấy phép MIT - xem file LICENSE để biết thêm chi tiết.
