forked from PolymerLabs/arcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema2base.ts
executable file
·175 lines (151 loc) · 5.96 KB
/
schema2base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @license
* Copyright (c) 2019 Google Inc. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* Code distributed by Google as part of this project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import fs from 'fs';
import path from 'path';
import minimist from 'minimist';
import {Manifest} from '../runtime/manifest.js';
import {Runtime} from '../runtime/runtime.js';
import {SchemaGraph, SchemaNode, SchemaSource} from './schema2graph.js';
import {ParticleSpec} from '../runtime/particle-spec.js';
export type AddFieldOptions = Readonly<{
field: string;
typeName: string;
isOptional?: boolean;
refClassName?: string;
refSchemaHash?: string;
listTypeName?: string;
isCollection?: boolean;
}>;
export interface ClassGenerator {
addField(opts: AddFieldOptions): void;
escapeIdentifier(ident: string): string;
generatePredicates(): void;
generate(schemaHash: string, fieldCount: number): string;
}
export class NodeAndGenerator {
node: SchemaNode;
generator: ClassGenerator;
hash: string;
}
export abstract class Schema2Base {
namespace: string;
constructor(readonly opts: minimist.ParsedArgs) {
Runtime.init('../..');
}
async call() {
fs.mkdirSync(this.opts.outdir, {recursive: true});
for (const file of this.opts._) {
if (fs.existsSync(file)) {
await this.processFile(file);
} else {
throw new Error(`File not found: ${file}`);
}
}
}
private async processFile(src: string) {
const outName = this.opts.outfile || this.outputName(path.basename(src));
const outPath = path.join(this.opts.outdir, outName);
if (this.opts.update && fs.existsSync(outPath) && fs.statSync(outPath).mtimeMs > fs.statSync(src).mtimeMs) {
return;
}
const manifest = await Runtime.parseFile(src);
if (manifest.errors.some(e => e.severity !== 'warning')) {
return;
}
this.namespace = manifest.meta.namespace;
if (!this.namespace) {
throw new Error(`Namespace is required in '${src}' for code generation.`);
}
const classes = await this.processManifest(manifest);
if (classes.length === 0) {
console.warn(`Could not find any particle connections with schemas in '${src}'`);
return;
}
const outFile = fs.openSync(outPath, 'w');
fs.writeSync(outFile, this.fileHeader(outName));
for (const text of classes) {
fs.writeSync(outFile, text.replace(/ +\n/g, '\n'));
}
fs.writeSync(outFile, this.fileFooter());
fs.closeSync(outFile);
}
async processManifest(manifest: Manifest): Promise<string[]> {
// TODO: consider an option to generate one file per particle
const classes: string[] = [];
for (const particle of manifest.particles) {
const nodes = await this.calculateNodeAndGenerators(particle);
classes.push(...nodes.map(({generator, node, hash}) =>
generator.generate(hash, Object.entries(node.schema.fields).length)));
if (this.opts.test_harness) {
classes.push(this.generateTestHarness(particle, nodes.map(n => n.node)));
continue;
}
classes.push(this.generateParticleClass(particle, nodes));
}
return classes;
}
async calculateNodeAndGenerators(particle: ParticleSpec): Promise<NodeAndGenerator[]> {
const graph = new SchemaGraph(particle);
const nodes: NodeAndGenerator[] = [];
for (const node of graph.walk()) {
const generator = this.getClassGenerator(node);
if (!node.schema) {
continue;
}
for (const [field, descriptor] of Object.entries(node.schema.fields)) {
if (descriptor.kind === 'schema-primitive') {
if (['Text', 'URL', 'Number', 'Boolean'].includes(descriptor.type)) {
generator.addField({field, typeName: descriptor.type});
} else {
throw new Error(`Schema type '${descriptor.type}' for field '${field}' is not supported`);
}
} else if (descriptor.kind === 'schema-reference' || (descriptor.kind === 'schema-collection' && descriptor.schema.kind === 'schema-reference')) {
const isCollection = descriptor.kind === 'schema-collection';
const schemaNode = node.refs.get(field);
generator.addField({
field,
typeName: 'Reference',
isCollection,
refClassName: schemaNode.entityClassName,
refSchemaHash: await schemaNode.schema.hash(),
});
} else if (descriptor.kind === 'schema-collection') {
const schema = descriptor.schema;
if (!((schema.kind === 'kotlin-primitive') || ['Text', 'URL', 'Number', 'Boolean'].includes(schema.type))) {
throw new Error(`Schema type '${schema.type}' for field '${field}' is not supported`);
}
generator.addField({field, typeName: schema.type, isCollection: true});
} else if (descriptor.kind === 'kotlin-primitive') {
generator.addField({field, typeName: descriptor.type});
} else if (descriptor.kind === 'schema-ordered-list') {
generator.addField({field, typeName: 'List', listTypeName: descriptor.schema.type});
}
else {
throw new Error(`Schema kind '${descriptor.kind}' for field '${field}' is not supported`);
}
}
if (node.schema.refinement) {
generator.generatePredicates();
}
const hash = await node.schema.hash();
nodes.push({node, generator, hash});
}
return nodes;
}
upperFirst(s: string): string {
return s[0].toUpperCase() + s.slice(1);
}
outputName(baseName: string): string { return ''; }
fileHeader(outName: string): string { return ''; }
fileFooter(): string { return ''; }
abstract getClassGenerator(node: SchemaNode): ClassGenerator;
abstract generateParticleClass(particle: ParticleSpec, nodes: NodeAndGenerator[]): string;
abstract generateTestHarness(particle: ParticleSpec, nodes: SchemaNode[]): string;
}