Browse Source

feat: divide proxy schematics into two steps

pull/5137/head
Arman Ozak 6 years ago
parent
commit
1e41f48c5a
  1. 5
      npm/ng-packs/packages/schematics/src/collection.json
  2. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-enum/shared/enums/__namespace@dir__/__name@kebab__.ts.template
  3. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-model/shared/models/__namespace@dir__/index.ts.template
  4. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template
  5. 48
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  6. 39
      npm/ng-packs/packages/schematics/src/commands/api/schema.json
  7. 16
      npm/ng-packs/packages/schematics/src/commands/api/schema.ts
  8. 35
      npm/ng-packs/packages/schematics/src/commands/proxy/index.ts
  9. 30
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.json
  10. 11
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts

5
npm/ng-packs/packages/schematics/src/collection.json

@ -4,6 +4,11 @@
"description": "ABP Proxy Generator Schematics", "description": "ABP Proxy Generator Schematics",
"factory": "./commands/proxy", "factory": "./commands/proxy",
"schema": "./commands/proxy/schema.json" "schema": "./commands/proxy/schema.json"
},
"api": {
"description": "ABP API Generator Schematics",
"factory": "./commands/api",
"schema": "./commands/api/schema.json"
} }
} }
} }

0
npm/ng-packs/packages/schematics/src/commands/proxy/files-enum/__namespacePath__/enums/__name@kebab__.ts.template → npm/ng-packs/packages/schematics/src/commands/api/files-enum/shared/enums/__namespace@dir__/__name@kebab__.ts.template

0
npm/ng-packs/packages/schematics/src/commands/proxy/files-model/__sharedPath__/models/__name@kebab__.ts.template → npm/ng-packs/packages/schematics/src/commands/api/files-model/shared/models/__namespace@dir__/index.ts.template

0
npm/ng-packs/packages/schematics/src/commands/proxy/files-service/__sharedPath__/services/__name@kebab__.service.ts.template → npm/ng-packs/packages/schematics/src/commands/api/files-service/shared/services/__namespace@dir__/__name@kebab__.service.ts.template

48
npm/ng-packs/packages/schematics/src/commands/api/index.ts

@ -0,0 +1,48 @@
import { normalize, strings } from '@angular-devkit/core';
import { applyTemplates, branchAndMerge, chain, move, SchematicContext, Tree, url } from '@angular-devkit/schematics';
import app from '../../mocks/app.gitignore';
import { applyWithOverwrite, buildDefaultPath, createApiDefinitionReader, resolveProject } from '../../utils';
import * as cases from '../../utils/text';
import type { Schema as GenerateProxySchema } from './schema';
export default function(params: GenerateProxySchema) {
const solution = params.solution;
const moduleName = strings.camelize(params.module || 'app');
return chain([
async (tree: Tree, _context: SchematicContext) => {
const target = await resolveProject(tree, params.target!);
const targetPath = buildDefaultPath(target.definition);
const readApiDefinition = createApiDefinitionReader(`${targetPath}/shared/api-definition.json`);
const data = readApiDefinition(tree);
data.modules.app = app;
const controllers = Object.values(data.modules[moduleName]?.controllers || {});
const createServiceFiles = chain(
controllers.map(controller => {
const {controllerName: name, type, actions, interfaces} = controller;
const namespaceRegex = new RegExp('^' + solution + '\.(Controllers\.)?');
const namespace = type.replace(namespaceRegex, '').split('.').slice(0, -1).join('.');
return applyWithOverwrite(url('./files-service'), [
applyTemplates({
...cases,
solution,
namespace,
name,
apiName: data.modules[moduleName].remoteServiceName,
apiUrl: controller.actions[0]?.url,
actions,
interfaces,
}),
move(normalize(targetPath)),
]);
}
),
);
return branchAndMerge(chain([createServiceFiles]));
},
]);
}

39
npm/ng-packs/packages/schematics/src/commands/api/schema.json

@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAbpGenerateAPI",
"title": "ABP Generate API Schema",
"type": "object",
"properties": {
"solution": {
"alias": "x",
"description": "Solution name",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter the solution name. (case-sensitive, eg. Acme.BookStore)"
},
"target": {
"alias": "t",
"description": "Angular project to generate code in",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
},
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
}
},
"required": ["solution"]
}

16
npm/ng-packs/packages/schematics/src/commands/api/schema.ts

