Skip to content

Commit

Permalink
#602: final implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
petermasking committed Jan 16, 2025
1 parent b033ef6 commit 06c6afc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
41 changes: 19 additions & 22 deletions packages/build/src/source/segment/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,22 @@ export default class SegmentReader
{
for (const [filename, moduleImports] of Object.entries(definition))
{
this.#createModule(segment, filename, moduleImports);
this.#createModule(segment, filename, moduleImports, true);
}
}

#createModule(segment: Segment, filename: string, moduleImports: Imports): Module
#createModule(segment: Segment, filename: string, moduleImports: Imports, segmented: boolean): void
{
const moduleFilename = this.#makeModuleFilename(filename);
const location = this.#fileHelper.extractPath(moduleFilename);

const module = segment.hasModule(moduleFilename)
? segment.getModule(moduleFilename) as Module
: new Module(moduleFilename, location, {});
: new Module(moduleFilename, location, {}, segmented);

module.addImports(moduleImports);

segment.setModule(module);

return module;
}

#createProcedure(segment: Segment, module: Module, implementation: Implementation): void
{
const procedure = segment.hasProcedure(implementation.fqn)
? segment.getProcedure(implementation.fqn) as Procedure
: new Procedure(implementation.fqn);

procedure.addImplementation(implementation);

module.addMember(implementation);
segment.setProcedure(procedure);
}

#makeModuleFilename(filename: string): string
Expand Down Expand Up @@ -171,16 +157,13 @@ export default class SegmentReader

this.#createMember(segment, module, model, properties);

trace.shift();
trace.shift(); // Remove the first entry, as it is the module itself.

for (const entry of trace)
{
const entryImport = { [entry.importKey]: { access: properties.access } };
const entryProperties = { ...properties, importKey: entry.importKey };

const module = this.#createModule(segment, entry.filename, entryImport);

this.#createMember(segment, module, model, entryProperties);
this.#createModule(segment, entry.filename, entryImport, false);
}
}

Expand Down Expand Up @@ -247,6 +230,7 @@ export default class SegmentReader
const clazz = new Class(properties.id, properties.importKey, properties.fqn, model);

module.addMember(clazz);

segment.setClass(clazz);
}

Expand All @@ -261,4 +245,17 @@ export default class SegmentReader

this.#createProcedure(segment, module, implementation);
}

#createProcedure(segment: Segment, module: Module, implementation: Implementation): void
{
const procedure = segment.hasProcedure(implementation.fqn)
? segment.getProcedure(implementation.fqn) as Procedure
: new Procedure(implementation.fqn);

procedure.addImplementation(implementation);

module.addMember(implementation);

segment.setProcedure(procedure);
}
}
6 changes: 5 additions & 1 deletion packages/build/src/source/segment/models/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export default class Module
readonly #location: string;
readonly #imports: Imports;
readonly #members: Member[] = [];
readonly #segmented: boolean;

constructor(filename: string, location: string, imports: Imports)
constructor(filename: string, location: string, imports: Imports, segmented: boolean)
{
this.#filename = filename;
this.#location = location;
this.#imports = imports;
this.#segmented = segmented;
}

get filename() { return this.#filename; }
Expand All @@ -26,6 +28,8 @@ export default class Module
get imports() { return this.#imports; }

get members() { return this.#members; }

get segmented() { return this.#segmented; }

hasClasses(): boolean
{
Expand Down
5 changes: 5 additions & 0 deletions packages/build/src/source/segment/models/Segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export default class Segment
return this.#modules.get(filename);
}

getSegmentedModules(): Module[]
{
return this.modules.filter(module => module.segmented);
}

setModule(module: Module): void
{
this.#modules.set(module.filename, module);
Expand Down
9 changes: 6 additions & 3 deletions packages/build/src/target/module/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ export default class Builder

async #buildModule(module: Module, resources: ResourcesList, segmentation: Segmentation): Promise<void>
{
await this.#buildCommonModule(module, resources, segmentation);

const moduleSegments = segmentation.getSegments(module.filename);

if (moduleSegments.length === 0)
{
// For unsegmented modules we only need to build the common module.
// This will overwrite the original module file.

return;
return this.#buildCommonModule(module, resources, segmentation);
}

const segmentBuilds = moduleSegments.map(segment => this.#buildSegmentModule(module, resources, segment, segmentation));
Expand All @@ -54,6 +53,10 @@ export default class Builder
: Promise.resolve();

await Promise.all([...segmentBuilds, remoteBuild]);

// The segment files will replace the original module file, so we can delete it.

this.#targetFileManager.delete(module.filename);
}

async #buildCommonModule(module: Module, resources: ResourcesList, segmentation: Segmentation): Promise<void>
Expand Down
6 changes: 5 additions & 1 deletion packages/build/src/target/segment/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export default class Builder
{
const imports = [];

for (const module of segment.modules)
// We only want to include modules that are defined in the segment configuration.
// The other modules contain classes and procedures that are re-exported by at least one segmented module.
// Adding them would cause a duplicate declaration error.

for (const module of segment.getSegmentedModules())
{
const filename = this.#fileHelper.addSubExtension(module.filename, segment.name);
const members = this.#createModuleImportMembers(module);
Expand Down

0 comments on commit 06c6afc

Please sign in to comment.