@tsplc/compiler 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.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyFile,
|
|
3
|
+
mkdir,
|
|
4
|
+
mkdtemp,
|
|
5
|
+
realpath,
|
|
6
|
+
rm,
|
|
7
|
+
writeFile,
|
|
8
|
+
} from "node:fs/promises";
|
|
9
|
+
import { dirname, join, resolve } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
buildCodesys,
|
|
12
|
+
codesysBuildConfig,
|
|
13
|
+
type CodesysPlan,
|
|
14
|
+
type CodesysProject,
|
|
15
|
+
type SourceMapping,
|
|
16
|
+
diagnoseCodesys,
|
|
17
|
+
emitCodesys,
|
|
18
|
+
ioTargets,
|
|
19
|
+
publicationSelectors,
|
|
20
|
+
planCodesys,
|
|
21
|
+
} from "@tsplc/compiler/codesys";
|
|
22
|
+
import { analyze, checkHostConfig } from "@tsplc/compiler";
|
|
23
|
+
import {
|
|
24
|
+
type Controller,
|
|
25
|
+
type BuildTarget,
|
|
26
|
+
type NormalizedConfig,
|
|
27
|
+
type ResolvedTsplcConfig,
|
|
28
|
+
compilerConfig,
|
|
29
|
+
loadTsplcConfig,
|
|
30
|
+
simulationConfig,
|
|
31
|
+
} from "@tsplc/config";
|
|
32
|
+
import {
|
|
33
|
+
deviceTree,
|
|
34
|
+
type ProjectTargets,
|
|
35
|
+
loadHardware,
|
|
36
|
+
resolveIo,
|
|
37
|
+
} from "@tsplc/typegen";
|
|
38
|
+
import { installLibraries } from "@tsplc/build";
|
|
39
|
+
import {
|
|
40
|
+
type Diagnostic,
|
|
41
|
+
hasErrors,
|
|
42
|
+
normalizeModule,
|
|
43
|
+
} from "@tsplc/compiler/ir";
|
|
44
|
+
import {
|
|
45
|
+
cleanArtifacts,
|
|
46
|
+
engineeringDirectory,
|
|
47
|
+
owned,
|
|
48
|
+
publishArtifacts,
|
|
49
|
+
} from "./artifacts.ts";
|
|
50
|
+
|
|
51
|
+
const failure = (
|
|
52
|
+
origin: Diagnostic["origin"],
|
|
53
|
+
code: string,
|
|
54
|
+
cause: unknown,
|
|
55
|
+
): Diagnostic => ({
|
|
56
|
+
origin,
|
|
57
|
+
code,
|
|
58
|
+
severity: "error",
|
|
59
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* `buildProject`'s `loaded` and `configured` options. With `simulate` the build omits fieldbus
|
|
66
|
+
* I/O so a plant simulator can write the inputs; generated types and the debug manifest keep
|
|
67
|
+
* describing the hardware project.
|
|
68
|
+
*/
|
|
69
|
+
export const buildConfigs = (
|
|
70
|
+
loaded: LoadedConfig,
|
|
71
|
+
options: { simulate?: boolean },
|
|
72
|
+
) =>
|
|
73
|
+
options.simulate
|
|
74
|
+
? {
|
|
75
|
+
loaded: { ...loaded, config: simulationConfig(loaded.config) },
|
|
76
|
+
configured: loaded.config,
|
|
77
|
+
}
|
|
78
|
+
: { loaded };
|
|
79
|
+
|
|
80
|
+
interface ProjectOptions {
|
|
81
|
+
loaded?: LoadedConfig;
|
|
82
|
+
buildTarget?: BuildTarget;
|
|
83
|
+
/** The configuration `loaded` simulates, whose hardware the debug manifest describes. */
|
|
84
|
+
configured?: ResolvedTsplcConfig;
|
|
85
|
+
/**
|
|
86
|
+
* The host config check's diagnostics, when the caller ran it before loading the config. The
|
|
87
|
+
* check type-checks the config and takes seconds.
|
|
88
|
+
*/
|
|
89
|
+
hostConfigDiagnostics?: readonly Diagnostic[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function loadProject(
|
|
93
|
+
file: string,
|
|
94
|
+
{ loaded, buildTarget, configured, hostConfigDiagnostics }: ProjectOptions,
|
|
95
|
+
): Promise<{
|
|
96
|
+
config?: NormalizedConfig;
|
|
97
|
+
configFile: string;
|
|
98
|
+
shared?: ResolvedTsplcConfig;
|
|
99
|
+
project?: CodesysProject;
|
|
100
|
+
diagnostics: readonly Diagnostic[];
|
|
101
|
+
}> {
|
|
102
|
+
let configFile = resolve(file);
|
|
103
|
+
try {
|
|
104
|
+
configFile = await realpath(configFile);
|
|
105
|
+
const diagnostics = hostConfigDiagnostics ?? checkHostConfig(configFile);
|
|
106
|
+
if (hasErrors(diagnostics)) {
|
|
107
|
+
return { configFile, diagnostics };
|
|
108
|
+
}
|
|
109
|
+
const { config: shared, env } =
|
|
110
|
+
loaded ?? (await loadTsplcConfig(configFile));
|
|
111
|
+
const root = dirname(configFile);
|
|
112
|
+
const catalog = await loadHardware(shared, root);
|
|
113
|
+
|
|
114
|
+
const config = compilerConfig(
|
|
115
|
+
shared,
|
|
116
|
+
resolveIo(shared, catalog),
|
|
117
|
+
buildTarget,
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
configFile,
|
|
121
|
+
shared,
|
|
122
|
+
config: { ...config, ...sourceInput(config, root) },
|
|
123
|
+
project: {
|
|
124
|
+
root,
|
|
125
|
+
env,
|
|
126
|
+
...(shared.debug && { debug: shared.debug }),
|
|
127
|
+
...(shared.telemetry && { telemetry: shared.telemetry }),
|
|
128
|
+
...(shared.server && { server: shared.server }),
|
|
129
|
+
// A simulation build drops fieldbus IO, but the debugger still shows the hardware it stands in for.
|
|
130
|
+
hardware: {
|
|
131
|
+
catalog: configured ? await loadHardware(configured, root) : catalog,
|
|
132
|
+
config: configured ?? shared,
|
|
133
|
+
tree: shared.codesys?.deviceTree !== undefined,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
diagnostics,
|
|
137
|
+
};
|
|
138
|
+
} catch (cause) {
|
|
139
|
+
return { configFile, diagnostics: [failure("config", "PLC5003", cause)] };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The analyzer's entry points, resolved against the config file's directory. */
|
|
144
|
+
function sourceInput(
|
|
145
|
+
compiler: {
|
|
146
|
+
tsconfig: string;
|
|
147
|
+
programs: Record<string, { module: string; export: string }>;
|
|
148
|
+
},
|
|
149
|
+
root: string,
|
|
150
|
+
) {
|
|
151
|
+
return {
|
|
152
|
+
tsconfig: resolve(root, compiler.tsconfig),
|
|
153
|
+
programs: Object.fromEntries(
|
|
154
|
+
Object.entries(compiler.programs).map(([name, entry]) => [
|
|
155
|
+
name,
|
|
156
|
+
{ ...entry, module: resolve(root, entry.module) },
|
|
157
|
+
]),
|
|
158
|
+
),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* I/O keys and publication selectors from analyzed PLC source. It ignores the
|
|
164
|
+
* config's own type errors, so a typo cannot switch off the types that flag it;
|
|
165
|
+
* it is undefined only while the source itself fails analysis.
|
|
166
|
+
*/
|
|
167
|
+
export async function projectTargets(
|
|
168
|
+
file = "tsplc.config.ts",
|
|
169
|
+
options: { loaded?: LoadedConfig } = {},
|
|
170
|
+
): Promise<ProjectTargets | undefined> {
|
|
171
|
+
const configFile = await realpath(resolve(file));
|
|
172
|
+
const { config, env } = options.loaded ?? (await loadTsplcConfig(configFile));
|
|
173
|
+
if (!config.compiler) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
const source = analyze({
|
|
177
|
+
...sourceInput(config.compiler, dirname(configFile)),
|
|
178
|
+
services: config.server,
|
|
179
|
+
env,
|
|
180
|
+
});
|
|
181
|
+
const core = source.module && normalizeModule(source.module).core;
|
|
182
|
+
return core
|
|
183
|
+
? { io: ioTargets(core), publication: publicationSelectors(core) }
|
|
184
|
+
: undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function checkProject(
|
|
188
|
+
file = "tsplc.config.ts",
|
|
189
|
+
options: ProjectOptions = {},
|
|
190
|
+
): Promise<{
|
|
191
|
+
configFile: string;
|
|
192
|
+
config?: NormalizedConfig;
|
|
193
|
+
shared?: ResolvedTsplcConfig;
|
|
194
|
+
plan?: CodesysPlan;
|
|
195
|
+
diagnostics: readonly Diagnostic[];
|
|
196
|
+
}> {
|
|
197
|
+
const loaded = await loadProject(file, options);
|
|
198
|
+
if (!loaded.config || !loaded.project) {
|
|
199
|
+
return loaded;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const config = loaded.config;
|
|
203
|
+
if (config.target.kind !== "codesys") {
|
|
204
|
+
return {
|
|
205
|
+
...loaded,
|
|
206
|
+
diagnostics: [
|
|
207
|
+
failure(
|
|
208
|
+
"backend",
|
|
209
|
+
"PLC7001",
|
|
210
|
+
`Unsupported target: ${config.target.kind}`,
|
|
211
|
+
),
|
|
212
|
+
],
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const source = analyze({
|
|
217
|
+
tsconfig: config.tsconfig,
|
|
218
|
+
programs: config.programs,
|
|
219
|
+
services: loaded.shared?.server,
|
|
220
|
+
env: loaded.project.env,
|
|
221
|
+
});
|
|
222
|
+
if (!source.module) {
|
|
223
|
+
return { ...loaded, diagnostics: source.diagnostics };
|
|
224
|
+
}
|
|
225
|
+
const normalized = normalizeModule(source.module);
|
|
226
|
+
if (!normalized.core) {
|
|
227
|
+
return { ...loaded, diagnostics: normalized.diagnostics };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const tasks = config.tasks.map((task) => ({
|
|
231
|
+
name: task.name,
|
|
232
|
+
intervalNanoseconds: task.schedule.interval.nanoseconds,
|
|
233
|
+
priority: task.priority,
|
|
234
|
+
programs: task.programs,
|
|
235
|
+
}));
|
|
236
|
+
const planned = planCodesys(
|
|
237
|
+
normalized.core,
|
|
238
|
+
{ tasks },
|
|
239
|
+
config.target.options,
|
|
240
|
+
loaded.project,
|
|
241
|
+
);
|
|
242
|
+
return {
|
|
243
|
+
...loaded,
|
|
244
|
+
...planned,
|
|
245
|
+
diagnostics: [
|
|
246
|
+
...loaded.diagnostics,
|
|
247
|
+
...source.diagnostics,
|
|
248
|
+
...normalized.diagnostics,
|
|
249
|
+
...planned.diagnostics,
|
|
250
|
+
],
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The installed libraries and fieldbus device tree a CODESYS project of this program needs. */
|
|
255
|
+
async function engineeringInputs(
|
|
256
|
+
shared: ResolvedTsplcConfig,
|
|
257
|
+
configFile: string,
|
|
258
|
+
bundle: Pick<ReturnType<typeof emitCodesys>, "ioMappings">,
|
|
259
|
+
options: { frozen?: boolean },
|
|
260
|
+
) {
|
|
261
|
+
const root = dirname(configFile);
|
|
262
|
+
const tree = await deviceTree(shared, await loadHardware(shared, root));
|
|
263
|
+
return {
|
|
264
|
+
libraries: await installLibraries(shared.dependencies, root, options),
|
|
265
|
+
// The emitted names locate mapped IO; the tree maps their channels.
|
|
266
|
+
deviceTree: tree && {
|
|
267
|
+
...tree,
|
|
268
|
+
mappings: bundle.ioMappings.map(({ address, variable }) => ({
|
|
269
|
+
address,
|
|
270
|
+
variable,
|
|
271
|
+
})),
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export async function buildProject(
|
|
277
|
+
file = "tsplc.config.ts",
|
|
278
|
+
options: ProjectOptions & {
|
|
279
|
+
emitOnly?: boolean;
|
|
280
|
+
frozenLockfile?: boolean;
|
|
281
|
+
controller?: Controller;
|
|
282
|
+
/** Resolve libraries and the device tree into `controllerConfig` even when only emitting. */
|
|
283
|
+
forController?: boolean;
|
|
284
|
+
} = {},
|
|
285
|
+
): Promise<{
|
|
286
|
+
directory?: string;
|
|
287
|
+
status?: "emitted" | "vendor-compiled";
|
|
288
|
+
controllerConfig?: ReturnType<typeof codesysBuildConfig>;
|
|
289
|
+
diagnostics: readonly Diagnostic[];
|
|
290
|
+
}> {
|
|
291
|
+
let staging: string | undefined;
|
|
292
|
+
try {
|
|
293
|
+
const configFile = await realpath(resolve(file));
|
|
294
|
+
// Withdraw the previous success receipt before checking the new source.
|
|
295
|
+
// Old artifacts remain owned and intact until publication succeeds.
|
|
296
|
+
const previous = await owned(configFile, true);
|
|
297
|
+
if (previous.unknown.length) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`Output contains unrelated files: ${previous.unknown.join(", ")}`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const checked = await checkProject(configFile, options);
|
|
304
|
+
if (!checked.plan || !checked.config) {
|
|
305
|
+
return { diagnostics: checked.diagnostics };
|
|
306
|
+
}
|
|
307
|
+
const bundle = emitCodesys(checked.plan);
|
|
308
|
+
if (hasErrors(bundle.diagnostics)) {
|
|
309
|
+
return { diagnostics: bundle.diagnostics };
|
|
310
|
+
}
|
|
311
|
+
const shared = checked.shared;
|
|
312
|
+
const engineering =
|
|
313
|
+
shared && (!options.emitOnly || options.forController)
|
|
314
|
+
? await engineeringInputs(shared, configFile, bundle, {
|
|
315
|
+
frozen: options.frozenLockfile,
|
|
316
|
+
})
|
|
317
|
+
: undefined;
|
|
318
|
+
const outDir =
|
|
319
|
+
shared?.build && resolve(dirname(configFile), shared.build.outDir);
|
|
320
|
+
const controllerConfig = outDir
|
|
321
|
+
? codesysBuildConfig(checked.plan, bundle, {
|
|
322
|
+
configFile,
|
|
323
|
+
sourceDir: join(previous.output, "st"),
|
|
324
|
+
outDir,
|
|
325
|
+
controller: options.controller,
|
|
326
|
+
...engineering,
|
|
327
|
+
})
|
|
328
|
+
: undefined;
|
|
329
|
+
|
|
330
|
+
await mkdir(previous.root, { recursive: true });
|
|
331
|
+
staging = await mkdtemp(join(previous.root, "stage-"));
|
|
332
|
+
const artifacts = [
|
|
333
|
+
...bundle.artifacts,
|
|
334
|
+
{
|
|
335
|
+
path: "mappings.json",
|
|
336
|
+
content: `${JSON.stringify(compactMappings(bundle.mappings))}\n`,
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
path: "io-symbols.json",
|
|
340
|
+
content: `${JSON.stringify(bundle.ioSymbols, null, 2)}\n`,
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
path: "publication.json",
|
|
344
|
+
content: `${JSON.stringify(bundle.publication, null, 2)}\n`,
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
path: "config.json",
|
|
348
|
+
content: `${JSON.stringify(checked.config, null, 2)}\n`,
|
|
349
|
+
},
|
|
350
|
+
...(bundle.debug
|
|
351
|
+
? [{ path: "debug.json", content: `${JSON.stringify(bundle.debug)}\n` }]
|
|
352
|
+
: []),
|
|
353
|
+
];
|
|
354
|
+
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);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
let vendor: Awaited<ReturnType<typeof buildCodesys>> | undefined;
|
|
361
|
+
if (!options.emitOnly) {
|
|
362
|
+
// The engineering cache has its own ownership marker and survives clean.
|
|
363
|
+
const engineeringDir = outDir ?? join(previous.root, "engineering");
|
|
364
|
+
await engineeringDirectory(engineeringDir, configFile);
|
|
365
|
+
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
|
+
});
|
|
373
|
+
} catch (cause) {
|
|
374
|
+
return { diagnostics: diagnoseCodesys(cause, bundle) };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const vendorArtifacts = [
|
|
378
|
+
["Application.project", vendor.project],
|
|
379
|
+
["Application.app", vendor.output],
|
|
380
|
+
["Application.crc", vendor.checksum],
|
|
381
|
+
] as const;
|
|
382
|
+
for (const [name, path] of vendorArtifacts) {
|
|
383
|
+
if (!path) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const destination = join(staging, "vendor", name);
|
|
387
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
388
|
+
await copyFile(path, destination);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const status = vendor ? "vendor-compiled" : "emitted";
|
|
393
|
+
await publishArtifacts(staging, previous, {
|
|
394
|
+
configFile,
|
|
395
|
+
status,
|
|
396
|
+
...(vendor && {
|
|
397
|
+
vendor: { application: vendor.application, tasks: vendor.tasks },
|
|
398
|
+
}),
|
|
399
|
+
});
|
|
400
|
+
// Publication moved this directory into place; it is no longer staging.
|
|
401
|
+
staging = undefined;
|
|
402
|
+
return {
|
|
403
|
+
directory: previous.output,
|
|
404
|
+
status,
|
|
405
|
+
...(controllerConfig && { controllerConfig }),
|
|
406
|
+
diagnostics: [...checked.diagnostics, ...bundle.diagnostics],
|
|
407
|
+
};
|
|
408
|
+
} catch (cause) {
|
|
409
|
+
return { diagnostics: [failure("config", "PLC5004", cause)] };
|
|
410
|
+
} finally {
|
|
411
|
+
if (staging) {
|
|
412
|
+
await rm(staging, { recursive: true, force: true });
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Source mappings with each ST path and source file written once: `mappings` rows are
|
|
419
|
+
* `[object, line, file, start, end, sourceLine, sourceColumn]`, indexing `objects` and `files`.
|
|
420
|
+
*/
|
|
421
|
+
function compactMappings(mappings: readonly SourceMapping[]) {
|
|
422
|
+
const objects = new Map<string, number>();
|
|
423
|
+
const files = new Map<string, number>();
|
|
424
|
+
const index = (table: Map<string, number>, value: string) => {
|
|
425
|
+
const known = table.get(value);
|
|
426
|
+
if (known !== undefined) {
|
|
427
|
+
return known;
|
|
428
|
+
}
|
|
429
|
+
table.set(value, table.size);
|
|
430
|
+
return table.size - 1;
|
|
431
|
+
};
|
|
432
|
+
const rows = mappings.map(({ path, line, span }) => [
|
|
433
|
+
index(objects, path),
|
|
434
|
+
line,
|
|
435
|
+
index(files, span.file),
|
|
436
|
+
span.start,
|
|
437
|
+
span.end,
|
|
438
|
+
span.line,
|
|
439
|
+
span.column,
|
|
440
|
+
]);
|
|
441
|
+
return {
|
|
442
|
+
objects: [...objects.keys()],
|
|
443
|
+
files: [...files.keys()],
|
|
444
|
+
mappings: rows,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export async function cleanProject(file = "tsplc.config.ts"): Promise<{
|
|
449
|
+
diagnostics: readonly Diagnostic[];
|
|
450
|
+
}> {
|
|
451
|
+
try {
|
|
452
|
+
await cleanArtifacts(await realpath(resolve(file)));
|
|
453
|
+
return { diagnostics: [] };
|
|
454
|
+
} catch (cause) {
|
|
455
|
+
return { diagnostics: [failure("config", "PLC5004", cause)] };
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { basename, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import type { ReleaseFile } from "@tsplc/agent/client";
|
|
5
|
+
import type { ReleaseManifest } from "@tsplc/agent";
|
|
6
|
+
import {
|
|
7
|
+
buildTargetFor,
|
|
8
|
+
controllerFor,
|
|
9
|
+
serverTokenEnv,
|
|
10
|
+
type loadTsplcConfig,
|
|
11
|
+
} from "@tsplc/config";
|
|
12
|
+
import { hasErrors } from "@tsplc/compiler/ir";
|
|
13
|
+
import { contractVariables } from "@tsplc/opcua/contract";
|
|
14
|
+
import { generateProject } from "@tsplc/typegen";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import { buildConfigs, buildProject, projectTargets } from "./project.ts";
|
|
17
|
+
import { bundleServer } from "./server.ts";
|
|
18
|
+
|
|
19
|
+
type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
|
|
20
|
+
|
|
21
|
+
/** Every published leaf the generated contract describes, as CODESYS expressions name it. */
|
|
22
|
+
const releaseVariables = async (
|
|
23
|
+
config: LoadedConfig["config"],
|
|
24
|
+
root: string,
|
|
25
|
+
) => {
|
|
26
|
+
if (!config.typegen) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const contract = z
|
|
30
|
+
.object({
|
|
31
|
+
symbols: z.array(
|
|
32
|
+
z.object({ name: z.string(), gvl: z.string(), type: z.string() }),
|
|
33
|
+
),
|
|
34
|
+
typeFields: z.record(
|
|
35
|
+
z.string(),
|
|
36
|
+
z.array(
|
|
37
|
+
z.object({
|
|
38
|
+
field: z.string(),
|
|
39
|
+
type: z.string(),
|
|
40
|
+
description: z.string(),
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
),
|
|
44
|
+
})
|
|
45
|
+
.parse(
|
|
46
|
+
await import(
|
|
47
|
+
pathToFileURL(resolve(root, config.typegen.dir, "plc.ts")).href
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
return contractVariables(contract).map(({ path, type }) => ({ path, type }));
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Compiles the program and bundles its server into the files `tsplc deploy` uploads: the
|
|
55
|
+
* production program, or with `--simulate` the development one without fieldbus I/O, for a
|
|
56
|
+
* virtual PLC whose inputs a plant simulation writes, as `tsplc run --simulate` builds it.
|
|
57
|
+
*/
|
|
58
|
+
export const prepareRelease = async (
|
|
59
|
+
file: string,
|
|
60
|
+
loaded: LoadedConfig,
|
|
61
|
+
flags: { simulate?: boolean; "frozen-lockfile"?: boolean },
|
|
62
|
+
) => {
|
|
63
|
+
const { config, root } = loaded;
|
|
64
|
+
if (!config.compiler) {
|
|
65
|
+
throw new Error("tsplc deploy deploys tsPLC programs; configure compiler.");
|
|
66
|
+
}
|
|
67
|
+
const command = flags.simulate ? "run" : "deploy";
|
|
68
|
+
const emitted = await buildProject(file, {
|
|
69
|
+
emitOnly: true,
|
|
70
|
+
forController: true,
|
|
71
|
+
frozenLockfile: flags["frozen-lockfile"],
|
|
72
|
+
...buildConfigs(loaded, flags),
|
|
73
|
+
buildTarget: buildTargetFor(command),
|
|
74
|
+
controller: controllerFor(config, command),
|
|
75
|
+
});
|
|
76
|
+
if (hasErrors(emitted.diagnostics)) {
|
|
77
|
+
return { diagnostics: emitted.diagnostics };
|
|
78
|
+
}
|
|
79
|
+
const settings = emitted.controllerConfig;
|
|
80
|
+
if (!settings) {
|
|
81
|
+
throw new Error("Configure build settings for deployment.");
|
|
82
|
+
}
|
|
83
|
+
await generateProject(file, {
|
|
84
|
+
loaded,
|
|
85
|
+
targets: await projectTargets(file, { loaded }),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const portable = (path: string) => path.split(sep).join("/");
|
|
89
|
+
const libraries = (settings.libraries ?? []).map((library) => ({
|
|
90
|
+
...library,
|
|
91
|
+
release: `libraries/${library.sha256}-${basename(library.path)}`,
|
|
92
|
+
}));
|
|
93
|
+
const files: ReleaseFile[] = [
|
|
94
|
+
{ path: "template.project", content: Bun.file(settings.template) },
|
|
95
|
+
...libraries.map((library) => ({
|
|
96
|
+
path: library.release,
|
|
97
|
+
content: Bun.file(library.path),
|
|
98
|
+
})),
|
|
99
|
+
];
|
|
100
|
+
// The agent compiles only the ST. Plain TypeScript programs emit their generated modules under `.tsplc/`.
|
|
101
|
+
for await (const source of new Bun.Glob("**/*.st").scan({
|
|
102
|
+
cwd: settings.sourceDir,
|
|
103
|
+
dot: true,
|
|
104
|
+
})) {
|
|
105
|
+
files.push({
|
|
106
|
+
path: `st/${portable(source)}`,
|
|
107
|
+
content: Bun.file(resolve(settings.sourceDir, source)),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
// The agent's debugger reads the program through its debug manifest.
|
|
111
|
+
const debugManifest = resolve(settings.sourceDir, "debug.json");
|
|
112
|
+
if (existsSync(debugManifest)) {
|
|
113
|
+
files.push({ path: "debug.json", content: Bun.file(debugManifest) });
|
|
114
|
+
}
|
|
115
|
+
const serverScript =
|
|
116
|
+
config.server?.runtime && (await bundleServer(config, root));
|
|
117
|
+
const server = serverScript
|
|
118
|
+
? {
|
|
119
|
+
path: `server/${basename(serverScript)}`,
|
|
120
|
+
content: Bun.file(serverScript),
|
|
121
|
+
}
|
|
122
|
+
: undefined;
|
|
123
|
+
if (server) {
|
|
124
|
+
files.push(server);
|
|
125
|
+
}
|
|
126
|
+
const manifest: ReleaseManifest = {
|
|
127
|
+
// Paths are relative to the release; the agent supplies the output and controller settings.
|
|
128
|
+
config: {
|
|
129
|
+
io: {},
|
|
130
|
+
deviceTree: settings.deviceTree,
|
|
131
|
+
controller: settings.controller,
|
|
132
|
+
libraryReferences: settings.libraryReferences,
|
|
133
|
+
libraries: libraries.map(({ release, ...library }) => ({
|
|
134
|
+
...library,
|
|
135
|
+
path: release,
|
|
136
|
+
})),
|
|
137
|
+
task: settings.task,
|
|
138
|
+
tasks: settings.tasks,
|
|
139
|
+
removeObjects: settings.removeObjects,
|
|
140
|
+
symbols: settings.symbols,
|
|
141
|
+
timeouts: settings.timeouts,
|
|
142
|
+
template: "template.project",
|
|
143
|
+
sourceDir: "st",
|
|
144
|
+
entry: `st/${portable(relative(settings.sourceDir, settings.entry))}`,
|
|
145
|
+
},
|
|
146
|
+
variables: await releaseVariables(config, root),
|
|
147
|
+
server: server?.path,
|
|
148
|
+
debug: config.debug,
|
|
149
|
+
serverTokenEnv: serverTokenEnv(config),
|
|
150
|
+
secrets: config.secrets,
|
|
151
|
+
};
|
|
152
|
+
return { release: { manifest, files } };
|
|
153
|
+
};
|