@tsplc/build 0.2.17 → 0.2.18
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/dist/cli.js +628 -352
- package/dist/cli.js.map +30 -28
- package/dist/index.js +548 -387
- package/dist/index.js.map +27 -25
- package/dist/libraries.js +68 -27
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile +2 -0
- package/docker/Dockerfile.dockerignore +4 -1
- package/docker/Dockerfile.source +3 -0
- package/docker/config.ts +10 -5
- package/docker/runner.ts +140 -127
- package/package.json +54 -53
- package/python/build.py +183 -124
- package/python/codesys_online.py +44 -45
- package/python/hardware.py +9 -9
- package/python/test_build.py +3 -3
- package/python/test_codesys_online.py +18 -12
- package/python/test_installer_packages.py +1 -0
- package/python/worker.py +11 -7
- package/src/artifacts.ts +82 -60
- package/src/build.ts +71 -62
- package/src/cli.ts +109 -100
- package/src/controller-online.ts +400 -425
- package/src/controller-protocol.ts +140 -0
- package/src/controller-session.ts +466 -397
- package/src/device-tree.ts +19 -15
- package/src/diagnostics.ts +59 -49
- package/src/docker-build.ts +217 -156
- package/src/docker-context.ts +49 -27
- package/src/docker-run.ts +137 -111
- package/src/engineering-settings.ts +42 -0
- package/src/host/codesys-command.ts +35 -34
- package/src/host/codesys-host.ts +157 -129
- package/src/host/codesys-worker.ts +318 -301
- package/src/host/windows-path.ts +63 -59
- package/src/import-request.ts +24 -0
- package/src/index.ts +37 -31
- package/src/io.ts +152 -116
- package/src/libraries.ts +214 -146
- package/src/package-path.ts +3 -1
- package/src/run.ts +132 -108
- package/src/runtime-status.ts +25 -19
- package/src/sources.ts +264 -225
- package/src/st/declarations.ts +181 -193
- package/src/st/lexer.ts +102 -118
- package/src/st/parse.ts +193 -171
- package/src/st/reader.ts +47 -42
- package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
- package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
- package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
- 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
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
methods: "method",
|
|
32
|
+
properties: "property",
|
|
33
|
+
actions: "action",
|
|
33
34
|
};
|
|
34
35
|
const canOwn = (kind: ObjectKind) =>
|
|
35
|
-
|
|
36
|
+
["program", "function_block", "interface", "property"].includes(kind);
|
|
36
37
|
const assertUnique = (objects: SourceObject[], scope: string) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
name: string,
|
|
54
|
+
files: Files,
|
|
55
|
+
role?: ObjectKind,
|
|
56
|
+
interfaceMember = false,
|
|
56
57
|
): Promise<ParsedSource & { source: string }> => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
|
|
153
|
+
config: Pick<ResolvedConfig, "sourceDir" | "entry" | "io" | "tasks">,
|
|
144
154
|
) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
};
|