@ -0,0 +1,16 @@
export interface Schema {
/**
* Solution name
*/
solution: string;
/**
* Angular project to generate code in
*/
target?: string;
/**
* Backend module to generate code for
*/
module?: string;
}

35
npm/ng-packs/packages/schematics/src/commands/proxy/index.ts

@ -1,9 +1,8 @@
import { normalize, strings } from '@angular-devkit/core'; import { strings } from '@angular-devkit/core';
import { applyTemplates, branchAndMerge, chain, move, SchematicContext, Tree, url } from '@angular-devkit/schematics'; import { branchAndMerge, chain, schematic, SchematicContext, Tree } from '@angular-devkit/schematics';
import { API_DEFINITION_ENDPOINT } from '../../constants'; import { API_DEFINITION_ENDPOINT } from '../../constants';
import { ApiDefinition } from '../../models'; import { ApiDefinition } from '../../models';
import { applyWithOverwrite, buildDefaultPath, getSourceJson, getSourceUrl, isLibrary, resolveProject } from '../../utils'; import { buildDefaultPath, createApiDefinitionSaver, getApiDefinition, getSourceUrl, resolveProject } from '../../utils';
import * as cases from '../../utils/text';
import type { Schema as GenerateProxySchema } from './schema'; import type { Schema as GenerateProxySchema } from './schema';
export default function(params: GenerateProxySchema) { export default function(params: GenerateProxySchema) {
@ -13,34 +12,14 @@ export default function(params: GenerateProxySchema) {
async (tree: Tree, _context: SchematicContext) => { async (tree: Tree, _context: SchematicContext) => {
const source = await resolveProject(tree, params.source!); const source = await resolveProject(tree, params.source!);
const target = await resolveProject(tree, params.target!); const target = await resolveProject(tree, params.target!);
const isModule = isLibrary(target.definition);
const sourceUrl = getSourceUrl(tree, source, moduleName); const sourceUrl = getSourceUrl(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition); const targetPath = buildDefaultPath(target.definition);
const data: ApiDefinition = await getSourceJson(sourceUrl + API_DEFINITION_ENDPOINT); const data: ApiDefinition = await getApiDefinition(sourceUrl + API_DEFINITION_ENDPOINT);
const controllers = Object.values(data.modules[moduleName]?.controllers || {}); const saveApiDefinition = createApiDefinitionSaver(data, `${targetPath}/shared/api-definition.json`)
const createApi = schematic('api', params);
const createServiceFiles = chain( return branchAndMerge(chain([saveApiDefinition, createApi]));
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)),
]);
}
),
);
console.log(isModule);
return branchAndMerge(chain([createServiceFiles]));
}, },
]); ]);
} }

30
npm/ng-packs/packages/schematics/src/commands/proxy/schema.json

@ -1,18 +1,18 @@
{ {
"$schema": "http://json-schema.org/schema", "$schema": "http://json-schema.org/schema",
"id": "SchematicsAbpEntityModule", "id": "SchematicsAbpGenerateProxy",
"title": "ABP Entity Module Schema", "title": "ABP Generate Proxy Schema",
"type": "object", "type": "object",
"properties": { "properties": {
"module": { "solution": {
"alias": "m", "alias": "x",
"description": "The name of the backend module to generate code for", "description": "Solution name",
"type": "string", "type": "string",
"$default": { "$default": {
"$source": "argv", "$source": "argv",
"index": 0 "index": 0
}, },
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")" "x-prompt": "Please enter the solution name. (case-sensitive, eg. Acme.BookStore)"
}, },
"source": { "source": {
"alias": "s", "alias": "s",
@ -26,10 +26,24 @@
}, },
"target": { "target": {
"alias": "t", "alias": "t",
"description": "Angular project to place the generated code in", "description": "Angular project to generate code in",
"type": "string", "type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")" "x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
},
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"type": "string",
"$default": {
"$source": "argv",
"index": 3
},
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
} }
}, },
"required": [] "required": ["solution"]
} }

11
npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts

@ -1,8 +1,8 @@
export interface Schema { export interface Schema {
/** /**
* The name of the backend module to generate code for * Solution name
*/ */
module?: string; solution: string;
/** /**
* Angular project to resolve API definition URL from * Angular project to resolve API definition URL from
@ -10,7 +10,12 @@ export interface Schema {
source?: string; source?: string;
/** /**
* Angular project to place the generated code in * Angular project to generate code in
*/ */
target?: string; target?: string;
/**
* Backend module to generate code for
*/
module?: string;
} }

Loading…
Cancel
Save