@tsplc/compiler 0.2.21 → 0.4.0

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 (115) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +280 -223
  3. package/dist/cli.js.map +12 -12
  4. package/dist/codesys.js +452 -330
  5. package/dist/codesys.js.map +24 -24
  6. package/dist/elaborate-capture.js +112 -7
  7. package/dist/elaborate-capture.js.map +5 -4
  8. package/dist/index.js +5682 -5407
  9. package/dist/index.js.map +39 -37
  10. package/dist/ir.js +1154 -327
  11. package/dist/ir.js.map +23 -20
  12. package/dist/project.js +68 -29
  13. package/dist/project.js.map +4 -4
  14. package/dist/retain.js +8 -0
  15. package/dist/retain.js.map +10 -0
  16. package/dist/server-check.js +59 -0
  17. package/dist/server-check.js.map +11 -0
  18. package/dist/telemetry.js +19 -10
  19. package/dist/telemetry.js.map +3 -3
  20. package/package.json +33 -12
  21. package/src/codesys/config.ts +1 -1
  22. package/src/codesys/debug.ts +7 -21
  23. package/src/codesys/engineering.ts +8 -12
  24. package/src/codesys/global-lists.ts +19 -17
  25. package/src/codesys/global-names.ts +3 -3
  26. package/src/codesys/http.ts +37 -34
  27. package/src/codesys/index.ts +5 -22
  28. package/src/codesys/instrumentation.ts +3 -1
  29. package/src/codesys/io-paths.ts +2 -1
  30. package/src/codesys/manifest.ts +35 -18
  31. package/src/codesys/numeric.ts +2 -4
  32. package/src/codesys/plan.ts +94 -26
  33. package/src/codesys/publication.ts +4 -3
  34. package/src/codesys/routines.ts +2 -2
  35. package/src/codesys/sources.ts +1 -0
  36. package/src/codesys/st-files.ts +86 -50
  37. package/src/codesys/storage.ts +44 -9
  38. package/src/codesys/task-access.ts +32 -10
  39. package/src/codesys/telemetry.ts +93 -46
  40. package/src/codesys/types.ts +81 -3
  41. package/src/codesys/vendor.ts +1 -1
  42. package/src/frontend/analyzer.ts +15 -14
  43. package/src/frontend/async.ts +23 -21
  44. package/src/frontend/blocks.ts +7 -6
  45. package/src/frontend/configuration.ts +8 -6
  46. package/src/frontend/const-functions.ts +5 -5
  47. package/src/frontend/cooperative.ts +2 -1
  48. package/src/frontend/coroutines.ts +24 -21
  49. package/src/frontend/elaborate/calls.ts +12 -7
  50. package/src/frontend/elaborate/capture-protocol.ts +103 -0
  51. package/src/frontend/elaborate/capture.ts +48 -19
  52. package/src/frontend/elaborate/dialect.ts +2 -2
  53. package/src/frontend/elaborate/expressions.ts +9 -6
  54. package/src/frontend/elaborate/frame.ts +1 -1
  55. package/src/frontend/elaborate/graph.ts +27 -43
  56. package/src/frontend/elaborate/index.ts +52 -17
  57. package/src/frontend/elaborate/initializers.ts +3 -2
  58. package/src/frontend/elaborate/layouts.ts +2 -2
  59. package/src/frontend/elaborate/model.ts +34 -21
  60. package/src/frontend/elaborate/retention.ts +206 -0
  61. package/src/frontend/elaborate/specialize.ts +7 -6
  62. package/src/frontend/elaborate/statements.ts +47 -38
  63. package/src/frontend/elaborate/storage.ts +26 -6
  64. package/src/frontend/exposure.ts +6 -6
  65. package/src/frontend/expressions.ts +26 -25
  66. package/src/frontend/intrinsics.ts +3 -3
  67. package/src/frontend/module-graph.ts +1 -1
  68. package/src/frontend/positions.ts +2 -1
  69. package/src/frontend/promise-chains.ts +1 -0
  70. package/src/frontend/promises.ts +8 -7
  71. package/src/frontend/routines.ts +3 -2
  72. package/src/frontend/sdk.ts +2 -2
  73. package/src/frontend/services.ts +23 -20
  74. package/src/frontend/statements.ts +8 -6
  75. package/src/frontend/tasks.ts +11 -6
  76. package/src/frontend/telemetry.ts +1 -1
  77. package/src/frontend/types.ts +11 -9
  78. package/src/frontend/typescript.ts +92 -42
  79. package/src/index.ts +2 -1
  80. package/src/ir/aggregates.ts +4 -3
  81. package/src/ir/allocation.ts +2 -1
  82. package/src/ir/core-model.ts +38 -8
  83. package/src/ir/declarations.ts +12 -12
  84. package/src/ir/domains.ts +8 -8
  85. package/src/ir/fold.ts +2 -2
  86. package/src/ir/index.ts +53 -40
  87. package/src/ir/job-errors.ts +22 -11
  88. package/src/ir/layouts.ts +3 -2
  89. package/src/ir/link.ts +6 -24
  90. package/src/ir/model.ts +3 -2
  91. package/src/ir/normalization-error.ts +1 -1
  92. package/src/ir/normalize.ts +19 -12
  93. package/src/ir/numeric.ts +1 -0
  94. package/src/{codesys → ir}/operands.ts +2 -6
  95. package/src/ir/ranges.ts +2 -2
  96. package/src/ir/routine.ts +35 -20
  97. package/src/ir/scalars.ts +3 -3
  98. package/src/ir/share.ts +862 -0
  99. package/src/ir/temporaries.ts +236 -0
  100. package/src/ir/types.ts +1 -1
  101. package/src/project/artifacts.ts +12 -2
  102. package/src/project/cli.ts +42 -44
  103. package/src/project/debugger.ts +15 -9
  104. package/src/project/index.ts +1 -1
  105. package/src/project/plant.ts +34 -23
  106. package/src/project/project-commands.ts +92 -73
  107. package/src/project/project.ts +91 -42
  108. package/src/project/release.ts +50 -57
  109. package/src/project/server-check.ts +54 -0
  110. package/src/project/server.ts +60 -41
  111. package/src/project/terminal.ts +3 -1
  112. package/stdlib/retain.ts +2 -0
  113. package/types/globals.d.ts +1 -0
  114. package/types/retain.d.ts +6 -0
  115. package/types/telemetry.d.ts +2 -2
