@tsplc/build 0.2.17 → 0.2.19

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.
Files changed (51) hide show
  1. package/dist/cli.js +628 -352
  2. package/dist/cli.js.map +30 -28
  3. package/dist/index.js +548 -387
  4. package/dist/index.js.map +27 -25
  5. package/dist/libraries.js +68 -27
  6. package/dist/libraries.js.map +3 -3
  7. package/docker/Dockerfile +2 -0
  8. package/docker/Dockerfile.dockerignore +4 -1
  9. package/docker/Dockerfile.source +3 -0
  10. package/docker/config.ts +10 -5
  11. package/docker/runner.ts +140 -127
  12. package/package.json +54 -53
  13. package/python/build.py +183 -124
  14. package/python/codesys_online.py +44 -45
  15. package/python/hardware.py +9 -9
  16. package/python/test_build.py +3 -3
  17. package/python/test_codesys_online.py +18 -12
  18. package/python/test_installer_packages.py +1 -0
  19. package/python/worker.py +11 -7
  20. package/src/artifacts.ts +82 -60
  21. package/src/build.ts +71 -62
  22. package/src/cli.ts +109 -100
  23. package/src/controller-online.ts +400 -425
  24. package/src/controller-protocol.ts +140 -0
  25. package/src/controller-session.ts +466 -397
  26. package/src/device-tree.ts +19 -15
  27. package/src/diagnostics.ts +59 -49
  28. package/src/docker-build.ts +217 -156
  29. package/src/docker-context.ts +49 -27
  30. package/src/docker-run.ts +137 -111
  31. package/src/engineering-settings.ts +42 -0
  32. package/src/host/codesys-command.ts +35 -34
  33. package/src/host/codesys-host.ts +157 -129
  34. package/src/host/codesys-worker.ts +318 -301
  35. package/src/host/windows-path.ts +63 -59
  36. package/src/import-request.ts +24 -0
  37. package/src/index.ts +37 -31
  38. package/src/io.ts +152 -116
  39. package/src/libraries.ts +214 -146
  40. package/src/package-path.ts +3 -1
  41. package/src/run.ts +132 -108
  42. package/src/runtime-status.ts +25 -19
  43. package/src/sources.ts +264 -225
  44. package/src/st/declarations.ts +181 -193
  45. package/src/st/lexer.ts +102 -118
  46. package/src/st/parse.ts +193 -171
  47. package/src/st/reader.ts +47 -42
  48. package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
  49. package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
  50. package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
  51. package/src/config.ts +0 -12
package/src/sources.ts CHANGED
@@ -1,248 +1,287 @@
1
1
  import { readdir, readFile } from "node:fs/promises";
2
2
  import { basename, resolve } from "node:path";
3
- import type { ResolvedConfig } from "./config";
3
+ import { identifierPattern } from "@tsplc/iec/st";
4
+ import type { ResolvedConfig } from "@tsplc/config/build";
4
5
  import { type ObjectKind, type ParsedSource, parseSource } from "./st/parse";
5
6
  import { bindStructuredTextIo } from "./io.ts";
6
7
 
7
8
  export type SourceObject = ParsedSource & {
8
- source: string;
9
- folder: string[];
10
- children: SourceObject[];
11
- };
12
- type Files = {
13
- combined: string[];
14
- declaration: string[];
15
- implementation: string[];
9
+ source: string;
10
+ folder: string[];
11
+ children: SourceObject[];
16
12
  };
13
+ interface Files {
14
+ combined: string[];
15
+ declaration: string[];
16
+ implementation: string[];
17
+ }
17
18
  const prefixes: Record<string, ObjectKind> = {
18
- FB: "function_block",
19
- PRG: "program",
20
- FUN: "function",
21
- F: "function",
22
- I: "interface",
23
- ST: "dut",
24
- E: "dut",
25
- T: "dut",
26
- U: "dut",
27
- GVL: "gvl",
19
+ FB: "function_block",
20
+ PRG: "program",
21
+ FUN: "function",
22
+ F: "function",
23
+ I: "interface",
24
+ ST: "dut",
25
+ E: "dut",
26
+ T: "dut",
27
+ U: "dut",
28
+ GVL: "gvl",
28
29
  };
