@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.
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
@@ -4,142 +4,170 @@ import { access, open, readFile, rm, writeFile } from "node:fs/promises";
4
4
  import { delimiter, resolve } from "node:path";
5
5
  import { promisify } from "node:util";
6
6
  import { z } from "zod";
7
- import type { ResolvedConfig } from "../config";
8
- import { codesysCommand, codesysCommandFilename, getHostCommand } from "./codesys-command";
7
+ import type { ResolvedConfig } from "@tsplc/config/build";
8
+ import {
9
+ codesysCommand,
10
+ codesysCommandFilename,
11
+ getHostCommand,
12
+ } from "./codesys-command";
9
13
  import { toWindowsPath } from "./windows-path";
10
14
 
11
15
  const execute = promisify(execFile);
12
16
  const exitSchema = z.object({
13
- pid: z.int().positive(),
14
- code: z.int().nullable(),
15
- signal: z.string().nullable(),
17
+ pid: z.int().positive(),
18
+ code: z.int().nullable(),
19
+ signal: z.string().nullable(),
16
20
  });
17
21
 
18
22
  export const createCodesysHost = (config: ResolvedConfig) => {
19
- const native = process.platform === "win32";
20
- const { codesys, timeouts } = config;
21
- const hostEnv = {
22
- // Child processes inherit OS settings such as PATH and DISPLAY.
23
- ...process.env,
24
- WINEPREFIX: codesys.wine?.prefix,
25
- WINEDEBUG: codesys.wine?.debug,
26
- CODESYS_BUILD_NATIVE_EXIT: String(codesys.wine?.nativeExit ?? false),
27
- };
23
+ const native = process.platform === "win32";
24
+ const { codesys, timeouts } = config;
25
+ const hostEnv = {
26
+ // Child processes inherit OS settings such as PATH and DISPLAY.
27
+ ...process.env,
28
+ WINEPREFIX: codesys.wine?.prefix,
29
+ WINEDEBUG: codesys.wine?.debug,
30
+ CODESYS_BUILD_NATIVE_EXIT: String(codesys.wine?.nativeExit ?? false),
31
+ };
28
32
 
29
- /** Start the headless worker script; its launcher outlives this CLI process. */
30
- const startWorker = async (directory: string) => {
31
- if (!native) {
32
- await execute(codesys.wine?.binary ?? "wine", ["--version"], {
33
- env: hostEnv,
34
- timeout: timeouts.hostCommand,
35
- }).catch((cause: unknown) => {
36
- throw new Error("Wine is missing. Install Wine; Winetricks alone cannot run CODESYS", {
37
- cause,
38
- });
39
- });
40
- const candidates = (process.env["PATH"] ?? "").split(delimiter);
41
- const available = await Promise.all(
42
- candidates.map((path) =>
43
- access(resolve(path, "script"), constants.X_OK).then(
44
- () => true,
45
- () => false,
46
- ),
47
- ),
48
- );
49
- if (!available.some(Boolean)) {
50
- throw new Error(
51
- "Headless Wine builds require the script utility (install util-linux on Linux)",
52
- );
53
- }
54
- }
55
- await writeFile(resolve(directory, codesysCommandFilename), codesysCommand);
56
- const env = {
57
- ...hostEnv,
58
- CODESYS_BUILD_SCRIPT: await toWindowsPath(resolve(directory, "worker.py"), codesys.wine),
59
- CODESYS_BUILD_SESSION: await toWindowsPath(directory, codesys.wine),
60
- CODESYS_BUILD_EXE: await toWindowsPath(codesys.executable, codesys.wine),
61
- CODESYS_BUILD_PROFILE: codesys.profile,
62
- };
63
- const launcher = getHostCommand(codesys);
64
- const logPath = resolve(directory, "codesys.log");
65
- const exitPath = resolve(directory, "launcher-exit.json");
66
- await rm(exitPath, { force: true });
67
- const log = await open(logPath, "w");
68
- try {
69
- const child = spawn(launcher.executable, launcher.args, {
70
- cwd: directory,
71
- detached: true,
72
- windowsHide: true,
73
- env,
74
- stdio: ["ignore", log.fd, log.fd],
75
- });
76
- child.once("exit", (code, signal) => {
77
- try {
78
- appendFileSync(logPath, `\nCODESYS launcher exit status: ${signal ?? code}\n`);
79
- } catch {
80
- // A failed diagnostic write must preserve the original process failure.
81
- }
82
- try {
83
- const temporary = `${exitPath}.${child.pid}.tmp`;
84
- writeFileSync(temporary, JSON.stringify({ pid: child.pid, code, signal }));
85
- renameSync(temporary, exitPath);
86
- } catch {
87
- // The log still records the exit if the optional status file cannot be published.
88
- }
89
- });
90
- await new Promise<void>((resolve, reject) => {
91
- child.once("error", reject);
92
- child.once("spawn", resolve);
93
- });
94
- if (!child.pid) {
95
- throw new Error("CODESYS launcher has no process ID");
96
- }
97
- child.unref();
98
- return child.pid;
99
- } finally {
100
- await log.close();
101
- }
102
- };
33
+ /** Start the headless worker script; its launcher outlives this CLI process. */
34
+ const startWorker = async (directory: string) => {
35
+ if (!native) {
36
+ await execute(codesys.wine?.binary ?? "wine", ["--version"], {
37
+ env: hostEnv,
38
+ timeout: timeouts.hostCommand,
39
+ }).catch((cause: unknown) => {
40
+ throw new Error(
41
+ "Wine is missing. Install Wine; Winetricks alone cannot run CODESYS",
42
+ {
43
+ cause,
44
+ },
45
+ );
46
+ });
47
+ const candidates = (process.env["PATH"] ?? "").split(delimiter);
48
+ const available = await Promise.all(
49
+ candidates.map((path) =>
50
+ access(resolve(path, "script"), constants.X_OK).then(
51
+ () => true,
52
+ () => false,
53
+ ),
54
+ ),
55
+ );
56
+ if (!available.some(Boolean)) {
57
+ throw new Error(
58
+ "Headless Wine builds require the script utility (install util-linux on Linux)",
59
+ );
60
+ }
61
+ }
62
+ await writeFile(resolve(directory, codesysCommandFilename), codesysCommand);
63
+ const env = {
64
+ ...hostEnv,
65
+ CODESYS_BUILD_SCRIPT: await toWindowsPath(
66
+ resolve(directory, "worker.py"),
67
+ codesys.wine,
68
+ ),
69
+ CODESYS_BUILD_SESSION: await toWindowsPath(directory, codesys.wine),
70
+ CODESYS_BUILD_EXE: await toWindowsPath(codesys.executable, codesys.wine),
71
+ CODESYS_BUILD_PROFILE: codesys.profile,
72
+ };
73
+ const launcher = getHostCommand(codesys);
74
+ const logPath = resolve(directory, "codesys.log");
75
+ const exitPath = resolve(directory, "launcher-exit.json");
76
+ await rm(exitPath, { force: true });
77
+ const log = await open(logPath, "w");
78
+ try {
79
+ const child = spawn(launcher.executable, launcher.args, {
80
+ cwd: directory,
81
+ detached: true,
82
+ windowsHide: true,
83
+ env,
84
+ stdio: ["ignore", log.fd, log.fd],
85
+ });
86
+ child.once("exit", (code, signal) => {
87
+ try {
88
+ appendFileSync(
89
+ logPath,
90
+ `\nCODESYS launcher exit status: ${signal ?? code}\n`,
91
+ );
92
+ } catch {
93
+ // A failed diagnostic write must preserve the original process failure.
94
+ }
95
+ try {
96
+ const temporary = `${exitPath}.${child.pid}.tmp`;
97
+ writeFileSync(
98
+ temporary,
99
+ JSON.stringify({ pid: child.pid, code, signal }),
100
+ );
101
+ renameSync(temporary, exitPath);
102
+ } catch {
103
+ // The log still records the exit if the optional status file cannot be published.
104
+ }
105
+ });
106
+ await new Promise<void>((resolve, reject) => {
107
+ child.once("error", reject);
108
+ child.once("spawn", resolve);
109
+ });
110
+ if (!child.pid) {
111
+ throw new Error("CODESYS launcher has no process ID");
112
+ }
113
+ child.unref();
114
+ return child.pid;
115
+ } finally {
116
+ await log.close();
117
+ }
118
+ };
103
119
 
104
- return {
105
- exitStatus: async (directory: string, pid: number) => {
106
- const path = resolve(directory, "launcher-exit.json");
107
- const value = await readFile(path, "utf8").catch((error: unknown) => {
108
- if (error instanceof Error && "code" in error && error.code === "ENOENT") return;
109
- throw error;
110
- });
111
- if (value === undefined) return;
112
- const status = exitSchema.parse(JSON.parse(value));
113
- if (status.pid !== pid) return;
114
- if (!native && status.code === 0 && status.signal === null) {
115
- const logPath = resolve(directory, "codesys.log");
116
- const log = (await readFile(logPath, "utf8"))
117
- // Terminal color escapes must not hide a native assertion signature.
118
- // eslint-disable-next-line no-control-regex
119
- .replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "")
120
- .replace(/\r/g, "");
121
- // A Wine helper can abort under Rosetta while the main launcher returns 0.
122
- // That command still failed; publishing its artifacts would hide the fault.
123
- const assertion =
124
- /^assertion failed \[[^\n]+\]:[^\n]*\n[ \t]*\(ThreadContext[^\n]*\)/m.exec(log);
125
- if (assertion) {
126
- throw new Error(
127
- `Wine translation aborted: ${assertion[0].replace(/\s+/g, " ")}. See ${logPath}`,
128
- );
129
- }
130
- }
131
- return status;
132
- },
133
- startWorker,
134
- terminate: async (pid: number) => {
135
- if (native) {
136
- await execute("taskkill.exe", ["/pid", String(pid), "/t", "/f"], {
137
- env: hostEnv,
138
- timeout: timeouts.hostCommand,
139
- });
140
- return;
141
- }
142
- process.kill(-pid, "SIGKILL");
143
- },
144
- };
120
+ return {
121
+ exitStatus: async (directory: string, pid: number) => {
122
+ const path = resolve(directory, "launcher-exit.json");
123
+ const value = await readFile(path, "utf8").catch((error: unknown) => {
124
+ if (
125
+ error instanceof Error &&
126
+ "code" in error &&
127
+ error.code === "ENOENT"
128
+ ) {
129
+ return;
130
+ }
131
+ throw error;
132
+ });
133
+ if (value === undefined) {
134
+ return;
135
+ }
136
+ const status = exitSchema.parse(JSON.parse(value));
137
+ if (status.pid !== pid) {
138
+ return;
139
+ }
140
+ if (!native && status.code === 0 && status.signal === null) {
141
+ const logPath = resolve(directory, "codesys.log");
142
+ const log = (await readFile(logPath, "utf8"))
143
+ // Terminal color escapes must not hide a native assertion signature.
144
+ // eslint-disable-next-line no-control-regex
145
+ .replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "")
146
+ .replace(/\r/g, "");
147
+ // A Wine helper can abort under Rosetta while the main launcher returns 0.
148
+ // That command still failed; publishing its artifacts would hide the fault.
149
+ const assertion =
150
+ /^assertion failed \[[^\n]+\]:[^\n]*\n[ \t]*\(ThreadContext[^\n]*\)/m.exec(
151
+ log,
152
+ );
153
+ if (assertion) {
154
+ throw new Error(
155
+ `Wine translation aborted: ${assertion[0].replace(/\s+/g, " ")}. See ${logPath}`,
156
+ );
157
+ }
158
+ }
159
+ return status;
160
+ },
161
+ startWorker,
162
+ terminate: async (pid: number) => {
163
+ if (native) {
164
+ await execute("taskkill.exe", ["/pid", String(pid), "/t", "/f"], {
165
+ env: hostEnv,
166
+ timeout: timeouts.hostCommand,
167
+ });
168
+ return;
169
+ }
170
+ process.kill(-pid, "SIGKILL");
171
+ },
172
+ };
145
173
  };