Skip to content

Commit bcc30b1

Browse files
authored
Merge pull request #99 from mat-app/Generator--index-and-detail-generation
add insertFormWidget
2 parents 3add31b + 212ea0c commit bcc30b1

43 files changed

Lines changed: 1385 additions & 418 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { astFindSource, SourceLineCol } from "../../ast";
2+
import ts, { factory } from "typescript";
3+
4+
export interface Node {
5+
getText(includeJsDocComments?: boolean): string;
6+
}
7+
8+
export interface Symbol {
9+
isAnonymous(): boolean;
10+
isAny(): boolean;
11+
isArray(): boolean;
12+
isBoolean(): boolean;
13+
isString(): boolean;
14+
isNumber(): boolean;
15+
isBooleanLiteral(): boolean;
16+
isEnumLiteral(): boolean;
17+
isNumberLiteral(): boolean;
18+
isStringLiteral(): boolean;
19+
isClass(): boolean;
20+
isClassOrInterface(): boolean;
21+
isEnum(): boolean;
22+
isInterface(): boolean;
23+
isObject(): boolean;
24+
//isTypeParameter(): this is TypeParameter;
25+
isTuple(): boolean;
26+
isUnion(): boolean;
27+
isIntersection(): boolean;
28+
isUnionOrIntersection(): boolean;
29+
isUnknown(): boolean;
30+
isNull(): boolean;
31+
isUndefined(): boolean;
32+
}
33+
34+
export function findVariableDeclarations(
35+
parentNode: ts.Node,
36+
array: ts.VariableDeclaration[]
37+
) {
38+
if (parentNode != undefined) {
39+
if (ts.isVariableDeclaration(parentNode)) {
40+
array.push(parentNode);
41+
}
42+
43+
if (parentNode.getChildCount() > 0) {
44+
var children = parentNode.getChildren();
45+
children.forEach((child) => {
46+
findVariableDeclarations(child, array);
47+
});
48+
}
49+
}
50+
}
51+
52+
export function findObjectLiteralExpression(
53+
parentNode: ts.Node,
54+
array: ts.ObjectLiteralExpression[]
55+
) {
56+
if (parentNode != undefined) {
57+
if (ts.isObjectLiteralExpression(parentNode)) {
58+
array.push(parentNode);
59+
}
60+
61+
if (parentNode.getChildCount() > 0) {
62+
var children = parentNode.getChildren();
63+
children.forEach((child) => {
64+
findObjectLiteralExpression(child, array);
65+
});
66+
}
67+
}
68+
}
69+
70+
export function findPropertyAssignment(
71+
parentNode: ts.Node,
72+
array: ts.PropertyAssignment[]
73+
) {
74+
if (parentNode != undefined) {
75+
if (parentNode.getChildCount() > 0) {
76+
var children = parentNode.getChildren();
77+
children.forEach((child) => {
78+
if (ts.isPropertyAssignment(child)) {
79+
array.push(child);
80+
}
81+
});
82+
}
83+
}
84+
}

packages/react-lowcode/src/codegen/generation/ts/hooks.ts renamed to packages/react-lowcode/src/codegen/ast/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hook } from "../../../ast/hooks"
1+
import { Hook } from "../../ast/hooks"
22
import { createImportDeclaration } from "./imports"
33

44

packages/react-lowcode/src/codegen/generation/ts/identifier.ts renamed to packages/react-lowcode/src/codegen/ast/identifier.ts

File renamed without changes.

packages/react-lowcode/src/codegen/generation/ts/imports.ts renamed to packages/react-lowcode/src/codegen/ast/imports.ts

File renamed without changes.

packages/react-lowcode/src/codegen/generation/ts/parameters.ts renamed to packages/react-lowcode/src/codegen/ast/parameters.ts

File renamed without changes.
File renamed without changes.

packages/react-lowcode/src/codegen/generation/ts/variables.ts renamed to packages/react-lowcode/src/codegen/ast/variables.ts

