Skip to content

Commit aff420c

Browse files
committed
flushing out more of plan gen
1 parent f11860d commit aff420c

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

src/tools/plan-generator.ts

+11-24
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import {Recipe} from '../runtime/recipe/recipe.js';
1111
import {Type} from '../runtime/type.js';
1212
import {Particle} from '../runtime/recipe/particle.js';
13+
import {Manifest} from '../runtime/manifest.js';
1314

1415
export class PlanGenerator {
15-
constructor(private resolutions: Recipe[], private scope: string = 'arcs.core.data') {
16-
}
16+
constructor(private resolutions: Recipe[], private manifest: Manifest, private scope: string = 'arcs.core.data') {}
1717

1818
/** Generates a Kotlin file with plan classes derived from resolved recipes. */
1919
async generate(): Promise<string> {
@@ -35,7 +35,9 @@ export class PlanGenerator {
3535
const particles = recipe.particles.map(this.createParticle);
3636

3737
const plan = `\
38-
object ${planName} : Plan(listOf())`;
38+
object ${planName} : Plan(listOf(
39+
${particles.join('\n,')}
40+
))`;
3941

4042
plans.push(plan);
4143
}
@@ -44,11 +46,14 @@ object ${planName} : Plan(listOf())`;
4446
}
4547

4648
createParticle(particle: Particle): string {
49+
const spec = particle.spec;
50+
const location = (spec && (spec.implBlobUrl || (spec.implFile && spec.implFile.replace('/', '.')))) || '';
51+
4752
return `\
4853
Particle(
49-
${particle.name},
50-
${particle.spec.implFile.replace('/', '.')}
51-
mapOf()
54+
${particle.name},
55+
"${location}",
56+
mapOf()
5257
)`;
5358
}
5459

@@ -96,22 +101,4 @@ ${this.scope === 'arcs.core.data' ? '' : 'import arcs.core.data.*'}
96101
fileFooter(): string {
97102
return ``;
98103
}
99-
100-
private mapOf(items: Map<string, string>, indent: number): string {
101-
if (items.size === 0) return 'mapOf()';
102-
103-
const mapping = [...items.entries()].map(([key, val]) => `"${key}" to ${val}`);
104-
105-
return `mapOf(${this.joinWithinLimit(mapping, indent)})`;
106-
}
107-
108-
private joinWithinLimit(items: string[], indent: number, lineLength: number = 120): string {
109-
for(const delim of [', ', '\n' + ' '.repeat(indent)]) {
110-
const candidate = items.join(delim);
111-
const maxLength = Math.max(...candidate.split('\n').map(line => line.length));
112-
if (indent + maxLength <= lineLength) return candidate;
113-
}
114-
115-
return items.join(', '); // Default: have poor formatting
116-
}
117104
}

src/tools/recipe2plan.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function recipe2plan(path: string, scope: string): Promise<string>
2424

2525
const recipes = await (new StorageKeyRecipeResolver(manifest)).resolve();
2626

27-
const generator = new PlanGenerator(recipes, scope);
27+
const generator = new PlanGenerator(recipes, manifest, scope);
2828

2929
return await generator.generate();
3030
}

src/tools/tests/plan-generator-tests.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
import {PlanGenerator} from '../plan-generator.js';
1212
import {Recipe} from '../../runtime/recipe/recipe.js';
1313
import {assert} from '../../platform/chai-node.js';
14+
import {Manifest} from '../../runtime/manifest.js';
1415

1516
describe('recipe2plan', () => {
1617
describe('plan-generator', () => {
1718
it('imports arcs.core.data when the package is different', () => {
18-
const generator = new PlanGenerator([], 'some.package');
19+
const generator = new PlanGenerator([], new Manifest({id: 'test'}), 'some.package');
1920

2021
const actual = generator.fileHeader();
2122

2223
assert.include(actual, 'import arcs.core.data.*');
2324
});
2425
it('does not import arcs.core.data when the package is the same', () => {
25-
const generator = new PlanGenerator([], 'arcs.core.data');
26+
const generator = new PlanGenerator([], new Manifest({id: 'test'}), 'arcs.core.data');
2627

2728
const actual = generator.fileHeader();
2829

0 commit comments

Comments
 (0)