29
30
  const childFolders: Record<string, ObjectKind> = {
30
- methods: "method",
31
- properties: "property",
32
- actions: "action",
31
+ methods: "method",
32
+ properties: "property",
33
+ actions: "action",
33
34
  };
34
35
  const canOwn = (kind: ObjectKind) =>
35
- ["program", "function_block", "interface", "property"].includes(kind);
36
+ ["program", "function_block", "interface", "property"].includes(kind);
36
37
  const assertUnique = (objects: SourceObject[], scope: string) => {
37
- const seen = new Map<string, string>();
38
- for (const object of objects) {
39
- const key = object.name.toLowerCase();
40
- const previous = seen.get(key);
41
- if (previous) {
42
- throw new Error(
43
- `Duplicate IEC name ${object.name} in ${scope}: ${previous} and ${object.source}`,
44
- );
45
- }
46
- seen.set(key, object.source);
47
- assertUnique(object.children, `${scope}.${object.name}`);
48
- }
38
+ const seen = new Map<string, string>();
39
+ for (const object of objects) {
40
+ const key = object.name.toLowerCase();
41
+ const previous = seen.get(key);
42
+ if (previous) {
43
+ throw new Error(
44
+ `Duplicate IEC name ${object.name} in ${scope}: ${previous} and ${object.source}`,
45
+ );
46
+ }
47
+ seen.set(key, object.source);
48
+ assertUnique(object.children, `${scope}.${object.name}`);
49
+ }
49
50
  };
50
51
 