@@ -1,27 +1,27 @@
1
1
  import { resolve } from "node:path";
2
- import {
3
- buildTargetFor,
4
- controllerFor,
5
- type Environment,
6
- loadTsplcConfig,
7
- rawBuildConfig,
8
- } from "@tsplc/config";
9
2
  import {
10
3
  buildProject as buildSt,
4
+ checkRawSources,
11
5
  configureDocker,
12
6
  configureRuntime,
13
- inspectController,
7
+ inspectProjectController,
14
8
  installLibraries,
15
- loadSources,
9
+ type ProgressStream,
16
10
  rawEngineeringSettings,
17
- resolveConfig,
18
11
  runProject,
19
12
  updateController,
20
13
  } from "@tsplc/build";
21
- import { checkHostConfig } from "@tsplc/compiler";
14
+ import { type CompileEnvironment, checkHostConfig } from "@tsplc/compiler";
22
15
  import { type Diagnostic, hasErrors } from "@tsplc/compiler/ir";
16
+ import {
17
+ buildTargetFor,
18
+ controllerFor,
19
+ type LoadedConfig,
20
+ loadTsplcConfig,
21
+ rawBuildConfig,
22
+ } from "@tsplc/config";
23
+ import { resolveConfig } from "@tsplc/config/build";
23
24
  import { generateProject } from "@tsplc/typegen";
