@tsplc/typegen 0.2.16 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +798 -544
- package/dist/cli.js.map +15 -9
- package/dist/codegen.js +252 -177
- package/dist/codegen.js.map +5 -5
- package/dist/hardware.js +447 -338
- package/dist/hardware.js.map +10 -5
- package/dist/index.js +848 -602
- package/dist/index.js.map +16 -9
- package/package.json +55 -62
- package/src/cli.ts +68 -51
- package/src/codegen/constants.ts +177 -86
- package/src/codegen/generate.ts +436 -388
- package/src/codegen/parser.ts +358 -287
- package/src/device-tree.ts +152 -140
- package/src/hardware/channels.ts +157 -0
- package/src/hardware/eds.ts +408 -0
- package/src/hardware/mapping-types.ts +88 -0
- package/src/hardware/modbus.ts +105 -0
- package/src/hardware/process-image.ts +102 -0
- package/src/hardware/selection.ts +136 -0
- package/src/hardware.ts +195 -844
- package/src/index.ts +25 -1
- package/src/project.ts +246 -151
package/src/index.ts
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
export { generateTypes } from "./codegen/generate.ts";
|
|
2
|
-
export
|
|
2
|
+
export {
|
|
3
|
+
type HardwareCatalog,
|
|
4
|
+
type SelectedAssembly,
|
|
5
|
+
loadHardware,
|
|
6
|
+
resolveIo,
|
|
7
|
+
} from "./hardware.ts";
|
|
8
|
+
export {
|
|
9
|
+
type EdsAssembly,
|
|
10
|
+
type EdsConnection,
|
|
11
|
+
type EdsMember,
|
|
12
|
+
type EdsParameter,
|
|
13
|
+
type EdsSize,
|
|
14
|
+
type HardwareDescriptor,
|
|
15
|
+
parseEds,
|
|
16
|
+
readEdsRecords,
|
|
17
|
+
} from "./hardware/eds.ts";
|
|
18
|
+
export type { IoChannel } from "./hardware/channels.ts";
|
|
19
|
+
export type { ModbusRegister } from "./hardware/modbus.ts";
|
|
20
|
+
export { ioScalarBits } from "@tsplc/iec";
|
|
21
|
+
export { ioLayout } from "./hardware/process-image.ts";
|
|
22
|
+
export {
|
|
23
|
+
type IoTarget,
|
|
24
|
+
type ProjectTargets,
|
|
25
|
+
generateHardwareTypes,
|
|
26
|
+
} from "./hardware/mapping-types.ts";
|
|
3
27
|
export { deviceTree, normalizeEds } from "./device-tree.ts";
|
|
4
28
|
|
|
5
29
|
export { generateProject } from "./project.ts";
|
package/src/project.ts
CHANGED
|
@@ -2,167 +2,262 @@ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, resolve } from "node:path";
|
|
3
3
|
import { format } from "prettier";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
loadTsplcConfig,
|
|
6
|
+
publicationSchema,
|
|
7
|
+
typegenSchema,
|
|
8
|
+
type ResolvedTsplcConfig,
|
|
9
9
|
} from "@tsplc/config";
|
|
10
|
+
import { blockCommentEnd, lineCommentEnd } from "@tsplc/iec/st";
|
|
10
11
|
import { generateTypes, stFiles } from "./codegen/generate.ts";
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
generateHardwareTypes,
|
|
14
|
+
type ProjectTargets,
|
|
15
|
+
loadHardware,
|
|
16
|
+
resolveIo,
|
|
17
|
+
} from "./hardware.ts";
|
|
18
|
+
|
|
19
|
+
/** Where the compiler emits ST, with its IO symbol paths, publication and machine interfaces. */
|
|
20
|
+
const emittedSt = ".tsplc/artifacts/st";
|
|
21
|
+
|
|
22
|
+
// Prettier's format() never reads a Prettier config, so generated files keep Prettier's
|
|
23
|
+
// defaults in whichever repository typegen runs.
|
|
24
|
+
const formatTypeScript = (source: string) =>
|
|
25
|
+
format(source, { parser: "typescript" });
|
|
26
|
+
|
|
27
|
+
const readPublication = async (root: string) =>
|
|
28
|
+
publicationSchema.parse(
|
|
29
|
+
JSON.parse(
|
|
30
|
+
await readFile(resolve(root, emittedSt, "publication.json"), "utf8"),
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
interface ContractSources {
|
|
35
|
+
types: string[];
|
|
36
|
+
globals: string[];
|
|
37
|
+
/** Taken from typegen directories rather than inferred from emitted or build sources. */
|
|
38
|
+
explicit: boolean;
|
|
39
|
+
}
|
|
12
40
|
|
|
13
41
|
/**
|
|
14
42
|
* Explicit typegen directories take every .st file. Otherwise TYPE and VAR_GLOBAL files are
|
|
15
43
|
* picked from the compiler's emitted ST or the raw build sources.
|
|
16
44
|
*/
|
|
17
45
|
async function contractSources(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
): Promise<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
config: ResolvedTsplcConfig,
|
|
47
|
+
root: string,
|
|
48
|
+
settings: { types?: string | string[]; globals?: string | string[] },
|
|
49
|
+
): Promise<ContractSources | undefined> {
|
|
50
|
+
const directories = (setting: string | string[] | undefined) =>
|
|
51
|
+
setting ? [setting].flat().map((path) => resolve(root, path)) : [];
|
|
52
|
+
const types = directories(settings.types);
|
|
53
|
+
const globals = directories(settings.globals);
|
|
54
|
+
if (types.length || globals.length) {
|
|
55
|
+
const sources = {
|
|
56
|
+
types: await filesIn(types),
|
|
57
|
+
globals: await filesIn(globals),
|
|
58
|
+
explicit: true,
|
|
59
|
+
};
|
|
60
|
+
if (!sources.types.length && !sources.globals.length) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
"no .st declarations found in the configured typegen directories",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
return sources;
|
|
66
|
+
}
|
|
67
|
+
const source = config.compiler ? emittedSt : config.build?.sourceDir;
|
|
68
|
+
if (!source) {
|
|
69
|
+
if (config.typegen) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Configure build.sourceDir or typegen.types/globals to generate PLC types.",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const sourceRoot = resolve(root, source);
|
|
77
|
+
const files = await stFiles(sourceRoot);
|
|
78
|
+
if (!files.length) {
|
|
79
|
+
throw new Error(`no .st declarations found in ${sourceRoot}`);
|
|
80
|
+
}
|
|
81
|
+
const sources: ContractSources = { types: [], globals: [], explicit: false };
|
|
82
|
+
for (const file of files) {
|
|
83
|
+
const text = await readFile(file, "utf8");
|
|
84
|
+
const code = text.slice(codeStart(text, 0));
|
|
85
|
+
if (/^TYPE\b/i.test(code)) {
|
|
86
|
+
sources.types.push(file);
|
|
87
|
+
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
88
|
+
sources.globals.push(file);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return sources;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Where ST code begins at or after `offset`: past whitespace, comments and pragmas. */
|
|
95
|
+
function codeStart(text: string, offset: number): number {
|
|
96
|
+
const start = offset + text.slice(offset).search(/\S|$/);
|
|
97
|
+
if (text.startsWith("(*", start)) {
|
|
98
|
+
const end = blockCommentEnd(text, start);
|
|
99
|
+
return end === null ? start : codeStart(text, end);
|
|
100
|
+
}
|
|
101
|
+
if (text.startsWith("//", start)) {
|
|
102
|
+
return codeStart(text, lineCommentEnd(text, start));
|
|
103
|
+
}
|
|
104
|
+
if (text.startsWith("{", start)) {
|
|
105
|
+
const end = text.indexOf("}", start);
|
|
106
|
+
return end < 0 ? start : codeStart(text, end + 1);
|
|
107
|
+
}
|
|
108
|
+
return start;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Every .st file under each directory, in directory order. */
|
|
112
|
+
async function filesIn(directories: string[]): Promise<string[]> {
|
|
113
|
+
const files: string[] = [];
|
|
114
|
+
for (const directory of directories) {
|
|
115
|
+
files.push(...(await stFiles(directory)));
|
|
116
|
+
}
|
|
117
|
+
return files;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** An HMI control may target only a published symbol or a field or element under one. */
|
|
121
|
+
function checkHmiTargets(
|
|
122
|
+
controls: readonly { target: string }[],
|
|
123
|
+
published: readonly string[],
|
|
124
|
+
) {
|
|
125
|
+
for (const { target } of controls) {
|
|
126
|
+
if (
|
|
127
|
+
!published.some(
|
|
128
|
+
(symbol) =>
|
|
129
|
+
target === symbol ||
|
|
130
|
+
target.startsWith(`${symbol}.`) ||
|
|
131
|
+
target.startsWith(`${symbol}[`),
|
|
132
|
+
)
|
|
133
|
+
) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`HMI control ${target} is not in the production publication.`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface Output {
|
|
142
|
+
path: string;
|
|
143
|
+
content: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Writes each output through a temporary file, so a reader never sees half of one. */
|
|
147
|
+
async function writeOutputs(outputs: Output[]) {
|
|
148
|
+
for (const output of outputs) {
|
|
149
|
+
await mkdir(dirname(output.path), { recursive: true });
|
|
150
|
+
const temp = `${output.path}.${process.pid}.tmp`;
|
|
151
|
+
await writeFile(temp, output.content);
|
|
152
|
+
await rename(temp, output.path);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function checkOutputs(outputs: Output[]) {
|
|
157
|
+
for (const output of outputs) {
|
|
158
|
+
const actual = await readFile(output.path, "utf8").catch(() => undefined);
|
|
159
|
+
// Consumers may format generated TS with their own formatter. Compare the
|
|
160
|
+
// same normalized syntax while preserving changes to values and comments.
|
|
161
|
+
const normalized =
|
|
162
|
+
actual !== undefined && /\.ts$/i.test(output.path)
|
|
163
|
+
? await formatTypeScript(actual)
|
|
164
|
+
: actual;
|
|
165
|
+
if (normalized !== output.content) {
|
|
166
|
+
throw new Error(`Generated file is missing or stale: ${output.path}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
52
169
|
}
|
|
53
170
|
|
|
54
171
|
export async function generateProject(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
172
|
+
file = "tsplc.config.ts",
|
|
173
|
+
options: {
|
|
174
|
+
check?: boolean;
|
|
175
|
+
hardwareOnly?: boolean;
|
|
176
|
+
/** Source-derived I/O keys and publication selectors, independent of the active target. */
|
|
177
|
+
targets?: ProjectTargets;
|
|
178
|
+
loaded?: Awaited<ReturnType<typeof loadTsplcConfig>>;
|
|
179
|
+
} = {},
|
|
63
180
|
) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
content: await format(machine, { parser: "typescript" }),
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
for (const output of outputs) {
|
|
150
|
-
if (options.check) {
|
|
151
|
-
const actual = await readFile(output.path, "utf8").catch(() => undefined);
|
|
152
|
-
// Consumers may format generated TS with their own formatter. Compare the
|
|
153
|
-
// same normalized syntax while preserving changes to values and comments.
|
|
154
|
-
const normalized =
|
|
155
|
-
actual !== undefined && /\.ts$/i.test(output.path)
|
|
156
|
-
? await format(actual, { parser: "typescript" })
|
|
157
|
-
: actual;
|
|
158
|
-
if (normalized !== output.content)
|
|
159
|
-
throw new Error(`Generated file is missing or stale: ${output.path}`);
|
|
160
|
-
} else {
|
|
161
|
-
await mkdir(dirname(output.path), { recursive: true });
|
|
162
|
-
const temp = `${output.path}.${process.pid}.tmp`;
|
|
163
|
-
await writeFile(temp, output.content);
|
|
164
|
-
await rename(temp, output.path);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return { catalog, io, files: outputs.map((o) => o.path) };
|
|
181
|
+
const { config, root, configFile } =
|
|
182
|
+
options.loaded ?? (await loadTsplcConfig(file));
|
|
183
|
+
const settings = typegenSchema.parse(config.typegen ?? {});
|
|
184
|
+
const catalog = await loadHardware(config, root);
|
|
185
|
+
const io = resolveIo(config, catalog);
|
|
186
|
+
const project = {
|
|
187
|
+
version: 1,
|
|
188
|
+
configFile,
|
|
189
|
+
config,
|
|
190
|
+
symbols: config.compiler
|
|
191
|
+
? JSON.parse(
|
|
192
|
+
await readFile(
|
|
193
|
+
resolve(root, emittedSt, "io-symbols.json"),
|
|
194
|
+
"utf8",
|
|
195
|
+
).catch(() => "{}"),
|
|
196
|
+
)
|
|
197
|
+
: Object.fromEntries(
|
|
198
|
+
Object.keys(io)
|
|
199
|
+
.filter((symbol) => symbol.includes("."))
|
|
200
|
+
.map((symbol) => [symbol, symbol]),
|
|
201
|
+
),
|
|
202
|
+
};
|
|
203
|
+
const outputs: Output[] = [
|
|
204
|
+
{
|
|
205
|
+
path: resolve(root, settings.dir, "hardware.ts"),
|
|
206
|
+
content: await formatTypeScript(
|
|
207
|
+
generateHardwareTypes(
|
|
208
|
+
catalog,
|
|
209
|
+
options.targets?.io,
|
|
210
|
+
options.targets?.publication,
|
|
211
|
+
settings.ioTypes,
|
|
212
|
+
),
|
|
213
|
+
),
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
path: resolve(root, ".tsplc/project.json"),
|
|
217
|
+
content: JSON.stringify(project, null, 2) + "\n",
|
|
218
|
+
},
|
|
219
|
+
];
|
|
220
|
+
const sources = options.hardwareOnly
|
|
221
|
+
? undefined
|
|
222
|
+
: await contractSources(config, root, settings);
|
|
223
|
+
if (sources) {
|
|
224
|
+
const inferred = !!config.compiler && !sources.explicit;
|
|
225
|
+
outputs.push({
|
|
226
|
+
path: resolve(root, settings.dir, "plc.ts"),
|
|
227
|
+
content: await formatTypeScript(
|
|
228
|
+
await generateTypes({
|
|
229
|
+
types: sources.types,
|
|
230
|
+
globals: sources.globals,
|
|
231
|
+
publishedGlobalsOnly: inferred,
|
|
232
|
+
declarations: settings.declarations,
|
|
233
|
+
publication: inferred ? await readPublication(root) : undefined,
|
|
234
|
+
}),
|
|
235
|
+
),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
if (
|
|
239
|
+
config.hmi &&
|
|
240
|
+
config.publish &&
|
|
241
|
+
config.compiler &&
|
|
242
|
+
!options.hardwareOnly
|
|
243
|
+
) {
|
|
244
|
+
checkHmiTargets(
|
|
245
|
+
config.hmi.controls,
|
|
246
|
+
(await readPublication(root)).production,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (config.compiler && !options.hardwareOnly) {
|
|
250
|
+
const machine = await readFile(
|
|
251
|
+
resolve(root, emittedSt, "machines.ts"),
|
|
252
|
+
"utf8",
|
|
253
|
+
).catch(() => undefined);
|
|
254
|
+
if (machine) {
|
|
255
|
+
outputs.push({
|
|
256
|
+
path: resolve(root, settings.dir, "machines.ts"),
|
|
257
|
+
content: await formatTypeScript(machine),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
await (options.check ? checkOutputs(outputs) : writeOutputs(outputs));
|
|
262
|
+
return { catalog, io, files: outputs.map((output) => output.path) };
|
|
168
263
|
}
|