@tsplc/compiler 0.5.13 → 0.5.15

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 (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. package/types/plc.d.ts +0 -4
package/dist/project.js DELETED
@@ -1,626 +0,0 @@
1
- // packages/compiler/src/project/project.ts
2
- import {
3
- copyFile,
4
- mkdir as mkdir2,
5
- mkdtemp,
6
- realpath,
7
- rm as rm2,
8
- writeFile as writeFile2
9
- } from "node:fs/promises";
10
- import { basename, dirname as dirname2, join as join2, resolve } from "node:path";
11
- import { installLibraries } from "@tsplc/build";
12
- import {
13
- analyze,
14
- checkHostConfig
15
- } from "@tsplc/compiler";
16
- import {
17
- buildCodesys,
18
- codesysBuildConfig,
19
- diagnoseCodesys,
20
- emitCodesys,
21
- ioTargets,
22
- planCodesys,
23
- publicationSelectors
24
- } from "@tsplc/compiler/codesys";
25
- import {
26
- hasErrors,
27
- normalizeModule
28
- } from "@tsplc/compiler/ir";
29
- import {
30
- compilerConfig,
31
- loadTsplcConfig,
32
- simulationConfig
33
- } from "@tsplc/config";
34
- import { emittedFiles, emittedStDirectory as emittedStDirectory2 } from "@tsplc/config/project-files";
35
- import { explainFileName } from "@tsplc/debug/explain-file";
36
- import { debugManifestFile } from "@tsplc/debug/manifest";
37
- import {
38
- deviceTree,
39
- loadHardware,
40
- resolveIo
41
- } from "@tsplc/typegen";
42
-
43
- // packages/compiler/src/runtime/launch.ts
44
- import { generatedDirectory } from "@tsplc/config/project-files";
45
- import { serviceOfPath } from "@tsplc/server";
46
- import { z } from "zod";
47
- var runtimeDirectory = `${generatedDirectory}/runtime`;
48
- var launchSchema = z.strictObject({
49
- config: z.string().nonempty(),
50
- target: z.string().nonempty().nullable(),
51
- services: z.record(z.string(), z.string().startsWith("/tsplc/services/"))
52
- });
53
- var runtimeStateSchema = z.strictObject({
54
- pid: z.number().int().positive(),
55
- endpoint: z.string().startsWith("opc.tcp://"),
56
- startedAt: z.iso.datetime()
57
- });
58
- var blockRoute = (runtime) => {
59
- if (runtime?.kind === "http") {
60
- return URL.parse(runtime.url)?.pathname;
61
- }
62
- return runtime?.kind === "rpc" ? runtime.path : undefined;
63
- };
64
- var serviceRoutes = (core) => Object.fromEntries(core.blocks.flatMap((block) => {
65
- const route = blockRoute(block.runtime);
66
- const procedure = route && serviceOfPath(route);
67
- return route && procedure ? [[procedure, route]] : [];
68
- }));
69
-
70
- // packages/compiler/src/project/artifacts.ts
71
- import { createHash } from "node:crypto";
72
- import {
73
- lstat,
74
- mkdir,
75
- readdir,
76
- readFile,
77
- rename,
78
- rm,
79
- writeFile
80
- } from "node:fs/promises";
81
- import { dirname, join } from "node:path";
82
- import { sha256Digest } from "@tsplc/config/build";
83
- import {
84
- emittedStDirectory,
85
- generatedDirectory as generatedDirectory2
86
- } from "@tsplc/config/project-files";
87
- import { z as z2 } from "zod";
88
- var missing = (cause) => cause instanceof Error && ("code" in cause) && cause.code === "ENOENT";
89
- var digest = (value) => createHash("sha256").update(value).digest("hex");
90
- var generatedFileSchema = z2.object({
91
- path: z2.string().regex(/^[A-Za-z0-9_ .-]+(?:\/[A-Za-z0-9_ .-]+)*$/).refine((path) => !path.split("/").some((part) => part === "." || part === "..") && !["manifest.json", "receipt.json"].includes(path)),
92
- sha256: sha256Digest
93
- });
94
- var manifestSchema = z2.object({
95
- kind: z2.literal("tsplc-artifacts"),
96
- version: z2.literal(1),
97
- configFile: z2.string(),
98
- files: z2.array(z2.unknown())
99
- });
100
- var buildStatusSchema = z2.enum(["emitted", "vendor-compiled"]);
101
- var receiptSchema = z2.object({
102
- kind: z2.literal("tsplc-build"),
103
- version: z2.literal(1),
104
- configFile: z2.string(),
105
- status: buildStatusSchema
106
- });
107
- var engineeringMarkerSchema = z2.object({
108
- kind: z2.literal("tsplc-engineering"),
109
- version: z2.literal(1),
110
- configFile: z2.string()
111
- });
112
- function manifest(value, configFile) {
113
- const parsed = manifestSchema.safeParse(value);
114
- if (!parsed.success || parsed.data.configFile !== configFile) {
115
- throw new Error("Output directory has no matching tsPLC ownership manifest.");
116
- }
117
- const files = parsed.data.files.map((item) => {
118
- const file = generatedFileSchema.safeParse(item);
119
- if (!file.success) {
120
- throw new Error("Invalid generated file ownership entry.");
121
- }
122
- return file.data;
123
- });
124
- if (new Set(files.map((item) => item.path)).size !== files.length) {
125
- throw new Error("Duplicate generated file ownership entry.");
126
- }
127
- return { ...parsed.data, files };
128
- }
129
- async function directory(path) {
130
- try {
131
- const entry = await lstat(path);
132
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
133
- throw new Error(`Generated directory must be a real directory: ${path}`);
134
- }
135
- return true;
136
- } catch (cause) {
137
- if (missing(cause)) {
138
- return false;
139
- }
140
- throw cause;
141
- }
142
- }
143
- async function filesIn(path, prefix = "") {
144
- const result = [];
145
- for (const entry of await readdir(path, { withFileTypes: true })) {
146
- const name = prefix + entry.name;
147
- if (entry.isDirectory() && !entry.isSymbolicLink()) {
148
- result.push(...await filesIn(join(path, entry.name), `${name}/`));
149
- } else {
150
- result.push(name);
151
- }
152
- }
153
- return result;
154
- }
155
- async function engineeringDirectory(path, configFile) {
156
- const marker = join(path, "ownership.json");
157
- const exists = await directory(path);
158
- if (!exists || !(await readdir(path)).length) {
159
- await mkdir(path, { recursive: true });
160
- await writeFile(marker, `${JSON.stringify({ kind: "tsplc-engineering", version: 1, configFile }, null, 2)}
161
- `, { flag: "wx" });
162
- } else {
163
- const entry = await lstat(marker).catch((cause) => {
164
- if (missing(cause)) {
165
- throw new Error(`Engineering cache has no matching tsPLC ownership marker: ${path}`);
166
- }
167
- throw cause;
168
- });
169
- if (!entry.isFile() || entry.isSymbolicLink()) {
170
- throw new Error("Engineering ownership marker must be a real file.");
171
- }
172
- const value = engineeringMarkerSchema.safeParse(JSON.parse(await readFile(marker, "utf8")));
173
- if (!value.success || value.data.configFile !== configFile) {
174
- throw new Error(`Engineering cache has no matching tsPLC ownership marker: ${path}`);
175
- }
176
- }
177
- const inspect = async (directory) => {
178
- for (const entry of await readdir(directory, { withFileTypes: true })) {
179
- const child = join(directory, entry.name);
180
- if (entry.isSymbolicLink()) {
181
- throw new Error(`Engineering cache must not contain symlinks: ${child}`);
182
- }
183
- if (entry.isDirectory()) {
184
- await inspect(child);
185
- }
186
- }
187
- };
188
- await inspect(path);
189
- }
190
- async function owned(configFile, withdrawReceipt = false) {
191
- const root = join(dirname(configFile), generatedDirectory2);
192
- const output = join(dirname(configFile), dirname(emittedStDirectory));
193
- if (!await directory(root) || !await directory(output)) {
194
- return { root, output, unknown: [] };
195
- }
196
- const manifestPath = join(output, "manifest.json");
197
- const manifestEntry = await lstat(manifestPath);
198
- if (!manifestEntry.isFile() || manifestEntry.isSymbolicLink()) {
199
- throw new Error("Ownership manifest must be a real file.");
200
- }
201
- const ownership = manifest(JSON.parse(await readFile(manifestPath, "utf8")), configFile);
202
- const receipt = join(output, "receipt.json");
203
- try {
204
- const entry = await lstat(receipt);
205
- if (!entry.isFile() || entry.isSymbolicLink()) {
206
- throw new Error("Build receipt must be a real file.");
207
- }
208
- const value = receiptSchema.safeParse(JSON.parse(await readFile(receipt, "utf8")));
209
- if (!value.success || value.data.configFile !== configFile) {
210
- throw new Error("Build receipt is not owned by this configuration.");
211
- }
212
- if (withdrawReceipt) {
213
- await rm(receipt);
214
- }
215
- } catch (cause) {
216
- if (!missing(cause)) {
217
- throw cause;
218
- }
219
- }
220
- const names = new Set([
221
- ...ownership.files.map((file) => file.path),
222
- "manifest.json",
223
- "receipt.json"
224
- ]);
225
- const checked = new Set;
226
- for (const file of ownership.files) {
227
- let parent = output;
228
- for (const part of file.path.split("/").slice(0, -1)) {
229
- parent = join(parent, part);
230
- if (checked.has(parent)) {
231
- continue;
232
- }
233
- if (!await directory(parent)) {
234
- throw new Error(`Missing generated directory: ${parent}`);
235
- }
236
- checked.add(parent);
237
- }
238
- const path = join(output, file.path);
239
- const entry = await lstat(path);
240
- if (!entry.isFile() || entry.isSymbolicLink() || digest(await readFile(path)) !== file.sha256) {
241
- throw new Error(`Generated file was replaced or edited; preserve it before rebuilding/cleaning: ${path}`);
242
- }
243
- }
244
- return {
245
- root,
246
- output,
247
- ownership,
248
- unknown: (await filesIn(output)).filter((name) => !names.has(name))
249
- };
250
- }
251
- async function publishArtifacts(staging, previous, receipt) {
252
- const configFile = receipt.configFile;
253
- const generated = (await filesIn(staging)).sort();
254
- const ownership = {
255
- kind: "tsplc-artifacts",
256
- version: 1,
257
- configFile,
258
- files: []
259
- };
260
- for (const path of generated) {
261
- ownership.files.push({
262
- path,
263
- sha256: digest(await readFile(join(staging, path)))
264
- });
265
- }
266
- await writeFile(join(staging, "manifest.json"), `${JSON.stringify(ownership, null, 2)}
267
- `);
268
- await writeFile(join(staging, "receipt.json"), `${JSON.stringify({
269
- kind: "tsplc-build",
270
- version: 1,
271
- generatedAt: new Date().toISOString(),
272
- ...receipt
273
- }, null, 2)}
274
- `);
275
- const current = await owned(configFile);
276
- if (JSON.stringify(current.ownership) !== JSON.stringify(previous.ownership)) {
277
- throw new Error("Output ownership changed during the build; preserve it before rebuilding.");
278
- }
279
- if (current.unknown.length) {
280
- throw new Error(`Output contains unrelated files: ${current.unknown.join(", ")}`);
281
- }
282
- const backup = `${staging}-previous`;
283
- if (previous.ownership) {
284
- await rename(previous.output, backup);
285
- }
286
- try {
287
- await rename(staging, previous.output);
288
- } catch (cause) {
289
- if (previous.ownership) {
290
- await rename(backup, previous.output);
291
- }
292
- throw cause;
293
- }
294
- if (previous.ownership) {
295
- await rm(backup, { recursive: true });
296
- }
297
- }
298
- async function cleanArtifacts(configFile) {
299
- const generated = await owned(configFile);
300
- if (!generated.ownership) {
301
- return;
302
- }
303
- for (const entry of generated.ownership.files) {
304
- await rm(join(generated.output, entry.path));
305
- }
306
- await rm(join(generated.output, "receipt.json"), { force: true });
307
- await rm(join(generated.output, "manifest.json"));
308
- const removeEmpty = async (path) => {
309
- for (const entry of await readdir(path, { withFileTypes: true })) {
310
- if (entry.isDirectory() && !entry.isSymbolicLink()) {
311
- await removeEmpty(join(path, entry.name));
312
- }
313
- }
314
- if (!(await readdir(path)).length) {
315
- await rm(path, { recursive: true });
316
- }
317
- };
318
- await removeEmpty(generated.output);
319
- }
320
-
321
- // packages/compiler/src/project/project.ts
322
- var emittedSt = basename(emittedStDirectory2);
323
- var failure = (origin, code, cause) => ({
324
- origin,
325
- code,
326
- severity: "error",
327
- message: cause instanceof Error ? cause.message : String(cause)
328
- });
329
- var loadEnvironment = (environment) => ({
330
- env: environment ?? process.env,
331
- target: undefined,
332
- forAgent: false
333
- });
334
- async function loadProject(file, {
335
- loaded,
336
- buildTarget,
337
- configured,
338
- hostConfigDiagnostics,
339
- environment
340
- }) {
341
- let configFile = resolve(file);
342
- try {
343
- configFile = await realpath(configFile);
344
- const diagnostics = hostConfigDiagnostics ?? checkHostConfig(configFile);
345
- if (hasErrors(diagnostics)) {
346
- return { configFile, diagnostics };
347
- }
348
- const { config: shared, env } = loaded ?? await loadTsplcConfig(configFile, loadEnvironment(environment));
349
- const root = dirname2(configFile);
350
- const catalog = await loadHardware(shared, root);
351
- const config = compilerConfig(shared, resolveIo(shared, catalog), buildTarget);
352
- return {
353
- configFile,
354
- shared,
355
- config: { ...config, ...sourceInput(config, root) },
356
- project: {
357
- root,
358
- environment: env,
359
- ...shared.debug && { debug: shared.debug },
360
- ...shared.telemetry && { telemetry: shared.telemetry },
361
- ...shared.server && { server: shared.server },
362
- ...shared.hmi && { hmi: shared.hmi },
363
- hardware: {
364
- catalog: configured ? await loadHardware(configured, root) : catalog,
365
- config: configured ?? shared,
366
- tree: shared.codesys?.deviceTree !== undefined
367
- }
368
- },
369
- diagnostics
370
- };
371
- } catch (cause) {
372
- return { configFile, diagnostics: [failure("config", "PLC5003", cause)] };
373
- }
374
- }
375
- function sourceInput(compiler, root) {
376
- return {
377
- tsconfig: resolve(root, compiler.tsconfig),
378
- programs: Object.fromEntries(Object.entries(compiler.programs).map(([name, entry]) => [
379
- name,
380
- { ...entry, module: resolve(root, entry.module) }
381
- ]))
382
- };
383
- }
384
- async function projectTargets(file = "tsplc.config.ts", options = {}) {
385
- const configFile = await realpath(resolve(file));
386
- const { config, env } = options.loaded ?? await loadTsplcConfig(configFile, loadEnvironment(options.environment));
387
- if (!config.compiler) {
388
- return;
389
- }
390
- const source = analyze({
391
- ...sourceInput(config.compiler, dirname2(configFile)),
392
- services: config.server,
393
- environment: env
394
- });
395
- const core = source.module && normalizeModule(source.module).core;
396
- return core ? coreTargets(core) : undefined;
397
- }
398
- var coreTargets = (core) => ({
399
- io: ioTargets(core),
400
- publication: publicationSelectors(core)
401
- });
402
- async function checkProject(file = "tsplc.config.ts", options = {}) {
403
- const loaded = await loadProject(file, options);
404
- if (!loaded.config || !loaded.project) {
405
- return loaded;
406
- }
407
- const config = loaded.config;
408
- if (config.target.kind !== "codesys") {
409
- return {
410
- ...loaded,
411
- diagnostics: [
412
- failure("backend", "PLC7001", `Unsupported target: ${config.target.kind}`)
413
- ]
414
- };
415
- }
416
- const source = analyze({
417
- tsconfig: config.tsconfig,
418
- programs: config.programs,
419
- services: loaded.shared?.server,
420
- environment: loaded.project.environment
421
- });
422
- if (!source.module) {
423
- return { ...loaded, diagnostics: source.diagnostics };
424
- }
425
- const normalized = normalizeModule(source.module);
426
- if (!normalized.core) {
427
- return { ...loaded, diagnostics: normalized.diagnostics };
428
- }
429
- const tasks = config.tasks.map((task) => ({
430
- name: task.name,
431
- intervalNanoseconds: task.schedule.interval.nanoseconds,
432
- priority: task.priority,
433
- programs: task.programs
434
- }));
435
- const planned = planCodesys(normalized.core, { tasks }, config.target.options, loaded.project);
436
- return {
437
- ...loaded,
438
- ...planned,
439
- diagnostics: [
440
- ...loaded.diagnostics,
441
- ...source.diagnostics,
442
- ...normalized.diagnostics,
443
- ...planned.diagnostics
444
- ]
445
- };
446
- }
447
- async function engineeringInputs(shared, configFile, bundle, options) {
448
- const root = dirname2(configFile);
449
- const tree = await deviceTree(shared, await loadHardware(shared, root));
450
- return {
451
- libraries: await installLibraries(shared.dependencies, root, options),
452
- deviceTree: tree && {
453
- ...tree,
454
- mappings: bundle.ioMappings.map(({ address, variable }) => ({
455
- address,
456
- variable
457
- }))
458
- }
459
- };
460
- }
461
- async function buildProject(file = "tsplc.config.ts", options = {}) {
462
- let staging;
463
- try {
464
- const configFile = await realpath(resolve(file));
465
- const previous = await owned(configFile, true);
466
- if (previous.unknown.length) {
467
- throw new Error(`Output contains unrelated files: ${previous.unknown.join(", ")}`);
468
- }
469
- const checked = await checkProject(configFile, options);
470
- if (!checked.plan || !checked.config) {
471
- return { diagnostics: checked.diagnostics };
472
- }
473
- const bundle = emitCodesys(checked.plan);
474
- if (hasErrors(bundle.diagnostics)) {
475
- return { diagnostics: bundle.diagnostics };
476
- }
477
- const shared = checked.shared;
478
- const engineering = shared && (!options.emitOnly || options.forController) ? await engineeringInputs(shared, configFile, bundle, {
479
- frozen: options.frozenLockfile
480
- }) : undefined;
481
- const outDir = shared?.build && resolve(dirname2(configFile), shared.build.outDir);
482
- const controllerConfig = outDir ? codesysBuildConfig(checked.plan, bundle, {
483
- configFile,
484
- sourceDir: join2(previous.output, emittedSt),
485
- outDir,
486
- controller: options.controller,
487
- ...engineering
488
- }) : undefined;
489
- await mkdir2(previous.root, { recursive: true });
490
- staging = await mkdtemp(join2(previous.root, "stage-"));
491
- const artifacts = [
492
- ...bundle.artifacts,
493
- {
494
- path: "mappings.json",
495
- content: `${JSON.stringify(compactMappings(bundle.mappings))}
496
- `
497
- },
498
- {
499
- path: emittedFiles.ioSymbols,
500
- content: `${JSON.stringify(bundle.ioSymbols, null, 2)}
501
- `
502
- },
503
- {
504
- path: emittedFiles.publication,
505
- content: `${JSON.stringify(bundle.publication, null, 2)}
506
- `
507
- },
508
- {
509
- path: "config.json",
510
- content: `${JSON.stringify(checked.config, null, 2)}
511
- `
512
- },
513
- ...bundle.debug ? [
514
- {
515
- path: debugManifestFile,
516
- content: `${JSON.stringify(bundle.debug)}
517
- `
518
- }
519
- ] : [],
520
- ...bundle.explain === undefined ? [] : [{ path: explainFileName, content: bundle.explain }]
521
- ];
522
- const stagedSt = join2(staging, emittedSt);
523
- const directories = new Set(artifacts.map((artifact) => dirname2(join2(stagedSt, artifact.path))));
524
- for (const directory of directories) {
525
- await mkdir2(directory, { recursive: true });
526
- }
527
- for (const artifact of artifacts) {
528
- await writeFile2(join2(stagedSt, artifact.path), artifact.content);
529
- }
530
- let vendor;
531
- if (!options.emitOnly) {
532
- const engineeringDir = outDir ?? join2(previous.root, "engineering");
533
- await engineeringDirectory(engineeringDir, configFile);
534
- try {
535
- vendor = await buildCodesys(checked.plan, bundle, {
536
- configFile,
537
- sourceDir: join2(staging, emittedSt),
538
- outDir: engineeringDir,
539
- controller: options.controller,
540
- ...engineering
541
- }, { progress: options.progress });
542
- } catch (cause) {
543
- return { diagnostics: diagnoseCodesys(cause, bundle) };
544
- }
545
- const vendorArtifacts = [
546
- ["Application.project", vendor.project],
547
- ["Application.app", vendor.output],
548
- ["Application.crc", vendor.checksum]
549
- ];
550
- for (const [name, path] of vendorArtifacts) {
551
- if (!path) {
552
- continue;
553
- }
554
- const destination = join2(staging, "vendor", name);
555
- await mkdir2(dirname2(destination), { recursive: true });
556
- await copyFile(path, destination);
557
- }
558
- }
559
- const status = vendor ? "vendor-compiled" : "emitted";
560
- await publishArtifacts(staging, previous, {
561
- configFile,
562
- status,
563
- ...vendor && {
564
- vendor: { application: vendor.application, tasks: vendor.tasks }
565
- }
566
- });
567
- staging = undefined;
568
- return {
569
- directory: previous.output,
570
- status,
571
- ...controllerConfig && { controllerConfig },
572
- targets: coreTargets(checked.plan.core),
573
- services: serviceRoutes(checked.plan.core),
574
- diagnostics: [...checked.diagnostics, ...bundle.diagnostics]
575
- };
576
- } catch (cause) {
577
- return { diagnostics: [failure("config", "PLC5004", cause)] };
578
- } finally {
579
- if (staging) {
580
- await rm2(staging, { recursive: true, force: true });
581
- }
582
- }
583
- }
584
- function compactMappings(mappings) {
585
- const objects = new Map;
586
- const files = new Map;
587
- const index = (table, value) => {
588
- const known = table.get(value);
589
- if (known !== undefined) {
590
- return known;
591
- }
592
- table.set(value, table.size);
593
- return table.size - 1;
594
- };
595
- const rows = mappings.map(({ path, line, span }) => [
596
- index(objects, path),
597
- line,
598
- index(files, span.file),
599
- span.start,
600
- span.end,
601
- span.line,
602
- span.column
603
- ]);
604
- return {
605
- objects: [...objects.keys()],
606
- files: [...files.keys()],
607
- mappings: rows
608
- };
609
- }
610
- async function cleanProject(file = "tsplc.config.ts") {
611
- try {
612
- await cleanArtifacts(await realpath(resolve(file)));
613
- return { diagnostics: [] };
614
- } catch (cause) {
615
- return { diagnostics: [failure("config", "PLC5004", cause)] };
616
- }
617
- }
618
- export {
619
- buildProject,
620
- checkProject,
621
- cleanProject,
622
- projectTargets
623
- };
624
-
625
- //# debugId=245EC16F9FA9A58064756E2164756E21
626
- //# sourceMappingURL=project.js.map