@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { EmitResult } from "@tsplc/compiler/ir";
|
|
2
|
+
import { emittedFiles } from "@tsplc/config/project-files";
|
|
3
|
+
import { type ServiceChannel, serviceChannelSchema } from "@tsplc/server/wire";
|
|
4
|
+
import { publicComment } from "@tsplc/typegen";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
/** `machines.ts`, which lists the service channels for the server and the Bun runtime to read back. */
|
|
8
|
+
export const serviceChannelsFile = (
|
|
9
|
+
channels: readonly ServiceChannel[],
|
|
10
|
+
): EmitResult["artifacts"][number] => ({
|
|
11
|
+
path: emittedFiles.machines,
|
|
12
|
+
content: `// Generated service channels. Do not edit.\n${publicComment}export const serviceChannels=${JSON.stringify(channels, null, 2)} as const;\n`,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/** The service channels a `machines.ts` lists. */
|
|
16
|
+
export const parseServiceChannels = (machines: string): ServiceChannel[] => {
|
|
17
|
+
const listed = /export const serviceChannels=([\s\S]*?) as const;/.exec(
|
|
18
|
+
machines,
|
|
19
|
+
);
|
|
20
|
+
if (!listed?.[1]) {
|
|
21
|
+
throw new Error(`${emittedFiles.machines} lists no service channels.`);
|
|
22
|
+
}
|
|
23
|
+
return z.array(serviceChannelSchema).parse(JSON.parse(listed[1]));
|
|
24
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
3
|
+
import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
4
|
+
import type { SourceRange } from "@tsplc/debug/manifest";
|
|
5
|
+
import { required } from "./required";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Whether a compiled file is the project's own source: anything but an installed dependency. A
|
|
9
|
+
* monorepo's workspace packages count wherever they sit, as their files resolve to their real paths.
|
|
10
|
+
*/
|
|
11
|
+
const projectSource = (root: string, file: string): boolean => {
|
|
12
|
+
const path = relative(root, file);
|
|
13
|
+
return !isAbsolute(path) && !path.split(/[\\/]/).includes("node_modules");
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** The nearest directory at or above `directory` that holds a `package.json`. */
|
|
17
|
+
const packageDirectory = (directory: string): string | undefined => {
|
|
18
|
+
if (existsSync(join(directory, "package.json"))) {
|
|
19
|
+
return directory;
|
|
20
|
+
}
|
|
21
|
+
const parent = dirname(directory);
|
|
22
|
+
return parent === directory ? undefined : packageDirectory(parent);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The folders a source module's objects go in, without a leading `src`: its path in the project,
|
|
27
|
+
* or a workspace package's own, so its objects keep the folders they would have in its own project.
|
|
28
|
+
* `undefined` for a module that is not the project's own source.
|
|
29
|
+
*/
|
|
30
|
+
export const sourceFolders = (
|
|
31
|
+
root: string,
|
|
32
|
+
module: string,
|
|
33
|
+
): string[] | undefined => {
|
|
34
|
+
if (!/^[\w./ -]+\.tsx?$/.test(module)) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const file = resolve(root, module);
|
|
38
|
+
if (!projectSource(root, file)) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
const base = relative(root, file).startsWith("..")
|
|
42
|
+
? (packageDirectory(dirname(file)) ?? dirname(file))
|
|
43
|
+
: root;
|
|
44
|
+
const segments = relative(base, file).replaceAll("\\", "/").split("/");
|
|
45
|
+
const folders = segments.slice(0, -1);
|
|
46
|
+
return folders[0] === "src" ? folders.slice(1) : folders;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** A file's text and the offset each of its lines starts at. */
|
|
50
|
+
const lineStarts = (text: string) => ({
|
|
51
|
+
text,
|
|
52
|
+
lines: [0, ...[...text.matchAll(/\n/g)].map(({ index }) => index + 1)],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/** The index of the last line that starts at or before `offset`, by bisection. */
|
|
56
|
+
const lineIndex = (starts: readonly number[], offset: number): number => {
|
|
57
|
+
let low = 0;
|
|
58
|
+
let high = starts.length - 1;
|
|
59
|
+
while (low < high) {
|
|
60
|
+
const middle = (low + high + 1) >> 1;
|
|
61
|
+
if (required(starts[middle]) <= offset) {
|
|
62
|
+
low = middle;
|
|
63
|
+
} else {
|
|
64
|
+
high = middle - 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return low;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Project files by index, as paths relative to the project root, so a workspace package's start with
|
|
72
|
+
* `../`. Spans outside the project's own sources, such as virtual modules and dependencies, have no range.
|
|
73
|
+
*/
|
|
74
|
+
export class Sources {
|
|
75
|
+
readonly files: string[] = [];
|
|
76
|
+
private readonly loaded = new Map<
|
|
77
|
+
string,
|
|
78
|
+
{ text: string; lines: number[] } | undefined
|
|
79
|
+
>();
|
|
80
|
+
|
|
81
|
+
constructor(private readonly root: string) {}
|
|
82
|
+
|
|
83
|
+
private load(file: string) {
|
|
84
|
+
if (this.loaded.has(file)) {
|
|
85
|
+
return this.loaded.get(file);
|
|
86
|
+
}
|
|
87
|
+
const source =
|
|
88
|
+
projectSource(this.root, file) && existsSync(file)
|
|
89
|
+
? lineStarts(readFileSync(file, "utf8"))
|
|
90
|
+
: undefined;
|
|
91
|
+
this.loaded.set(file, source);
|
|
92
|
+
return source;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
range(span: SourceSpan): SourceRange | undefined {
|
|
96
|
+
const source = this.load(span.file);
|
|
97
|
+
if (!source || span.end > source.text.length) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const position = (offset: number) => {
|
|
101
|
+
const line = lineIndex(source.lines, offset);
|
|
102
|
+
return {
|
|
103
|
+
line: line + 1,
|
|
104
|
+
column: offset - required(source.lines[line]) + 1,
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
const path = relative(this.root, span.file).replaceAll("\\", "/");
|
|
108
|
+
const known = this.files.indexOf(path);
|
|
109
|
+
const file = known < 0 ? this.files.push(path) - 1 : known;
|
|
110
|
+
const start = position(span.start);
|
|
111
|
+
const end = position(span.end);
|
|
112
|
+
return { file, ...start, endLine: end.line, endColumn: end.column };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
text(span: SourceSpan): string | undefined {
|
|
116
|
+
return this.load(span.file)?.text.slice(span.start, span.end);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EmitResult,
|
|
3
|
+
GeneratedObject,
|
|
4
|
+
SourceSpan,
|
|
5
|
+
} from "@tsplc/compiler/ir";
|
|
6
|
+
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
7
|
+
import type { SourceMapping } from "./emit";
|
|
8
|
+
import { required } from "./required";
|
|
9
|
+
import { sourceFolders } from "./sources";
|
|
10
|
+
import { moduleFile } from "./storage";
|
|
11
|
+
import type { EmissionContext } from "./symbols";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A line of an object's file that a source span produced, before `stFiles` places the object.
|
|
15
|
+
* `object` is the object's id.
|
|
16
|
+
*/
|
|
17
|
+
export interface ObjectMapping {
|
|
18
|
+
object: string;
|
|
19
|
+
file: "declaration" | "implementation";
|
|
20
|
+
line: number;
|
|
21
|
+
span: SourceSpan;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An object with the files `stFiles` wrote it to. */
|
|
25
|
+
export type PlacedObject = GeneratedObject & {
|
|
26
|
+
/** Its only file, or `declaration.st` beside its implementation. */
|
|
27
|
+
sourcePath: string;
|
|
28
|
+
implementationPath?: string;
|
|
29
|
+
/** Its path below the Application in the CODESYS project, which keeps methods beside their block. */
|
|
30
|
+
nativePath: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Writes each object to its ST files, in its source module's folder: an object without an
|
|
35
|
+
* implementation is one `.st` file, any other a folder with `declaration.st` and
|
|
36
|
+
* `implementation.st`. Returns the objects with their files and each mapping at its file.
|
|
37
|
+
*/
|
|
38
|
+
export function stFiles(
|
|
39
|
+
context: EmissionContext,
|
|
40
|
+
objects: readonly GeneratedObject[],
|
|
41
|
+
mappings: readonly ObjectMapping[],
|
|
42
|
+
): {
|
|
43
|
+
artifacts: EmitResult["artifacts"];
|
|
44
|
+
objects: PlacedObject[];
|
|
45
|
+
mappings: SourceMapping[];
|
|
46
|
+
} {
|
|
47
|
+
const paths = objectPaths(context, objects);
|
|
48
|
+
const placed = objects.map((object): PlacedObject => {
|
|
49
|
+
const { path, nativePath } = paths(object);
|
|
50
|
+
return object.implementation === undefined
|
|
51
|
+
? { ...object, sourcePath: `${path}.st`, nativePath }
|
|
52
|
+
: {
|
|
53
|
+
...object,
|
|
54
|
+
sourcePath: `${path}/declaration.st`,
|
|
55
|
+
implementationPath: `${path}/implementation.st`,
|
|
56
|
+
nativePath,
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const owners = new Map(placed.map((object) => [object.id, object]));
|
|
60
|
+
return {
|
|
61
|
+
artifacts: placed.flatMap((object) => [
|
|
62
|
+
{ path: object.sourcePath, content: object.declaration },
|
|
63
|
+
...(object.implementation === undefined
|
|
64
|
+
? []
|
|
65
|
+
: [
|
|
66
|
+
{
|
|
67
|
+
path: required(object.implementationPath),
|
|
68
|
+
content: object.implementation,
|
|
69
|
+
},
|
|
70
|
+
]),
|
|
71
|
+
]),
|
|
72
|
+
objects: placed,
|
|
73
|
+
mappings: mappings.map(({ object, file, line, span }) => {
|
|
74
|
+
const owner = required(owners.get(object));
|
|
75
|
+
return {
|
|
76
|
+
path:
|
|
77
|
+
file === "implementation"
|
|
78
|
+
? required(owner.implementationPath)
|
|
79
|
+
: owner.sourcePath,
|
|
80
|
+
line,
|
|
81
|
+
span,
|
|
82
|
+
};
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The folder of an object that no source module places. The IO list is generated too, and a
|
|
89
|
+
* folder of its own could share a name with a program's state list (`io` gives `Io`).
|
|
90
|
+
*/
|
|
91
|
+
const generatedFolder = (object: GeneratedObject) =>
|
|
92
|
+
object.kind === "dataType" ? "Types" : "_Generated";
|
|
93
|
+
|
|
94
|
+
/** The module an object was emitted from, as the id of its symbol, program entry or variables names it. */
|
|
95
|
+
const sourceModules = ({ plan, symbols }: EmissionContext) => {
|
|
96
|
+
const groups = new Map(symbols.globals.map((group) => [group.id, group]));
|
|
97
|
+
const programs = new Map(
|
|
98
|
+
plan.core.programs.map((program) => [program.id, program]),
|
|
99
|
+
);
|
|
100
|
+
return (object: GeneratedObject): string => {
|
|
101
|
+
const group = groups.get(object.id);
|
|
102
|
+
if (group) {
|
|
103
|
+
// Variable IDs are relative to tsconfig, unlike their diagnostic spans.
|
|
104
|
+
return moduleFile(required(group.variables[0]).id);
|
|
105
|
+
}
|
|
106
|
+
return moduleFile(programs.get(object.id)?.entry ?? object.id);
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Each object's path without extension, in its source module's folder or its block's for a
|
|
112
|
+
* method, and its path in the CODESYS project, where a method sits directly below its block.
|
|
113
|
+
*/
|
|
114
|
+
function objectPaths(
|
|
115
|
+
context: EmissionContext,
|
|
116
|
+
objects: readonly GeneratedObject[],
|
|
117
|
+
): (object: GeneratedObject) => { path: string; nativePath: string } {
|
|
118
|
+
const byId = new Map(objects.map((item) => [item.id, item]));
|
|
119
|
+
const sourceModule = sourceModules(context);
|
|
120
|
+
const objectPath = (
|
|
121
|
+
object: GeneratedObject,
|
|
122
|
+
): { path: string; nativePath: string } => {
|
|
123
|
+
if (object.kind === "method") {
|
|
124
|
+
const parent = objectPath(required(byId.get(required(object.parent))));
|
|
125
|
+
return {
|
|
126
|
+
path: `${parent.path}/methods/${object.name}`,
|
|
127
|
+
nativePath: `${parent.nativePath}/${object.name}`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const folders = required(placed.get(object));
|
|
131
|
+
const path = [folderPath(folders), object.name].filter(Boolean).join("/");
|
|
132
|
+
return { path, nativePath: path };
|
|
133
|
+
};
|
|
134
|
+
const placedFolders = (object: GeneratedObject): string[] => {
|
|
135
|
+
const folders = sourceFolders(
|
|
136
|
+
context.plan.project.root,
|
|
137
|
+
sourceModule(object),
|
|
138
|
+
);
|
|
139
|
+
if (!folders) {
|
|
140
|
+
return [generatedFolder(object)];
|
|
141
|
+
}
|
|
142
|
+
// A plain program's state lists and types are generated beside its entry module, whose
|
|
143
|
+
// folder they belong in: CODESYS would show the hidden directory as a folder of its own.
|
|
144
|
+
if (folders.at(-1) === generatedDirectory) {
|
|
145
|
+
return folders.slice(0, -1);
|
|
146
|
+
}
|
|
147
|
+
// A DUT directory can also contain sibling input/output layouts. Keep
|
|
148
|
+
// that directory for every DUT, or its primary type would block the folder.
|
|
149
|
+
return object.kind !== "dataType" && folders.at(-1) === object.name
|
|
150
|
+
? folders.slice(0, -1)
|
|
151
|
+
: folders;
|
|
152
|
+
};
|
|
153
|
+
const placed = new Map(
|
|
154
|
+
objects
|
|
155
|
+
.filter((object) => object.kind !== "method")
|
|
156
|
+
.map((object) => [object, placedFolders(object)]),
|
|
157
|
+
);
|
|
158
|
+
const folderPath = separatedFolders(
|
|
159
|
+
[...placed].map(([object, folders]) => ({ folders, name: object.name })),
|
|
160
|
+
);
|
|
161
|
+
return objectPath;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Each folder's path once no folder shares its name with an object beside it. CODESYS refuses that,
|
|
166
|
+
* ignoring case, as when a module's folder is named like an object another module places next to it:
|
|
167
|
+
* the folder takes the next free numbered name. Folders only organize the project, so no IEC name changes.
|
|
168
|
+
*/
|
|
169
|
+
export function separatedFolders(
|
|
170
|
+
objects: readonly { folders: readonly string[]; name: string }[],
|
|
171
|
+
): (folders: readonly string[]) => string {
|
|
172
|
+
const childrenOf = (entries: Iterable<readonly [string, string]>) => {
|
|
173
|
+
const children = new Map<string, Set<string>>();
|
|
174
|
+
for (const [parent, name] of entries) {
|
|
175
|
+
const names = children.get(parent) ?? new Set<string>();
|
|
176
|
+
names.add(name.toLowerCase());
|
|
177
|
+
children.set(parent, names);
|
|
178
|
+
}
|
|
179
|
+
return (parent: string) => children.get(parent) ?? new Set<string>();
|
|
180
|
+
};
|
|
181
|
+
const objectsIn = childrenOf(
|
|
182
|
+
objects.map(({ folders, name }) => [folders.join("/"), name] as const),
|
|
183
|
+
);
|
|
184
|
+
const foldersIn = childrenOf(
|
|
185
|
+
objects.flatMap(({ folders }) =>
|
|
186
|
+
folders.map((name, depth) => [folders.slice(0, depth).join("/"), name]),
|
|
187
|
+
),
|
|
188
|
+
);
|
|
189
|
+
const renamed = new Map<string, string>();
|
|
190
|
+
const given = new Map<string, Set<string>>();
|
|
191
|
+
const folderPath = (folders: readonly string[]): string => {
|
|
192
|
+
const key = folders.join("/");
|
|
193
|
+
const known = renamed.get(key);
|
|
194
|
+
if (known !== undefined) {
|
|
195
|
+
return known;
|
|
196
|
+
}
|
|
197
|
+
if (folders.length === 0) {
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
const parentKey = folders.slice(0, -1).join("/");
|
|
201
|
+
const name = required(folders.at(-1));
|
|
202
|
+
const taken = given.get(parentKey) ?? new Set<string>();
|
|
203
|
+
given.set(parentKey, taken);
|
|
204
|
+
const blocked = (candidate: string) =>
|
|
205
|
+
objectsIn(parentKey).has(candidate.toLowerCase()) ||
|
|
206
|
+
taken.has(candidate.toLowerCase()) ||
|
|
207
|
+
(candidate !== name && foldersIn(parentKey).has(candidate.toLowerCase()));
|
|
208
|
+
const free = (count: number): string =>
|
|
209
|
+
blocked(`${name}_${count}`) ? free(count + 1) : `${name}_${count}`;
|
|
210
|
+
const chosen = blocked(name) ? free(2) : name;
|
|
211
|
+
taken.add(chosen.toLowerCase());
|
|
212
|
+
const path = [folderPath(folders.slice(0, -1)), chosen]
|
|
213
|
+
.filter(Boolean)
|
|
214
|
+
.join("/");
|
|
215
|
+
renamed.set(key, path);
|
|
216
|
+
return path;
|
|
217
|
+
};
|
|
218
|
+
return folderPath;
|
|
219
|
+
}
|