GeezerVison is showing github.com — feat: support projectRemark which added in globalContextData · Micircle/lowcode-engine@deeeed2
File treeExpand file treeCollapse file tree Open diff view settings modules/code-generator/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ProjectBuilder.ts +17-10Lines changed: 17 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,31 +90,34 @@ export class ProjectBuilder implements IProjectBuilder { | 90 | 90 | async generateProject(originalSchema: ProjectSchema | string): Promise<ResultDir> { | 91 | 91 | // Init | 92 | 92 | const { schemaParser } = this; | 93 | | - const builders = this.createModuleBuilders(); | 94 | 93 |
| 95 | 94 | const projectRoot = await this.template.generateTemplate(); | 96 | 95 |
| 97 | 96 | let schema: ProjectSchema = | 98 | 97 | typeof originalSchema === 'string' ? JSON.parse(originalSchema) : originalSchema; | 99 | 98 |
| 100 | | - // Validate | 101 | | - if (!schemaParser.validate(schema)) { | 102 | | - throw new CodeGeneratorError('Schema is invalid'); | 103 | | - } | 104 | | - | 105 | 99 | // Parse / Format | 106 | | - | 107 | 100 | // Preprocess | 108 | 101 | for (const preProcessor of this.projectPreProcessors) { | 109 | 102 | // eslint-disable-next-line no-await-in-loop | 110 | 103 | schema = await preProcessor(schema); | 111 | 104 | } | 112 | 105 |
| | 106 | + // Validate | | 107 | + if (!schemaParser.validate(schema)) { | | 108 | + throw new CodeGeneratorError('Schema is invalid'); | | 109 | + } | | 110 | + | 113 | 111 | // Collect Deps | 114 | 112 | // Parse JSExpression | 115 | 113 | const parseResult: IParseResult = schemaParser.parse(schema); | 116 | 114 | let buildResult: IModuleInfo[] = []; | 117 | 115 |
| | 116 | + const builders = this.createModuleBuilders({ | | 117 | + extraContextData: { | | 118 | + projectRemark: parseResult?.project?.projectRemark, | | 119 | + }, | | 120 | + }); | 118 | 121 | // Generator Code module | 119 | 122 | // components | 120 | 123 | // pages | @@ -253,11 +256,12 @@ export class ProjectBuilder implements IProjectBuilder { | 253 | 256 | // TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下? | 254 | 257 |
| 255 | 258 | // Post Process | 256 | | - | | 259 | + const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent; | 257 | 260 | // Combine Modules | 258 | 261 | buildResult.forEach((moduleInfo) => { | 259 | 262 | let targetDir = getDirFromRoot(projectRoot, moduleInfo.path); | 260 | | - if (moduleInfo.moduleName) { | | 263 | + // if project only contain single component, skip creation of directory. | | 264 | + if (moduleInfo.moduleName && !isSingleComponent) { | 261 | 265 | const dir = createResultDir(moduleInfo.moduleName); | 262 | 266 | addDirectory(targetDir, dir); | 263 | 267 | targetDir = dir; | @@ -278,7 +282,8 @@ export class ProjectBuilder implements IProjectBuilder { | 278 | 282 | return finalResult; | 279 | 283 | } | 280 | 284 |
| 281 | | - private createModuleBuilders(): Record<string, IModuleBuilder> { | | 285 | + private createModuleBuilders(extraContextData: Record<string, unknown> = {}): | | 286 | + Record<string, IModuleBuilder> { | 282 | 287 | const builders: Record<string, IModuleBuilder> = {}; | 283 | 288 |
| 284 | 289 | Object.keys(this.plugins).forEach((pluginName) => { | @@ -291,10 +296,12 @@ export class ProjectBuilder implements IProjectBuilder { | 291 | 296 | plugins: this.plugins[pluginName], | 292 | 297 | postProcessors: this.postProcessors, | 293 | 298 | contextData: { | | 299 | + // template: this.template, | 294 | 300 | inStrictMode: this.inStrictMode, | 295 | 301 | tolerateEvalErrors: true, | 296 | 302 | evalErrorsHandler: '', | 297 | 303 | ...this.extraContextData, | | 304 | + ...extraContextData, | 298 | 305 | }, | 299 | 306 | ...options, | 300 | 307 | }); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/parser/SchemaParser.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ import { handleSubNodes, isValidContainerType } from '../utils/schema'; | 36 | 36 | import { uniqueArray } from '../utils/common'; | 37 | 37 | import { componentAnalyzer } from '../analyzer/componentAnalyzer'; | 38 | 38 | import { ensureValidClassName } from '../utils/validate'; | | 39 | +import type { ProjectRemark } from '../types/intermediate'; | 39 | 40 |
| 40 | 41 | const defaultContainer: IContainerInfo = { | 41 | 42 | containerType: 'Component', | @@ -319,10 +320,17 @@ export class SchemaParser implements ISchemaParser { | 319 | 320 | utilsDeps, | 320 | 321 | packages: npms || [], | 321 | 322 | dataSourcesTypes: this.collectDataSourcesTypes(schema), | | 323 | + projectRemark: this.getProjectRemark(containers), | 322 | 324 | }, | 323 | 325 | }; | 324 | 326 | } | 325 | 327 |
| | 328 | + getProjectRemark(containers: IContainerInfo[]): ProjectRemark { | | 329 | + return { | | 330 | + isSingleComponent: containers.length === 1 && containers[0].containerType === 'Component', | | 331 | + }; | | 332 | + } | | 333 | + | 326 | 334 | getComponentNames(children: NodeDataType): string[] { | 327 | 335 | return handleSubNodes<string>( | 328 | 336 | children, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectDataSourceEngine.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,7 @@ import { | 27 | 27 |
| 28 | 28 | import { generateCompositeType } from '../../../utils/compositeType'; | 29 | 29 | import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser'; | 30 | | -import { isContainerSchema } from '../../../utils/schema'; | | 30 | +import { isValidContainerType } from '../../../utils/schema'; | 31 | 31 | import { REACT_CHUNK_NAME } from './const'; | 32 | 32 |
| 33 | 33 | export interface PluginConfig { | @@ -67,7 +67,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 67 | 67 | }; | 68 | 68 |
| 69 | 69 | const scope = Scope.createRootScope(); | 70 | | - const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null; | | 70 | + const dataSourceConfig = isValidContainerType(pre.ir) ? pre.ir.dataSource : null; | 71 | 71 | const dataSourceItems: InterpretDataSourceConfig[] = | 72 | 72 | (dataSourceConfig && dataSourceConfig.list) || []; | 73 | 73 | const dataSourceEngineOptions = { runtimeConfig: true }; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectUtils.ts +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { | 11 | 11 | FileType, | 12 | 12 | ICodeStruct, | 13 | 13 | IContainerInfo, | | 14 | + IProjectTemplate, | 14 | 15 | } from '../../../types'; | 15 | 16 |
| 16 | 17 | export interface PluginConfig { | @@ -32,6 +33,19 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 32 | 33 | next.contextData.useRefApi = true; | 33 | 34 | const useRef = !!ir.analyzeResult?.isUsingRef; | 34 | 35 |
| | 36 | + // const isSingleComponent = next.contextData?.projectRemark?.isSingleComponent; | | 37 | + // const template = next.contextData?.template; | | 38 | + | | 39 | + // function getRelativeUtilsPath(template: IProjectTemplate, isSingleComponent: boolean) { | | 40 | + // let relativeUtilsPath = '../../utils'; | | 41 | + // const utilsPath = template.slots.utils.path; | | 42 | + // if (ir.containerType === 'Component') { | | 43 | + // // TODO: isSingleComponent | | 44 | + // relativeUtilsPath = getRelativePath(template.slots.components.path.join('/'), utilsPath.join('/')); | | 45 | + // } | | 46 | + // return relativeUtilsPath; | | 47 | + // } | | 48 | + | 35 | 49 | next.chunks.push({ | 36 | 50 | type: ChunkType.STRING, | 37 | 51 | fileType: cfg.fileType, | @@ -89,15 +103,15 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 89 | 103 | type: ChunkType.STRING, | 90 | 104 | fileType: cfg.fileType, | 91 | 105 | name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | 92 | | - content: ` $ = () => null; `, | | 106 | + content: ' $ = () => null; ', | 93 | 107 | linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | 94 | 108 | }); | 95 | 109 |
| 96 | 110 | next.chunks.push({ | 97 | 111 | type: ChunkType.STRING, | 98 | 112 | fileType: cfg.fileType, | 99 | 113 | name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | 100 | | - content: ` $$ = () => []; `, | | 114 | + content: ' $$ = () => []; ', | 101 | 115 | linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | 102 | 116 | }); | 103 | 117 | } | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/intermediate.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,14 @@ export interface IRouterInfo extends IWithDependency { | 33 | 33 | }>; | 34 | 34 | } | 35 | 35 |
| | 36 | +/** | | 37 | + * project's remarks | | 38 | + */ | | 39 | +export interface ProjectRemark { | | 40 | + /** if current project only contain one container which type is `Component` */ | | 41 | + isSingleComponent?: boolean; | | 42 | +} | | 43 | + | 36 | 44 | export interface IProjectInfo { | 37 | 45 | css?: string; | 38 | 46 | containersDeps?: IDependency[]; | @@ -43,6 +51,7 @@ export interface IProjectInfo { | 43 | 51 | meta?: { name?: string; title?: string } | Record<string, any>; | 44 | 52 | config?: Record<string, any>; | 45 | 53 | dataSourcesTypes?: string[]; | | 54 | + projectRemark?: ProjectRemark; | 46 | 55 | } | 47 | 56 |
| 48 | 57 | export interface IPageMeta { | |
|