Skip to content

Commit

Permalink
flushing out more of plan gen
Browse files Browse the repository at this point in the history
  • Loading branch information
alxmrs committed Mar 5, 2020
1 parent f11860d commit aff420c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
35 changes: 11 additions & 24 deletions src/tools/plan-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import {Recipe} from '../runtime/recipe/recipe.js';
import {Type} from '../runtime/type.js';
import {Particle} from '../runtime/recipe/particle.js';
import {Manifest} from '../runtime/manifest.js';

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

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

const plan = `\
object ${planName} : Plan(listOf())`;
object ${planName} : Plan(listOf(
${particles.join('\n,')}
))`;

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

createParticle(particle: Particle): string {
const spec = particle.spec;
const location = (spec && (spec.implBlobUrl || (spec.implFile && spec.implFile.replace('/', '.')))) || '';

return `\
Particle(
${particle.name},
${particle.spec.implFile.replace('/', '.')}
mapOf()
${particle.name},
"${location}",
mapOf()
)`;
}

Expand Down Expand Up @@ -96,22 +101,4 @@ ${this.scope === 'arcs.core.data' ? '' : 'import arcs.core.data.*'}
fileFooter(): string {
return ``;
}

private mapOf(items: Map<string, string>, indent: number): string {
if (items.size === 0) return 'mapOf()';

const mapping = [...items.entries()].map(([key, val]) => `"${key}" to ${val}`);

return `mapOf(${this.joinWithinLimit(mapping, indent)})`;
}

private joinWithinLimit(items: string[], indent: number, lineLength: number = 120): string {
for(const delim of [', ', '\n' + ' '.repeat(indent)]) {
const candidate = items.join(delim);
const maxLength = Math.max(...candidate.split('\n').map(line => line.length));
if (indent + maxLength <= lineLength) return candidate;
}

return items.join(', '); // Default: have poor formatting
}
}
2 changes: 1 addition & 1 deletion src/tools/recipe2plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function recipe2plan(path: string, scope: string): Promise<string>

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

const generator = new PlanGenerator(recipes, scope);
const generator = new PlanGenerator(recipes, manifest, scope);

return await generator.generate();
}
5 changes: 3 additions & 2 deletions src/tools/tests/plan-generator-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
import {PlanGenerator} from '../plan-generator.js';
import {Recipe} from '../../runtime/recipe/recipe.js';
import {assert} from '../../platform/chai-node.js';
import {Manifest} from '../../runtime/manifest.js';

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

const actual = generator.fileHeader();

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

const actual = generator.fileHeader();

Expand Down

0 comments on commit aff420c

Please sign in to comment.