File renamed without changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import ts from "typescript"
2+
import { astFindSource,SourceLineCol} from "../../ast"
3+
4+
export function findWidgetParentNode(sourceCode:string, position: SourceLineCol): ts.Node | null | undefined{
5+
let astCode = astFindSource(sourceCode, position)
6+
7+
if(astCode){
8+
let parent = astCode.parent
9+
10+
while(parent){
11+
if(isWidgetDeclaration(parent)){
12+
return parent
13+
}
14+
15+
parent = parent.parent
16+
}
17+
}else{
18+
console.log('cannot find widget element')
19+
}
20+
21+
return undefined
22+
}
23+
24+
export function isDataTableWidget(sourceCode:string, position: SourceLineCol): boolean{
25+
let isDataTableDeclaration = false
26+
let astCode = astFindSource(sourceCode, position)
27+
28+
if(astCode){
29+
const identifier = findIdentifier(astCode)
30+
31+
if(identifier){
32+
isDataTableDeclaration = identifier.getText() === 'DataGrid'
33+
}
34+
}
35+
36+
return isDataTableDeclaration
37+
}
38+
39+
function findIdentifier(root: ts.Node): ts.Identifier | undefined{
40+
let identifier = undefined
41+
root.getChildren().forEach(child => {
42+
if(ts.isIdentifier(child))
43+
identifier = child
44+
return
45+
});
46+
47+
return identifier
48+
}
49+
50+
function isWidgetDeclaration(node: ts.Node){
51+
return isTableDeclaration(node) || isDetailDeclaration(node)
52+
}
53+
54+
function isTableDeclaration(node: ts.Node): boolean{
55+
///TODO: check also for export key word
56+
return ts.isFunctionDeclaration(node)
57+
}
58+
59+
function isDetailDeclaration(node: ts.Node): boolean{
60+
///TODO: check also for export key word
61+
return ts.isVariableDeclaration(node)
62+
}

packages/react-lowcode/src/codegen/cli/index.ts

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,77 @@ import ts, {factory} from 'typescript'
88
import { graphqlGenTs1 } from "../tests/typeAlias.example"
99
import { Formatter, TableType, UiFramework } from "../definition/context-types"
1010
import { AppGenerator } from "../generation/generators/app-generator"
11-
import { createAst, parseGraphqlTypes, sourceFileEntity } from "../tests/helper"
12-
13-
const LIST_COMPONENT = 'list'
14-
const DETAIL_COMPONENT = 'detail'
11+
import sourceFileEntity, { createAst, parseGraphqlTypes } from "../tests/helper"
1512

1613
const args = yargs.options({
1714
'basePath': { type: 'string', demandOption: true, alias: 'p' },
1815
'fileName': { type: 'string', demandOption: true, alias: 'f' },
19-
'ui': { type: 'string', demandOption: true, alias: 'u', choices: [UiFramework.MaterialUI.toString(), UiFramework.Grommet.toString()] },
20-
'formatter': { type: 'string', demandOption: true, alias: 'fr', choices: [Formatter.ReactIntl.toString(), Formatter.None.toString()] },
21-
'component': { type: 'string', demandOption: true, alias: 'c', choices: [LIST_COMPONENT, DETAIL_COMPONENT] },
22-
'table': { type: 'string', demandOption: true, alias: 't', choices: [TableType.DataTable.toString(), TableType.BasicTable.toString()] },
16+
'ui': { type: 'string', demandOption: true, alias: 'u' },
17+
'formatter': { type: 'string', demandOption: true, alias: 'fr' },
18+
'component': { type: 'string', demandOption: true, alias: 'c' },
19+
'table': { type: 'string', demandOption: true, alias: 't' },
2320
}).argv;
2421

