@tsplc/typegen 0.2.21 → 0.3.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.
- package/README.md +1 -1
- package/dist/cli.js +223 -198
- package/dist/cli.js.map +11 -11
- package/dist/codegen.js +8 -7
- package/dist/codegen.js.map +3 -3
- package/dist/hardware.js +171 -171
- package/dist/hardware.js.map +8 -8
- package/dist/index.js +225 -203
- package/dist/index.js.map +12 -12
- package/dist/project-snapshot.js +46 -0
- package/dist/project-snapshot.js.map +10 -0
- package/package.json +20 -4
- package/src/cli.ts +1 -1
- package/src/codegen/generate.ts +10 -7
- package/src/device-tree.ts +2 -1
- package/src/hardware/channels.ts +1 -1
- package/src/hardware/eds.ts +29 -26
- package/src/hardware/mapping-types.ts +2 -1
- package/src/hardware/modbus.ts +2 -1
- package/src/hardware.ts +7 -6
- package/src/index.ts +13 -13
- package/src/project-snapshot.ts +63 -0
- package/src/project.ts +49 -22
package/dist/index.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
// packages/typegen/src/index.ts
|
|
2
|
+
import { ioScalarBits as ioScalarBits4 } from "@tsplc/iec";
|
|
3
|
+
|
|
1
4
|
// packages/typegen/src/codegen/generate.ts
|
|
2
5
|
import { readdir, readFile } from "node:fs/promises";
|
|
3
6
|
import { basename, join } from "node:path";
|
|
7
|
+
import { elementaryTypesByName } from "@tsplc/iec";
|
|
8
|
+
import { identifierPattern as identifierPattern2 } from "@tsplc/iec/st";
|
|
4
9
|
|
|
5
10
|
// packages/typegen/src/codegen/constants.ts
|
|
6
11
|
import {
|
|
@@ -126,10 +131,6 @@ function constantResolver(lists) {
|
|
|
126
131
|
return constant;
|
|
127
132
|
}
|
|
128
133
|
|
|
129
|
-
// packages/typegen/src/codegen/generate.ts
|
|
130
|
-
import { elementaryTypesByName } from "@tsplc/iec";
|
|
131
|
-
import { identifierPattern as identifierPattern2 } from "@tsplc/iec/st";
|
|
132
|
-
|
|
133
134
|
// packages/typegen/src/codegen/parser.ts
|
|
134
135
|
import {
|
|
135
136
|
blockCommentEnd,
|
|
@@ -577,7 +578,8 @@ function emit(declarations, globals, {
|
|
|
577
578
|
const visited = new Set;
|
|
578
579
|
function visitReference(type) {
|
|
579
580
|
if (type.kind === "array") {
|
|
580
|
-
|
|
581
|
+
visitReference(type.element);
|
|
582
|
+
return;
|
|
581
583
|
}
|
|
582
584
|
if (type.kind === "named" && !scalars[type.name.toUpperCase()]) {
|
|
583
585
|
visit(declaration(type.name));
|
|
@@ -592,7 +594,9 @@ function emit(declarations, globals, {
|
|
|
592
594
|
}
|
|
593
595
|
visiting.add(type.name);
|
|
594
596
|
if (type.kind === "struct") {
|
|
595
|
-
type.members.forEach((member) =>
|
|
597
|
+
type.members.forEach((member) => {
|
|
598
|
+
visitReference(member.type);
|
|
599
|
+
});
|
|
596
600
|
}
|
|
597
601
|
if (type.kind === "alias") {
|
|
598
602
|
visitReference(type.type);
|
|
@@ -722,9 +726,9 @@ ${list.members.map((member) => `${documentation(member.description)}${quote(memb
|
|
|
722
726
|
return output.join(`
|
|
723
727
|
`);
|
|
724
728
|
}
|
|
725
|
-
// packages/typegen/src/
|
|
726
|
-
import {
|
|
727
|
-
import {
|
|
729
|
+
// packages/typegen/src/device-tree.ts
|
|
730
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
731
|
+
import { iecName } from "@tsplc/config/build";
|
|
728
732
|
|
|
729
733
|
// packages/typegen/src/hardware/eds.ts
|
|
730
734
|
var cipTypes = {
|
|
@@ -1004,9 +1008,6 @@ function parseEds(text, file = "device.eds") {
|
|
|
1004
1008
|
};
|
|
1005
1009
|
}
|
|
1006
1010
|
|
|
1007
|
-
// packages/typegen/src/hardware/channels.ts
|
|
1008
|
-
import { ioScalarBits } from "@tsplc/iec";
|
|
1009
|
-
|
|
1010
1011
|
// packages/typegen/src/hardware/process-image.ts
|
|
1011
1012
|
import { ioArray, ioScalar, sizePrefix } from "@tsplc/iec";
|
|
1012
1013
|
function ioLayout(type) {
|
|
@@ -1065,7 +1066,180 @@ function regionPlacer(regions, { generatedTree }) {
|
|
|
1065
1066
|
};
|
|
1066
1067
|
}
|
|
1067
1068
|
|
|
1069
|
+
// packages/typegen/src/device-tree.ts
|
|
1070
|
+
var quoted = (value) => `"${value.replaceAll('"', '""')}"`;
|
|
1071
|
+
function normalizeEds(text, selection) {
|
|
1072
|
+
const sections = edsSections(text);
|
|
1073
|
+
const connections = [...sections.get("Connection Manager") ?? []].filter(([key]) => /^Connection\d+$/.test(key));
|
|
1074
|
+
const selected = connections.find(([key]) => key === selection.connection)?.[1];
|
|
1075
|
+
if (!selected) {
|
|
1076
|
+
throw new Error(`EDS has no ${selection.connection}.`);
|
|
1077
|
+
}
|
|
1078
|
+
const fields = [...selected.fields];
|
|
1079
|
+
const resized = new Map;
|
|
1080
|
+
for (const [sizeField, formatField, size] of [
|
|
1081
|
+
[3, 4, selection.outputSize],
|
|
1082
|
+
[6, 7, selection.inputSize]
|
|
1083
|
+
]) {
|
|
1084
|
+
const assembly = fields[formatField];
|
|
1085
|
+
if (!assembly) {
|
|
1086
|
+
continue;
|
|
1087
|
+
}
|
|
1088
|
+
if (size === undefined) {
|
|
1089
|
+
throw new Error(`Select a payload size for ${assembly}.`);
|
|
1090
|
+
}
|
|
1091
|
+
fields[sizeField] = String(size);
|
|
1092
|
+
resized.set(assembly, size);
|
|
1093
|
+
}
|
|
1094
|
+
const edits = connections.map(([, record]) => ({
|
|
1095
|
+
start: record.start,
|
|
1096
|
+
end: record.end,
|
|
1097
|
+
text: record === selected ? `Connection1 = ${fields.map((value, index) => index >= 12 ? quoted(value) : value).join(",")};` : ""
|
|
1098
|
+
}));
|
|
1099
|
+
for (const [key, record] of sections.get("Assembly") ?? []) {
|
|
1100
|
+
const size = resized.get(key);
|
|
1101
|
+
if (size === undefined) {
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
const [name = "", path = "", , descriptor = ""] = record.fields;
|
|
1105
|
+
const rawBytes = [
|
|
1106
|
+
quoted(name),
|
|
1107
|
+
path && quoted(path),
|
|
1108
|
+
size,
|
|
1109
|
+
descriptor,
|
|
1110
|
+
"",
|
|
1111
|
+
"",
|
|
1112
|
+
size * 8,
|
|
1113
|
+
""
|
|
1114
|
+
];
|
|
1115
|
+
edits.push({
|
|
1116
|
+
start: record.start,
|
|
1117
|
+
end: record.end,
|
|
1118
|
+
text: `${key} = ${rawBytes.join(",")};`
|
|
1119
|
+
});
|
|
1120
|
+
resized.delete(key);
|
|
1121
|
+
}
|
|
1122
|
+
if (resized.size) {
|
|
1123
|
+
throw new Error(`EDS has no ${[...resized.keys()].join(", ")} assembly.`);
|
|
1124
|
+
}
|
|
1125
|
+
return edits.sort((a, b) => b.start - a.start).reduce((result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end), text);
|
|
1126
|
+
}
|
|
1127
|
+
async function deviceTree(config, catalog) {
|
|
1128
|
+
const settings = config.codesys?.deviceTree;
|
|
1129
|
+
if (!settings) {
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
const devices = await Promise.all(catalog.devices.map(async (device) => {
|
|
1133
|
+
if (!iecName.safeParse(device.id).success) {
|
|
1134
|
+
throw new Error(`${device.id}: a device tree adapter needs an IEC identifier name.`);
|
|
1135
|
+
}
|
|
1136
|
+
const connection = device.connection;
|
|
1137
|
+
if (!connection) {
|
|
1138
|
+
throw new Error(`${device.id}: select the connection to configure.`);
|
|
1139
|
+
}
|
|
1140
|
+
const region = (direction) => {
|
|
1141
|
+
const assembly = device.assemblies.find((item) => item.direction === direction);
|
|
1142
|
+
return assembly && { address: assembly.address, size: assembly.size };
|
|
1143
|
+
};
|
|
1144
|
+
const inputs = region("input");
|
|
1145
|
+
const outputs = region("output");
|
|
1146
|
+
const comments = Object.entries(config.io).flatMap(([key, binding]) => {
|
|
1147
|
+
if (!("device" in binding) || binding.device !== device.id) {
|
|
1148
|
+
return [];
|
|
1149
|
+
}
|
|
1150
|
+
const channel = device.channels.find((item) => item.id === binding.channel);
|
|
1151
|
+
if (channel.assembly === undefined) {
|
|
1152
|
+
return [];
|
|
1153
|
+
}
|
|
1154
|
+
if (channel.bits === 1) {
|
|
1155
|
+
return [{ address: channel.address, text: key }];
|
|
1156
|
+
}
|
|
1157
|
+
const area = channel.direction === "input" ? inputs : outputs;
|
|
1158
|
+
const bytes = channel.bits / 8;
|
|
1159
|
+
return [
|
|
1160
|
+
{
|
|
1161
|
+
address: `${area.address.slice(0, 3)}${byteOffset(area.address) + channel.offsetBits / 8}`,
|
|
1162
|
+
text: bytes > 1 ? `${key} (${bytes} bytes)` : key
|
|
1163
|
+
}
|
|
1164
|
+
];
|
|
1165
|
+
});
|
|
1166
|
+
const network = config.devices[device.id]?.network;
|
|
1167
|
+
if (device.modbus && !network?.address) {
|
|
1168
|
+
throw new Error(`${device.id}: Modbus registers need its network address.`);
|
|
1169
|
+
}
|
|
1170
|
+
return {
|
|
1171
|
+
name: device.id,
|
|
1172
|
+
eds: normalizeEds(await readFile2(device.descriptor, "utf8"), {
|
|
1173
|
+
connection: connection.id,
|
|
1174
|
+
inputSize: inputs?.size,
|
|
1175
|
+
outputSize: outputs?.size
|
|
1176
|
+
}),
|
|
1177
|
+
...network?.address && { address: network.address },
|
|
1178
|
+
...network?.rpi && {
|
|
1179
|
+
rpiMicroseconds: Number(BigInt(network.rpi.nanoseconds) / 1000n)
|
|
1180
|
+
},
|
|
1181
|
+
...inputs && { inputs },
|
|
1182
|
+
...outputs && { outputs },
|
|
1183
|
+
...device.modbus && { modbus: device.modbus },
|
|
1184
|
+
comments
|
|
1185
|
+
};
|
|
1186
|
+
}));
|
|
1187
|
+
const [first, ...rest] = devices;
|
|
1188
|
+
if (!first) {
|
|
1189
|
+
throw new Error("A generated device tree needs at least one device.");
|
|
1190
|
+
}
|
|
1191
|
+
return { ethernet: settings.ethernet ?? {}, devices: [first, ...rest] };
|
|
1192
|
+
}
|
|
1193
|
+
// packages/typegen/src/hardware/mapping-types.ts
|
|
1194
|
+
import { ioScalarBits } from "@tsplc/iec";
|
|
1195
|
+
var directBinding = (layout) => {
|
|
1196
|
+
const scalar = layout.replace(/^\d+x/, "");
|
|
1197
|
+
const integer = /^INT(\d+)$/.exec(scalar);
|
|
1198
|
+
const bits = integer ? Number(integer[1]) : ioScalarBits(scalar);
|
|
1199
|
+
const size = bits === undefined ? undefined : addressSize(bits);
|
|
1200
|
+
if (!size) {
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
return size === "X" ? '{ address: `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }' : `{ address: \`%\${"I" | "Q"}${size}\${number}\`; type?: string }`;
|
|
1204
|
+
};
|
|
1205
|
+
function generateHardwareTypes(catalog, targets, publication, ioTypes = true) {
|
|
1206
|
+
const selectors = publication?.map((selector) => JSON.stringify(selector)).join(" | ") ?? "string";
|
|
1207
|
+
const publicationTypes = `// Generated by tsPLC. Run tsplc typegen after changing EDS files, device selections or PLC storage.
|
|
1208
|
+
export type PublicationSelector = ${selectors || "never"};
|
|
1209
|
+
export type PlcPublication = { readonly production: readonly PublicationSelector[]; readonly development: readonly PublicationSelector[] };
|
|
1210
|
+
`;
|
|
1211
|
+
if (!ioTypes) {
|
|
1212
|
+
return publicationTypes;
|
|
1213
|
+
}
|
|
1214
|
+
const channel = (device, id, options) => `{ device: ${JSON.stringify(device)}; channel: ${JSON.stringify(id)}${options} }`;
|
|
1215
|
+
const channels = catalog.devices.flatMap((device) => device.channels.filter((item) => item.address).map((item) => ({
|
|
1216
|
+
binding: channel(device.id, item.id, item.direction === "input" && item.type === "BOOL" ? "; inverted?: boolean; shared?: boolean" : ""),
|
|
1217
|
+
layout: ioLayout(item.type)
|
|
1218
|
+
})));
|
|
1219
|
+
const direct = '{ address: `%${"I" | "Q"}${"B" | "W" | "D"}${number}` | `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }';
|
|
1220
|
+
const anyBinding = [...channels.map((item) => item.binding), direct].join(`
|
|
1221
|
+
| `);
|
|
1222
|
+
const mapping = targets ? [...new Set(targets.map((target) => target.layout))].sort().map((layout) => {
|
|
1223
|
+
const keys = targets.filter((target) => target.layout === layout).map((target) => `\`${target.key}\``);
|
|
1224
|
+
const bindings = [
|
|
1225
|
+
...channels.filter((item) => item.layout === layout).map((item) => item.binding),
|
|
1226
|
+
directBinding(layout)
|
|
1227
|
+
].filter(Boolean);
|
|
1228
|
+
return `{
|
|
1229
|
+
readonly [K in ${keys.join(" | ")}]?: ${bindings.join(" | ") || "never"};
|
|
1230
|
+
}`;
|
|
1231
|
+
}).join(" & ") || "Readonly<Record<string, never>>" : "Readonly<Record<string, HardwareIoBinding>>";
|
|
1232
|
+
return `${publicationTypes}export type HardwareIoBinding = ${anyBinding};
|
|
1233
|
+
export type HardwareIoMapping = ${mapping};
|
|
1234
|
+
export const hardware = ${JSON.stringify(catalog, null, 2)} as const;
|
|
1235
|
+
`;
|
|
1236
|
+
}
|
|
1237
|
+
// packages/typegen/src/hardware.ts
|
|
1238
|
+
import { readdir as readdir2, readFile as readFile3 } from "node:fs/promises";
|
|
1239
|
+
import { resolve } from "node:path";
|
|
1240
|
+
|
|
1068
1241
|
// packages/typegen/src/hardware/channels.ts
|
|
1242
|
+
import { ioScalarBits as ioScalarBits2 } from "@tsplc/iec";
|
|
1069
1243
|
function assemblyChannels({
|
|
1070
1244
|
device,
|
|
1071
1245
|
direction,
|
|
@@ -1128,7 +1302,7 @@ function assemblyChannels({
|
|
|
1128
1302
|
const count = counts.get(name) ?? 0;
|
|
1129
1303
|
counts.set(name, count + 1);
|
|
1130
1304
|
const key = `${prefix}.${name}${repeated.has(name) ? `[${count}]` : ""}`;
|
|
1131
|
-
const scalarBits =
|
|
1305
|
+
const scalarBits = ioScalarBits2(member.type);
|
|
1132
1306
|
const channel = {
|
|
1133
1307
|
device,
|
|
1134
1308
|
id: key,
|
|
@@ -1273,53 +1447,6 @@ function selectAssemblies(id, settings, descriptor, connection) {
|
|
|
1273
1447
|
|
|
1274
1448
|
// packages/typegen/src/hardware.ts
|
|
1275
1449
|
import { ioScalarBits as ioScalarBits3 } from "@tsplc/iec";
|
|
1276
|
-
|
|
1277
|
-
// packages/typegen/src/hardware/mapping-types.ts
|
|
1278
|
-
import { ioScalarBits as ioScalarBits2 } from "@tsplc/iec";
|
|
1279
|
-
var directBinding = (layout) => {
|
|
1280
|
-
const scalar = layout.replace(/^\d+x/, "");
|
|
1281
|
-
const integer = /^INT(\d+)$/.exec(scalar);
|
|
1282
|
-
const bits = integer ? Number(integer[1]) : ioScalarBits2(scalar);
|
|
1283
|
-
const size = bits === undefined ? undefined : addressSize(bits);
|
|
1284
|
-
if (!size) {
|
|
1285
|
-
return;
|
|
1286
|
-
}
|
|
1287
|
-
return size === "X" ? '{ address: `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }' : `{ address: \`%\${"I" | "Q"}${size}\${number}\`; type?: string }`;
|
|
1288
|
-
};
|
|
1289
|
-
function generateHardwareTypes(catalog, targets, publication, ioTypes = true) {
|
|
1290
|
-
const selectors = publication?.map((selector) => JSON.stringify(selector)).join(" | ") ?? "string";
|
|
1291
|
-
const publicationTypes = `// Generated by tsPLC. Run tsplc typegen after changing EDS files, device selections or PLC storage.
|
|
1292
|
-
export type PublicationSelector = ${selectors || "never"};
|
|
1293
|
-
export type PlcPublication = { readonly production: readonly PublicationSelector[]; readonly development: readonly PublicationSelector[] };
|
|
1294
|
-
`;
|
|
1295
|
-
if (!ioTypes) {
|
|
1296
|
-
return publicationTypes;
|
|
1297
|
-
}
|
|
1298
|
-
const channel = (device, id, options) => `{ device: ${JSON.stringify(device)}; channel: ${JSON.stringify(id)}${options} }`;
|
|
1299
|
-
const channels = catalog.devices.flatMap((device) => device.channels.filter((item) => item.address).map((item) => ({
|
|
1300
|
-
binding: channel(device.id, item.id, item.direction === "input" && item.type === "BOOL" ? "; inverted?: boolean; shared?: boolean" : ""),
|
|
1301
|
-
layout: ioLayout(item.type)
|
|
1302
|
-
})));
|
|
1303
|
-
const direct = '{ address: `%${"I" | "Q"}${"B" | "W" | "D"}${number}` | `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }';
|
|
1304
|
-
const anyBinding = [...channels.map((item) => item.binding), direct].join(`
|
|
1305
|
-
| `);
|
|
1306
|
-
const mapping = targets ? [...new Set(targets.map((target) => target.layout))].sort().map((layout) => {
|
|
1307
|
-
const keys = targets.filter((target) => target.layout === layout).map((target) => `\`${target.key}\``);
|
|
1308
|
-
const bindings = [
|
|
1309
|
-
...channels.filter((item) => item.layout === layout).map((item) => item.binding),
|
|
1310
|
-
directBinding(layout)
|
|
1311
|
-
].filter(Boolean);
|
|
1312
|
-
return `{
|
|
1313
|
-
readonly [K in ${keys.join(" | ")}]?: ${bindings.join(" | ") || "never"};
|
|
1314
|
-
}`;
|
|
1315
|
-
}).join(" & ") || "Readonly<Record<string, never>>" : "Readonly<Record<string, HardwareIoBinding>>";
|
|
1316
|
-
return `${publicationTypes}export type HardwareIoBinding = ${anyBinding};
|
|
1317
|
-
export type HardwareIoMapping = ${mapping};
|
|
1318
|
-
export const hardware = ${JSON.stringify(catalog, null, 2)} as const;
|
|
1319
|
-
`;
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
// packages/typegen/src/hardware.ts
|
|
1323
1450
|
async function edsFiles(directory) {
|
|
1324
1451
|
const entries = await readdir2(directory, { withFileTypes: true }).catch((cause) => {
|
|
1325
1452
|
if (cause && typeof cause === "object" && "code" in cause && cause.code === "ENOENT") {
|
|
@@ -1348,7 +1475,7 @@ async function readDescriptors(config, root) {
|
|
|
1348
1475
|
for (const device of Object.values(config.devices)) {
|
|
1349
1476
|
files.add(resolve(root, device.file));
|
|
1350
1477
|
}
|
|
1351
|
-
return Promise.all([...files].map(async (file) => parseEds(await
|
|
1478
|
+
return Promise.all([...files].map(async (file) => parseEds(await readFile3(file, "utf8"), file)));
|
|
1352
1479
|
}
|
|
1353
1480
|
async function loadHardware(config, root) {
|
|
1354
1481
|
const descriptors = await readDescriptors(config, root);
|
|
@@ -1427,149 +1554,43 @@ function resolveIo(config, catalog) {
|
|
|
1427
1554
|
];
|
|
1428
1555
|
}));
|
|
1429
1556
|
}
|
|
1430
|
-
|
|
1431
|
-
// packages/typegen/src/index.ts
|
|
1432
|
-
import { ioScalarBits as ioScalarBits4 } from "@tsplc/iec";
|
|
1433
|
-
|
|
1434
|
-
// packages/typegen/src/device-tree.ts
|
|
1435
|
-
import { readFile as readFile3 } from "node:fs/promises";
|
|
1436
|
-
import { iecName } from "@tsplc/config/build";
|
|
1437
|
-
var quoted = (value) => `"${value.replaceAll('"', '""')}"`;
|
|
1438
|
-
function normalizeEds(text, selection) {
|
|
1439
|
-
const sections = edsSections(text);
|
|
1440
|
-
const connections = [...sections.get("Connection Manager") ?? []].filter(([key]) => /^Connection\d+$/.test(key));
|
|
1441
|
-
const selected = connections.find(([key]) => key === selection.connection)?.[1];
|
|
1442
|
-
if (!selected) {
|
|
1443
|
-
throw new Error(`EDS has no ${selection.connection}.`);
|
|
1444
|
-
}
|
|
1445
|
-
const fields = [...selected.fields];
|
|
1446
|
-
const resized = new Map;
|
|
1447
|
-
for (const [sizeField, formatField, size] of [
|
|
1448
|
-
[3, 4, selection.outputSize],
|
|
1449
|
-
[6, 7, selection.inputSize]
|
|
1450
|
-
]) {
|
|
1451
|
-
const assembly = fields[formatField];
|
|
1452
|
-
if (!assembly) {
|
|
1453
|
-
continue;
|
|
1454
|
-
}
|
|
1455
|
-
if (size === undefined) {
|
|
1456
|
-
throw new Error(`Select a payload size for ${assembly}.`);
|
|
1457
|
-
}
|
|
1458
|
-
fields[sizeField] = String(size);
|
|
1459
|
-
resized.set(assembly, size);
|
|
1460
|
-
}
|
|
1461
|
-
const edits = connections.map(([, record]) => ({
|
|
1462
|
-
start: record.start,
|
|
1463
|
-
end: record.end,
|
|
1464
|
-
text: record === selected ? `Connection1 = ${fields.map((value, index) => index >= 12 ? quoted(value) : value).join(",")};` : ""
|
|
1465
|
-
}));
|
|
1466
|
-
for (const [key, record] of sections.get("Assembly") ?? []) {
|
|
1467
|
-
const size = resized.get(key);
|
|
1468
|
-
if (size === undefined) {
|
|
1469
|
-
continue;
|
|
1470
|
-
}
|
|
1471
|
-
const [name = "", path = "", , descriptor = ""] = record.fields;
|
|
1472
|
-
const rawBytes = [
|
|
1473
|
-
quoted(name),
|
|
1474
|
-
path && quoted(path),
|
|
1475
|
-
size,
|
|
1476
|
-
descriptor,
|
|
1477
|
-
"",
|
|
1478
|
-
"",
|
|
1479
|
-
size * 8,
|
|
1480
|
-
""
|
|
1481
|
-
];
|
|
1482
|
-
edits.push({
|
|
1483
|
-
start: record.start,
|
|
1484
|
-
end: record.end,
|
|
1485
|
-
text: `${key} = ${rawBytes.join(",")};`
|
|
1486
|
-
});
|
|
1487
|
-
resized.delete(key);
|
|
1488
|
-
}
|
|
1489
|
-
if (resized.size) {
|
|
1490
|
-
throw new Error(`EDS has no ${[...resized.keys()].join(", ")} assembly.`);
|
|
1491
|
-
}
|
|
1492
|
-
return edits.sort((a, b) => b.start - a.start).reduce((result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end), text);
|
|
1493
|
-
}
|
|
1494
|
-
async function deviceTree(config, catalog) {
|
|
1495
|
-
const settings = config.codesys?.deviceTree;
|
|
1496
|
-
if (!settings) {
|
|
1497
|
-
return;
|
|
1498
|
-
}
|
|
1499
|
-
const devices = await Promise.all(catalog.devices.map(async (device) => {
|
|
1500
|
-
if (!iecName.safeParse(device.id).success) {
|
|
1501
|
-
throw new Error(`${device.id}: a device tree adapter needs an IEC identifier name.`);
|
|
1502
|
-
}
|
|
1503
|
-
const connection = device.connection;
|
|
1504
|
-
if (!connection) {
|
|
1505
|
-
throw new Error(`${device.id}: select the connection to configure.`);
|
|
1506
|
-
}
|
|
1507
|
-
const region = (direction) => {
|
|
1508
|
-
const assembly = device.assemblies.find((item) => item.direction === direction);
|
|
1509
|
-
return assembly && { address: assembly.address, size: assembly.size };
|
|
1510
|
-
};
|
|
1511
|
-
const inputs = region("input");
|
|
1512
|
-
const outputs = region("output");
|
|
1513
|
-
const comments = Object.entries(config.io).flatMap(([key, binding]) => {
|
|
1514
|
-
if (!("device" in binding) || binding.device !== device.id) {
|
|
1515
|
-
return [];
|
|
1516
|
-
}
|
|
1517
|
-
const channel = device.channels.find((item) => item.id === binding.channel);
|
|
1518
|
-
if (channel.assembly === undefined) {
|
|
1519
|
-
return [];
|
|
1520
|
-
}
|
|
1521
|
-
if (channel.bits === 1) {
|
|
1522
|
-
return [{ address: channel.address, text: key }];
|
|
1523
|
-
}
|
|
1524
|
-
const area = channel.direction === "input" ? inputs : outputs;
|
|
1525
|
-
const bytes = channel.bits / 8;
|
|
1526
|
-
return [
|
|
1527
|
-
{
|
|
1528
|
-
address: `${area.address.slice(0, 3)}${byteOffset(area.address) + channel.offsetBits / 8}`,
|
|
1529
|
-
text: bytes > 1 ? `${key} (${bytes} bytes)` : key
|
|
1530
|
-
}
|
|
1531
|
-
];
|
|
1532
|
-
});
|
|
1533
|
-
const network = config.devices[device.id]?.network;
|
|
1534
|
-
if (device.modbus && !network?.address) {
|
|
1535
|
-
throw new Error(`${device.id}: Modbus registers need its network address.`);
|
|
1536
|
-
}
|
|
1537
|
-
return {
|
|
1538
|
-
name: device.id,
|
|
1539
|
-
eds: normalizeEds(await readFile3(device.descriptor, "utf8"), {
|
|
1540
|
-
connection: connection.id,
|
|
1541
|
-
inputSize: inputs?.size,
|
|
1542
|
-
outputSize: outputs?.size
|
|
1543
|
-
}),
|
|
1544
|
-
...network?.address && { address: network.address },
|
|
1545
|
-
...network?.rpi && {
|
|
1546
|
-
rpiMicroseconds: Number(BigInt(network.rpi.nanoseconds) / 1000n)
|
|
1547
|
-
},
|
|
1548
|
-
...inputs && { inputs },
|
|
1549
|
-
...outputs && { outputs },
|
|
1550
|
-
...device.modbus && { modbus: device.modbus },
|
|
1551
|
-
comments
|
|
1552
|
-
};
|
|
1553
|
-
}));
|
|
1554
|
-
const [first, ...rest] = devices;
|
|
1555
|
-
if (!first) {
|
|
1556
|
-
throw new Error("A generated device tree needs at least one device.");
|
|
1557
|
-
}
|
|
1558
|
-
return { ethernet: settings.ethernet ?? {}, devices: [first, ...rest] };
|
|
1559
|
-
}
|
|
1560
1557
|
// packages/typegen/src/project.ts
|
|
1561
1558
|
import { mkdir, readFile as readFile4, rename, writeFile } from "node:fs/promises";
|
|
1562
1559
|
import { dirname, resolve as resolve2 } from "node:path";
|
|
1563
|
-
import { format } from "prettier";
|
|
1564
1560
|
import {
|
|
1561
|
+
ioSymbolsSchema,
|
|
1565
1562
|
loadTsplcConfig,
|
|
1566
1563
|
publicationSchema,
|
|
1567
1564
|
typegenSchema
|
|
1568
1565
|
} from "@tsplc/config";
|
|
1566
|
+
import {
|
|
1567
|
+
emittedFiles,
|
|
1568
|
+
emittedStDirectory,
|
|
1569
|
+
projectSnapshotFile
|
|
1570
|
+
} from "@tsplc/config/project-files";
|
|
1569
1571
|
import { blockCommentEnd as blockCommentEnd2, lineCommentEnd as lineCommentEnd2 } from "@tsplc/iec/st";
|
|
1570
|
-
|
|
1572
|
+
import { format } from "prettier";
|
|
1571
1573
|
var formatTypeScript = (source) => format(source, { parser: "typescript" });
|
|
1572
|
-
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await readFile4(resolve2(root,
|
|
1574
|
+
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await readFile4(resolve2(root, emittedStDirectory, emittedFiles.publication), "utf8")));
|
|
1575
|
+
var generatedFiles = {
|
|
1576
|
+
hardware: "hardware.ts",
|
|
1577
|
+
contract: "plc.ts",
|
|
1578
|
+
machines: "machines.ts"
|
|
1579
|
+
};
|
|
1580
|
+
async function readIoSymbols(root) {
|
|
1581
|
+
const file = resolve2(root, emittedStDirectory, emittedFiles.ioSymbols);
|
|
1582
|
+
const text = await readFile4(file, "utf8").catch((error) => {
|
|
1583
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
1584
|
+
return "{}";
|
|
1585
|
+
}
|
|
1586
|
+
throw error;
|
|
1587
|
+
});
|
|
1588
|
+
const symbols = ioSymbolsSchema.safeParse(JSON.parse(text));
|
|
1589
|
+
if (!symbols.success) {
|
|
1590
|
+
throw new Error(`Invalid IO symbols in ${file}`, { cause: symbols.error });
|
|
1591
|
+
}
|
|
1592
|
+
return symbols.data;
|
|
1593
|
+
}
|
|
1573
1594
|
async function contractSources(config, root, settings) {
|
|
1574
1595
|
const directories = (setting) => setting ? [setting].flat().map((path) => resolve2(root, path)) : [];
|
|
1575
1596
|
const types = directories(settings.types);
|
|
@@ -1585,7 +1606,7 @@ async function contractSources(config, root, settings) {
|
|
|
1585
1606
|
}
|
|
1586
1607
|
return sources;
|
|
1587
1608
|
}
|
|
1588
|
-
const source = config.compiler ?
|
|
1609
|
+
const source = config.compiler ? emittedStDirectory : config.build?.sourceDir;
|
|
1589
1610
|
if (!source) {
|
|
1590
1611
|
if (config.typegen) {
|
|
1591
1612
|
throw new Error("Configure build.sourceDir or typegen.types/globals to generate PLC types.");
|
|
@@ -1666,16 +1687,16 @@ async function generateProject(file = "tsplc.config.ts", options = {}) {
|
|
|
1666
1687
|
version: 1,
|
|
1667
1688
|
configFile,
|
|
1668
1689
|
config,
|
|
1669
|
-
symbols: config.compiler ?
|
|
1690
|
+
symbols: config.compiler ? await readIoSymbols(root) : Object.fromEntries(Object.keys(io).filter((symbol) => symbol.includes(".")).map((symbol) => [symbol, symbol]))
|
|
1670
1691
|
};
|
|
1671
1692
|
const outputs = [
|
|
1672
1693
|
{
|
|
1673
|
-
path: resolve2(root, settings.dir,
|
|
1694
|
+
path: resolve2(root, settings.dir, generatedFiles.hardware),
|
|
1674
1695
|
content: await formatTypeScript(generateHardwareTypes(catalog, options.targets?.io, options.targets?.publication, settings.ioTypes))
|
|
1675
1696
|
},
|
|
1676
1697
|
{
|
|
1677
|
-
path: resolve2(root,
|
|
1678
|
-
content: JSON.stringify(project, null, 2)
|
|
1698
|
+
path: resolve2(root, projectSnapshotFile),
|
|
1699
|
+
content: `${JSON.stringify(project, null, 2)}
|
|
1679
1700
|
`
|
|
1680
1701
|
}
|
|
1681
1702
|
];
|
|
@@ -1683,7 +1704,7 @@ async function generateProject(file = "tsplc.config.ts", options = {}) {
|
|
|
1683
1704
|
if (sources) {
|
|
1684
1705
|
const inferred = !!config.compiler && !sources.explicit;
|
|
1685
1706
|
outputs.push({
|
|
1686
|
-
path: resolve2(root, settings.dir,
|
|
1707
|
+
path: resolve2(root, settings.dir, generatedFiles.contract),
|
|
1687
1708
|
content: await formatTypeScript(await generateTypes({
|
|
1688
1709
|
types: sources.types,
|
|
1689
1710
|
globals: sources.globals,
|
|
@@ -1697,12 +1718,12 @@ async function generateProject(file = "tsplc.config.ts", options = {}) {
|
|
|
1697
1718
|
checkHmiTargets(config.hmi.controls, (await readPublication(root)).production);
|
|
1698
1719
|
}
|
|
1699
1720
|
if (config.compiler && !options.hardwareOnly) {
|
|
1700
|
-
const machine = await readFile4(resolve2(root,
|
|
1721
|
+
const machine = await readFile4(resolve2(root, emittedStDirectory, emittedFiles.machines), "utf8").catch(() => {
|
|
1701
1722
|
return;
|
|
1702
1723
|
});
|
|
1703
1724
|
if (machine) {
|
|
1704
1725
|
outputs.push({
|
|
1705
|
-
path: resolve2(root, settings.dir,
|
|
1726
|
+
path: resolve2(root, settings.dir, generatedFiles.machines),
|
|
1706
1727
|
content: await formatTypeScript(machine)
|
|
1707
1728
|
});
|
|
1708
1729
|
}
|
|
@@ -1715,6 +1736,7 @@ export {
|
|
|
1715
1736
|
generateHardwareTypes,
|
|
1716
1737
|
generateProject,
|
|
1717
1738
|
generateTypes,
|
|
1739
|
+
generatedFiles,
|
|
1718
1740
|
ioLayout,
|
|
1719
1741
|
ioScalarBits4 as ioScalarBits,
|
|
1720
1742
|
loadHardware,
|
|
@@ -1724,5 +1746,5 @@ export {
|
|
|
1724
1746
|
resolveIo
|
|
1725
1747
|
};
|
|
1726
1748
|
|
|
1727
|
-
//# debugId=
|
|
1749
|
+
//# debugId=A7ED35C107BB6F7A64756E2164756E21
|
|
1728
1750
|
//# sourceMappingURL=index.js.map
|