Browse Source

feat: add a base for model generation in schematics

pull/5137/head
Arman Ozak 6 years ago
parent
commit
bf017bb7f0
  1. 25
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  2. 8
      npm/ng-packs/packages/schematics/src/models/api-definition.ts
  3. 1
      npm/ng-packs/packages/schematics/src/models/index.ts
  4. 16
      npm/ng-packs/packages/schematics/src/models/method.ts
  5. 39
      npm/ng-packs/packages/schematics/src/models/model.ts
  6. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  7. 10
      npm/ng-packs/packages/schematics/src/utils/model.ts
  8. 6
      npm/ng-packs/packages/schematics/src/utils/service.ts

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

@ -1,7 +1,7 @@
import { normalize, strings } from '@angular-devkit/core';
import { applyTemplates, branchAndMerge, chain, move, SchematicContext, SchematicsException, Tree, url } from '@angular-devkit/schematics';
import { Exception } from '../../enums';
import { applyWithOverwrite, buildDefaultPath, createApiDefinitionReader, createControllerToServiceMapper, interpolate, resolveProject, serializeParameters } from '../../utils';
import { applyWithOverwrite, buildDefaultPath, createApiDefinitionReader, createControllerToServiceMapper, createImportRefToModelMapper, interpolate, resolveProject, serializeParameters } from '../../utils';
import * as cases from '../../utils/text';
import type { Schema as GenerateProxySchema } from './schema';
@ -20,14 +20,33 @@ export default function(params: GenerateProxySchema) {
const mapControllerToService = createControllerToServiceMapper(solution, definition.remoteServiceName);
const controllers = Object.values(definition.controllers || {});
const importRefs: string[] = [];
const createServiceFiles = chain(
controllers.map(controller => {
const service = mapControllerToService(controller);
service.imports.forEach(({refs}) => refs.forEach(ref => importRefs.push(ref)));
return applyWithOverwrite(url('./files-service'), [
applyTemplates({
...cases,
serializeParameters,
...mapControllerToService(controller),
...service,
}),
move(normalize(targetPath)),
]);
}
),
);
const mapImportRefToModel = createImportRefToModelMapper(solution);
const createModelFiles = chain(
importRefs.map(ref => {
return applyWithOverwrite(url('./files-model'), [
applyTemplates({
...cases,
...mapImportRefToModel(ref),
}),
move(normalize(targetPath)),
]);
@ -35,7 +54,7 @@ export default function(params: GenerateProxySchema) {
),
);
return branchAndMerge(chain([createServiceFiles]));
return branchAndMerge(chain([createServiceFiles, createModelFiles]));
},
]);
}

8
npm/ng-packs/packages/schematics/src/models/api-definition.ts

@ -11,10 +11,10 @@ export interface Type {
enumNames: string[] | null;
enumValues: number[] | null;
genericArguments: string[] | null;
properties: Property[] | null;
properties: PropertyDef[] | null;
}
export interface Property {
export interface PropertyDef {
name: string;
type: string;
typeSimple: string;
@ -29,11 +29,11 @@ export interface Module {
export interface Controller {
controllerName: string;
type: string;
interfaces: Interface[];
interfaces: InterfaceDef[];
actions: Record<string, Action>;
}
export interface Interface {
export interface InterfaceDef {
type: string;
}

1
npm/ng-packs/packages/schematics/src/models/index.ts

@ -1,6 +1,7 @@
export * from './api-definition';
export * from './import';
export * from './method';
export * from './model';
export * from './project';
export * from './service';
export * from './util';

16
npm/ng-packs/packages/schematics/src/models/method.ts

@ -1,6 +1,7 @@
import { strings } from '@angular-devkit/core';
import { eBindingSourceId, eMethodModifier } from '../enums';
import { ParameterInBody } from './api-definition';
import { Property } from './model';
import { Omissible } from './util';
export class Method {
@ -18,7 +19,7 @@ export class Signature {
generics = '';
modifier = eMethodModifier.Public;
name: string;
parameters: Parameter[] = [];
parameters: Property[] = [];
returnType = '';
constructor(options: SignatureOptions) {
@ -70,16 +71,3 @@ export type BodyOptions = Omissible<
Omit<Body, 'registerActionParameter'>,
'params' | 'requestType'
>;
export class Parameter {
name: string;
type: string;
default: string = ''; // convert actual value with JSON.stringify if not null
optional: '' | '?' = '';
constructor(options: ParameterOptions) {
Object.assign(this, options);
}
}
export type ParameterOptions = Omissible<Parameter, 'default' | 'optional'>;

39
npm/ng-packs/packages/schematics/src/models/model.ts

@ -0,0 +1,39 @@
import { Import } from './import';
import { Omissible } from './util';
export class Model {
imports: Import[] = [];
interfaces: Interface[] = [];
namespace: string;
constructor(options: ModelOptions) {
Object.assign(this, options);
}
}
export type ModelOptions = Omissible<Model, 'imports' | 'interfaces'>;
export class Interface {
base: string | null;
identifier: string;
properties: Property[] = [];
constructor(options: InterfaceOptions) {
Object.assign(this, options);
}
}
export type InterfaceOptions = Omissible<Interface, 'properties'>;
export class Property {
name: string;
type: string;
default: string = '';
optional: '' | '?' = '';
constructor(options: PropertyOptions) {
Object.assign(this, options);
}
}
export type PropertyOptions = Omissible<Property, 'default' | 'optional'>;

1
npm/ng-packs/packages/schematics/src/utils/index.ts

@ -1,6 +1,7 @@
export * from './angular';
export * from './ast';
export * from './common';
export * from './model';
export * from './namespace';
export * from './path';
export * from './rule';

10
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -0,0 +1,10 @@
import { Model } from '../models';
import { parseNamespace } from './namespace';
export function createImportRefToModelMapper(solution: string) {
return (importRef: string) => {
return new Model({
namespace: parseNamespace(solution, importRef),
});
};
}

6
npm/ng-packs/packages/schematics/src/utils/service.ts

@ -1,10 +1,10 @@
import { eImportKeyword } from '../enums';
import { Action, Body, Controller, Import, Method, Parameter, Service, Signature } from '../models';
import { Action, Body, Controller, Import, Method, Property, Service, Signature } from '../models';
import { parseNamespace } from './namespace';
import { relativePathFromServiceToModel } from './path';
import { parseGenerics } from './tree';
export function serializeParameters(parameters: Parameter[]) {
export function serializeParameters(parameters: Property[]) {
return parameters.map(p => p.name + p.optional + ': ' + p.type + p.default, '').join(', ');
}
@ -113,7 +113,7 @@ export function createActionToSignatureMapper(solution: string) {
signature.parameters = action.parametersOnMethod.map(p => {
const type = adaptType(p.typeSimple);
const parameter = new Parameter({ name: p.name, type });
const parameter = new Property({ name: p.name, type });
if (p.defaultValue) parameter.default = ` = ${p.defaultValue}`;
else if (p.isOptional) parameter.optional = '?';
return parameter;

Loading…
Cancel
Save