Skip to content

Commit

Permalink
Merge pull request #5222 from abpframework/feat/5202
Browse files Browse the repository at this point in the history
Introduced separate commands for adding, removing, and refreshing proxies
  • Loading branch information
hikalkan authored Aug 29, 2020
2 parents 88ad0b5 + 47b1aa1 commit 0d27c0f
Show file tree
Hide file tree
Showing 26 changed files with 425 additions and 108 deletions.
18 changes: 14 additions & 4 deletions npm/ng-packs/packages/schematics/src/collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{
"schematics": {
"proxy": {
"description": "ABP Proxy Generator Schematics",
"factory": "./commands/proxy",
"schema": "./commands/proxy/schema.json"
"proxy-add": {
"description": "ABP Proxy Generator Add Schematics",
"factory": "./commands/proxy-add",
"schema": "./commands/proxy-add/schema.json"
},
"proxy-refresh": {
"description": "ABP Proxy Generator Refresh Schematics",
"factory": "./commands/proxy-refresh",
"schema": "./commands/proxy-refresh/schema.json"
},
"proxy-remove": {
"description": "ABP Proxy Generator Remove Schematics",
"factory": "./commands/proxy-remove",
"schema": "./commands/proxy-remove/schema.json"
},
"api": {
"description": "ABP API Generator Schematics",
Expand Down
22 changes: 15 additions & 7 deletions npm/ng-packs/packages/schematics/src/commands/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
url,
} from '@angular-devkit/schematics';
import { Exception } from '../../enums';
import { ServiceGeneratorParams } from '../../models';
import { GenerateProxySchema, ServiceGeneratorParams } from '../../models';
import {
applyWithOverwrite,
buildDefaultPath,
createApiDefinitionReader,
createControllerToServiceMapper,
createImportRefsToModelReducer,
createImportRefToEnumMapper,
createProxyConfigReader,
createProxyConfigWriterCreator,
EnumGeneratorParams,
generateProxyConfigJson,
getEnumNamesFromImports,
getRootNamespace,
interpolate,
Expand All @@ -28,7 +30,6 @@ import {
serializeParameters,
} from '../../utils';
import * as cases from '../../utils/text';
import { Schema as GenerateProxySchema } from './schema';

export default function(schema: GenerateProxySchema) {
const params = removeDefaultPlaceholders(schema);
Expand All @@ -40,9 +41,9 @@ export default function(schema: GenerateProxySchema) {
const target = await resolveProject(tree, params.target!);
const solution = getRootNamespace(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition);
const definitionPath = `${targetPath}/shared/api-definition.json`;
const readApiDefinition = createApiDefinitionReader(definitionPath);
const data = readApiDefinition(tree);
const readProxyConfig = createProxyConfigReader(targetPath);
const createProxyConfigWriter = createProxyConfigWriterCreator(targetPath);
const data = readProxyConfig(tree);
const types = data.types;
const modules = data.modules;
if (!types || !modules) throw new SchematicsException(Exception.InvalidApiDefinition);
Expand Down Expand Up @@ -80,7 +81,14 @@ export default function(schema: GenerateProxySchema) {
modelImports,
});

return branchAndMerge(chain([generateServices, generateModels, generateEnums]));
if (!data.generated.includes(moduleName)) data.generated.push(moduleName);
data.generated.sort();
const json = generateProxyConfigJson(data);
const overwriteProxyConfig = createProxyConfigWriter('overwrite', json);

return branchAndMerge(
chain([generateServices, generateModels, generateEnums, overwriteProxyConfig]),
);
},
]);
}
Expand Down
16 changes: 0 additions & 16 deletions npm/ng-packs/packages/schematics/src/commands/api/schema.ts

This file was deleted.

51 changes: 51 additions & 0 deletions npm/ng-packs/packages/schematics/src/commands/proxy-add/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { strings } from '@angular-devkit/core';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
createProxyWarningSaver,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';

export default function(schema: GenerateProxySchema) {
const params = removeDefaultPlaceholders(schema);
const moduleName = strings.camelize(params.module || 'app');

return chain([
async (host: Tree, _context: SchematicContext) => {
const target = await resolveProject(host, params.target!);
const targetPath = buildDefaultPath(target.definition);
const readProxyConfig = createProxyConfigReader(targetPath);
let generated: string[] = [];

try {
generated = readProxyConfig(host).generated;
const index = generated.findIndex(m => m === moduleName);
if (index < 0) generated.push(moduleName);
} catch (_) {
generated.push(moduleName);
}

const getApiDefinition = createApiDefinitionGetter(params);
const data = { generated, ...(await getApiDefinition(host)) };
data.generated = [];

const clearProxy = createProxyClearer(targetPath);

const saveProxyConfig = createProxyConfigSaver(data, targetPath);

const saveProxyWarning = createProxyWarningSaver(targetPath);

const generateApis = createApisGenerator(schema, generated);

return chainAndMerge([clearProxy, saveProxyConfig, saveProxyWarning, generateApis])(host);
},
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
"properties": {
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"description": "Backend module name",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"source": {
"alias": "s",
"description": "Angular project to resolve root namespace & API definition URL from",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter Angular project name to resolve root namespace & API definition URL from. (default: workspace \"defaultProject\")"
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Angular project to generate code in",
"description": "Target Angular project to place the generated code",
"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 target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}
},
"required": []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';

export default function(schema: GenerateProxySchema) {
const params = removeDefaultPlaceholders(schema);

return async (host: Tree, _context: SchematicContext) => {
const target = await resolveProject(host, params.target!);
const targetPath = buildDefaultPath(target.definition);

const readProxyConfig = createProxyConfigReader(targetPath);
const { generated } = readProxyConfig(host);

const getApiDefinition = createApiDefinitionGetter(params);
const data = { generated, ...(await getApiDefinition(host)) };
data.generated = [];

const clearProxy = createProxyClearer(targetPath);

const saveProxyConfig = createProxyConfigSaver(data, targetPath);

const generateApis = createApisGenerator(schema, generated);

return chainAndMerge([clearProxy, saveProxyConfig, generateApis])(host);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAbpGenerateProxy",
"title": "ABP Generate Proxy Schema",
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module name",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"source": {
"alias": "s",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}
},
"required": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { strings } from '@angular-devkit/core';
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';

export default function(schema: GenerateProxySchema) {
const params = removeDefaultPlaceholders(schema);
const moduleName = strings.camelize(params.module || 'app');

return async (host: Tree, _context: SchematicContext) => {
const target = await resolveProject(host, params.target!);
const targetPath = buildDefaultPath(target.definition);

const readProxyConfig = createProxyConfigReader(targetPath);
const { generated } = readProxyConfig(host);

const index = generated.findIndex(m => m === moduleName);
if (index < 0) return host;
generated.splice(index, 1);

const getApiDefinition = createApiDefinitionGetter(params);
const data = { generated, ...(await getApiDefinition(host)) };
data.generated = [];

const clearProxy = createProxyClearer(targetPath);

const saveProxyConfig = createProxyConfigSaver(data, targetPath);

const generateApis = createApisGenerator(schema, generated);

return chainAndMerge([clearProxy, saveProxyConfig, generateApis])(host);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAbpGenerateProxy",
"title": "ABP Generate Proxy Schema",
"type": "object",
"properties": {
"module": {
"alias": "m",
"description": "Backend module name",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter backend module name. (default: \"app\")"
},
"source": {
"alias": "s",
"description": "Source Angular project for API definition URL & root namespace resolution",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter source Angular project for API definition URL & root namespace resolution. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
"description": "Target Angular project to place the generated code",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Plese enter target Angular project to place the generated code. (default: workspace \"defaultProject\")"
}
},
"required": []
}
42 changes: 0 additions & 42 deletions npm/ng-packs/packages/schematics/src/commands/proxy/index.ts

This file was deleted.

Loading

0 comments on commit 0d27c0f

Please sign in to comment.