@tsplc/compiler 0.2.14 → 0.2.17
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/backends/codesys/debug.ts +509 -0
- package/backends/codesys/emit.ts +701 -9
- package/backends/codesys/http.ts +110 -34
- package/backends/codesys/index.ts +2 -1
- package/backends/codesys/io-paths.ts +3 -1
- package/backends/codesys/manifest.ts +312 -0
- package/backends/codesys/plan.ts +39 -9
- package/backends/codesys/publication.ts +2 -2
- package/backends/codesys/storage.ts +10 -3
- package/backends/codesys/task-access.ts +51 -1
- package/backends/codesys/telemetry.ts +593 -0
- package/backends/codesys/types.ts +2 -1
- package/dist/cli.js +190 -33
- package/dist/cli.js.map +6 -6
- package/dist/codesys.js +2148 -432
- package/dist/codesys.js.map +14 -11
- package/dist/elaborate-capture.js +7 -1
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +877 -459
- package/dist/index.js.map +16 -15
- package/dist/ir.js +87 -23
- package/dist/ir.js.map +10 -9
- package/dist/project.js +42 -9
- package/dist/project.js.map +3 -3
- package/dist/telemetry.js +83 -0
- package/dist/telemetry.js.map +12 -0
- package/ir/core-model.ts +13 -0
- package/ir/core.ts +1 -0
- package/ir/errors.ts +20 -0
- package/ir/index.ts +1 -0
- package/ir/link.ts +17 -8
- package/ir/model.ts +39 -0
- package/ir/routine.ts +62 -29
- package/ir/types.ts +7 -1
- package/package.json +12 -6
- package/project/cli.ts +96 -6
- package/project/plant.ts +58 -9
- package/project/project.ts +56 -10
- package/src/frontend/async.ts +284 -195
- package/src/frontend/configuration.ts +7 -21
- package/src/frontend/const-functions.ts +44 -38
- package/src/frontend/elaborate/capture.ts +7 -0
- package/src/frontend/elaborate/index.ts +135 -61
- package/src/frontend/elaborate/layouts.ts +8 -0
- package/src/frontend/elaborate/model.ts +13 -0
- package/src/frontend/elaborate/specialize.ts +302 -111
- package/src/frontend/exposure.ts +28 -26
- package/src/frontend/positions.ts +272 -0
- package/src/frontend/promises.ts +52 -47
- package/src/frontend/services.ts +16 -26
- package/src/frontend/types.ts +14 -0
- package/src/frontend/typescript.ts +185 -65
- package/stdlib/telemetry.ts +72 -0
- package/types/telemetry.d.ts +44 -0
package/ir/routine.ts
CHANGED
|
@@ -92,6 +92,7 @@ export class RoutineNormalizer {
|
|
|
92
92
|
id: routine.id,
|
|
93
93
|
name: routine.name,
|
|
94
94
|
...("owner" in routine && routine.owner && { owner: routine.owner }),
|
|
95
|
+
...("origin" in routine && routine.origin && { origin: routine.origin }),
|
|
95
96
|
parameters,
|
|
96
97
|
locals: routine.locals,
|
|
97
98
|
body: routine.body,
|
|
@@ -549,35 +550,7 @@ export class RoutineNormalizer {
|
|
|
549
550
|
span: value.span,
|
|
550
551
|
});
|
|
551
552
|
} else if (input.mode === "inOut" && value.kind === "borrow") {
|
|
552
|
-
|
|
553
|
-
checked = placeType(
|
|
554
|
-
value.place,
|
|
555
|
-
this.declarations.types,
|
|
556
|
-
this.sourceScope,
|
|
557
|
-
this.ranges,
|
|
558
|
-
!block,
|
|
559
|
-
);
|
|
560
|
-
if (
|
|
561
|
-
!root ||
|
|
562
|
-
checked.issue ||
|
|
563
|
-
checked.type !== value.type ||
|
|
564
|
-
(!block && !this.declarations.isValueType(value.type)) ||
|
|
565
|
-
this.counters.has(root.id) ||
|
|
566
|
-
(root.storage === "local" && !initialized.has(root.id))
|
|
567
|
-
)
|
|
568
|
-
reject(
|
|
569
|
-
checked.issue ?? "Borrow requires initialized mutable value storage of the exact type.",
|
|
570
|
-
value.span,
|
|
571
|
-
);
|
|
572
|
-
arguments_.push({
|
|
573
|
-
kind: "borrow",
|
|
574
|
-
type: value.type,
|
|
575
|
-
place: {
|
|
576
|
-
root: value.place.root,
|
|
577
|
-
path: this.normalizePath(value.place, initialized, output),
|
|
578
|
-
},
|
|
579
|
-
span: value.span,
|
|
580
|
-
});
|
|
553
|
+
arguments_.push(this.borrow(value, initialized, output));
|
|
581
554
|
} else {
|
|
582
555
|
if (value.kind === "borrow")
|
|
583
556
|
reject("Readonly value inputs cannot receive borrowed references.", value.span);
|
|
@@ -587,6 +560,43 @@ export class RoutineNormalizer {
|
|
|
587
560
|
return arguments_;
|
|
588
561
|
}
|
|
589
562
|
|
|
563
|
+
private borrow(
|
|
564
|
+
value: Extract<Expression, { kind: "borrow" }>,
|
|
565
|
+
initialized: Set<string>,
|
|
566
|
+
output: CoreStatement[],
|
|
567
|
+
): CoreExpression {
|
|
568
|
+
const block = this.declarations.types.get(value.type)?.kind === "block";
|
|
569
|
+
const root = this.sourceScope.get(value.place.root),
|
|
570
|
+
checked = placeType(
|
|
571
|
+
value.place,
|
|
572
|
+
this.declarations.types,
|
|
573
|
+
this.sourceScope,
|
|
574
|
+
this.ranges,
|
|
575
|
+
!block,
|
|
576
|
+
);
|
|
577
|
+
if (
|
|
578
|
+
!root ||
|
|
579
|
+
checked.issue ||
|
|
580
|
+
checked.type !== value.type ||
|
|
581
|
+
(!block && !this.declarations.isValueType(value.type)) ||
|
|
582
|
+
this.counters.has(root.id) ||
|
|
583
|
+
(root.storage === "local" && !initialized.has(root.id))
|
|
584
|
+
)
|
|
585
|
+
reject(
|
|
586
|
+
checked.issue ?? "Borrow requires initialized mutable value storage of the exact type.",
|
|
587
|
+
value.span,
|
|
588
|
+
);
|
|
589
|
+
return {
|
|
590
|
+
kind: "borrow",
|
|
591
|
+
type: value.type,
|
|
592
|
+
place: {
|
|
593
|
+
root: value.place.root,
|
|
594
|
+
path: this.normalizePath(value.place, initialized, output),
|
|
595
|
+
},
|
|
596
|
+
span: value.span,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
590
600
|
private body(
|
|
591
601
|
statements: readonly Statement[],
|
|
592
602
|
initialized: Set<string>,
|
|
@@ -811,6 +821,29 @@ export class RoutineNormalizer {
|
|
|
811
821
|
flow = "return";
|
|
812
822
|
break;
|
|
813
823
|
}
|
|
824
|
+
case "telemetry": {
|
|
825
|
+
const evaluate = (value: Expression) => this.expression(value, initialized, output);
|
|
826
|
+
const { record } = item;
|
|
827
|
+
output.push({
|
|
828
|
+
kind: "telemetry",
|
|
829
|
+
record:
|
|
830
|
+
record.kind === "exception"
|
|
831
|
+
? { kind: "exception", code: evaluate(record.code) }
|
|
832
|
+
: record,
|
|
833
|
+
properties: item.properties.map(({ name, value, length }) => {
|
|
834
|
+
if (!length) return { name, value: evaluate(value) };
|
|
835
|
+
if (value.kind !== "borrow")
|
|
836
|
+
reject("Telemetry text reads a byte array in place.", value.span);
|
|
837
|
+
return {
|
|
838
|
+
name,
|
|
839
|
+
value: this.borrow(value, initialized, output),
|
|
840
|
+
length: evaluate(length),
|
|
841
|
+
};
|
|
842
|
+
}),
|
|
843
|
+
span: item.span,
|
|
844
|
+
});
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
814
847
|
case "break":
|
|
815
848
|
case "continue":
|
|
816
849
|
if (!inLoop) reject("Loop control appears outside a bounded loop.", item.span);
|
package/ir/types.ts
CHANGED
|
@@ -26,7 +26,13 @@ export type PlcType =
|
|
|
26
26
|
index?: TypeId;
|
|
27
27
|
}
|
|
28
28
|
| { id: TypeId; kind: "record"; fields: readonly Field[] }
|
|
29
|
-
| {
|
|
29
|
+
| {
|
|
30
|
+
id: TypeId;
|
|
31
|
+
kind: "enum";
|
|
32
|
+
base: TypeId;
|
|
33
|
+
/** `text` is the string a plain TypeScript program's literal union member stands for. */
|
|
34
|
+
members: readonly { name: string; value: string; text?: string }[];
|
|
35
|
+
}
|
|
30
36
|
| { id: TypeId; kind: "block"; block: SymbolId; output?: TypeId };
|
|
31
37
|
|
|
32
38
|
/** The block allocated by a type: a block itself, or a fixed array of one. */
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "git+https://github.com/livingforjesus/tsplc.git",
|
|
18
18
|
"directory": "packages/compiler"
|
|
19
19
|
},
|
|
20
|
-
"version": "0.2.
|
|
20
|
+
"version": "0.2.17",
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
@@ -45,6 +45,11 @@
|
|
|
45
45
|
"bun": "./stdlib/clock.ts",
|
|
46
46
|
"default": "./dist/clock.js"
|
|
47
47
|
},
|
|
48
|
+
"./telemetry": {
|
|
49
|
+
"types": "./types/telemetry.d.ts",
|
|
50
|
+
"bun": "./stdlib/telemetry.ts",
|
|
51
|
+
"default": "./dist/telemetry.js"
|
|
52
|
+
},
|
|
48
53
|
"./math": {
|
|
49
54
|
"types": "./stdlib/math.ts",
|
|
50
55
|
"bun": "./stdlib/math.ts",
|
|
@@ -90,14 +95,15 @@
|
|
|
90
95
|
"bun": ">=1.4.3"
|
|
91
96
|
},
|
|
92
97
|
"dependencies": {
|
|
93
|
-
"@tsplc/agent": "0.2.
|
|
94
|
-
"@tsplc/build": "0.2.
|
|
95
|
-
"@tsplc/config": "0.2.
|
|
96
|
-
"@tsplc/
|
|
98
|
+
"@tsplc/agent": "0.2.17",
|
|
99
|
+
"@tsplc/build": "0.2.17",
|
|
100
|
+
"@tsplc/config": "0.2.17",
|
|
101
|
+
"@tsplc/debug": "0.2.17",
|
|
102
|
+
"@tsplc/typegen": "0.2.17",
|
|
97
103
|
"typescript": "6.0.3",
|
|
98
104
|
"zod": "4.3.6",
|
|
99
105
|
"@types/bun": "1.4.2",
|
|
100
|
-
"@tsplc/server": "0.2.
|
|
106
|
+
"@tsplc/server": "0.2.17"
|
|
101
107
|
},
|
|
102
108
|
"bin": {
|
|
103
109
|
"tsplc": "./dist/cli.js"
|
package/project/cli.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import { buildServer } from "./server.ts";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
3
4
|
import { parseArgs } from "node:util";
|
|
4
|
-
import { resolve } from "node:path";
|
|
5
|
+
import { relative, resolve } from "node:path";
|
|
5
6
|
import {
|
|
6
7
|
buildTargetFor,
|
|
7
8
|
controllerFor,
|
|
@@ -21,11 +22,25 @@ import {
|
|
|
21
22
|
updateController,
|
|
22
23
|
} from "@tsplc/build";
|
|
23
24
|
import { checkHostConfig } from "@tsplc/compiler";
|
|
25
|
+
import {
|
|
26
|
+
createDebugger,
|
|
27
|
+
createLoginAdapter,
|
|
28
|
+
createOpcUaAdapter,
|
|
29
|
+
loadManifest,
|
|
30
|
+
opcUaConnection,
|
|
31
|
+
serveDebugger,
|
|
32
|
+
} from "@tsplc/debug";
|
|
24
33
|
import { type Diagnostic, formatDiagnostic, hasErrors } from "@tsplc/compiler/ir";
|
|
25
34
|
import { deviceTree } from "@tsplc/typegen";
|
|
26
35
|
import { loadHardware, resolveIo } from "@tsplc/typegen/hardware";
|
|
27
36
|
import { generateProject } from "@tsplc/typegen";
|
|
28
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
connect,
|
|
39
|
+
plantCommands,
|
|
40
|
+
readThroughAgent,
|
|
41
|
+
runPlantCommand,
|
|
42
|
+
serverTokenEnv,
|
|
43
|
+
} from "./plant.ts";
|
|
29
44
|
import { buildProject, checkProject, cleanProject, projectTargets } from "./project.ts";
|
|
30
45
|
|
|
31
46
|
async function main() {
|
|
@@ -48,6 +63,9 @@ async function main() {
|
|
|
48
63
|
restore: { type: "boolean" },
|
|
49
64
|
select: { type: "boolean" },
|
|
50
65
|
yes: { type: "boolean", short: "y" },
|
|
66
|
+
port: { type: "string" },
|
|
67
|
+
"server-url": { type: "string" },
|
|
68
|
+
opcua: { type: "boolean" },
|
|
51
69
|
},
|
|
52
70
|
});
|
|
53
71
|
const command = positionals[0],
|
|
@@ -57,14 +75,24 @@ async function main() {
|
|
|
57
75
|
`tsplc install|typegen|check|build|run|download|online-change|inspect|clean|configure [--config tsplc.config.ts] [--plc|--server] [--simulate] [--runtime] [--emit-only] [--format json] [--read symbol]
|
|
58
76
|
|
|
59
77
|
Plant agent (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN):
|
|
60
|
-
tsplc deploy [--download] [--select] [--initialize-user]
|
|
78
|
+
tsplc deploy [--download] [--select] [--initialize-user] [--simulate]
|
|
61
79
|
tsplc target | status | login | logout
|
|
62
80
|
tsplc vars [path] | read <expression>... | write <expression> <value>
|
|
63
81
|
tsplc force <expression> <value> | unforce [expression]... [--restore]
|
|
64
|
-
tsplc start | stop | reset warm|cold (-y skips confirmations)
|
|
82
|
+
tsplc start | stop | reset warm|cold (-y skips confirmations)
|
|
83
|
+
|
|
84
|
+
Debugger, on http://127.0.0.1, for the built .tsplc/artifacts/st/debug.json; it reads through
|
|
85
|
+
the plant agent's login (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN), or --opcua at config monitor:
|
|
86
|
+
tsplc debug [--port 4870] [--server-url url] [--opcua]`,
|
|
65
87
|
);
|
|
66
88
|
return 0;
|
|
67
89
|
}
|
|
90
|
+
if (command === "debug") {
|
|
91
|
+
if (positionals.length > 1)
|
|
92
|
+
throw new Error("Usage: tsplc debug [--port 4870] [--server-url url] [--opcua]");
|
|
93
|
+
await serveProjectDebugger(await loadTsplcConfig(file), values);
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
68
96
|
if (plantCommands.has(command)) {
|
|
69
97
|
const failed = await runPlantCommand(command, positionals.slice(1), values, {
|
|
70
98
|
file,
|
|
@@ -96,8 +124,8 @@ Plant agent (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN):
|
|
|
96
124
|
throw new Error("--runtime applies only to configure.");
|
|
97
125
|
if ((values.plc || values.server) && command !== "build")
|
|
98
126
|
throw new Error("--plc and --server apply only to build.");
|
|
99
|
-
if (values.simulate && !["run", "download", "online-change"].includes(command))
|
|
100
|
-
throw new Error("--simulate applies only to run, download
|
|
127
|
+
if (values.simulate && !["run", "download", "online-change", "deploy"].includes(command))
|
|
128
|
+
throw new Error("--simulate applies only to run, download, online-change and deploy.");
|
|
101
129
|
if (values.plc && values.server)
|
|
102
130
|
throw new Error("Choose --plc or --server, or omit both for a full build.");
|
|
103
131
|
if (values.server && values["emit-only"])
|
|
@@ -162,6 +190,7 @@ Plant agent (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN):
|
|
|
162
190
|
emitOnly: values["emit-only"],
|
|
163
191
|
frozenLockfile: values["frozen-lockfile"],
|
|
164
192
|
loaded: target,
|
|
193
|
+
...(values.simulate && { configured: config }),
|
|
165
194
|
controller: controllerFor(config, command),
|
|
166
195
|
buildTarget: buildTargetFor(command),
|
|
167
196
|
});
|
|
@@ -224,6 +253,7 @@ Plant agent (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN):
|
|
|
224
253
|
forController: true,
|
|
225
254
|
frozenLockfile: values["frozen-lockfile"],
|
|
226
255
|
loaded: target,
|
|
256
|
+
...(values.simulate && { configured: config }),
|
|
227
257
|
buildTarget: buildTargetFor(command),
|
|
228
258
|
});
|
|
229
259
|
if (hasErrors(emitted.diagnostics)) throw new DiagnosticFailure(emitted);
|
|
@@ -262,6 +292,66 @@ Plant agent (TSPLC_AGENT_URL, TSPLC_AGENT_TOKEN):
|
|
|
262
292
|
}
|
|
263
293
|
return report(result, values.format);
|
|
264
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Serves the debugger for the project's last build until interrupted. It reads the controller
|
|
297
|
+
* through the plant agent's login, or with `--opcua` at the config's `monitor` endpoint, and
|
|
298
|
+
* follows the server's telemetry and requests when the server's base URL (`--server-url`, or
|
|
299
|
+
* `server.baseUrlEnv`) and the PLC's bearer token are in the environment.
|
|
300
|
+
*/
|
|
301
|
+
async function serveProjectDebugger(
|
|
302
|
+
{ config, root }: Awaited<ReturnType<typeof loadTsplcConfig>>,
|
|
303
|
+
flags: { port?: string; "server-url"?: string; opcua?: boolean },
|
|
304
|
+
) {
|
|
305
|
+
if (flags.opcua && !config.monitor)
|
|
306
|
+
throw new Error("Configure monitor with the controller's OPC UA endpoint to use --opcua.");
|
|
307
|
+
const agentUrl = process.env["TSPLC_AGENT_URL"];
|
|
308
|
+
if (!flags.opcua && !(agentUrl && process.env["TSPLC_AGENT_TOKEN"]))
|
|
309
|
+
throw new Error(
|
|
310
|
+
"Set TSPLC_AGENT_URL and TSPLC_AGENT_TOKEN to read through the plant agent's login, or pass --opcua for a local virtual PLC.",
|
|
311
|
+
);
|
|
312
|
+
const file = resolve(root, ".tsplc/artifacts/st/debug.json");
|
|
313
|
+
if (!existsSync(file))
|
|
314
|
+
throw new Error(
|
|
315
|
+
`No debug manifest at ${relative(process.cwd(), file)}: build with debug configured.`,
|
|
316
|
+
);
|
|
317
|
+
const port = Number(flags.port ?? 4870);
|
|
318
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
319
|
+
throw new Error("--port must be a TCP port.");
|
|
320
|
+
const manifest = await loadManifest(file);
|
|
321
|
+
const serverUrl =
|
|
322
|
+
flags["server-url"] ?? (config.server?.baseUrlEnv && process.env[config.server.baseUrlEnv]);
|
|
323
|
+
const tokenEnv = serverTokenEnv(config);
|
|
324
|
+
const token = tokenEnv && process.env[tokenEnv];
|
|
325
|
+
const log = (message: string) => console.log(`${new Date().toISOString()} ${message}`);
|
|
326
|
+
const debug = await createDebugger({
|
|
327
|
+
manifest,
|
|
328
|
+
adapter:
|
|
329
|
+
flags.opcua && config.monitor
|
|
330
|
+
? createOpcUaAdapter(opcUaConnection(config.monitor, { root, env: process.env }))
|
|
331
|
+
: createLoginAdapter({
|
|
332
|
+
read: readThroughAgent(connect()),
|
|
333
|
+
probe: manifest.build.target,
|
|
334
|
+
endpoint: `the plant agent at ${agentUrl}`,
|
|
335
|
+
}),
|
|
336
|
+
stateDirectory: resolve(root, ".tsplc/debug"),
|
|
337
|
+
watch: config.debug?.watch ?? {},
|
|
338
|
+
...(serverUrl && token && { server: { url: serverUrl, token } }),
|
|
339
|
+
projectRoot: root,
|
|
340
|
+
log,
|
|
341
|
+
});
|
|
342
|
+
const server = serveDebugger(debug, { port, log });
|
|
343
|
+
console.log(`tsPLC debugger at ${server.url}`);
|
|
344
|
+
if (!serverUrl || !token)
|
|
345
|
+
console.log(
|
|
346
|
+
"Not following the server's telemetry and requests: set server.baseUrlEnv or --server-url, and the server's bearer token variable.",
|
|
347
|
+
);
|
|
348
|
+
await new Promise((stopped) => {
|
|
349
|
+
process.once("SIGINT", stopped);
|
|
350
|
+
process.once("SIGTERM", stopped);
|
|
351
|
+
});
|
|
352
|
+
await server.stop(true);
|
|
353
|
+
await debug.close();
|
|
354
|
+
}
|
|
265
355
|
class DiagnosticFailure extends Error {
|
|
266
356
|
constructor(readonly result: unknown) {
|
|
267
357
|
super("PLC validation failed.");
|
package/project/plant.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
1
2
|
import { basename, relative, resolve, sep } from "node:path";
|
|
2
3
|
import { createInterface } from "node:readline/promises";
|
|
3
4
|
import { pathToFileURL } from "node:url";
|
|
@@ -9,7 +10,8 @@ import {
|
|
|
9
10
|
} from "@tsplc/agent/client";
|
|
10
11
|
import type { Credentials, ReleaseManifest, Target, Variable } from "@tsplc/agent";
|
|
11
12
|
import type { LoginCommand, LoginResult } from "@tsplc/build";
|
|
12
|
-
import { controllerFor, type loadTsplcConfig } from "@tsplc/config";
|
|
13
|
+
import { controllerFor, simulationConfig, type loadTsplcConfig } from "@tsplc/config";
|
|
14
|
+
import { LoginUnavailable, noLogin } from "@tsplc/debug";
|
|
13
15
|
import { hasErrors } from "@tsplc/compiler/ir";
|
|
14
16
|
import { generateProject } from "@tsplc/typegen";
|
|
15
17
|
import { z } from "zod";
|
|
@@ -37,6 +39,7 @@ export interface PlantFlags {
|
|
|
37
39
|
"initialize-user"?: boolean;
|
|
38
40
|
restore?: boolean;
|
|
39
41
|
select?: boolean;
|
|
42
|
+
simulate?: boolean;
|
|
40
43
|
yes?: boolean;
|
|
41
44
|
"frozen-lockfile"?: boolean;
|
|
42
45
|
}
|
|
@@ -116,7 +119,8 @@ const timedProgress = () => {
|
|
|
116
119
|
};
|
|
117
120
|
};
|
|
118
121
|
|
|
119
|
-
|
|
122
|
+
/** The plant agent named by TSPLC_AGENT_URL and TSPLC_AGENT_TOKEN. */
|
|
123
|
+
export const connect = () => {
|
|
120
124
|
const environment = z
|
|
121
125
|
.object({
|
|
122
126
|
TSPLC_AGENT_URL: z.url({
|
|
@@ -131,6 +135,26 @@ const connect = () => {
|
|
|
131
135
|
});
|
|
132
136
|
};
|
|
133
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Reads through the agent's open login, as `tsplc read` does, without opening one: the
|
|
140
|
+
* debugger has no controller credentials.
|
|
141
|
+
*/
|
|
142
|
+
export const readThroughAgent =
|
|
143
|
+
(agent: AgentClient) =>
|
|
144
|
+
async ([first, ...rest]: string[]) => {
|
|
145
|
+
if (!first) return {};
|
|
146
|
+
try {
|
|
147
|
+
const result = await agent.command({ command: "read", expressions: [first, ...rest] });
|
|
148
|
+
if (!("values" in result)) throw new Error("The agent answered a read without values");
|
|
149
|
+
return result.values;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
const { login } = await agent.status();
|
|
152
|
+
if (!login) throw new LoginUnavailable(noLogin, { cause: error });
|
|
153
|
+
if ("error" in login) throw new Error(login.error, { cause: error });
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
134
158
|
const selectTarget = async (agent: AgentClient): Promise<Target> => {
|
|
135
159
|
const interfaces = await agent.interfaces();
|
|
136
160
|
if (!interfaces.length) throw new Error("The plant PC has no IPv4 network.");
|
|
@@ -177,17 +201,32 @@ const contractVariables = async (config: LoadedConfig["config"], root: string) =
|
|
|
177
201
|
});
|
|
178
202
|
};
|
|
179
203
|
|
|
180
|
-
/**
|
|
204
|
+
/**
|
|
205
|
+
* The variable holding the bearer token the PLC sends the project's own server: the HTTP
|
|
206
|
+
* credential for the server's base URL.
|
|
207
|
+
*/
|
|
208
|
+
export const serverTokenEnv = (config: LoadedConfig["config"]) =>
|
|
209
|
+
config.codesys?.http?.credentials.find(({ originEnv }) => originEnv === config.server?.baseUrlEnv)
|
|
210
|
+
?.bearerTokenEnv;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Compiles the program and bundles its server into the files `tsplc deploy` uploads: the
|
|
214
|
+
* production program, or with `--simulate` the development one without fieldbus I/O, for a
|
|
215
|
+
* virtual PLC whose inputs a plant simulation writes, as `tsplc run --simulate` builds it.
|
|
216
|
+
*/
|
|
181
217
|
const prepareRelease = async (file: string, loaded: LoadedConfig, flags: PlantFlags) => {
|
|
182
218
|
const { config, root } = loaded;
|
|
183
219
|
if (!config.compiler) throw new Error("tsplc deploy deploys tsPLC programs; configure compiler.");
|
|
220
|
+
const command = flags.simulate ? "run" : "deploy";
|
|
184
221
|
const emitted = await buildProject(file, {
|
|
185
222
|
emitOnly: true,
|
|
186
223
|
forController: true,
|
|
187
224
|
frozenLockfile: flags["frozen-lockfile"],
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
225
|
+
...(flags.simulate
|
|
226
|
+
? { loaded: { ...loaded, config: simulationConfig(config) }, configured: config }
|
|
227
|
+
: { loaded }),
|
|
228
|
+
buildTarget: flags.simulate ? "development" : "production",
|
|
229
|
+
controller: controllerFor(config, command),
|
|
191
230
|
});
|
|
192
231
|
if (hasErrors(emitted.diagnostics)) return { diagnostics: emitted.diagnostics };
|
|
193
232
|
const settings = emitted.controllerConfig;
|
|
@@ -209,6 +248,10 @@ const prepareRelease = async (file: string, loaded: LoadedConfig, flags: PlantFl
|
|
|
209
248
|
path: `st/${portable(source)}`,
|
|
210
249
|
content: Bun.file(resolve(settings.sourceDir, source)),
|
|
211
250
|
});
|
|
251
|
+
// The agent's debugger reads the program through its debug manifest.
|
|
252
|
+
const debugManifest = resolve(settings.sourceDir, "debug.json");
|
|
253
|
+
if (existsSync(debugManifest))
|
|
254
|
+
files.push({ path: "debug.json", content: Bun.file(debugManifest) });
|
|
212
255
|
let server: string | undefined;
|
|
213
256
|
if (config.server?.runtime) {
|
|
214
257
|
const script = await bundleServer(config, root);
|
|
@@ -234,6 +277,8 @@ const prepareRelease = async (file: string, loaded: LoadedConfig, flags: PlantFl
|
|
|
234
277
|
},
|
|
235
278
|
variables: await contractVariables(config, root),
|
|
236
279
|
server,
|
|
280
|
+
debug: config.debug,
|
|
281
|
+
serverTokenEnv: serverTokenEnv(config),
|
|
237
282
|
};
|
|
238
283
|
return { release: { manifest, files } };
|
|
239
284
|
};
|
|
@@ -359,7 +404,7 @@ export const runPlantCommand = async (
|
|
|
359
404
|
};
|
|
360
405
|
switch (command) {
|
|
361
406
|
case "deploy": {
|
|
362
|
-
expect(0, "[--download] [--select] [--initialize-user]");
|
|
407
|
+
expect(0, "[--download] [--select] [--initialize-user] [--simulate]");
|
|
363
408
|
const deployed = await deploy(agent, project.file, await project.load(), flags);
|
|
364
409
|
if ("diagnostics" in deployed) return deployed;
|
|
365
410
|
console.log(`Deployed release ${deployed.id} (${deployed.operation}).`);
|
|
@@ -409,8 +454,12 @@ export const runPlantCommand = async (
|
|
|
409
454
|
))
|
|
410
455
|
)
|
|
411
456
|
throw new Error("Cancelled.");
|
|
412
|
-
await agent.logout();
|
|
413
|
-
console.log(
|
|
457
|
+
const { failure } = await agent.logout();
|
|
458
|
+
console.log(
|
|
459
|
+
failure
|
|
460
|
+
? `The login couldn't log out (${failure}); its container was removed.`
|
|
461
|
+
: "Logged out.",
|
|
462
|
+
);
|
|
414
463
|
return;
|
|
415
464
|
}
|
|
416
465
|
case "vars": {
|
package/project/project.ts
CHANGED
|
@@ -4,6 +4,8 @@ import {
|
|
|
4
4
|
buildCodesys,
|
|
5
5
|
codesysBuildConfig,
|
|
6
6
|
type CodesysPlan,
|
|
7
|
+
type CodesysProject,
|
|
8
|
+
type SourceMapping,
|
|
7
9
|
diagnoseCodesys,
|
|
8
10
|
emitCodesys,
|
|
9
11
|
ioTargets,
|
|
@@ -37,10 +39,12 @@ async function loadProject(
|
|
|
37
39
|
file: string,
|
|
38
40
|
loaded?: LoadedConfig,
|
|
39
41
|
buildTarget?: BuildTarget,
|
|
42
|
+
configured?: ResolvedTsplcConfig,
|
|
40
43
|
): Promise<{
|
|
41
44
|
config?: NormalizedConfig;
|
|
42
45
|
configFile: string;
|
|
43
46
|
shared?: ResolvedTsplcConfig;
|
|
47
|
+
project?: CodesysProject;
|
|
44
48
|
diagnostics: readonly Diagnostic[];
|
|
45
49
|
}> {
|
|
46
50
|
let configFile = resolve(file);
|
|
@@ -50,16 +54,25 @@ async function loadProject(
|
|
|
50
54
|
if (hasErrors(diagnostics)) return { configFile, diagnostics };
|
|
51
55
|
const shared = (loaded ?? (await loadTsplcConfig(configFile))).config;
|
|
52
56
|
const root = dirname(configFile);
|
|
57
|
+
const catalog = await loadHardware(shared, root);
|
|
53
58
|
|
|
54
|
-
const config = compilerConfig(
|
|
55
|
-
shared,
|
|
56
|
-
resolveIo(shared, await loadHardware(shared, root)),
|
|
57
|
-
buildTarget,
|
|
58
|
-
);
|
|
59
|
+
const config = compilerConfig(shared, resolveIo(shared, catalog), buildTarget);
|
|
59
60
|
return {
|
|
60
61
|
configFile,
|
|
61
62
|
shared,
|
|
62
63
|
config: { ...config, ...sourceInput(config, root) },
|
|
64
|
+
project: {
|
|
65
|
+
root,
|
|
66
|
+
...(shared.debug && { debug: shared.debug }),
|
|
67
|
+
...(shared.telemetry && { telemetry: shared.telemetry }),
|
|
68
|
+
...(shared.server && { server: shared.server }),
|
|
69
|
+
// A simulation build drops fieldbus IO, but the debugger still shows the hardware it stands in for.
|
|
70
|
+
hardware: {
|
|
71
|
+
catalog: configured ? await loadHardware(configured, root) : catalog,
|
|
72
|
+
config: configured ?? shared,
|
|
73
|
+
tree: shared.codesys?.deviceTree !== undefined,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
63
76
|
diagnostics,
|
|
64
77
|
};
|
|
65
78
|
} catch (cause) {
|
|
@@ -105,7 +118,12 @@ export async function projectTargets(
|
|
|
105
118
|
|
|
106
119
|
export async function checkProject(
|
|
107
120
|
file = "tsplc.config.ts",
|
|
108
|
-
options: {
|
|
121
|
+
options: {
|
|
122
|
+
loaded?: LoadedConfig;
|
|
123
|
+
buildTarget?: BuildTarget;
|
|
124
|
+
/** The configuration `loaded` simulates, whose hardware the debug manifest describes. */
|
|
125
|
+
configured?: ResolvedTsplcConfig;
|
|
126
|
+
} = {},
|
|
109
127
|
): Promise<{
|
|
110
128
|
configFile: string;
|
|
111
129
|
config?: NormalizedConfig;
|
|
@@ -113,8 +131,8 @@ export async function checkProject(
|
|
|
113
131
|
plan?: CodesysPlan;
|
|
114
132
|
diagnostics: readonly Diagnostic[];
|
|
115
133
|
}> {
|
|
116
|
-
const loaded = await loadProject(file, options.loaded, options.buildTarget);
|
|
117
|
-
if (!loaded.config) {
|
|
134
|
+
const loaded = await loadProject(file, options.loaded, options.buildTarget, options.configured);
|
|
135
|
+
if (!loaded.config || !loaded.project) {
|
|
118
136
|
return loaded;
|
|
119
137
|
}
|
|
120
138
|
|
|
@@ -145,7 +163,7 @@ export async function checkProject(
|
|
|
145
163
|
priority: task.priority,
|
|
146
164
|
programs: task.programs,
|
|
147
165
|
}));
|
|
148
|
-
const planned = planCodesys(normalized.core, { tasks }, config.target.options);
|
|
166
|
+
const planned = planCodesys(normalized.core, { tasks }, config.target.options, loaded.project);
|
|
149
167
|
return {
|
|
150
168
|
...loaded,
|
|
151
169
|
...planned,
|
|
@@ -185,6 +203,7 @@ export async function buildProject(
|
|
|
185
203
|
loaded?: LoadedConfig;
|
|
186
204
|
controller?: Controller;
|
|
187
205
|
buildTarget?: BuildTarget;
|
|
206
|
+
configured?: ResolvedTsplcConfig;
|
|
188
207
|
/** Resolve libraries and the device tree into `controllerConfig` even when only emitting. */
|
|
189
208
|
forController?: boolean;
|
|
190
209
|
} = {},
|
|
@@ -207,6 +226,7 @@ export async function buildProject(
|
|
|
207
226
|
const checked = await checkProject(configFile, {
|
|
208
227
|
loaded: options.loaded,
|
|
209
228
|
buildTarget: options.buildTarget,
|
|
229
|
+
configured: options.configured,
|
|
210
230
|
});
|
|
211
231
|
if (!checked.plan || !checked.config) {
|
|
212
232
|
return { diagnostics: checked.diagnostics };
|
|
@@ -234,10 +254,13 @@ export async function buildProject(
|
|
|
234
254
|
staging = await mkdtemp(join(previous.root, "stage-"));
|
|
235
255
|
const artifacts = [
|
|
236
256
|
...bundle.artifacts,
|
|
237
|
-
{ path: "mappings.json", content: `${JSON.stringify(bundle.mappings
|
|
257
|
+
{ path: "mappings.json", content: `${JSON.stringify(compactMappings(bundle.mappings))}\n` },
|
|
238
258
|
{ path: "io-symbols.json", content: `${JSON.stringify(bundle.ioSymbols, null, 2)}\n` },
|
|
239
259
|
{ path: "publication.json", content: `${JSON.stringify(bundle.publication, null, 2)}\n` },
|
|
240
260
|
{ path: "config.json", content: `${JSON.stringify(checked.config, null, 2)}\n` },
|
|
261
|
+
...(bundle.debug
|
|
262
|
+
? [{ path: "debug.json", content: `${JSON.stringify(bundle.debug)}\n` }]
|
|
263
|
+
: []),
|
|
241
264
|
];
|
|
242
265
|
for (const artifact of artifacts) {
|
|
243
266
|
const path = join(staging, "st", artifact.path);
|
|
@@ -300,6 +323,29 @@ export async function buildProject(
|
|
|
300
323
|
}
|
|
301
324
|
}
|
|
302
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Source mappings with each ST path and source file written once: `mappings` rows are
|
|
328
|
+
* `[object, line, file, start, end, sourceLine, sourceColumn]`, indexing `objects` and `files`.
|
|
329
|
+
*/
|
|
330
|
+
function compactMappings(mappings: readonly SourceMapping[]) {
|
|
331
|
+
const objects = new Map<string, number>(),
|
|
332
|
+
files = new Map<string, number>();
|
|
333
|
+
const index = (table: Map<string, number>, value: string) => {
|
|
334
|
+
if (!table.has(value)) table.set(value, table.size);
|
|
335
|
+
return table.get(value)!;
|
|
336
|
+
};
|
|
337
|
+
const rows = mappings.map(({ path, line, span }) => [
|
|
338
|
+
index(objects, path),
|
|
339
|
+
line,
|
|
340
|
+
index(files, span.file),
|
|
341
|
+
span.start,
|
|
342
|
+
span.end,
|
|
343
|
+
span.line,
|
|
344
|
+
span.column,
|
|
345
|
+
]);
|
|
346
|
+
return { objects: [...objects.keys()], files: [...files.keys()], mappings: rows };
|
|
347
|
+
}
|
|
348
|
+
|
|
303
349
|
export async function cleanProject(file = "tsplc.config.ts"): Promise<{
|
|
304
350
|
diagnostics: readonly Diagnostic[];
|
|
305
351
|
}> {
|