|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC. |
| 4 | + * This code may only be used under the BSD style license found at |
| 5 | + * http://polymer.github.io/LICENSE.txt |
| 6 | + * Code distributed by Google as part of this project is also |
| 7 | + * subject to an additional IP rights grant found at |
| 8 | + * http://polymer.github.io/PATENTS.txt |
| 9 | + */ |
| 10 | +import {Recipe} from '../runtime/recipe/recipe.js'; |
| 11 | +import {Type} from '../runtime/type.js'; |
| 12 | +import {Particle} from '../runtime/recipe/particle.js'; |
| 13 | +import {KotlinGenerationUtils, quote, tryImport} from './kotlin-generation-utils.js'; |
| 14 | +import {HandleConnection} from '../runtime/recipe/handle-connection.js'; |
| 15 | +import {StorageKey} from '../runtime/storageNG/storage-key.js'; |
| 16 | +import {Direction} from '../runtime/manifest-ast-nodes.js'; |
| 17 | + |
| 18 | +const ktUtils = new KotlinGenerationUtils(); |
| 19 | + |
| 20 | +export class PlanGeneratorError extends Error { |
| 21 | + constructor(message: string) { |
| 22 | + super(message); |
| 23 | + this.name = 'PlanGeneratorError'; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** Generates plan objects from resolved recipes. */ |
| 28 | +export class PlanGenerator { |
| 29 | + constructor(private resolvedRecipes: Recipe[], private scope: string) { |
| 30 | + } |
| 31 | + |
| 32 | + /** Generates a Kotlin file with plan classes derived from resolved recipes. */ |
| 33 | + generate(): string { |
| 34 | + const planOutline = [ |
| 35 | + this.fileHeader(), |
| 36 | + ...this.createPlans(), |
| 37 | + this.fileFooter() |
| 38 | + ]; |
| 39 | + |
| 40 | + return planOutline.join('\n'); |
| 41 | + } |
| 42 | + |
| 43 | + /** Converts a resolved recipe into a `Plan` object. */ |
| 44 | + createPlans(): string[] { |
| 45 | + return this.resolvedRecipes.map(recipe => { |
| 46 | + const planName = `${recipe.name}Plan`; |
| 47 | + |
| 48 | + const particles = recipe.particles.map(p => this.createParticle(p)); |
| 49 | + |
| 50 | + const start = `object ${planName} : `; |
| 51 | + return `${start}${ktUtils.applyFun('Plan', [ktUtils.listOf(particles)], start.length)}`; |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + /** Generates a Kotlin `Plan.Particle` instantiation from a Particle. */ |
| 56 | + createParticle(particle: Particle): string { |
| 57 | + const spec = particle.spec; |
| 58 | + const location = (spec && (spec.implBlobUrl || (spec.implFile && spec.implFile.replace('/', '.')))) || ''; |
| 59 | + |
| 60 | + const connectionMappings = Object.entries(particle.connections) |
| 61 | + .map(([key, conn]) => `"${key}" to ${this.createHandleConnection(conn)}`); |
| 62 | + |
| 63 | + return ktUtils.applyFun('Particle', [ |
| 64 | + quote(particle.name), |
| 65 | + quote(location), |
| 66 | + ktUtils.mapOf(connectionMappings, 12) |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + /** Generates a Kotlin `Plan.HandleConnection` from a HandleConnection. */ |
| 71 | + createHandleConnection(connection: HandleConnection): string { |
| 72 | + const storageKey = this.createStorageKey(connection.handle.storageKey); |
| 73 | + const mode = this.createDirection(connection.direction); |
| 74 | + const type = this.createType(connection.type); |
| 75 | + const ttl = 'null'; |
| 76 | + |
| 77 | + return ktUtils.applyFun('HandleConnection', [storageKey, mode, type, ttl], 24); |
| 78 | + } |
| 79 | + |
| 80 | + /** Generates a Kotlin `HandleMode` from a Direction. */ |
| 81 | + createDirection(direction: Direction): string { |
| 82 | + switch (direction) { |
| 83 | + case 'reads': return 'HandleMode.Read'; |
| 84 | + case 'writes': return 'HandleMode.Write'; |
| 85 | + case 'reads writes': return 'HandleMode.ReadWrite'; |
| 86 | + default: throw new PlanGeneratorError( |
| 87 | + `HandleConnection direction '${direction}' is not supported.`); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** Generates a Kotlin `StorageKey` from a StorageKey. */ |
| 92 | + createStorageKey(storageKey: StorageKey | undefined): string { |
| 93 | + return `StorageKeyParser.parse("${(storageKey || '').toString()}")`; |
| 94 | + } |
| 95 | + |
| 96 | + /** Generates a Kotlin `core.arc.type.Type` from a Type. */ |
| 97 | + // TODO(alxr): Implement |
| 98 | + createType(type: Type): string { |
| 99 | + switch (type.tag) { |
| 100 | + case 'Collection': |
| 101 | + break; |
| 102 | + case 'Entity': |
| 103 | + break; |
| 104 | + case 'Handle': |
| 105 | + break; |
| 106 | + case 'Reference': |
| 107 | + break; |
| 108 | + case 'Singleton': |
| 109 | + break; |
| 110 | + case 'TypeVariable': |
| 111 | + break; |
| 112 | + case 'Arc': |
| 113 | + case 'BigCollection': |
| 114 | + case 'Count': |
| 115 | + case 'Interface': |
| 116 | + case 'Slot': |
| 117 | + case 'Tuple': |
| 118 | + default: |
| 119 | + throw Error(`Type of ${type.tag} is not supported.`); |
| 120 | + } |
| 121 | + return 'null'; |
| 122 | + } |
| 123 | + |
| 124 | + fileHeader(): string { |
| 125 | + return `\ |
| 126 | +/* ktlint-disable */ |
| 127 | +@file:Suppress("PackageName", "TopLevelName") |
| 128 | +
|
| 129 | +package ${this.scope} |
| 130 | +
|
| 131 | +// |
| 132 | +// GENERATED CODE -- DO NOT EDIT |
| 133 | +// |
| 134 | +
|
| 135 | +${tryImport('arcs.core.data.*', this.scope)} |
| 136 | +${tryImport('arcs.core.storage.*', this.scope)} |
| 137 | +`; |
| 138 | + } |
| 139 | + |
| 140 | + fileFooter(): string { |
| 141 | + return ``; |
| 142 | + } |
| 143 | +} |
0 commit comments