Skip to content

Commit 0071810

Browse files
committed
Fix imports in apigen schematic
1 parent 9f18fef commit 0071810

File tree

14 files changed

+107
-68
lines changed

14 files changed

+107
-68
lines changed

projects/angular-odata/schematics/apigen/angular/api-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Base } from './base';
33
import { url, Source } from '@angular-devkit/schematics';
44
import { Schema as ApiGenSchema } from '../schema';
55
import { Package } from './package';
6+
import { Import } from './import';
67

78
export class ApiConfig extends Base {
89
constructor(pkg: Package, options: ApiGenSchema) {
@@ -11,7 +12,7 @@ export class ApiConfig extends Base {
1112
public override template(): Source {
1213
return url('./files/api-config');
1314
}
14-
public override variables(): { [name: string]: any } {
15+
public override variables(imports: Import[]): { [name: string]: any } {
1516
return {
1617
serviceRootUrl: this.options.serviceRootUrl,
1718
metadataUrl: this.options.metadata,

projects/angular-odata/schematics/apigen/angular/base.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ export class Callable {
105105
parameters.length === 0
106106
? 'null'
107107
: `{${parameters
108-
.map((p) => {
109-
const op = optional.includes(p);
110-
return `${p.Name}${op ? '?' : ''}: ${toTypescriptType(p.Type)}`;
111-
})
112-
.join(', ')}}`;
108+
.map((p) => {
109+
const op = optional.includes(p);
110+
return `${p.Name}${op ? '?' : ''}: ${toTypescriptType(p.Type)}`;
111+
})
112+
.join(', ')}}`;
113113
return `public ${methodName}(${keyParameter}) {
114114
return this.${bindingMethod}(${key}).${baseMethod}<${parametersType}, ${retType}>('${this.fullName()}');
115115
}`;
@@ -235,7 +235,7 @@ export abstract class Base {
235235
constructor(
236236
protected pkg: Package,
237237
protected options: ApiGenSchema,
238-
) {}
238+
) { }
239239

240240
public abstract name(): string;
241241
public abstract fileName(): string;
@@ -244,7 +244,7 @@ export abstract class Base {
244244

245245
public abstract importTypes(): string[];
246246
public abstract template(): Source;
247-
public abstract variables(): { [name: string]: any };
247+
public abstract variables(imports: Import[]): { [name: string]: any };
248248

249249
public path(): string {
250250
const directory = this.directory();
@@ -253,7 +253,6 @@ export abstract class Base {
253253
}
254254

255255
public imports(): Import[] {
256-
this.cleanImportedNames();
257256
const groups = this.dependencies
258257
.filter((a) => a[2].path() != this.path())
259258
.reduce(
@@ -270,16 +269,20 @@ export abstract class Base {
270269
const imports = Object.entries(groups).map(([path, items]) => {
271270
const names = items.reduce((acc, i) => [...acc, i[0]], [] as string[]);
272271
const aliases = items.reduce((acc, i) => [...acc, i[1]], [] as string[]);
273-
return new Import(names, aliases, path);
272+
return new Import(names, aliases, items[0][2].path(), path);
274273
});
275274
return imports;
276275
}
277276

278-
public importedName?: string;
279-
public cleanImportedNames() {
280-
for (let d of this.dependencies) {
281-
d[2].importedName = d[1];
277+
public importedName(imports: Import[]): string {
278+
const imp = imports.find((i) => i.absPath === this.path());
279+
if (imp !== undefined) {
280+
const index = imp.names.findIndex((n) => n === this.name());
281+
if (index !== -1) {
282+
return imp.aliases[index];
283+
}
282284
}
285+
return this.name();
283286
}
284287

285288
protected dependencies: [string, string, Base][] = [];
@@ -288,7 +291,7 @@ export abstract class Base {
288291
const name = renderable.name()!;
289292
let alias = name;
290293
while (this.dependencies.some((d) => d[1] === alias)) {
291-
alias = getRandomName();
294+
alias = getRandomName({suffix: name});
292295
}
293296
this.dependencies.push([name, alias, renderable]);
294297
}
@@ -319,7 +322,7 @@ export class Index extends Base {
319322
public override template(): Source {
320323
return url('./files/index');
321324
}
322-
public override variables(): { [name: string]: any } {
325+
public override variables(imports: Import[]): { [name: string]: any } {
323326
return { ...this.options };
324327
}
325328
public override name() {
@@ -350,7 +353,7 @@ export class Metadata extends Base {
350353
public override template(): Source {
351354
return url('./files/metadata');
352355
}
353-
public override variables(): { [name: string]: any } {
356+
public override variables(imports: Import[]): { [name: string]: any } {
354357
return { content: JSON.stringify(this.meta.toJson(), null, 2) };
355358
}
356359
public override name() {

projects/angular-odata/schematics/apigen/angular/collection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Schema as ApiGenSchema } from '../schema';
66
import { Model } from './model';
77
import { Entity } from './entity';
88
import { Package } from './package';
9+
import { Import } from './import';
910

1011
export class Collection extends Base {
1112
constructor(
@@ -25,7 +26,7 @@ export class Collection extends Base {
2526
public override template(): Source {
2627
return url('./files/collection');
2728
}
28-
public override variables(): { [name: string]: any } {
29+
public override variables(imports: Import[]): { [name: string]: any } {
2930
return {
3031
type: this.name() + 'Collection',
3132
baseType: this.edmType.BaseType ? this.edmType.BaseType + 'Collection' : null,

projects/angular-odata/schematics/apigen/angular/entity.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Schema as ApiGenSchema } from '../schema';
66
import { CsdlNavigationProperty, CsdlProperty } from '../metadata/csdl/csdl-structural-property';
77
import { toTypescriptType } from '../utils';
88
import { Package } from './package';
9+
import { Import } from './import';
910

1011
export class EntityProperty {
1112
constructor(
@@ -19,16 +20,16 @@ export class EntityProperty {
1920
return name + (!required ? '?' : '');
2021
}
2122

22-
type() {
23+
type(imports: Import[]) {
2324
const pkg = this.entity.getPackage();
2425
const enumType = pkg.findEnum(this.edmType.Type);
2526
const entityType = pkg.findEntity(this.edmType.Type);
2627
let type = 'any';
2728
if (enumType !== undefined) {
28-
type = enumType.importedName!;
29+
type = enumType.importedName(imports)!;
2930
type += this.edmType.Collection ? '[]' : '';
3031
} else if (entityType !== undefined) {
31-
type = entityType.importedName!;
32+
type = entityType.importedName(imports)!;
3233
type += this.edmType.Collection ? '[]' : '';
3334
} else {
3435
type = toTypescriptType(this.edmType.Type);
@@ -42,6 +43,10 @@ export class EntityProperty {
4243
this.edmType.Type.startsWith('Edm.Geography') || this.edmType.Type.startsWith('Edm.Geometry')
4344
);
4445
}
46+
47+
isDuration(): boolean {
48+
return this.edmType.Type.startsWith('Edm.Duration');
49+
}
4550
}
4651

4752
export class Entity extends Base {
@@ -56,13 +61,13 @@ export class Entity extends Base {
5661
public override template(): Source {
5762
return url('./files/entity');
5863
}
59-
public override variables(): { [name: string]: any } {
64+
public override variables(imports: Import[]): { [name: string]: any } {
6065
return {
6166
type: this.name() + (this.edmType instanceof CsdlEntityType ? 'EntityType' : 'ComplexType'),
6267
baseType: this.edmType.BaseType,
6368
properties: this.properties(),
64-
geoProperties: this.geoProperties(),
6569
hasGeoProperties: this.hasGeoProperties(),
70+
hasDurationProperties: this.hasDurationProperties(),
6671
};
6772
}
6873
public override name() {
@@ -103,10 +108,19 @@ export class Entity extends Base {
103108
...(this.edmType.NavigationProperty ?? []).map((p) => new EntityProperty(this, p)),
104109
];
105110
}
111+
106112
public geoProperties(): EntityProperty[] {
107113
return this.properties().filter((p) => p.isGeoSpatial());
108114
}
109115
public hasGeoProperties(): boolean {
110116
return this.geoProperties().length > 0;
111117
}
118+
119+
public durationProperties() {
120+
return this.properties().filter((p) => p.isDuration());
121+
}
122+
123+
public hasDurationProperties(): boolean {
124+
return this.durationProperties().length > 0;
125+
}
112126
}

projects/angular-odata/schematics/apigen/angular/enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Enum extends Base {
2626
public override template(): Source {
2727
return url('./files/enum');
2828
}
29-
public override variables(): { [name: string]: any } {
29+
public override variables(imports: Import[]): { [name: string]: any } {
3030
return {
3131
type: this.name() + 'EnumType',
3232
values: (this.edmType.Member ?? []).map((m) => new EnumValue(m)),

projects/angular-odata/schematics/apigen/angular/import.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
export class Import {
22
public names: string[] = [];
33
public aliases: string[] = [];
4-
public from: string;
5-
public constructor(names: string[], aliases: string[], from: string) {
4+
public absPath: string;
5+
public relPath: string;
6+
public constructor(names: string[], aliases: string[], absPath: string, relPath: string) {
67
this.names = names;
78
this.aliases = aliases;
8-
this.from = from;
9+
this.absPath = absPath;
10+
this.relPath = relPath;
911
}
1012

1113
public path(): string {
12-
let path = this.from.toString();
14+
let path = this.relPath.toString();
1315
if (!path.startsWith('../')) path = `./${path}`;
1416
return path;
1517
}

0 commit comments

Comments
 (0)