Skip to content

Commit 3e7406b

Browse files
authored
recipe2plan 3/n: Plan code-generation (#4865)
## Summary First iteration of generating Plan objects. ## Changelog * creating structure of ts r2p script * Created fast CLI rapper for r2p script * WIP figuring out ways to resolve a recipe * fix sp * Passing lint, still WIP * Build macros for recipe2plan work * Lightweight iteration on recipe2plan, stubbed out tests * Revised method to find corresponding create handles * Added type info * Added method to get all handles to manifest + get all handles by Id * fix: no flatMap * - currently, fails since it cannot find associated stores for handles. * uncleaned, but working recipe resolution * Cleaned up for r2p, pt 1 * Fixed test, added TODO * Fixed build rule, simplified runtime * tools/sigh lint * Impl suggestsions for r2p * Improved build rules * Removed flatMap, added TODOs and link to GH issue * fix, bad comparison op * Stub plan generator works * updated macro to take a package * Testing import is conditional on package * Incremental improvement * tidying * Fixed tests * added particle creation method * flushing out more of plan gen * Squashed commit of the following: commit 17a2ab1 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 16:30:45 2020 -0800 Fixed indentation commit 9094055 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 16:03:51 2020 -0800 revising from feedback commit d350821 Merge: 9054c23 3acb7e7 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:56:55 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into s2k-schemas commit 3acb7e7 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:56:42 2020 -0800 XXS CL to add a hash field to the Schema proto (#4752) commit 9054c23 Merge: d15008e 337b7b2 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:55:27 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into s2k-schemas commit 337b7b2 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:55:06 2020 -0800 recipe2plan 2/n: Validate Handles, Tests (#4834) * creating structure of ts r2p script * Created fast CLI rapper for r2p script * WIP figuring out ways to resolve a recipe * fix sp * Passing lint, still WIP * Build macros for recipe2plan work * Lightweight iteration on recipe2plan, stubbed out tests * Revised method to find corresponding create handles * Added type info * Added method to get all handles to manifest + get all handles by Id * fix: no flatMap * - currently, fails since it cannot find associated stores for handles. * uncleaned, but working recipe resolution * Cleaned up for r2p, pt 1 * stubbed out second test (short, short) * Squashed commit of the following: commit 62dcc57 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:38:14 2020 -0800 Fixed test, added TODO * updates to test, can't get two to fail * Squashed commit of the following: commit bede8d9 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:52:02 2020 -0800 Fixed build rule, simplified runtime commit 62dcc57 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:38:14 2020 -0800 Fixed test, added TODO * can't get test to fail... hmm... * Squashed commit of the following: commit f6df54a Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 15:44:32 2020 -0800 tools/sigh lint commit bede8d9 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:52:02 2020 -0800 Fixed build rule, simplified runtime commit 62dcc57 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:38:14 2020 -0800 Fixed test, added TODO * adjusting recipe copying to include triggers * Revert "adjusting recipe copying to include triggers" This reverts commit 9bc3734 * four tests complete * Added test, fixed minor async issue * Squashed commit of the following: commit 4b05a28 Merge: f4c1d26 99000c2 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:18:14 2020 -0800 Merge branch 'r2p' of github.com:alxrsngrtn/arcs into r2p commit f4c1d26 Merge: 140111f 5c6310d Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:14:35 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p Fixed lint errors commit 99000c2 Merge: 140111f 5c6310d Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:14:35 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p Fixed lint errors commit 140111f Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:08:12 2020 -0800 Removed flatMap, added TODOs and link to GH issue commit 5c6310d Author: jblebrun <[email protected]> Date: Tue Mar 3 11:02:34 2020 -0800 Simplify service test pattern (#4817) * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to Kotlin/kotlinx.coroutines#1222, if the test results in coroutines being finished on other threads, it's possible to receive "This job has not yet completed" exceptions, even though the jobs were properly terminated. Since we don't need the delay-skipping properties of `runBlockingTest`, I think it's OK to use `runBlocking`. commit 472bc84 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 10:27:56 2020 -0800 Improved build rules commit ca1ebf8 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 10:17:13 2020 -0800 Impl suggestsions for r2p commit b5578ea Author: Jason Feinstein <[email protected]> Date: Tue Mar 3 09:54:17 2020 -0800 Add tests for dereferencing references to the HandleManagerTest(s) (#4816) * Add dereferencing tests to the android handle manager test. * Add dereferencing tests to core HandleManager. commit a695797 Author: Jason Feinstein <[email protected]> Date: Mon Mar 2 18:14:07 2020 -0800 Create RawEntityDereferencerTest, storage Reference-> CrdtEntity.Reference (#4812) * Create RawEntityDereferencerTest, make storage Reference implement CrdtEntity.Reference. Also: Create ParcelableReference. * Add dep. Also, apparently read/writeBoolean is Q-only. * Just write null if there is no version map. * Just write null if there is no version map. commit ba7a107 Author: Gogul Balakrishnan <[email protected]> Date: Mon Mar 2 17:51:35 2020 -0800 Add a decoder for PrimitiveTypeProto and an option to disable android constraints in BUILD. (#4793) commit f6df54a Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 15:44:32 2020 -0800 tools/sigh lint commit bede8d9 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:52:02 2020 -0800 Fixed build rule, simplified runtime commit 62dcc57 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:38:14 2020 -0800 Fixed test, added TODO * fix ineq * existing tests passing * Updating invalid type test * Squashed commit of the following: commit b3d92d0 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 12:17:17 2020 -0800 updated test name commit ba7884b Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 12:16:09 2020 -0800 nested unit tests commit ef36fdf Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 12:15:00 2020 -0800 renamed to tryResolve commit 5703f02 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 12:07:18 2020 -0800 rm generator commit 658cb1a Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 12:01:54 2020 -0800 implemented more review suggestions commit 0056ee3 Merge: cae89c3 0f0f82a Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:54:23 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p commit 0f0f82a Author: Maria Mandlis <[email protected]> Date: Wed Mar 4 09:33:29 2020 -0800 add queryable capability (#4794) commit d032c75 Author: Maria Mandlis <[email protected]> Date: Wed Mar 4 09:23:07 2020 -0800 add creatimeTimestamp to entities (kt) (#4823) commit 30267fc Author: jblebrun <[email protected]> Date: Wed Mar 4 09:09:39 2020 -0800 Remove some unused deps (#4831) commit c519181 Author: Cameron Silvestrini <[email protected]> Date: Wed Mar 4 17:14:07 2020 +1100 Increase size of DatabaseImplTest to medium (#4827) * Increase size of DatabaseImplTest to medium Was a bit flaky. * Check flakiness * Revert presubmit tweak commit fa621c6 Author: jblebrun <[email protected]> Date: Tue Mar 3 22:05:07 2020 -0800 Fix a race condition when setting up `ServiceStore` message channel (#4826) This seems to be the cause of the flakiness in `AndroidAllocatorTest` and `AndroidHandleManagerTest` (#4781) commit 78bf4f0 Author: Gogul Balakrishnan <[email protected]> Date: Tue Mar 3 18:00:49 2020 -0800 Utility to convert type proto to field type (if possible). (#4814) commit cae89c3 Merge: 0360752 e587539 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 15:50:34 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p commit e587539 Author: jblebrun <[email protected]> Date: Tue Mar 3 15:07:16 2020 -0800 Apply simplified test pattern tests in `arcs.android.host` (#4821) As in 5c6310d commit 1af1aac Author: Joshua Pratt <[email protected]> Date: Wed Mar 4 09:48:15 2020 +1100 Multinomials (#4804) * multinomial * added tests * making constant readable again * reindent * added multivariate rearrangement tests * updated comment * pr comments: Co-authored-by: Ragav Sachdeva <[email protected]> commit 0360752 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:42:31 2020 -0800 fix, bad comparison op commit 4b05a28 Merge: f4c1d26 99000c2 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:18:14 2020 -0800 Merge branch 'r2p' of github.com:alxrsngrtn/arcs into r2p commit f4c1d26 Merge: 140111f 5c6310d Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:14:35 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p Fixed lint errors commit 99000c2 Merge: 140111f 5c6310d Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:14:35 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into r2p Fixed lint errors commit 140111f Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 11:08:12 2020 -0800 Removed flatMap, added TODOs and link to GH issue commit 5c6310d Author: jblebrun <[email protected]> Date: Tue Mar 3 11:02:34 2020 -0800 Simplify service test pattern (#4817) * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to Kotlin/kotlinx.coroutines#1222, if the test results in coroutines being finished on other threads, it's possible to receive "This job has not yet completed" exceptions, even though the jobs were properly terminated. Since we don't need the delay-skipping properties of `runBlockingTest`, I think it's OK to use `runBlocking`. commit 472bc84 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 10:27:56 2020 -0800 Improved build rules commit ca1ebf8 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 10:17:13 2020 -0800 Impl suggestsions for r2p commit b5578ea Author: Jason Feinstein <[email protected]> Date: Tue Mar 3 09:54:17 2020 -0800 Add tests for dereferencing references to the HandleManagerTest(s) (#4816) * Add dereferencing tests to the android handle manager test. * Add dereferencing tests to core HandleManager. commit a695797 Author: Jason Feinstein <[email protected]> Date: Mon Mar 2 18:14:07 2020 -0800 Create RawEntityDereferencerTest, storage Reference-> CrdtEntity.Reference (#4812) * Create RawEntityDereferencerTest, make storage Reference implement CrdtEntity.Reference. Also: Create ParcelableReference. * Add dep. Also, apparently read/writeBoolean is Q-only. * Just write null if there is no version map. * Just write null if there is no version map. commit ba7a107 Author: Gogul Balakrishnan <[email protected]> Date: Mon Mar 2 17:51:35 2020 -0800 Add a decoder for PrimitiveTypeProto and an option to disable android constraints in BUILD. (#4793) commit f6df54a Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 15:44:32 2020 -0800 tools/sigh lint commit bede8d9 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:52:02 2020 -0800 Fixed build rule, simplified runtime commit 62dcc57 Author: Alex Rosengarten <[email protected]> Date: Mon Mar 2 14:38:14 2020 -0800 Fixed test, added TODO * fixed nested tests * rewording, reorganizing * assert more ergonomic commit 298fdcc Author: Maria Mandlis <[email protected]> Date: Wed Mar 4 15:29:37 2020 -0800 update wasm readme (#4832) commit d15008e Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:11:14 2020 -0800 fixed build commit bbfc3d2 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:04:11 2020 -0800 EntitySpec Interface can return a schema commit 3b75c34 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 14:09:36 2020 -0800 ktlint fix commit 506457e Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 13:59:46 2020 -0800 addField arguments are more ergonomic commit 579b2e6 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 13:45:28 2020 -0800 grooming schema registry commit 3cb8023 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:39:30 2020 -0800 rm unused field commit 1cff99f Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:38:32 2020 -0800 rm import commit 855401c Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:31:14 2020 -0800 no difference w/ wasm golden commit 552e9ad Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:26:43 2020 -0800 Added test for mapOf commit 1430907 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:07:15 2020 -0800 sigh lint commit 9c22195 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:06:50 2020 -0800 minor adjustments commit 74ae770 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:58:20 2020 -0800 Improved shape of registry commit f69f99b Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:32:31 2020 -0800 excluding wasm from generation commit dcefb16 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:13:46 2020 -0800 Generation adds to schema registry commit f3b9ae5 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 07:57:15 2020 -0800 packaging in a schema registry commit 2ce6da2 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 22:30:39 2020 -0800 First iteration of schema gen on entities * Squashed commit of the following: commit b26f13e Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 17:14:33 2020 -0800 updated goldens commit f66fdd8 Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 16:56:26 2020 -0800 further cleanup commit 46b4f17 Merge: cfcf317 8621c58 Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 16:54:11 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into kt-gen-utils commit cfcf317 Merge: 60582ed 623daf7 Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 16:28:56 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into kt-gen-utils commit 60582ed Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 15:27:30 2020 -0800 lint fix, added a test commit 196a4a9 Author: Alex Rosengarten <[email protected]> Date: Thu Mar 5 15:16:35 2020 -0800 cleaned up ktUtils, passing tests commit 142fa2b Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 17:42:04 2020 -0800 first iteration for kt utils commit e55cb0b Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 17:36:00 2020 -0800 Squashed commit of the following: commit 17a2ab1 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 16:30:45 2020 -0800 Fixed indentation commit 9094055 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 16:03:51 2020 -0800 revising from feedback commit d350821 Merge: 9054c23 3acb7e7 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:56:55 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into s2k-schemas commit 9054c23 Merge: d15008e 337b7b2 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:55:27 2020 -0800 Merge branch 'master' of github.com:PolymerLabs/arcs into s2k-schemas commit d15008e Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:11:14 2020 -0800 fixed build commit bbfc3d2 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 15:04:11 2020 -0800 EntitySpec Interface can return a schema commit 3b75c34 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 14:09:36 2020 -0800 ktlint fix commit 506457e Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 13:59:46 2020 -0800 addField arguments are more ergonomic commit 579b2e6 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 13:45:28 2020 -0800 grooming schema registry commit 3cb8023 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:39:30 2020 -0800 rm unused field commit 1cff99f Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:38:32 2020 -0800 rm import commit 855401c Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:31:14 2020 -0800 no difference w/ wasm golden commit 552e9ad Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:26:43 2020 -0800 Added test for mapOf commit 1430907 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:07:15 2020 -0800 sigh lint commit 9c22195 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 11:06:50 2020 -0800 minor adjustments commit 74ae770 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:58:20 2020 -0800 Improved shape of registry commit f69f99b Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:32:31 2020 -0800 excluding wasm from generation commit dcefb16 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 10:13:46 2020 -0800 Generation adds to schema registry commit f3b9ae5 Author: Alex Rosengarten <[email protected]> Date: Wed Mar 4 07:57:15 2020 -0800 packaging in a schema registry commit 2ce6da2 Author: Alex Rosengarten <[email protected]> Date: Tue Mar 3 22:30:39 2020 -0800 First iteration of schema gen on entities * using kt gen utils * fixed merge issue * Added new kt util * creating handle connections * handle connection map, lots of progress * incomplete, but valid plan generator * Groomed plan gen * fix jvm golden * 1 missing semicolon * buildifier fix * Code quality improvements. * Code quality improvements. * Added golden in testdata, revealed codegen issue from refactoring! * Added golden test for r2p * mv tests to goldens dir * updating whitespace * name change * rm default package * guard imports Co-authored-by: Alex Rosengarten <[email protected]>
1 parent 29adb39 commit 3e7406b

File tree

10 files changed

+296
-31
lines changed

10 files changed

+296
-31
lines changed

java/arcs/core/data/testdata/BUILD

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ arcs_manifest_proto(
2323

2424
arcs_kt_plan(
2525
name = "example_plan",
26-
src = "Example.arcs",
26+
src = "WriterReaderExample.arcs",
27+
package = "arcs.core.data.testdata",
2728
visibility = ["//visibility:public"],
2829
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
particle Reader
2+
data: reads Thing {name: Text}
3+
4+
particle Writer
5+
data: writes Thing {name: Text}
6+
7+
@trigger
8+
launch startup
9+
arcId writingArcId
10+
recipe Ingestion
11+
thing: create persistent 'my-handle-id'
12+
Writer
13+
data: writes thing
14+
15+
@trigger
16+
launch startup
17+
arcId readingArcId
18+
recipe Consumption
19+
data: map 'my-handle-id'
20+
Reader
21+
data: reads data

src/tools/kotlin-generation-utils.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,32 @@ export class KotlinGenerationUtils {
3333
*
3434
* @param name name of the function
3535
* @param args list of arguments to the function
36-
* @param emptyName alternative name for the function with empty arguments.
3736
* @param startIndent (optional) starting indentation level.
37+
* @param emptyName alternative name for the function with empty arguments.
3838
*/
39-
applyFun(name: string, args: string[], emptyName: string = name, startIndent: number = 0): string {
39+
applyFun(name: string, args: string[], startIndent: number = 0, emptyName: string = name): string {
4040
if (args.length === 0) return `${emptyName}()`;
4141
return `${name}(${this.joinWithIndents(args, startIndent + name.length + 2)})`;
4242
}
4343

4444
/** Formats `mapOf` with correct indentation and defaults. */
4545
mapOf(args: string[], startIndent: number = 0): string {
46-
return this.applyFun('mapOf', args, 'emptyMap', startIndent);
46+
return this.applyFun('mapOf', args, startIndent, 'emptyMap');
4747
}
4848

4949
/** Formats `mutableMapOf` with correct indentation and defaults. */
5050
mutableMapOf(args: string[], startIndent: number = 0): string {
51-
return this.applyFun('mutableMapOf', args, 'mutableMapOf', startIndent);
51+
return this.applyFun('mutableMapOf', args, startIndent, 'mutableMapOf');
5252
}
5353

5454
/** Formats `listOf` with correct indentation and defaults. */
5555
listOf(args: string[], startIndent: number = 0): string {
56-
return this.applyFun('listOf', args, 'emptyList', startIndent);
56+
return this.applyFun('listOf', args, startIndent, 'emptyList');
5757
}
5858

5959
/** Formats `setOf` with correct indentation and defaults. */
6060
setOf(args: string[], startIndent: number = 0): string {
61-
return this.applyFun('setOf', args, 'emptySet', startIndent);
61+
return this.applyFun('setOf', args, startIndent, 'emptySet');
6262
}
6363

6464
/**
@@ -70,7 +70,12 @@ export class KotlinGenerationUtils {
7070
joinWithIndents(items: string[], extraIndent: number = 0): string {
7171
const candidate = items.join(', ');
7272
if (extraIndent + candidate.length <= this.pref.lineLength) return candidate;
73-
return `\n${leftPad(items.join(',\n'), this.pref.indent)}\n`;
73+
return `\n${this.indent(items.join(',\n'))}\n`;
74+
}
75+
76+
/** Indent a codeblock with the preferred indentation. */
77+
indent(block: string): string {
78+
return leftPad(block, this.pref.indent);
7479
}
7580
}
7681

@@ -82,3 +87,11 @@ export function leftPad(input: string, indent: number, skipFirst: boolean = fals
8287
.join('\n');
8388
}
8489

90+
/** Format a Kotlin string. */
91+
export function quote(s: string) { return `"${s}"`; }
92+
93+
/** Produces import statement if target is not within the same package. */
94+
export function tryImport(importName: string, packageName: string): string {
95+
const nonWild = importName.replace('.*', '');
96+
return packageName === nonWild ? '' : `import ${importName}`;
97+
}

src/tools/plan-generator.ts

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

src/tools/recipe2plan-cli.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import {Runtime} from '../runtime/runtime.js';
1414
import {recipe2plan} from './recipe2plan.js';
1515

1616
const opts = minimist(process.argv.slice(2), {
17-
string: ['outdir', 'outfile'],
18-
alias: {d: 'outdir', f: 'outfile'},
17+
string: ['outdir', 'outfile', 'package'],
18+
alias: {d: 'outdir', f: 'outfile', p: 'package'},
1919
default: {outdir: '.'}
2020
});
2121

@@ -30,6 +30,7 @@ Description
3030
Options
3131
--outfile, -f output filename; required
3232
--outdir, -d output directory; defaults to '.'
33+
--package, -p kotlin package.
3334
--help usage info
3435
`);
3536
process.exit(0);
@@ -40,6 +41,12 @@ if (!opts.outfile) {
4041
process.exit(1);
4142
}
4243

44+
45+
if (!opts.package) {
46+
console.error(`Parameter --package is required.`);
47+
process.exit(1);
48+
}
49+
4350
// TODO(alxr): Support generation from multiple manifests
4451
if (opts._.length > 1) {
4552
console.error(`Only a single manifest is allowed`);
@@ -56,7 +63,7 @@ async function main() {
5663
Runtime.init('../..');
5764
fs.mkdirSync(opts.outdir, {recursive: true});
5865

59-
const plans = await recipe2plan(opts._[0]);
66+
const plans = await recipe2plan(opts._[0], opts.package);
6067

6168
const outPath = path.join(opts.outdir, opts.outfile);
6269
console.log(outPath);

src/tools/recipe2plan.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,23 @@
88
* http://polymer.github.io/PATENTS.txt
99
*/
1010
import {Runtime} from '../runtime/runtime.js';
11-
import {Recipe} from '../runtime/recipe/recipe.js';
1211
import {StorageKeyRecipeResolver} from './storage-key-recipe-resolver.js';
12+
import {PlanGenerator} from './plan-generator.js';
1313

1414

1515
/**
1616
* Generates Kotlin Plans from recipes in an arcs manifest.
1717
*
1818
* @param path path/to/manifest.arcs
19+
* @param scope kotlin package name
1920
* @return Generated Kotlin code.
2021
*/
21-
export async function recipe2plan(path: string): Promise<string> {
22+
export async function recipe2plan(path: string, scope: string): Promise<string> {
2223
const manifest = await Runtime.parseFile(path);
2324

2425
const recipes = await (new StorageKeyRecipeResolver(manifest)).resolve();
2526

26-
const plans = await generatePlans(recipes);
27+
const generator = new PlanGenerator(recipes, scope);
2728

28-
return plans.join('\n');
29+
return generator.generate();
2930
}
30-
31-
32-
/**
33-
* Converts each resolved recipes into a Kotlin Plan class.
34-
*
35-
* @param resolutions A series of resolved recipes.
36-
* @return List of generated Kotlin plans
37-
*/
38-
async function generatePlans(resolutions: Recipe[]): Promise<string[]> {
39-
// TODO Implement
40-
return [''];
41-
}
42-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* ktlint-disable */
2+
@file:Suppress("PackageName", "TopLevelName")
3+
4+
package arcs.core.data.testdata
5+
6+
//
7+
// GENERATED CODE -- DO NOT EDIT
8+
//
9+
10+
import arcs.core.data.*
11+
import arcs.core.storage.*
12+
13+
object IngestionPlan : Plan(
14+
listOf(
15+
Particle(
16+
"Writer",
17+
"",
18+
mapOf(
19+
"data" to HandleConnection(StorageKeyParser.parse(""), HandleMode.Write, null, null)
20+
)
21+
)
22+
)
23+
)
24+
object ConsumptionPlan : Plan(
25+
listOf(
26+
Particle(
27+
"Reader",
28+
"",
29+
mapOf(
30+
"data" to HandleConnection(
31+
StorageKeyParser.parse("ramdisk://"),
32+
HandleMode.Read,
33+
null,
34+
null
35+
)
36+
)
37+
)
38+
)
39+
)
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
11+
import {PlanGenerator} from '../plan-generator.js';
12+
import {assert} from '../../platform/chai-node.js';
13+
14+
describe('recipe2plan', () => {
15+
describe('plan-generator', () => {
16+
it('imports arcs.core.data when the package is different', () => {
17+
const generator = new PlanGenerator([], 'some.package');
18+
19+
const actual = generator.fileHeader();
20+
21+
assert.include(actual, 'import arcs.core.data.*');
22+
});
23+
it('does not import arcs.core.data when the package is the same', () => {
24+
const generator = new PlanGenerator([], 'arcs.core.data');
25+
26+
const actual = generator.fileHeader();
27+
28+
assert.notInclude(actual, 'import arcs.core.data.*');
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)