Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add volo packages map and check during resolve import #20035

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions npm/ng-packs/packages/schematics/src/commands/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
sanitizeTypeNames,
sanitizeControllerTypeNames,
serializeParameters,
resolveAbpPackages,
resolveSelfGenericProps,
} from '../../utils';
import * as cases from '../../utils/text';
Expand Down Expand Up @@ -160,6 +161,8 @@ function createModelGenerator(params: ModelGeneratorParams) {
),
);

resolveAbpPackages(models);

return chain(
models.map(model =>
applyWithOverwrite(url('./files-model'), [
Expand Down
30 changes: 30 additions & 0 deletions npm/ng-packs/packages/schematics/src/constants/volo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,33 @@ export const VOLO_REMOTE_STREAM_CONTENT = [
'Volo.Abp.Content.IRemoteStreamContent',
'Volo.Abp.Content.RemoteStreamContent',
];

export const SAAS_NAMESPACE = 'Volo.Saas';
export const TENANT_KEY = 'tenant';

export const VOLO_PACKAGE_PROXY_IMPORTS = new Map<string, string>([
['Volo.Abp.Identity.IdentityUserDto', '@volo/abp.ng.identity/proxy'],
['Volo.Abp.Identity.IdentityRoleDto', '@volo/abp.ng.identity/proxy'],
['Volo.Abp.FileManagement.FileDescriptorDto', '@volo/abp.ng.file-management/proxy'],
['Volo.Abp.FileManagement.DirectoryContentDto', '@volo/abp.ng.file-management/proxy'],
['Volo.Abp.AuditLogging.AuditLogDto', '@volo/abp.ng.audit-logging/proxy'],
['Volo.Abp.AuditLogging.AuditLogActionDto', '@volo/abp.ng.audit-logging/proxy'],
['Volo.Abp.AuditLogging.EntityChangeDto', '@volo/abp.ng.audit-logging/proxy'],
['Volo.Abp.AuditLogging.EntityPropertyChangeDto', '@volo/abp.ng.audit-logging/proxy'],
['Volo.Chat.Messages', '@volo/abp.ng.chat/proxy'],
['Volo.Chat.ChatMessageDto', '@volo/abp.ng.chat/proxy'],
['Volo.Chat.ChatConversationDto', '@volo/abp.ng.chat/proxy'],
['Volo.Abp.Gdpr.GdprRequestDto', '@volo/abp.ng.gdpr/proxy'],
['Volo.Saas.Tenants.TenantDto', '@volo/abp.ng.saas/proxy'],
//TenantConnectionStringDto it must end with Strings.
['Volo.Saas.Tenants.TenantConnectionStringDto', '@volo/abp.ng.saas/proxy'],
['Volo.Saas.Editions.EditionDto', '@volo/abp.ng.saas/proxy'],
[
'Volo.Abp.TextTemplateManagement.TextTemplates.TextTemplateContentDto',
'@volo/abp.ng.saas/proxy',
],
[
'Volo.Abp.TextTemplateManagement.TextTemplates.TemplateDefinitionDto',
'@volo/abp.ng.saas/proxy',
],
]);
47 changes: 44 additions & 3 deletions npm/ng-packs/packages/schematics/src/utils/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VOLO_REGEX } from '../constants';
import { Interface, Model, Property, PropertyDef, Type, TypeWithEnum } from '../models';
import { Import, Interface, Model, Property, PropertyDef, Type, TypeWithEnum } from '../models';
import {
extractGenerics,
generateRefWithPlaceholders,
Expand All @@ -17,6 +16,8 @@ import {
extendsSelf,
removeTypeModifiers,
} from './type';
import { SAAS_NAMESPACE, TENANT_KEY, VOLO_PACKAGE_PROXY_IMPORTS, VOLO_REGEX } from '../constants';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const shouldQuote = require('should-quote');

Expand All @@ -40,7 +41,7 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) {
sortInterfaces(interfaces);

interfaces.forEach(_interface => {
if (VOLO_REGEX.test(_interface.ref)) return;
if (VOLO_REGEX.test(_interface.ref) || VOLO_PACKAGE_PROXY_IMPORTS.has(_interface.ref)) return;

if (types[_interface.ref]!.isEnum) {
if (!enums.includes(_interface.ref)) enums.push(_interface.ref);
Expand Down Expand Up @@ -202,6 +203,46 @@ export function parseBaseTypeWithGenericTypes(type: string): string[] {
return nodeToText(parsedTypeNode);
}

export function resolveAbpPackages(models: Model[]) {
for (const model of models) {
renamePropForTenant(model.interfaces);

model.imports.forEach((imp, i) => {
fixImportNameForTenant(imp);

for (const ref of imp.refs) {
const path = VOLO_PACKAGE_PROXY_IMPORTS.get(ref);
if (path) {
model.imports[i] = new Import({ ...imp, path });
}
}
});
}
}

function renamePropForTenant(interfaces: Interface[]) {
for (const inters of interfaces) {
for (const prop of inters.properties) {
const isTenant = prop.name.toLocaleLowerCase().includes(TENANT_KEY);
const isSaasDto = prop.refs.filter(f => f.startsWith(SAAS_NAMESPACE)).length > 0;

if (isTenant && isSaasDto) {
prop.type = 'Saas' + prop.type;
}
}
}
}

function fixImportNameForTenant(imp: Import) {
imp.specifiers.forEach((spe, index) => {
const isTenant = spe.toLocaleLowerCase().includes(TENANT_KEY);

if (isTenant) {
imp.specifiers[index] = 'Saas' + spe;
}
});
}

export function resolveSelfGenericProps(params: Partial<ModelGeneratorParams>) {
const { types, solution } = params;
if (!types || !solution) {
Expand Down
Loading