feat: parse controller and create service path

pull/5137/head
Arman Ozak 5 years ago
parent ab5a5a46b4
commit 5358d5b916

@ -1,8 +1,9 @@
import { strings } from '@angular-devkit/core';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { normalize, strings } from '@angular-devkit/core';
import { applyTemplates, branchAndMerge, chain, move, SchematicContext, Tree, url } from '@angular-devkit/schematics';
import { API_DEFINITION_ENDPOINT } from '../../constants';
import { ApiDefinition } from '../../models';
import { getSourceJson, getSourceUrl, resolveProject } from '../../utils';
import { applyWithOverwrite, buildDefaultPath, getSourceJson, getSourceUrl, isLibrary, resolveProject } from '../../utils';
import * as cases from '../../utils/text';
import type { Schema as GenerateProxySchema } from './schema';
export default function(params: GenerateProxySchema) {
@ -11,20 +12,35 @@ export default function(params: GenerateProxySchema) {
return chain([
async (tree: Tree, _context: SchematicContext) => {
const source = await resolveProject(tree, params.source!);
const url = getSourceUrl(tree, source, moduleName);
const data: ApiDefinition = await getSourceJson(url + API_DEFINITION_ENDPOINT);
const target = await resolveProject(tree, params.target!);
const isModule = isLibrary(target.definition);
const sourceUrl = getSourceUrl(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition);
const data: ApiDefinition = await getSourceJson(sourceUrl + API_DEFINITION_ENDPOINT);
const services = Object.entries(data.modules).map(([name, def]) => [
name,
Object.values(def.controllers).map(
({controllerName, actions}) => [controllerName, Object.keys(actions)]
const controllers = Object.values(data.modules[moduleName]?.controllers || {});
const createServiceFiles = chain(
controllers.map(controller => {
const {type} = controller;
const [namespace] = type.replace(/^Volo\.(Abp\.)?/, '').split('.');
return applyWithOverwrite(url('./files-service'), [
applyTemplates({
...cases,
name: controller.type.split('.').pop()!.replace('Controller', ''),
sharedPath: 'shared/' + strings.dasherize(namespace),
controller,
}),
move(normalize(targetPath)),
]);
}
),
]);
);
const defs = services.filter(([name]) => name === moduleName);
console.log(isModule);
console.log(defs);
return chain([]);
return branchAndMerge(chain([createServiceFiles]));
},
]);
}

@ -24,8 +24,8 @@
},
"x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")"
},
"destination": {
"alias": "d",
"target": {
"alias": "t",
"description": "Angular project to place the generated code in",
"type": "string",
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"

@ -1,9 +1,4 @@
export interface Schema {
/**
* Angular project to place the generated code in
*/
destination?: string;
/**
* The name of the backend module to generate code for
*/
@ -13,4 +8,9 @@ export interface Schema {
* Angular project to resolve API definition URL from
*/
source?: string;
/**
* Angular project to place the generated code in
*/
target?: string;
}

@ -1,6 +1,7 @@
export * from './angular';
export * from './ast';
export * from './common';
export * from './rule';
export * from './source';
export * from './text';
export * from './workspace';

@ -0,0 +1,26 @@
import {
apply,
forEach,
mergeWith,
Rule,
SchematicContext,
Source,
Tree,
} from '@angular-devkit/schematics';
export function applyWithOverwrite(source: Source, rules: Rule[]): Rule {
return (tree: Tree, _context: SchematicContext) => {
const rule = mergeWith(apply(source, [...rules, overwriteFileIfExists(tree)]));
return rule(tree, _context);
};
}
export function overwriteFileIfExists(tree: Tree): Rule {
return forEach(fileEntry => {
if (!tree.exists(fileEntry.path)) return fileEntry;
tree.overwrite(fileEntry.path, fileEntry.content);
return null;
});
}

@ -1,4 +1,4 @@
import type { experimental, workspaces } from '@angular-devkit/core';
import { experimental, strings, workspaces } from '@angular-devkit/core';
import { SchematicsException } from '@angular-devkit/schematics';
import type { Tree } from '@angular-devkit/schematics';
import { Exception } from '../enums';
@ -41,7 +41,29 @@ export async function resolveProject(
): Promise<Project> {
name = name || readWorkspaceSchema(tree).defaultProject!;
const workspace = await getWorkspace(tree);
const definition = workspace.projects.get(name);
let definition: Project['definition'] | undefined;
try {
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.dasherize(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.camelize(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition)
try {
name = strings.classify(name);
definition = workspace.projects.get(name);
} catch (_) {}
if (!definition) throw new SchematicsException(Exception.NoProject);

Loading…
Cancel
Save