Browse Source

Merge pull request #5246 from abpframework/fix/5245

Improved import experience of generated code in proxy generator
pull/5256/head
Bunyamin Coskuner 6 years ago
committed by GitHub
parent
commit
8abc965a4d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-enum/proxy/__namespace@dir__/__name@kebab__.enum.ts.template
  2. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-model/proxy/__namespace@dir__/models.ts.template
  3. 0
      npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template
  4. 13
      npm/ng-packs/packages/schematics/src/commands/proxy-add/index.ts
  5. 14
      npm/ng-packs/packages/schematics/src/commands/proxy-refresh/index.ts
  6. 14
      npm/ng-packs/packages/schematics/src/commands/proxy-remove/index.ts
  7. 10
      npm/ng-packs/packages/schematics/src/constants/proxy.ts
  8. 48
      npm/ng-packs/packages/schematics/src/utils/barrel.ts
  9. 2
      npm/ng-packs/packages/schematics/src/utils/enum.ts
  10. 8
      npm/ng-packs/packages/schematics/src/utils/file.ts
  11. 2
      npm/ng-packs/packages/schematics/src/utils/index.ts
  12. 32
      npm/ng-packs/packages/schematics/src/utils/path.ts
  13. 13
      npm/ng-packs/packages/schematics/src/utils/rule.ts
  14. 9
      npm/ng-packs/packages/schematics/src/utils/source.ts

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

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

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

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

@ -3,13 +3,14 @@ import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
createProxyIndexGenerator,
createProxyWarningSaver,
mergeAndAllowDelete,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';
@ -45,7 +46,15 @@ export default function(schema: GenerateProxySchema) {
const generateApis = createApisGenerator(schema, generated);
return chainAndMerge([clearProxy, saveProxyConfig, saveProxyWarning, generateApis])(host);
const generateIndex = createProxyIndexGenerator(targetPath);
return chain([
mergeAndAllowDelete(host, clearProxy),
saveProxyConfig,
saveProxyWarning,
generateApis,
generateIndex,
]);
},
]);
}

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

@ -1,13 +1,14 @@
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
createProxyIndexGenerator,
mergeAndAllowDelete,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';
@ -32,6 +33,13 @@ export default function(schema: GenerateProxySchema) {
const generateApis = createApisGenerator(schema, generated);
return chainAndMerge([clearProxy, saveProxyConfig, generateApis])(host);
const generateIndex = createProxyIndexGenerator(targetPath);
return chain([
mergeAndAllowDelete(host, clearProxy),
saveProxyConfig,
generateApis,
generateIndex,
]);
};
}

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

@ -1,14 +1,15 @@
import { strings } from '@angular-devkit/core';
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
import { GenerateProxySchema } from '../../models';
import {
buildDefaultPath,
chainAndMerge,
createApiDefinitionGetter,
createApisGenerator,
createProxyClearer,
createProxyConfigReader,
createProxyConfigSaver,
createProxyIndexGenerator,
mergeAndAllowDelete,
removeDefaultPlaceholders,
resolveProject,
} from '../../utils';
@ -38,6 +39,13 @@ export default function(schema: GenerateProxySchema) {
const generateApis = createApisGenerator(schema, generated);
return chainAndMerge([clearProxy, saveProxyConfig, generateApis])(host);
const generateIndex = createProxyIndexGenerator(targetPath);
return chain([
mergeAndAllowDelete(host, clearProxy),
saveProxyConfig,
generateApis,
generateIndex,
]);
};
}

10
npm/ng-packs/packages/schematics/src/constants/proxy.ts

@ -5,14 +5,14 @@ export const PROXY_WARNING_PATH = `${PROXY_PATH}/README.md`;
export const PROXY_WARNING = `# Proxy Generation Output
This directory includes the output of the latest proxy generation.
The \`services\`, \`models\`, and \`enums\` folders will be overwritten when proxy generation is run again.
Therefore, please do not place your own content in those folders.
The files and folders in it will be overwritten when proxy generation is run again.
Therefore, please do not place your own content in this folder.
In addition, \`generate-proxy.json\` works like a lock file.
It includes information used by the proxy generator, so please do not delete or modify it.
Finally, the name of this folder should not be changed for two reasons:
- Proxy generator will keep creating this folder and you will have multiple copies of the same content.
- ABP Suite generates imports from this folder and uses the path \`/proxy\` when doing so.
Finally, the name of the files and folders should not be changed for two reasons:
- Proxy generator will keep creating them at those paths and you will have multiple copies of the same content.
- ABP Suite generates files which include imports from this folder.
`;

48
npm/ng-packs/packages/schematics/src/utils/barrel.ts

