Browse Source

Merge pull request #5173 from abpframework/fix/5167

Resolved solution name from environment variables in proxy generator
pull/5175/head
Bunyamin Coskuner 6 years ago
committed by GitHub
parent
commit
c47af2bdee
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      npm/ng-packs/apps/dev-app/src/environments/environment.ts
  2. 1
      npm/ng-packs/packages/core/src/lib/models/config.ts
  3. 12
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  4. 26
      npm/ng-packs/packages/schematics/src/commands/api/schema.json
  5. 12
      npm/ng-packs/packages/schematics/src/commands/api/schema.ts
  6. 24
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.json
  7. 11
      npm/ng-packs/packages/schematics/src/commands/proxy/schema.ts
  8. 4
      npm/ng-packs/packages/schematics/src/enums/exception.ts
  9. 4
      npm/ng-packs/packages/schematics/src/utils/namespace.ts
  10. 25
      npm/ng-packs/packages/schematics/src/utils/source.ts
  11. 1
      templates/app/angular/src/environments/environment.ts
  12. 2
      templates/module/angular/projects/dev-app/src/environments/environment.ts

1
npm/ng-packs/apps/dev-app/src/environments/environment.ts

@ -20,6 +20,7 @@ export const environment = {
apis: {
default: {
url: 'https://localhost:44305',
rootNamespace: 'MyCompanyName.MyProjectName',
},
},
} as Config.Environment;

1
npm/ng-packs/packages/core/src/lib/models/config.ts