24
- import { loadHardware, resolveIo } from "@tsplc/typegen/hardware";
25
25
  import {
26
26
  buildConfigs,
27
27
  buildProject,
@@ -29,7 +29,7 @@ import {
29
29
  cleanProject,
30
30
  projectTargets,
31
31
  } from "./project.ts";
32
- import { buildServer } from "./server.ts";
32
+ import { buildServer, checkServer } from "./server.ts";
33
33
 
34
34
  const projectCommands = [
35
35
  "install",
@@ -51,7 +51,7 @@ export const projectCommand = (name: string) =>
51
51
 
52
52
  export interface ProjectFlags {
53
53
  "emit-only"?: boolean;
54
- format?: string;
54
+ format?: "json" | "text";
55
55
  read?: string[];
56
56
  "initialize-user"?: boolean;
57
57
  "frozen-lockfile"?: boolean;
@@ -73,16 +73,11 @@ export class DiagnosticFailure extends Error {
73
73
  }
74
74
  }
75
75
 
76
- type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
77
-
78
76
  /** Rejects the flags that don't apply to `command`. */
79
77
  export const checkProjectFlags = (
80
78
  command: ProjectCommand,
81
79
  flags: ProjectFlags,
82
80
  ) => {
83
- if (flags.format && !["json", "text"].includes(flags.format)) {
84
- throw new Error("--format must be json or text.");
85
- }
86
81
  if (flags["emit-only"] && command !== "build") {
87
82
  throw new Error("--emit-only is supported by build only.");
88
83
  }
@@ -112,31 +107,50 @@ export const checkProjectFlags = (
112
107
  }
113
108
  };
114
109
 
110
+ /** What a command runs with besides its flags: where progress goes and the environment it compiles with. */
111
+ interface CommandContext {
112
+ progress: ProgressStream;
113
+ environment: CompileEnvironment;
114
+ }
115
+
115
116
  export const runProjectCommand = async (
116
117
  command: ProjectCommand,
117
118
  file: string,
118
119
  flags: ProjectFlags,
119
- env: Environment,
120
+ environment: CompileEnvironment,
120
121
  ): Promise<CommandResult> => {
122
+ const context: CommandContext = {
123
+ // A JSON result owns standard output, so progress goes to standard error.
124
+ progress: flags.format === "json" ? "stderr" : "stdout",
125
+ environment,
126
+ };
127
+ const { progress } = context;
128
+ // The config, and the environment it compiles with, are the target's.
121
129
  const load = () =>
122
- loadTsplcConfig(file, { env, target: flags.target, forAgent: false });
130
+ loadTsplcConfig(file, {
131
+ env: environment,
132
+ target: flags.target,
133
+ forAgent: false,
134
+ });
123
135
  switch (command) {
124
136
  case "configure":
125
- return configure(flags);
137
+ return configure(flags, progress);
126
138
  case "clean":
127
139
  return cleanProject(file);
128
140
  case "install":
129
141
  return install(await load(), flags);
130
142
  case "typegen":
131
- return typegen(file, await load(), flags);
143
+ return typegen(file, await load(), flags, environment);
132
144
  case "check":
133
- return check(file, await loadCheckedConfig(file, load));
145
+ return check(file, await loadCheckedConfig(file, load), environment);
134
146
  case "build":
135
- return build(file, await loadCheckedConfig(file, load), flags);
147
+ return build(file, await loadCheckedConfig(file, load), flags, context);
136
148
  case "run":
137
- return run(file, await loadCheckedConfig(file, load), flags);
149
+ return run(file, await loadCheckedConfig(file, load), flags, context);
138
150
  case "inspect":
139
- return inspect(await load(), flags);
151
+ return inspectProjectController(await load(), flags.read, {
152
+ progress,
153
+ });
140
154
  case "download":
141
155
  case "online-change":
142
156
  return updateProgram(
@@ -144,6 +158,7 @@ export const runProjectCommand = async (
144
158
  file,
145
159
  await loadCheckedConfig(file, load),
146
160
  flags,
161
+ context,
147
162
  );
148
163
  }
149
164
  };
@@ -167,8 +182,13 @@ const loadCheckedConfig = async (
167
182
  };
168
183
 
169
184
  /** Builds the Docker image every build uses, or with `--runtime` the local PLC runtime's. */
170
- const configure = async (flags: Pick<ProjectFlags, "runtime">) => {
171
- await (flags.runtime ? configureRuntime() : configureDocker());
185
+ const configure = async (
186
+ flags: Pick<ProjectFlags, "runtime">,
187
+ progress: ProgressStream,
188
+ ) => {
189
+ await (flags.runtime
190
+ ? configureRuntime({ progress })
191
+ : configureDocker({ progress }));
172
192
  return { ok: true };
173
193
  };
174
194
 
@@ -185,11 +205,16 @@ const typegen = async (
185
205
  file: string,
186
206
  loaded: LoadedConfig,
187
207
  flags: Pick<ProjectFlags, "check">,
208
+ environment: CompileEnvironment,
188
209
  ) => {
189
- const targets = await projectTargets(file, { loaded });
210
+ const targets = await projectTargets(file, { loaded, environment });
190
211
  if (loaded.config.compiler && !flags.check) {
191
212
  await generateProject(file, { hardwareOnly: true, loaded, targets });
192
- const emitted = await buildProject(file, { emitOnly: true, loaded });
213
+ const emitted = await buildProject(file, {
214
+ emitOnly: true,
215
+ loaded,
216
+ environment,
217
+ });
193
218
  if (hasErrors(emitted.diagnostics)) {
194
219
  throw new DiagnosticFailure(emitted);
195
220
  }
@@ -200,21 +225,12 @@ const typegen = async (
200
225
  const check = async (
201
226
  file: string,
202
227
  { loaded, hostConfigDiagnostics }: CheckedConfig,
228
+ environment: CompileEnvironment,
203
229
  ) => {
204
- const { config, configFile, root } = loaded;
205
- if (config.compiler) {
206
- return checkProject(file, { loaded, hostConfigDiagnostics });
230
+ if (loaded.config.compiler) {
231
+ return checkProject(file, { loaded, hostConfigDiagnostics, environment });
207
232
  }
208
- const sources = await loadSources(
209
- resolveConfig(
210
- {
211
- ...rawBuildConfig(config),
212
- io: resolveIo(config, await loadHardware(config, root)),
213
- },
214
- configFile,
215
- ),
216
- );
217
- return { objects: sources.objects.length };
233
+ return { objects: (await checkRawSources(loaded)).objects.length };
218
234
  };
219
235
 
220
236
  /**
@@ -226,10 +242,14 @@ const buildPlc = async (
226
242
  file: string,
227
243
  { loaded, hostConfigDiagnostics }: CheckedConfig,
228
244
  flags: Pick<ProjectFlags, "emit-only" | "frozen-lockfile" | "simulate">,
245
+ { progress, environment }: CommandContext,
229
246
  ) => {
230
247
  const { config } = loaded;
231
248
  if (config.compiler) {
232
- const built = await buildProject(file, {
249
+ // The build's analysis already read the targets; `--format json` reports the rest.
250
+ const { targets, ...built } = await buildProject(file, {
251
+ progress,
252
+ environment,
233
253
  emitOnly: flags["emit-only"],
234
254
  frozenLockfile: flags["frozen-lockfile"],
235
255
  ...buildConfigs(loaded, flags),
@@ -240,10 +260,7 @@ const buildPlc = async (
240
260
  if (hasErrors(built.diagnostics)) {
241
261
  throw new DiagnosticFailure(built);
242
262
  }
243
- await generateProject(file, {
244
- loaded,
245
- targets: await projectTargets(file, { loaded }),
246
- });
263
+ await generateProject(file, { loaded, targets });
247
264
  return built;
248
265
  }
249
266
  if (flags["emit-only"]) {
@@ -255,6 +272,7 @@ const buildPlc = async (
255
272
  frozenLockfile: flags["frozen-lockfile"],
256
273
  simulate: flags.simulate,
257
274
  }),
275
+ { progress },
258
276
  );
259
277
  await generateProject(file, { hardwareOnly: !config.typegen, loaded });
260
278
  return built;
@@ -268,25 +286,33 @@ const build = async (
268
286
  ProjectFlags,
269
287
  "emit-only" | "frozen-lockfile" | "plc" | "server" | "simulate"
270
288
  >,
289
+ context: CommandContext,
271
290
  ) => {
272
291
  const { config, root } = checked.loaded;
273
292
  if (flags.server) {
274
- return buildServer(config, root);
293
+ return buildServer(await checkServer(config, root), config, root);
275
294
  }
276
- const plc = await buildPlc("build", file, checked, flags);
277
- if (flags.plc || flags["emit-only"] || !config.server?.runtime) {
295
+ const withServer =
296
+ !flags.plc && !flags["emit-only"] && config.server?.runtime;
297
+ // The server's type check runs on a worker thread while the PLC program builds.
298
+ const serverCheck = withServer ? checkServer(config, root) : undefined;
299
+ // A failed PLC build reports its own diagnostics; the check's outcome is read only after a success.
300
+ serverCheck?.catch(() => undefined);
301
+ const plc = await buildPlc("build", file, checked, flags, context);
302
+ if (!serverCheck) {
278
303
  return plc;
279
304
  }
280
- return { ...plc, ...(await buildServer(config, root)) };
305
+ return { ...plc, ...(await buildServer(await serverCheck, config, root)) };
281
306
  };
282
307
 
283
308
  const run = async (
284
309
  file: string,
285
310
  checked: CheckedConfig,
286
311
  flags: Pick<ProjectFlags, "frozen-lockfile" | "simulate">,
312
+ context: CommandContext,
287
313
  ) => {
288
314
  const { config, configFile } = checked.loaded;
289
- const built = await buildPlc("run", file, checked, flags);
315
+ const built = await buildPlc("run", file, checked, flags, context);
290
316
  if (!config.build) {
291
317
  throw new Error("Configure build to run this project.");
292
318
  }
@@ -296,28 +322,11 @@ const run = async (
296
322
  configFile,
297
323
  ),
298
324
  config.name,
325
+ { progress: context.progress },
299
326
  );
300
327
  return built;
301
328
  };
302
329
 
303
- const inspect = async (
304
- { config, configFile }: LoadedConfig,
305
- flags: Pick<ProjectFlags, "read">,
306
- ) => {
307
- if (!config.build) {
308
- throw new Error("Configure build settings and controller connection.");
309
- }
310
- // Inspection needs only the connection/baseline. It must remain usable
311
- // when local sources, descriptors or dependency caches are unavailable.
312
- return inspectController(
313
- resolveConfig(
314
- { ...config.build, controller: controllerFor(config, "inspect") },
315
- configFile,
316
- ),
317
- flags.read,
318
- );
319
- };
320
-
321
330
  /** Downloads or online-changes the program on the configured controller. */
322
331
  const updateProgram = async (
323
332
  operation: "download" | "online-change",
@@ -327,13 +336,20 @@ const updateProgram = async (
327
336
  ProjectFlags,
328
337
  "frozen-lockfile" | "simulate" | "initialize-user" | "read"
329
338
  >,
339
+ { progress, environment }: CommandContext,
330
340
  ) => {
331
341
  const { config } = checked.loaded;
332
342
  if (!config.build) {
333
343
  throw new Error("Configure build settings and controller connection.");
334
344
  }
335
345
  const settings = config.compiler
336
- ? await emittedControllerSettings(operation, file, checked, flags)
346
+ ? await emittedControllerSettings(
347
+ operation,
348
+ file,
349
+ checked,
350
+ flags,
351
+ environment,
352
+ )
337
353
  : await rawEngineeringSettings(checked.loaded, {
338
354
  command: operation,
339
355
  frozenLockfile: flags["frozen-lockfile"],
@@ -342,6 +358,7 @@ const updateProgram = async (
342
358
  return updateController(settings, operation, {
343
359
  initializeUser: flags["initialize-user"],
344
360
  read: flags.read,
361
+ progress,
345
362
  });
346
363
  };
347
364
 
@@ -350,10 +367,12 @@ const emittedControllerSettings = async (
350
367
  file: string,
351
368
  { loaded, hostConfigDiagnostics }: CheckedConfig,
352
369
  flags: Pick<ProjectFlags, "frozen-lockfile" | "simulate">,
370
+ environment: CompileEnvironment,
353
371
  ) => {
354
372
  const emitted = await buildProject(file, {
355
373
  emitOnly: true,
356
374
  forController: true,
375
+ environment,
357
376
  frozenLockfile: flags["frozen-lockfile"],
358
377
  ...buildConfigs(loaded, flags),
359
378
  hostConfigDiagnostics,
@@ -6,41 +6,49 @@ import {
6
6
  rm,
7
7
  writeFile,
8
8
  } from "node:fs/promises";
9
- import { dirname, join, resolve } from "node:path";
9
+ import { basename, dirname, join, resolve } from "node:path";
10
+ import { installLibraries, type ProgressOptions } from "@tsplc/build";
11
+ import {
12
+ analyze,
13
+ type CompileEnvironment,
14
+ checkHostConfig,
15
+ } from "@tsplc/compiler";
10
16
  import {
11
17
  buildCodesys,
12
- codesysBuildConfig,
13
18
  type CodesysPlan,
14
19
  type CodesysProject,
15
- type SourceMapping,
20
+ codesysBuildConfig,
16
21
  diagnoseCodesys,
17
22
  emitCodesys,
18
23
  ioTargets,
19
- publicationSelectors,
20
24
  planCodesys,
25
+ publicationSelectors,
26
+ type SourceMapping,
21
27
  } from "@tsplc/compiler/codesys";
22
- import { analyze, checkHostConfig } from "@tsplc/compiler";
23
28
  import {
24
- type Controller,
29
+ type Diagnostic,
30
+ hasErrors,
31
+ normalizeModule,
32
+ type VerifiedCoreModule,
33
+ } from "@tsplc/compiler/ir";
34
+ import {
25
35
  type BuildTarget,
26
- type NormalizedConfig,
27
- type ResolvedTsplcConfig,
36
+ type Controller,
28
37
  compilerConfig,
38
+ type LoadedConfig,
29
39
  loadTsplcConfig,
40
+ type NormalizedConfig,
41
+ type ResolvedTsplcConfig,
30
42
  simulationConfig,
31
43
  } from "@tsplc/config";
44
+ import { emittedFiles, emittedStDirectory } from "@tsplc/config/project-files";
45
+ import { debugManifestFile } from "@tsplc/debug/manifest";
32
46
  import {
33
47
  deviceTree,
34
- type ProjectTargets,
35
48
  loadHardware,
49
+ type ProjectTargets,
36
50
  resolveIo,
37
51
  } from "@tsplc/typegen";
38
- import { installLibraries } from "@tsplc/build";
39
- import {
40
- type Diagnostic,
41
- hasErrors,
42
- normalizeModule,
43
- } from "@tsplc/compiler/ir";
44
52
  import {
45
53
  cleanArtifacts,
46
54
  engineeringDirectory,
@@ -48,6 +56,9 @@ import {
48
56
  publishArtifacts,
49
57
  } from "./artifacts.ts";
50
58
 
59
+ /** The emitted ST's directory inside the compiler's artifacts directory. */
60
+ const emittedSt = basename(emittedStDirectory);
61
+
51
62
  const failure = (
52
63
  origin: Diagnostic["origin"],
53
64
  code: string,
@@ -59,8 +70,6 @@ const failure = (
59
70
  message: cause instanceof Error ? cause.message : String(cause),
60
71
  });
61
72
 
62
- type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
63
-
64
73
  /**
65
74
  * `buildProject`'s `loaded` and `configured` options. With `simulate` the build omits fieldbus
66
75
  * I/O so a plant simulator can write the inputs; generated types and the debug manifest keep
@@ -87,11 +96,26 @@ interface ProjectOptions {
87
96
  * check type-checks the config and takes seconds.
88
97
  */
89
98
  hostConfigDiagnostics?: readonly Diagnostic[];
99
+ /** What the program's `env()` calls, endpoints and telemetry read; this process's environment by default. */
100
+ environment?: CompileEnvironment;
90
101
  }
91
102
 
103
+ /** Loads the config in `environment`, this process's by default, with its only target's values. */
104
+ const loadEnvironment = (environment: CompileEnvironment | undefined) => ({
105
+ env: environment ?? process.env,
106
+ target: undefined,
107
+ forAgent: false,
108
+ });
109
+
92
110
  async function loadProject(
93
111
  file: string,
94
- { loaded, buildTarget, configured, hostConfigDiagnostics }: ProjectOptions,
112
+ {
113
+ loaded,
114
+ buildTarget,
115
+ configured,
116
+ hostConfigDiagnostics,
117
+ environment,
118
+ }: ProjectOptions,
95
119
  ): Promise<{
96
120
  config?: NormalizedConfig;
97
121
  configFile: string;
@@ -107,7 +131,8 @@ async function loadProject(
107
131
  return { configFile, diagnostics };
108
132
  }
109
133
  const { config: shared, env } =
110
- loaded ?? (await loadTsplcConfig(configFile));
134
+ loaded ??
135
+ (await loadTsplcConfig(configFile, loadEnvironment(environment)));
111
136
  const root = dirname(configFile);
112
137
  const catalog = await loadHardware(shared, root);
113
138
 
@@ -122,7 +147,7 @@ async function loadProject(
122
147
  config: { ...config, ...sourceInput(config, root) },
123
148
  project: {
124
149
  root,
125
- env,
150
+ environment: env,
126
151
  ...(shared.debug && { debug: shared.debug }),
127
152
  ...(shared.telemetry && { telemetry: shared.telemetry }),
128
153
  ...(shared.server && { server: shared.server }),
@@ -166,24 +191,29 @@ function sourceInput(
166
191
  */
167
192
  export async function projectTargets(
168
193
  file = "tsplc.config.ts",
169
- options: { loaded?: LoadedConfig } = {},
194
+ options: Pick<ProjectOptions, "loaded" | "environment"> = {},
170
195
  ): Promise<ProjectTargets | undefined> {
171
196
  const configFile = await realpath(resolve(file));
172
- const { config, env } = options.loaded ?? (await loadTsplcConfig(configFile));
197
+ const { config, env } =
198
+ options.loaded ??
199
+ (await loadTsplcConfig(configFile, loadEnvironment(options.environment)));
173
200
  if (!config.compiler) {
174
201
  return undefined;
175
202
  }
176
203
  const source = analyze({
177
204
  ...sourceInput(config.compiler, dirname(configFile)),
178
205
  services: config.server,
179
- env,
206
+ environment: env,
180
207
  });
181
208
  const core = source.module && normalizeModule(source.module).core;
182
- return core
183
- ? { io: ioTargets(core), publication: publicationSelectors(core) }
184
- : undefined;
209
+ return core ? coreTargets(core) : undefined;
185
210
  }
186
211
 
212
+ const coreTargets = (core: VerifiedCoreModule): ProjectTargets => ({
213
+ io: ioTargets(core),
214
+ publication: publicationSelectors(core),
215
+ });
216
+
187
217
  export async function checkProject(
188
218
  file = "tsplc.config.ts",
189
219
  options: ProjectOptions = {},
@@ -217,7 +247,7 @@ export async function checkProject(
217
247
  tsconfig: config.tsconfig,
218
248
  programs: config.programs,
219
249
  services: loaded.shared?.server,
220
- env: loaded.project.env,
250
+ environment: loaded.project.environment,
221
251
  });
222
252
  if (!source.module) {
223
253
  return { ...loaded, diagnostics: source.diagnostics };
@@ -281,11 +311,13 @@ export async function buildProject(
281
311
  controller?: Controller;
282
312
  /** Resolve libraries and the device tree into `controllerConfig` even when only emitting. */
283
313
  forController?: boolean;
284
- } = {},
314
+ } & ProgressOptions = {},
285
315
  ): Promise<{
286
316
  directory?: string;
287
317
  status?: "emitted" | "vendor-compiled";
288
318
  controllerConfig?: ReturnType<typeof codesysBuildConfig>;
319
+ /** What `projectTargets` reads from the program, from this build's own analysis. */
320
+ targets?: ProjectTargets;
289
321
  diagnostics: readonly Diagnostic[];
290
322
  }> {
291
323
  let staging: string | undefined;
@@ -320,7 +352,7 @@ export async function buildProject(
320
352
  const controllerConfig = outDir
321
353
  ? codesysBuildConfig(checked.plan, bundle, {
322
354
  configFile,
323
- sourceDir: join(previous.output, "st"),
355
+ sourceDir: join(previous.output, emittedSt),
324
356
  outDir,
325
357
  controller: options.controller,
326
358
  ...engineering,
@@ -336,11 +368,11 @@ export async function buildProject(
336
368
  content: `${JSON.stringify(compactMappings(bundle.mappings))}\n`,
337
369
  },
338
370
  {
339
- path: "io-symbols.json",
371
+ path: emittedFiles.ioSymbols,
340
372
  content: `${JSON.stringify(bundle.ioSymbols, null, 2)}\n`,
341
373
  },
342
374
  {
343
- path: "publication.json",
375
+ path: emittedFiles.publication,
344
376
  content: `${JSON.stringify(bundle.publication, null, 2)}\n`,
345
377
  },
346
378
  {
@@ -348,13 +380,24 @@ export async function buildProject(
348
380
  content: `${JSON.stringify(checked.config, null, 2)}\n`,
349
381
  },
350
382
  ...(bundle.debug
351
- ? [{ path: "debug.json", content: `${JSON.stringify(bundle.debug)}\n` }]
383
+ ? [
384
+ {
385
+ path: debugManifestFile,
386
+ content: `${JSON.stringify(bundle.debug)}\n`,
387
+ },
388
+ ]
352
389
  : []),
353
390
  ];
391
+ const stagedSt = join(staging, emittedSt);
392
+ // Thousands of artifacts share a few hundred directories; each is made once.
393
+ const directories = new Set(
394
+ artifacts.map((artifact) => dirname(join(stagedSt, artifact.path))),
395
+ );
396
+ for (const directory of directories) {
397
+ await mkdir(directory, { recursive: true });
398
+ }
354
399
  for (const artifact of artifacts) {
355
- const path = join(staging, "st", artifact.path);
356
- await mkdir(dirname(path), { recursive: true });
357
- await writeFile(path, artifact.content);
400
+ await writeFile(join(stagedSt, artifact.path), artifact.content);
358
401
  }
359
402
 
360
403
  let vendor: Awaited<ReturnType<typeof buildCodesys>> | undefined;
@@ -363,13 +406,18 @@ export async function buildProject(
363
406
  const engineeringDir = outDir ?? join(previous.root, "engineering");
364
407
  await engineeringDirectory(engineeringDir, configFile);
365
408
  try {
366
- vendor = await buildCodesys(checked.plan, bundle, {
367
- configFile,
368
- sourceDir: join(staging, "st"),
369
- outDir: engineeringDir,
370
- controller: options.controller,
371
- ...engineering,
372
- });
409
+ vendor = await buildCodesys(
410
+ checked.plan,
411
+ bundle,
412
+ {
413
+ configFile,
414
+ sourceDir: join(staging, emittedSt),
415
+ outDir: engineeringDir,
416
+ controller: options.controller,
417
+ ...engineering,
418
+ },
419
+ { progress: options.progress },
420
+ );
373
421
  } catch (cause) {
374
422
  return { diagnostics: diagnoseCodesys(cause, bundle) };
375
423
  }
@@ -403,6 +451,7 @@ export async function buildProject(
403
451
  directory: previous.output,
404
452
  status,
405
453
  ...(controllerConfig && { controllerConfig }),
454
+ targets: coreTargets(checked.plan.core),
406
455
  diagnostics: [...checked.diagnostics, ...bundle.diagnostics],
407
456
  };
408
457
  } catch (cause) {