@ -0,0 +1,48 @@
import { strings } from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { PROXY_PATH } from '../constants';
import { createFileSaver } from './file';
export function createProxyIndexGenerator(targetPath: string) {
return createBarrelsGenerator(targetPath + PROXY_PATH);
}
export function createBarrelsGenerator(rootPath: string) {
return (tree: Tree) => {
generateBarrelFromPath(tree, rootPath);
return tree;
};
}
export function generateBarrelFromPath(tree: Tree, indexPath: string) {
const saveFile = createFileSaver(tree);
const dir = tree.getDir(indexPath);
const _exports: string[] = [];
dir.subfiles.forEach(fragment => {
if (!fragment.endsWith('.ts')) return;
_exports.push(`export * from './${fragment.replace(/\.ts$/, '')}';`);
});
dir.subdirs.forEach(fragment => {
const subDirPath = indexPath + '/' + fragment;
const subDir = tree.getDir(subDirPath);
let hasFiles = false;
subDir.visit(() => (hasFiles = true));
if (!hasFiles) return;
_exports.push(`export * as ${strings.classify(fragment)} from './${fragment}';`);
generateBarrelFromPath(tree, subDirPath);
});
_exports.sort();
if (_exports.length)
saveFile(
indexPath + '/index.ts',
_exports.join(`
`),
);
}

2
npm/ng-packs/packages/schematics/src/utils/enum.ts

@ -13,7 +13,7 @@ export interface EnumGeneratorParams {
}
export function isEnumImport(path: string) {
return path.includes('/enums/');
return path.endsWith('.enum');
}
export function getEnumNamesFromImports(serviceImports: Record<string, string[]>) {

8
npm/ng-packs/packages/schematics/src/utils/file.ts

@ -0,0 +1,8 @@
import { Tree } from '@angular-devkit/schematics';
export function createFileSaver(tree: Tree) {
return (filePath: string, fileContent: string) =>
tree.exists(filePath)
? tree.overwrite(filePath, fileContent)
: tree.create(filePath, fileContent);
}

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

@ -1,8 +1,10 @@
export * from './angular';
export * from './api';
export * from './ast';
export * from './barrel';
export * from './common';
export * from './enum';
export * from './file';
export * from './import';
export * from './model';
export * from './namespace';

32
npm/ng-packs/packages/schematics/src/utils/path.ts

@ -1,15 +1,33 @@
import { dir, kebab } from './text';
import { strings } from '@angular-devkit/core';
import { kebab } from './text';
export function relativePathToEnum(namespace: string, enumNamespace: string, enumName: string) {
const repeats = namespace ? namespace.split('.').length : 0;
const path = '..' + '/..'.repeat(repeats) + '/enums/' + dir(enumNamespace);
return removeDoubleSlash(path + '/' + kebab(enumName));
const path = calculateRelativePath(namespace, enumNamespace);
return path + `/${kebab(enumName)}.enum`;
}
export function relativePathToModel(namespace: string, modelNamespace: string) {
const repeats = namespace ? namespace.split('.').length : 0;
const path = '..' + '/..'.repeat(repeats) + '/models/' + dir(modelNamespace);
return removeTrailingSlash(path);
const path = calculateRelativePath(namespace, modelNamespace);
return path + '/models';
}
function calculateRelativePath(ns1: string, ns2: string) {
if (ns1 === ns2) return '.';
const parts1 = ns1 ? ns1.split('.') : [];
const parts2 = ns2 ? ns2.split('.') : [];
while (parts1.length && parts2.length) {
if (parts1[0] !== parts2[0]) break;
parts1.shift();
parts2.shift();
}
const up = '../'.repeat(parts1.length) || '.';
const down = parts2.reduce((acc, p) => acc + '/' + strings.dasherize(p), '');
return removeTrailingSlash(removeDoubleSlash(up + down));
}
function removeDoubleSlash(path: string) {

13
npm/ng-packs/packages/schematics/src/utils/rule.ts

@ -1,6 +1,6 @@
import {
apply,
chain,
callRule,
forEach,
MergeStrategy,
mergeWith,
@ -18,12 +18,11 @@ export function applyWithOverwrite(source: Source, rules: Rule[]): Rule {
};
}
export function chainAndMerge(rules: Rule[]) {
return (host: Tree) => async (tree: Tree, context: SchematicContext) =>
host.merge(
(await (chain(rules)(tree, context) as any).toPromise()) as Tree,
MergeStrategy.AllowDeleteConflict,
);
export function mergeAndAllowDelete(host: Tree, rule: Rule) {
return async (tree: Tree, context: SchematicContext) => {
const nextTree = await callRule(rule, tree, context).toPromise();
host.merge(nextTree, MergeStrategy.AllowDeleteConflict);
};
}
export function overwriteFileIfExists(tree: Tree): Rule {

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

@ -106,15 +106,18 @@ export function createProxyConfigReader(targetPath: string) {
export function createProxyClearer(targetPath: string) {
targetPath += PROXY_PATH;
const proxyIndexPath = `${targetPath}/index.ts`;
return (tree: Tree) => {
try {
tree.getDir(targetPath).subdirs.forEach(dirName => {
if (!['enums', 'models', 'services'].includes(dirName)) return;
tree.delete(`${targetPath}/${dirName}`);
const dirPath = `${targetPath}/${dirName}`;
tree.getDir(dirPath).visit(filePath => tree.delete(filePath));
tree.delete(dirPath);
});
if (tree.exists(proxyIndexPath)) tree.delete(proxyIndexPath);
return tree;
} catch (_) {
throw new SchematicsException(interpolate(Exception.DirRemoveFailed, targetPath));

Loading…
Cancel
Save