@ -24,6 +24,7 @@ export namespace Config {
export interface ApiConfig {
[key: string]: string;
rootNamespace?: string;
url: string;
}

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

@ -20,6 +20,7 @@ import {
createImportRefToEnumMapper,
EnumGeneratorParams,
getEnumNamesFromImports,
getRootNamespace,
interpolate,
ModelGeneratorParams,
resolveProject,
@ -29,18 +30,21 @@ import * as cases from '../../utils/text';
import { 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 source = await resolveProject(tree, params.source!);
const target = await resolveProject(tree, params.target!);
const solution = getRootNamespace(tree, source, moduleName);
const targetPath = buildDefaultPath(target.definition);
const readApiDefinition = createApiDefinitionReader(
`${targetPath}/shared/api-definition.json`,
);
const definitionPath = `${targetPath}/shared/api-definition.json`;
const readApiDefinition = createApiDefinitionReader(definitionPath);
const data = readApiDefinition(tree);
const types = data.types;
const modules = data.modules;
if (!types || !modules) throw new SchematicsException(Exception.InvalidApiDefinition);
const definition = data.modules[moduleName];
if (!definition)
throw new SchematicsException(interpolate(Exception.InvalidModule, moduleName));

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

@ -4,36 +4,36 @@
"title": "ABP Generate API Schema",
"type": "object",
"properties": {
"solution": {
"alias": "x",
"description": "Solution name",
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter the solution name. (case-sensitive, eg. Acme.BookStore)"
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
},
"target": {
"alias": "t",
"description": "Angular project to generate code in",
"source": {
"alias": "s",
"description": "Angular project to resolve root namespace & API definition URL from",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
"x-prompt": "Plese enter Angular project name to resolve root namespace & API definition URL from. (default: workspace \"defaultProject\")"
},
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"target": {
"alias": "t",
"description": "Angular project to generate code in",
"type": "string",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
"x-prompt": "Plese enter Angular project name to place generated code in. (default: workspace \"defaultProject\")"
}
},
"required": ["solution"]
"required": []
}

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

@ -1,16 +1,16 @@
export interface Schema {
/**
* Solution name
* Backend module to generate code for
*/
solution: string;
module?: string;
/**
* Angular project to generate code in
* Angular project to resolve root namespace & API definition URL from
*/
target?: string;
source?: string;
/**
* Backend module to generate code for
* Angular project to generate code in
*/
module?: string;
target?: string;
}

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

@ -4,25 +4,25 @@
"title": "ABP Generate Proxy Schema",
"type": "object",
"properties": {
"solution": {
"alias": "x",
"description": "Solution name",
"module": {
"alias": "m",
"description": "Backend module to generate code for",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter the solution name. (case-sensitive, eg. Acme.BookStore)"
"x-prompt": "Please enter name of the backend module you wish to generate proxies for. (default: \"app\")"
},
"source": {
"alias": "s",
"description": "Angular project to resolve API definition URL from",
"description": "Angular project to resolve root namespace & API definition URL from",
"type": "string",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Plese enter Angular project name to resolve API definition URL from. (default: workspace \"defaultProject\")"
"x-prompt": "Plese enter Angular project name to resolve root namespace & API definition URL from. (default: workspace \"defaultProject\")"
},
"target": {
"alias": "t",
@ -33,17 +33,7 @@
"index": 2
},
"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": ["solution"]
"required": []
}

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

@ -1,11 +1,11 @@
export interface Schema {
/**
* Solution name
* Backend module to generate code for
*/
solution: string;
module?: string;
/**
* Angular project to resolve API definition URL from
* Angular project to resolve root namespace & API definition URL from
*/
source?: string;
@ -13,9 +13,4 @@ export interface Schema {
* Angular project to generate code in
*/
target?: string;
/**
* Backend module to generate code for
*/
module?: string;
}

4
npm/ng-packs/packages/schematics/src/enums/exception.ts

@ -1,12 +1,14 @@
export const enum Exception {
FileNotFound = '[File Not Found] There is no file at "{0}" path.',
InvalidModule = '[Invalid Module] Backend module "{0}" does not exist in API definition.',
InvalidApiDefinition = '[Invalid API Definition] The provided API definition is invalid.',
InvalidWorkspace = '[Invalid Workspace] The angular.json should be a valid JSON file.',
NoApi = '[API Not Available] Please double-check the URL in the source project environment and make sure your application is up and running.',
NoApiDefinition = '[API Definition Not Found] There is no valid API definition file at "{0}".',
NoApiDefinition = '[API Definition Not Found] There is no API definition file at "{0}".',
NoProject = '[Project Not Found] Either define a default project in your workspace or specify the project name in schematics options.',
NoTypeDefinition = '[Type Definition Not Found] There is no type definition for "{0}".',
NoWorkspace = '[Workspace Not Found] Make sure you are running schematics at the root directory of your workspace and it has an angular.json file.',
NoEnvironment = '[Environment Not Found] An environment file cannot be located in "{0}" project.',
NoApiUrl = '[API URL Not Found] Cannot resolve API URL for "{1}" module from "{0}" project.',
NoRootNamespace = '[Root Namespace Not Found] Cannot resolve root namespace for "{1}" module from "{0}" project.',
}

4
npm/ng-packs/packages/schematics/src/utils/namespace.ts

@ -8,8 +8,8 @@ export function parseNamespace(solution: string, type: string) {
.join('.');
solution.split('.').reduceRight((acc, part) => {
acc = part + '.' + acc;
const regex = new RegExp('^' + acc + '(Controllers.)?');
acc = `${part}\.${acc}`;
const regex = new RegExp(`^${acc}(Controllers\.)?`);
namespace = namespace.replace(regex, '');
return acc;
}, '');

25
npm/ng-packs/packages/schematics/src/utils/source.ts

@ -25,6 +25,31 @@ export async function getApiDefinition(url: string) {
return body;
}
export function getRootNamespace(tree: Tree, project: Project, moduleName: string) {
const environmentExpr = readEnvironment(tree, project.definition);
if (!environmentExpr)
throw new SchematicsException(interpolate(Exception.NoEnvironment, project.name));
let assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
moduleName,
'rootNamespace',
]);
if (!assignment)
assignment = getAssignedPropertyFromObjectliteral(environmentExpr, [
'apis',
'default',
'rootNamespace',
]);
if (!assignment)
throw new SchematicsException(interpolate(Exception.NoRootNamespace, project.name, moduleName));
return assignment.replace(/[`'"]/g, '');
}
export function getSourceUrl(tree: Tree, project: Project, moduleName: string) {
const environmentExpr = readEnvironment(tree, project.definition);

1
templates/app/angular/src/environments/environment.ts

@ -19,6 +19,7 @@ export const environment = {
apis: {
default: {
url: 'https://localhost:44305',
rootNamespace: 'MyCompanyName.MyProjectName',
},
},
} as Config.Environment;

2
templates/module/angular/projects/dev-app/src/environments/environment.ts

@ -18,9 +18,11 @@ export const environment = {
apis: {
default: {
url: 'https://localhost:44301',
rootNamespace: 'MyCompanyName.MyProjectName',
},
MyProjectName: {
url: 'https://localhost:44300',
rootNamespace: 'MyCompanyName.MyProjectName',
},
},
} as Config.Environment;

Loading…
Cancel
Save