51
52
  const readUnit = async (
52
- name: string,
53
- files: Files,
54
- role?: ObjectKind,
55
- interfaceMember = false,
53
+ name: string,
54
+ files: Files,
55
+ role?: ObjectKind,
56
+ interfaceMember = false,
56
57
  ): Promise<ParsedSource & { source: string }> => {
57
- if (!/^[A-Za-z_][A-Za-z_0-9]*$/.test(name)) {
58
- throw new Error(`Invalid IEC object name: ${name}`);
59
- }
60
- for (const [part, candidates] of Object.entries(files)) {
61
- if (candidates.length > 1) {
62
- throw new Error(`Ambiguous ${part} for ${name}: ${candidates.join(", ")}`);
63
- }
64
- }
65
- const combined = files.combined[0];
66
- const declaration = files.declaration[0];
67
- const implementation = files.implementation[0];
68
- if (combined && (declaration || implementation)) {
69
- throw new Error(`Both combined and split sources exist for ${name}`);
70
- }
71
- const path = combined ?? declaration ?? implementation;
72
- if (!path) {
73
- throw new Error(`Missing source for ${name}`);
74
- }
75
- if (role === "action") {
76
- if (!combined) {
77
- throw new Error(`Actions use one implementation-only .st file: ${path}`);
78
- }
79
- return {
80
- kind: "action",
81
- name,
82
- declaration: "",
83
- implementation: await readFile(combined, "utf8"),
84
- source: combined,
85
- };
86
- }
87
- if (role === "get" || role === "set") {
88
- if (!combined) {
89
- throw new Error(`Property accessors use one .st file: ${path}`);
90
- }
91
- const body = await readFile(combined, "utf8");
92
- const header = `METHOD ${name}\n`;
93
- const parsed = parseSource(`${header}${body}\nEND_METHOD`, {
94
- filename: combined,
95
- });
96
- return {
97
- ...parsed,
98
- kind: role,
99
- name,
100
- declaration: parsed.declaration.slice(header.length),
101
- source: combined,
102
- };
103
- }
104
- if (!combined && !declaration) {
105
- throw new Error(`Missing declaration for ${path}`);
106
- }
107
- const sourcePath = combined ?? declaration ?? path;
108
- const parsed = parseSource(await readFile(sourcePath, "utf8"), {
109
- filename: sourcePath,
110
- name,
111
- declarationOnly: !combined || interfaceMember,
112
- });
113
- if (parsed.name.toLowerCase() !== name.toLowerCase()) {
114
- throw new Error(`Source name ${name} does not match declaration ${parsed.name}: ${sourcePath}`);
115
- }
116
- if (role && parsed.kind !== role) {
117
- throw new Error(`Expected ${role}, found ${parsed.kind}: ${sourcePath}`);
118
- }
119
- if (!role && ["method", "property"].includes(parsed.kind)) {
120
- throw new Error(`Child object has no enclosing POU: ${sourcePath}`);
121
- }
122
- const prefix = /^([A-Z]+)_/.exec(name.toUpperCase())?.[1];
123
- if (!role && prefix && prefixes[prefix] && prefixes[prefix] !== parsed.kind) {
124
- throw new Error(`Prefix ${prefix}_ conflicts with ${parsed.kind}: ${sourcePath}`);
125
- }
126
- const declarationOnly =
127
- ["dut", "gvl", "interface", "property"].includes(parsed.kind) || interfaceMember;
128
- if (declarationOnly && implementation) {
129
- throw new Error(`This object has no implementation: ${implementation}`);
130
- }
131
- if (!combined && !declarationOnly && !implementation) {
132
- throw new Error(`Missing implementation for ${sourcePath}`);
133
- }
134
- return {
135
- ...parsed,
136
- ...(implementation ? { implementation: await readFile(implementation, "utf8") } : {}),
137
- source: sourcePath,
138
- };
58
+ if (!identifierPattern.test(name)) {
59
+ throw new Error(`Invalid IEC object name: ${name}`);
60
+ }
61
+ for (const [part, candidates] of Object.entries(files)) {
62
+ if (candidates.length > 1) {
63
+ throw new Error(
64
+ `Ambiguous ${part} for ${name}: ${candidates.join(", ")}`,
65
+ );
66
+ }
67
+ }
68
+ const combined = files.combined[0];
69
+ const declaration = files.declaration[0];
70
+ const implementation = files.implementation[0];
71
+ if (combined && (declaration || implementation)) {
72
+ throw new Error(`Both combined and split sources exist for ${name}`);
73
+ }
74
+ const path = combined ?? declaration ?? implementation;
75
+ if (!path) {
76
+ throw new Error(`Missing source for ${name}`);
77
+ }
78
+ if (role === "action") {
79
+ if (!combined) {
80
+ throw new Error(`Actions use one implementation-only .st file: ${path}`);
81
+ }
82
+ return {
83
+ kind: "action",
84
+ name,
85
+ declaration: "",
86
+ implementation: await readFile(combined, "utf8"),
87
+ source: combined,
88
+ };
89
+ }
90
+ if (role === "get" || role === "set") {
91
+ if (!combined) {
92
+ throw new Error(`Property accessors use one .st file: ${path}`);
93
+ }
94
+ const body = await readFile(combined, "utf8");
95
+ const header = `METHOD ${name}\n`;
96
+ const parsed = parseSource(`${header}${body}\nEND_METHOD`, {
97
+ filename: combined,
98
+ });
99
+ return {
100
+ ...parsed,
101
+ kind: role,
102
+ name,
103
+ declaration: parsed.declaration.slice(header.length),
104
+ source: combined,
105
+ };
106
+ }
107
+ if (!combined && !declaration) {
108
+ throw new Error(`Missing declaration for ${path}`);
109
+ }
110
+ const sourcePath = combined ?? declaration ?? path;
111
+ const parsed = parseSource(await readFile(sourcePath, "utf8"), {
112
+ filename: sourcePath,
113
+ name,
114
+ declarationOnly: !combined || interfaceMember,
115
+ });
116
+ if (parsed.name.toLowerCase() !== name.toLowerCase()) {
117
+ throw new Error(
118
+ `Source name ${name} does not match declaration ${parsed.name}: ${sourcePath}`,
119
+ );
120
+ }
121
+ if (role && parsed.kind !== role) {
122
+ throw new Error(`Expected ${role}, found ${parsed.kind}: ${sourcePath}`);
123
+ }
124
+ if (!role && ["method", "property"].includes(parsed.kind)) {
125
+ throw new Error(`Child object has no enclosing POU: ${sourcePath}`);
126
+ }
127
+ const prefix = /^([A-Z]+)_/.exec(name.toUpperCase())?.[1];
128
+ if (!role && prefix && prefixes[prefix] && prefixes[prefix] !== parsed.kind) {
129
+ throw new Error(
130
+ `Prefix ${prefix}_ conflicts with ${parsed.kind}: ${sourcePath}`,
131
+ );
132
+ }
133
+ const declarationOnly =
134
+ ["dut", "gvl", "interface", "property"].includes(parsed.kind) ||
135
+ interfaceMember;
136
+ if (declarationOnly && implementation) {
137
+ throw new Error(`This object has no implementation: ${implementation}`);
138
+ }
139
+ if (!combined && !declarationOnly && !implementation) {
140
+ throw new Error(`Missing implementation for ${sourcePath}`);
141
+ }
142
+ return {
143
+ ...parsed,
144
+ ...(implementation
145
+ ? { implementation: await readFile(implementation, "utf8") }
146
+ : {}),
147
+ source: sourcePath,
148
+ };
139
149
  };