25-
async function generateList(){
26-
const sourceFile = createAst('')
27-
const myClassFile = parseGraphqlTypes(graphqlGenTs1)
28-
const testEntity = sourceFileEntity(myClassFile)
22+
class LocalCodegenCli{
23+
async generateList(){
24+
const sourceFile = createAst('')
25+
const myClassFile = parseGraphqlTypes(graphqlGenTs1)
26+
const testEntity = sourceFileEntity(myClassFile)
2927

30-
let uif : UiFramework = (<any>UiFramework)[args['ui']];
31-
let formatter : Formatter = (<any>Formatter)[args['formatter']];
32-
let tableType : TableType = (<any>TableType)[args['table']];
33-
34-
let generationContext = {uiFramework: uif, formatter: formatter, index: {tableType: tableType, height: "400px"}};
35-
let generator = new AppGenerator(generationContext, testEntity!!);
28+
let uif : UiFramework = (<any>UiFramework)[args['ui']];
29+
let formatter : Formatter = (<any>Formatter)[args['formatter']];
30+
let tableType : TableType = (<any>TableType)[args['table']];
31+
32+
let generationContext = {uiFramework: uif, formatter: formatter, index: {tableType: tableType, height: "400px"}};
33+
let generator = new AppGenerator(generationContext, testEntity!!);
34+
35+
const page = generator.generateListComponent()
36+
37+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed })
38+
39+
let sourceCode = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray([...page!.imports, page!.functionDeclaration]), sourceFile)
40+
3641

37-
const page = generator.generateListPage()
38-
39-
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed })
40-
let sourceCode = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray([...page.imports, page.functionDeclaration]), sourceFile)
41-
saveFile(sourceCode, args['basePath'], args['fileName'])
42-
}
42+
this.saveFile(sourceCode, args['basePath'], args['fileName'])
43+
}
4344

44-
async function generateDetail(){
45-
const sourceFile = createAst('')
46-
const myClassFile = parseGraphqlTypes(graphqlGenTs1)
47-
const testEntity = sourceFileEntity(myClassFile)
45+
async generateDetail(){
46+
const sourceFile = createAst('')
47+
const myClassFile = parseGraphqlTypes(graphqlGenTs1)
48+
const testEntity = sourceFileEntity(myClassFile)
4849

49-
let uif : UiFramework = (<any>UiFramework)[args['ui']];
50-
let formatter : Formatter = (<any>Formatter)[args['formatter']];
51-
let tableType : TableType = (<any>TableType)[args['table']];
52-
53-
let generationContext = {uiFramework: uif, formatter: formatter, index: {tableType: tableType, height: "400px"}};
54-
let generator = new AppGenerator(generationContext, testEntity!!);
50+
let uif : UiFramework = (<any>UiFramework)[args['ui']];
51+
let formatter : Formatter = (<any>Formatter)[args['formatter']];
52+
let tableType : TableType = (<any>TableType)[args['table']];
53+
54+
let generationContext = {uiFramework: uif, formatter: formatter, index: {tableType: tableType, height: "400px"}};
55+
let generator = new AppGenerator(generationContext, testEntity!!);
56+
57+
const page = generator.generateDetailPage()
58+
59+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed })
60+
61+
let sourceCode = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray([...page.imports, page.functionDeclaration]), sourceFile)
62+
63+
this.saveFile(sourceCode, args['basePath'], args['fileName'])
64+
}
5565

56-
const page = generator.generateDetailPage()
57-
58-
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed })
59-
let sourceCode = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray([...page.imports, page.functionDeclaration]), sourceFile)
60-
saveFile(sourceCode, args['basePath'], args['fileName'])
61-
}
66+
async saveFile(sourceCode: string, basePath:string, fileName: string){
67+
let filePath = basePath + fileName
6268

63-
async function saveFile(sourceCode: string, basePath:string, fileName: string){
64-
let filePath = basePath + fileName
65-
66-
await fs.writeFile(filePath, sourceCode, function(err) {
67-
if (err) {
68-
return console.error(err);
69-
}
70-
console.log("File created!");
71-
});
69+
await fs.writeFile(filePath, sourceCode, function(err) {
70+
if (err) {
71+
return console.error(err);
72+
}
73+
console.log("File created!");
74+
});
75+
}
7276
}
7377

74-
if (args.component === LIST_COMPONENT)
75-
generateList()
76-
else if(args.component === DETAIL_COMPONENT){
77-
generateDetail()
78+
const cli = new LocalCodegenCli()
79+
80+
if(args['component'] === 'list')
81+
cli.generateList()
82+
else if(args['component'] === 'detail'){
83+
cli.generateDetail()
7884
}

0 commit comments

Comments
 (0)