@tsplc/build 0.2.16 → 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
package/src/build.ts CHANGED
@@ -1,76 +1,85 @@
1
1
  import { createHash } from "node:crypto";
2
- import { access, copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
+ import {
3
+ access,
4
+ copyFile,
5
+ mkdir,
6
+ readFile,
7
+ rm,
8
+ writeFile,
9
+ } from "node:fs/promises";
3
10
  import { resolve } from "node:path";
4
11
  import { buildFiles, publishBuild, readBuild } from "./artifacts";
5
- import { dockerImage, type ResolvedConfig } from "./config";
12
+ import { dockerImage, type ResolvedConfig } from "@tsplc/config/build";
6
13
  import { buildInDocker } from "./docker-build";
7
14
  import { writeDeviceTree } from "./device-tree";
8
15
  import { createCodesysWorker, withWorkerLock } from "./host/codesys-worker";
16
+ import { importRequest } from "./import-request";
9
17
  import { loadSources } from "./sources";
10
- import { toWindowsPath } from "./host/windows-path.ts";
11
18
 
12
19
  export const buildProject = async (config: ResolvedConfig) => {
13
- if (!config.docker) throw new Error("Builds require a Docker builder image.");
14
- return buildInDocker(config, dockerImage);
20
+ if (!config.docker) {
21
+ throw new Error("Builds require a Docker builder image.");
22
+ }
23
+ return buildInDocker(config, dockerImage);
15
24
  };
16
25
 
17
26
  /** The container runner owns Wine; this operation is not a public host build. */
18
27
  export const buildInContainer = async (config: ResolvedConfig) => {
19
- return withWorkerLock(config, async () => {
20
- // Invalidate the previous build before validation, so run cannot deploy stale
21
- // output after a failed rebuild. Keep the last project available for inspection.
22
- await rm(resolve(config.outDir, buildFiles.receipt), { force: true });
23
- const sources = await loadSources(config);
24
- await access(config.template);
25
- const worker = createCodesysWorker(config);
26
- const session = await worker.launch();
27
- const directory = resolve(config.outDir, ".codesys/build");
28
- await rm(directory, { recursive: true, force: true });
29
- await mkdir(resolve(directory, "runtime"), { recursive: true });
30
- const template = resolve(directory, "Template.project");
31
- await copyFile(config.template, template);
32
- const deviceTree = config.deviceTree && (await writeDeviceTree(config.deviceTree, directory));
33
- const request = {
34
- libraryReferences: config.libraryReferences,
35
- libraries: await Promise.all(
36
- (config.libraries ?? []).map(async (library) => ({
37
- ...library,
38
- path: await toWindowsPath(library.path, config.codesys.wine),
39
- })),
40
- ),
41
- ...sources,
42
- task: config.task,
43
- ...(config.tasks && { tasks: config.tasks }),
44
- removeObjects: config.removeObjects,
45
- symbols: config.symbols,
46
- ...(deviceTree && { deviceTree }),
47
- ...(config.controller && { controller: config.controller }),
48
- mode: "build",
49
- output: buildFiles.output,
50
- project: buildFiles.project,
51
- archive: buildFiles.archive,
52
- receipt: buildFiles.receipt,
53
- template: "Template.project",
54
- templateHash: createHash("sha256")
55
- .update(await readFile(template))
56
- .digest("hex"),
57
- };
58
- await writeFile(resolve(directory, "request.json"), JSON.stringify(request, null, 2));
59
- console.log(`CODESYS build: ${config.outDir}`);
60
- try {
61
- await worker.request(session, { action: "build" }, config.timeouts.build);
62
- const compiled = await readBuild(directory);
63
- if (config.tasks && JSON.stringify(compiled.tasks) !== JSON.stringify(config.tasks)) {
64
- throw new Error("CODESYS build receipt does not match the requested task plan");
65
- }
66
- // Builds and runs share the lock. Removing the receipt above keeps a failed
67
- // publication from looking complete even if only some files were replaced.
68
- return await publishBuild(compiled, config.outDir);
69
- } catch (cause) {
70
- throw new Error(
71
- `CODESYS build failed. Inspect ${directory}/progress.log, error.txt and ${session.directory}/codesys.log`,
72
- { cause },
73
- );
74
- }
75
- });
28
+ return withWorkerLock(config, async () => {
29
+ // Invalidate the previous build before validation, so run cannot deploy stale
30
+ // output after a failed rebuild. Keep the last project available for inspection.
31
+ await rm(resolve(config.outDir, buildFiles.receipt), { force: true });
32
+ const sources = await loadSources(config);
33
+ await access(config.template);
34
+ const worker = createCodesysWorker(config);
35
+ const session = await worker.launch();
36
+ const directory = resolve(config.outDir, ".codesys/build");
37
+ await rm(directory, { recursive: true, force: true });
38
+ await mkdir(resolve(directory, "runtime"), { recursive: true });
39
+ const template = resolve(directory, "Template.project");
40
+ await copyFile(config.template, template);
41
+ const deviceTree =
42
+ config.deviceTree &&
43
+ (await writeDeviceTree(config.deviceTree, directory));
44
+ const request = {
45
+ libraryReferences: config.libraryReferences,
46
+ ...(await importRequest(config, sources)),
47
+ ...(deviceTree && { deviceTree }),
48
+ ...(config.controller && { controller: config.controller }),
49
+ mode: "build",
50
+ output: buildFiles.output,
51
+ project: buildFiles.project,
52
+ archive: buildFiles.archive,
53
+ receipt: buildFiles.receipt,
54
+ template: "Template.project",
55
+ templateHash: createHash("sha256")
56
+ .update(await readFile(template))
57
+ .digest("hex"),
58
+ };
59
+ await writeFile(
60
+ resolve(directory, "request.json"),
61
+ JSON.stringify(request, null, 2),
62
+ );
63
+ console.log(`CODESYS build: ${config.outDir}`);
64
+ try {
65
+ await worker.request(session, { action: "build" }, config.timeouts.build);
66
+ const compiled = await readBuild(directory);
67
+ if (
68
+ config.tasks &&
69
+ JSON.stringify(compiled.tasks) !== JSON.stringify(config.tasks)
70
+ ) {
71
+ throw new Error(
72
+ "CODESYS build receipt does not match the requested task plan",
73
+ );
74
+ }
75
+ // Builds and runs share the lock. Removing the receipt above keeps a failed
76
+ // publication from looking complete even if only some files were replaced.
77
+ return await publishBuild(compiled, config.outDir);
78
+ } catch (cause) {
79
+ throw new Error(
80
+ `CODESYS build failed. Inspect ${directory}/progress.log, error.txt and ${session.directory}/codesys.log`,
81
+ { cause },
82
+ );
83
+ }
84
+ });
76
85
  };
package/src/cli.ts CHANGED
@@ -1,116 +1,125 @@
1
1
  #!/usr/bin/env bun
2
2
  import { parseArgs } from "node:util";
3
- import { deviceTree } from "@tsplc/typegen";
4
3
  import { loadHardware, resolveIo } from "@tsplc/typegen/hardware";
5
4
  import { controllerFor, loadTsplcConfig, rawBuildConfig } from "@tsplc/config";
5
+ import { z } from "zod";
6
6
  import { buildProject } from "./build.ts";
7
- import { resolveConfig } from "./config.ts";
7
+ import { resolveConfig } from "@tsplc/config/build";
8
8
  import { inspectController, updateController } from "./controller-online.ts";
9
9
  import { configureDocker, configureRuntime } from "./docker-build.ts";
10
+ import { rawEngineeringSettings } from "./engineering-settings.ts";
10
11
  import { installLibraries } from "./libraries.ts";
11
12
  import { runProject } from "./run.ts";
12
13
  import { loadSources } from "./sources.ts";
13
14
 
15
+ const commandSchema = z.enum([
16
+ "configure",
17
+ "install",
18
+ "check",
19
+ "build",
20
+ "run",
21
+ "download",
22
+ "online-change",
23
+ "inspect",
24
+ ]);
25
+
14
26
  async function main() {
15
- const { values, positionals } = parseArgs({
16
- allowPositionals: true,
17
- options: {
18
- config: { type: "string" },
19
- help: { type: "boolean", short: "h" },
20
- read: { type: "string", multiple: true },
21
- "initialize-user": { type: "boolean" },
22
- "frozen-lockfile": { type: "boolean" },
23
- runtime: { type: "boolean" },
24
- },
25
- });
26
- const command = positionals[0];
27
- const commands = new Set([
28
- "configure",
29
- "install",
30
- "check",
31
- "build",
32
- "run",
33
- "download",
34
- "online-change",
35
- "inspect",
36
- ]);
37
- if (positionals.length > 1) throw new Error("Specify one command.");
38
- if (!command || values.help) {
39
- console.log(
40
- "tsplc-build configure|install|check|build|run|download|online-change|inspect [--config tsplc.config.ts] [--read symbol] [--runtime]",
41
- );
42
- return;
43
- }
44
- if (!commands.has(command)) throw new Error(`Unknown command ${command}.`);
45
- if (values.runtime && command !== "configure")
46
- throw new Error("--runtime applies only to configure.");
47
- if (command === "configure") {
48
- await (values.runtime ? configureRuntime() : configureDocker());
49
- return;
50
- }
51
- const { config, configFile, root } = await loadTsplcConfig(values.config);
52
- if (command === "install") {
53
- const libraries = await installLibraries(config.dependencies, root, {
54
- frozen: values["frozen-lockfile"],
55
- });
56
- console.log(`Installed ${libraries.length} CODESYS libraries.`);
57
- return;
58
- }
59
- if (!config.build) throw new Error("Configure build options in tsplc.config.ts.");
60
- if (command === "inspect") {
61
- // Inspection only needs connection/baseline settings; it remains usable
62
- // without sources, EDS descriptors or installed library payloads.
63
- console.log(
64
- JSON.stringify(
65
- await inspectController(
66
- resolveConfig(
67
- { ...config.build, controller: controllerFor(config, command) },
68
- configFile,
69
- ),
70
- values.read,
71
- ),
72
- null,
73
- 2,
74
- ),
75
- );
76
- return;
77
- }
78
- if (config.compiler)
79
- throw new Error("Use tsplc for TypeScript projects. tsplc-build accepts raw Structured Text.");
80
- const build = rawBuildConfig(config);
81
- const hardware = await loadHardware(config, root);
82
- const io = resolveIo(config, hardware);
83
- if (command === "check") {
84
- const sources = await loadSources(resolveConfig({ ...build, io }, configFile));
85
- console.log(`Validated ${sources.objects.length} Structured Text objects.`);
86
- return;
87
- }
88
- const libraries = await installLibraries(config.dependencies, root, {
89
- frozen: values["frozen-lockfile"],
90
- });
91
- const settings = resolveConfig(
92
- {
93
- ...build,
94
- libraries,
95
- io,
96
- deviceTree: await deviceTree(config, hardware),
97
- controller: controllerFor(config, command),
98
- },
99
- configFile,
100
- );
101
- if (command === "build") await buildProject(settings);
102
- else if (command === "run") {
103
- await buildProject(settings);
104
- await runProject(settings, config.name);
105
- } else if (command === "download" || command === "online-change")
106
- await updateController(settings, command, {
107
- initializeUser: values["initialize-user"],
108
- read: values.read,
109
- });
27
+ const { values, positionals } = parseArgs({
28
+ allowPositionals: true,
29
+ options: {
30
+ config: { type: "string" },
31
+ help: { type: "boolean", short: "h" },
32
+ read: { type: "string", multiple: true },
33
+ "initialize-user": { type: "boolean" },
34
+ "frozen-lockfile": { type: "boolean" },
35
+ runtime: { type: "boolean" },
36
+ },
37
+ });
38
+ const [name] = positionals;
39
+ if (positionals.length > 1) {
40
+ throw new Error("Specify one command.");
41
+ }
42
+ if (!name || values.help) {
43
+ console.log(
44
+ "tsplc-build configure|install|check|build|run|download|online-change|inspect [--config tsplc.config.ts] [--read symbol] [--runtime]",
45
+ );
46
+ return;
47
+ }
48
+ const parsed = commandSchema.safeParse(name);
49
+ if (!parsed.success) {
50
+ throw new Error(`Unknown command ${name}.`);
51
+ }
52
+ const command = parsed.data;
53
+ if (values.runtime && command !== "configure") {
54
+ throw new Error("--runtime applies only to configure.");
55
+ }
56
+ if (command === "configure") {
57
+ await (values.runtime ? configureRuntime() : configureDocker());
58
+ return;
59
+ }
60
+ const loaded = await loadTsplcConfig(values.config);
61
+ const { config, configFile, root } = loaded;
62
+ if (command === "install") {
63
+ const libraries = await installLibraries(config.dependencies, root, {
64
+ frozen: values["frozen-lockfile"],
65
+ });
66
+ console.log(`Installed ${libraries.length} CODESYS libraries.`);
67
+ return;
68
+ }
69
+ if (!config.build) {
70
+ throw new Error("Configure build options in tsplc.config.ts.");
71
+ }
72
+ if (command === "inspect") {
73
+ // Inspection only needs connection/baseline settings; it remains usable
74
+ // without sources, EDS descriptors or installed library payloads.
75
+ console.log(
76
+ JSON.stringify(
77
+ await inspectController(
78
+ resolveConfig(
79
+ { ...config.build, controller: controllerFor(config, command) },
80
+ configFile,
81
+ ),
82
+ values.read,
83
+ ),
84
+ null,
85
+ 2,
86
+ ),
87
+ );
88
+ return;
89
+ }
90
+ if (config.compiler) {
91
+ throw new Error(
92
+ "Use tsplc for TypeScript projects. tsplc-build accepts raw Structured Text.",
93
+ );
94
+ }
95
+ if (command === "check") {
96
+ const build = rawBuildConfig(config);
97
+ const hardware = await loadHardware(config, root);
98
+ const sources = await loadSources(
99
+ resolveConfig({ ...build, io: resolveIo(config, hardware) }, configFile),
100
+ );
101
+ console.log(`Validated ${sources.objects.length} Structured Text objects.`);
102
+ return;
103
+ }
104
+ const settings = await rawEngineeringSettings(loaded, {
105
+ command,
106
+ frozenLockfile: values["frozen-lockfile"],
107
+ });
108
+ if (command === "build") {
109
+ await buildProject(settings);
110
+ } else if (command === "run") {
111
+ await buildProject(settings);
112
+ await runProject(settings, config.name);
113
+ } else {
114
+ await updateController(settings, command, {
115
+ initializeUser: values["initialize-user"],
116
+ read: values.read,
117
+ });
118
+ }
110
119
  }
111
120
  try {
112
- await main();
121
+ await main();
113
122
  } catch (cause) {
114
- console.error(cause instanceof Error ? cause.message : String(cause));
115
- process.exitCode = 1;
123
+ console.error(cause instanceof Error ? cause.message : String(cause));
124
+ process.exitCode = 1;
116
125
  }