|
|
@ -1,9 +1,33 @@ |
|
|
import { normalize, strings } from '@angular-devkit/core'; |
|
|
import { normalize, strings } from '@angular-devkit/core'; |
|
|
import { applyTemplates, branchAndMerge, chain, move, SchematicContext, SchematicsException, Tree, url } from '@angular-devkit/schematics'; |
|
|
import { |
|
|
|
|
|
applyTemplates, |
|
|
|
|
|
branchAndMerge, |
|
|
|
|
|
chain, |
|
|
|
|
|
move, |
|
|
|
|
|
Rule, |
|
|
|
|
|
SchematicContext, |
|
|
|
|
|
SchematicsException, |
|
|
|
|
|
Tree, |
|
|
|
|
|
url, |
|
|
|
|
|
} from '@angular-devkit/schematics'; |
|
|
import { Exception } from '../../enums'; |
|
|
import { Exception } from '../../enums'; |
|
|
import { applyWithOverwrite, buildDefaultPath, createApiDefinitionReader, createControllerToServiceMapper, createImportRefsToModelMapper, createImportRefToEnumMapper, getEnumNamesFromImports, interpolate, resolveProject, serializeParameters } from '../../utils'; |
|
|
import { ServiceGeneratorParams } from '../../models'; |
|
|
|
|
|
import { |
|
|
|
|
|
applyWithOverwrite, |
|
|
|
|
|
buildDefaultPath, |
|
|
|
|
|
createApiDefinitionReader, |
|
|
|
|
|
createControllerToServiceMapper, |
|
|
|
|
|
createImportRefsToModelMapper, |
|
|
|
|
|
createImportRefToEnumMapper, |
|
|
|
|
|
EnumGeneratorParams, |
|
|
|
|
|
getEnumNamesFromImports, |
|
|
|
|
|
interpolate, |
|
|
|
|
|
ModelGeneratorParams, |
|
|
|
|
|
resolveProject, |
|
|
|
|
|
serializeParameters, |
|
|
|
|
|
} from '../../utils'; |
|
|
import * as cases from '../../utils/text'; |
|
|
import * as cases from '../../utils/text'; |
|
|
import type { Schema as GenerateProxySchema } from './schema'; |
|
|
import { Schema as GenerateProxySchema } from './schema'; |
|
|
|
|
|
|
|
|
export default function(params: GenerateProxySchema) { |
|
|
export default function(params: GenerateProxySchema) { |
|
|
const solution = params.solution; |
|
|
const solution = params.solution; |
|
|
@ -13,79 +37,124 @@ export default function(params: GenerateProxySchema) { |
|
|
async (tree: Tree, _context: SchematicContext) => { |
|
|
async (tree: Tree, _context: SchematicContext) => { |
|
|
const target = await resolveProject(tree, params.target!); |
|
|
const target = await resolveProject(tree, params.target!); |
|
|
const targetPath = buildDefaultPath(target.definition); |
|
|
const targetPath = buildDefaultPath(target.definition); |
|
|
const readApiDefinition = createApiDefinitionReader(`${targetPath}/shared/api-definition.json`); |
|
|
const readApiDefinition = createApiDefinitionReader( |
|
|
|
|
|
`${targetPath}/shared/api-definition.json`, |
|
|
|
|
|
); |
|
|
const data = readApiDefinition(tree); |
|
|
const data = readApiDefinition(tree); |
|
|
const types = data.types; |
|
|
const types = data.types; |
|
|
const definition = data.modules[moduleName]; |
|
|
const definition = data.modules[moduleName]; |
|
|
if (!definition) throw new SchematicsException(interpolate(Exception.InvalidModule, moduleName)); |
|
|
if (!definition) |
|
|
|
|
|
throw new SchematicsException(interpolate(Exception.InvalidModule, moduleName)); |
|
|
|
|
|
|
|
|
const mapControllerToService = createControllerToServiceMapper(solution, types, definition.remoteServiceName); |
|
|
const apiName = definition.remoteServiceName; |
|
|
const controllers = Object.values(definition.controllers || {}); |
|
|
const controllers = Object.values(definition.controllers || {}); |
|
|
const serviceImports: Record<string, string[]> = {}; |
|
|
const serviceImports: Record<string, string[]> = {}; |
|
|
|
|
|
const generateServices = createServiceGenerator({ |
|
|
|
|
|
targetPath, |
|
|
|
|
|
solution, |
|
|
|
|
|
types, |
|
|
|
|
|
apiName, |
|
|
|
|
|
controllers, |
|
|
|
|
|
serviceImports, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
const createServiceFiles = chain( |
|
|
const modelImports: Record<string, string[]> = {}; |
|
|
controllers.map(controller => { |
|
|
const generateModels = createModelGenerator({ |
|
|
const service = mapControllerToService(controller); |
|
|
targetPath, |
|
|
service.imports.forEach(({refs, path}) => refs.forEach(ref => { |
|
|
solution, |
|
|
if (path === '@abp/ng.core') return; |
|
|
types, |
|
|
if (!serviceImports[path]) return (serviceImports[path] = [ref]); |
|
|
serviceImports, |
|
|
serviceImports[path] = [...new Set([...serviceImports[path], ref])]; |
|
|
modelImports, |
|
|
})); |
|
|
}); |
|
|
|
|
|
|
|
|
return applyWithOverwrite(url('./files-service'), [ |
|
|
const generateEnums = createEnumGenerator({ |
|
|
applyTemplates({ |
|
|
targetPath, |
|
|
...cases, |
|
|
solution, |
|
|
serializeParameters, |
|
|
types, |
|
|
...service, |
|
|
serviceImports, |
|
|
}), |
|
|
modelImports, |
|
|
move(normalize(targetPath)), |
|
|
}); |
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
), |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const mapImportRefsToModel = createImportRefsToModelMapper(solution, types); |
|
|
return branchAndMerge(chain([generateServices, generateModels, generateEnums])); |
|
|
const modelImports: Record<string, string[]> = {}; |
|
|
}, |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const createModelFiles = chain( |
|
|
function createEnumGenerator(params: EnumGeneratorParams) { |
|
|
Object.values(serviceImports).map(refs => { |
|
|
const { targetPath, serviceImports, modelImports } = params; |
|
|
const model = mapImportRefsToModel(refs); |
|
|
const mapImportRefToEnum = createImportRefToEnumMapper(params); |
|
|
model.imports.forEach(({refs, path}) => refs.forEach(ref => { |
|
|
const enumRefs = [ |
|
|
if (path === '@abp/ng.core') return; |
|
|
...new Set([ |
|
|
if (!modelImports[path]) return (modelImports[path] = [ref]); |
|
|
...getEnumNamesFromImports(serviceImports), |
|
|
modelImports[path] = [...new Set([...modelImports[path], ref])]; |
|
|
...getEnumNamesFromImports(modelImports), |
|
|
})); |
|
|
]), |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
return applyWithOverwrite(url('./files-model'), [ |
|
|
return chain( |
|
|
applyTemplates({ |
|
|
enumRefs.map(ref => { |
|
|
...cases, |
|
|
return applyWithOverwrite(url('./files-enum'), [ |
|
|
...model, |
|
|
applyTemplates({ |
|
|
}), |
|
|
...cases, |
|
|
move(normalize(targetPath)), |
|
|
...mapImportRefToEnum(ref), |
|
|
]); |
|
|
|
|
|
}), |
|
|
}), |
|
|
); |
|
|
move(normalize(targetPath)), |
|
|
|
|
|
]); |
|
|
|
|
|
}), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const mapImportRefToEnum = createImportRefToEnumMapper(solution, types); |
|
|
function createModelGenerator(params: ModelGeneratorParams) { |
|
|
const enumRefs = [...new Set([ |
|
|
const { targetPath, serviceImports, modelImports } = params; |
|
|
...getEnumNamesFromImports(serviceImports), |
|
|
const mapImportRefsToModel = createImportRefsToModelMapper(params); |
|
|
...getEnumNamesFromImports(modelImports), |
|
|
|
|
|
])]; |
|
|
|
|
|
|
|
|
|
|
|
const createEnumFiles = chain( |
|
|
return chain( |
|
|
enumRefs.map(ref => { |
|
|
Object.values(serviceImports).reduce((rules: Rule[], refs) => { |
|
|
return applyWithOverwrite(url('./files-enum'), [ |
|
|
const model = mapImportRefsToModel(refs); |
|
|
applyTemplates({ |
|
|
model.imports.forEach(({ refs, path }) => |
|
|
...cases, |
|
|
refs.forEach(ref => { |
|
|
...mapImportRefToEnum(ref), |
|
|
if (path === '@abp/ng.core') return; |
|
|
}), |
|
|
if (!modelImports[path]) return (modelImports[path] = [ref]); |
|
|
move(normalize(targetPath)), |
|
|
modelImports[path] = [...new Set([...modelImports[path], ref])]; |
|
|
]); |
|
|
|
|
|
}), |
|
|
}), |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
return branchAndMerge(chain([createServiceFiles, createModelFiles, createEnumFiles])); |
|
|
const rule = applyWithOverwrite(url('./files-model'), [ |
|
|
}, |
|
|
applyTemplates({ |
|
|
]); |
|
|
...cases, |
|
|
|
|
|
...model, |
|
|
|
|
|
}), |
|
|
|
|
|
move(normalize(targetPath)), |
|
|
|
|
|
]); |
|
|
|
|
|
rules.push(rule); |
|
|
|
|
|
|
|
|
|
|
|
return rules; |
|
|
|
|
|
}, []), |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function createServiceGenerator(params: ServiceGeneratorParams) { |
|
|
|
|
|
const { targetPath, controllers, serviceImports } = params; |
|
|
|
|
|
const mapControllerToService = createControllerToServiceMapper(params); |
|
|
|
|
|
|
|
|
|
|
|
return chain( |
|
|
|
|
|
controllers.map(controller => { |
|
|
|
|
|
const service = mapControllerToService(controller); |
|
|
|
|
|
service.imports.forEach(({ refs, path }) => |
|
|
|
|
|
refs.forEach(ref => { |
|
|
|
|
|
if (path === '@abp/ng.core') return; |
|
|
|
|
|
if (!serviceImports[path]) return (serviceImports[path] = [ref]); |
|
|
|
|
|
serviceImports[path] = [...new Set([...serviceImports[path], ref])]; |
|
|
|
|
|
}), |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return applyWithOverwrite(url('./files-service'), [ |
|
|
|
|
|
applyTemplates({ |
|
|
|
|
|
...cases, |
|
|
|
|
|
serializeParameters, |
|
|
|
|
|
...service, |
|
|
|
|
|
}), |
|
|
|
|
|
move(normalize(targetPath)), |
|
|
|
|
|
]); |
|
|
|
|
|
}), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|