140
150
 
141
151
  /** Folder placement organizes the IDE; it never changes IEC names or execution order. */
142
152
  export const loadSources = async (
143
- config: Pick<ResolvedConfig, "sourceDir" | "entry" | "io" | "tasks">,
153
+ config: Pick<ResolvedConfig, "sourceDir" | "entry" | "io" | "tasks">,
144
154
  ) => {
145
- const objects: SourceObject[] = [];
146
- const visit = async (
147
- directory: string,
148
- folder: string[],
149
- parent?: SourceObject,
150
- role?: ObjectKind,
151
- ): Promise<void> => {
152
- const entries = (await readdir(directory, { withFileTypes: true })).sort((a, b) =>
153
- a.name.localeCompare(b.name, "en"),
154
- );
155
- const groups = new Map<string, { name: string; files: Files }>();
156
- for (const entry of entries) {
157
- if (entry.isSymbolicLink()) {
158
- throw new Error(`Source symlinks are not supported: ${resolve(directory, entry.name)}`);
159
- }
160
- if (!entry.isFile() || !/\.st$/i.test(entry.name)) {
161
- continue;
162
- }
163
- const stem = entry.name.slice(0, -3);
164
- const suffix = /^(.*)\.(declaration|implementation)$/.exec(stem);
165
- const generic = ["source", "declaration", "implementation"].includes(stem);
166
- const name = generic ? basename(directory) : (suffix?.[1] ?? stem);
167
- const section = suffix?.[2] ?? stem;
168
- const part = section === "declaration" || section === "implementation" ? section : "combined";
169
- const key = name.toLowerCase();
170
- const group = groups.get(key) ?? {
171
- name,
172
- files: { combined: [], declaration: [], implementation: [] },
173
- };
174
- group.files[part].push(resolve(directory, entry.name));
175
- groups.set(key, group);
176
- }
177
- const units: SourceObject[] = [];
178
- const ownKey = basename(directory).toLowerCase();
179
- // Read an object's own source before its Get/Set files, which have no POU header.
180
- const ordered = [...groups].sort(([a], [b]) => Number(b === ownKey) - Number(a === ownKey));
181
- for (const [key, { name, files }] of ordered) {
182
- const ownProperty = units.find(
183
- (unit) => unit.kind === "property" && unit.name.toLowerCase() === ownKey,
184
- );
185
- const accessor = ownProperty && (key === "get" || key === "set") ? key : undefined;
186
- const unit: SourceObject = {
187
- ...(await readUnit(
188
- accessor ? `${accessor[0]?.toUpperCase()}${accessor.slice(1)}` : name,
189
- files,
190
- accessor ?? role,
191
- parent?.kind === "interface",
192
- )),
193
- folder: parent ? [] : folder,
194
- children: [],
195
- };
196
- if (accessor && ownProperty) {
197
- if (parent?.kind === "interface") {
198
- throw new Error(`Interface properties cannot implement accessors: ${unit.source}`);
199
- }
200
- ownProperty.children.push(unit);
201
- } else {
202
- units.push(unit);
203
- if (parent) {
204
- parent.children.push(unit);
205
- } else {
206
- objects.push(unit);
207
- }
208
- }
209
- }
210
- const own = units.find((unit) => unit.name.toLowerCase() === ownKey && canOwn(unit.kind));
211
- if (own && !parent) {
212
- own.folder = folder.slice(0, -1);
213
- }
214
- for (const entry of entries.filter((entry) => entry.isDirectory())) {
215
- const childRole = childFolders[entry.name];
216
- if (own && childRole) {
217
- if (own.kind === "property" || (own.kind === "interface" && childRole === "action")) {
218
- throw new Error(`Unsupported ${entry.name} under ${own.name}`);
219
- }
220
- await visit(resolve(directory, entry.name), [], own, childRole);
221
- } else if (parent) {
222
- await visit(resolve(directory, entry.name), [], parent, role);
223
- } else {
224
- await visit(resolve(directory, entry.name), [...folder, entry.name]);
225
- }
226
- }
227
- };
228
- await visit(resolve(config.sourceDir), []);
229
- assertUnique(objects, "Application");
230
- if (config.io) bindStructuredTextIo(objects, config.io);
231
- const entry = objects.find((object) => object.source === resolve(config.entry));
232
- if (!entry) {
233
- throw new Error(`Entry is not a discovered source: ${config.entry}`);
234
- }
235
- if (entry.kind !== "program") {
236
- throw new Error(`Entry must be a PROGRAM: ${config.entry}`);
237
- }
238
- if (config.tasks) {
239
- const programs = new Set(
240
- objects.filter((object) => object.kind === "program").map((object) => object.name),
241
- );
242
- for (const task of config.tasks)
243
- for (const program of task.programs)
244
- if (!programs.has(program))
245
- throw new Error(`Task ${task.name} references an undiscovered PROGRAM ${program}`);
246
- }
247
- return { entry: entry.name, objects };
155
+ const objects: SourceObject[] = [];
156
+ const visit = async (
157
+ directory: string,
158
+ folder: string[],
159
+ parent?: SourceObject,
160
+ role?: ObjectKind,
161
+ ): Promise<void> => {
162
+ const entries = (await readdir(directory, { withFileTypes: true })).sort(
163
+ (a, b) => a.name.localeCompare(b.name, "en"),
164
+ );
165
+ const groups = new Map<string, { name: string; files: Files }>();
166
+ for (const entry of entries) {
167
+ if (entry.isSymbolicLink()) {
168
+ throw new Error(
169
+ `Source symlinks are not supported: ${resolve(directory, entry.name)}`,
170
+ );
171
+ }
172
+ if (!entry.isFile() || !/\.st$/i.test(entry.name)) {
173
+ continue;
174
+ }
175
+ const stem = entry.name.slice(0, -3);
176
+ const suffix = /^(.*)\.(declaration|implementation)$/.exec(stem);
177
+ const generic = ["source", "declaration", "implementation"].includes(
178
+ stem,
179
+ );
180
+ const name = generic ? basename(directory) : (suffix?.[1] ?? stem);
181
+ const section = suffix?.[2] ?? stem;
182
+ const part =
183
+ section === "declaration" || section === "implementation"
184
+ ? section
185
+ : "combined";
186
+ const key = name.toLowerCase();
187
+ const group = groups.get(key) ?? {
188
+ name,
189
+ files: { combined: [], declaration: [], implementation: [] },
190
+ };
191
+ group.files[part].push(resolve(directory, entry.name));
192
+ groups.set(key, group);
193
+ }
194
+ const units: SourceObject[] = [];
195
+ const ownKey = basename(directory).toLowerCase();
196
+ // Read an object's own source before its Get/Set files, which have no POU header.
197
+ const ordered = [...groups].sort(
198
+ ([a], [b]) => Number(b === ownKey) - Number(a === ownKey),
199
+ );
200
+ for (const [key, { name, files }] of ordered) {
201
+ const ownProperty = units.find(
202
+ (unit) =>
203
+ unit.kind === "property" && unit.name.toLowerCase() === ownKey,
204
+ );
205
+ const accessor =
206
+ ownProperty && (key === "get" || key === "set") ? key : undefined;
207
+ const unit: SourceObject = {
208
+ ...(await readUnit(
209
+ accessor ? `${accessor[0]?.toUpperCase()}${accessor.slice(1)}` : name,
210
+ files,
211
+ accessor ?? role,
212
+ parent?.kind === "interface",
213
+ )),
214
+ folder: parent ? [] : folder,
215
+ children: [],
216
+ };
217
+ if (accessor && ownProperty) {
218
+ if (parent?.kind === "interface") {
219
+ throw new Error(
220
+ `Interface properties cannot implement accessors: ${unit.source}`,
221
+ );
222
+ }
223
+ ownProperty.children.push(unit);
224
+ } else {
225
+ units.push(unit);
226
+ if (parent) {
227
+ parent.children.push(unit);
228
+ } else {
229
+ objects.push(unit);
230
+ }
231
+ }
232
+ }
233
+ const own = units.find(
234
+ (unit) => unit.name.toLowerCase() === ownKey && canOwn(unit.kind),
235
+ );
236
+ if (own && !parent) {
237
+ own.folder = folder.slice(0, -1);
238
+ }
239
+ for (const entry of entries.filter((entry) => entry.isDirectory())) {
240
+ const childRole = childFolders[entry.name];
241
+ if (own && childRole) {
242
+ if (
243
+ own.kind === "property" ||
244
+ (own.kind === "interface" && childRole === "action")
245
+ ) {
246
+ throw new Error(`Unsupported ${entry.name} under ${own.name}`);
247
+ }
248
+ await visit(resolve(directory, entry.name), [], own, childRole);
249
+ } else if (parent) {
250
+ await visit(resolve(directory, entry.name), [], parent, role);
251
+ } else {
252
+ await visit(resolve(directory, entry.name), [...folder, entry.name]);
253
+ }
254
+ }
255
+ };
256
+ await visit(resolve(config.sourceDir), []);
257
+ assertUnique(objects, "Application");
258
+ if (config.io) {
259
+ bindStructuredTextIo(objects, config.io);
260
+ }
261
+ const entry = objects.find(
262
+ (object) => object.source === resolve(config.entry),
263
+ );
264
+ if (!entry) {
265
+ throw new Error(`Entry is not a discovered source: ${config.entry}`);
266
+ }
267
+ if (entry.kind !== "program") {
268
+ throw new Error(`Entry must be a PROGRAM: ${config.entry}`);
269
+ }
270
+ if (config.tasks) {
271
+ const programs = new Set(
272
+ objects
273
+ .filter((object) => object.kind === "program")
274
+ .map((object) => object.name),
275
+ );
276
+ for (const task of config.tasks) {
277
+ for (const program of task.programs) {
278
+ if (!programs.has(program)) {
279
+ throw new Error(
280
+ `Task ${task.name} references an undiscovered PROGRAM ${program}`,
281
+ );
282
+ }
283
+ }
284
+ }
285
+ }
286
+ return { entry: entry.name, objects };
248
287
  };