Skip to content

Commit 8b4f68d

Browse files
authored
Merge pull request #21 from dotnetprojects/copilot/create-typescript-version
Add TypeScript port of TiaCodeGen library
2 parents dca59ba + 28fdedb commit 8b4f68d

69 files changed

Lines changed: 7007 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

TiaCodegen-ts/package-lock.json

Lines changed: 3849 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TiaCodegen-ts/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "tia-codegen-ts",
3+
"version": "1.0.0",
4+
"description": "TypeScript port of TiaCodeGen C# library",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "tsc",
9+
"test": "jest --testPathPattern=tests",
10+
"typecheck": "tsc --noEmit"
11+
},
12+
"devDependencies": {
13+
"@types/jest": "^29.5.12",
14+
"@types/node": "^20.12.7",
15+
"jest": "^29.7.0",
16+
"ts-jest": "^29.1.2",
17+
"typescript": "^5.4.5"
18+
},
19+
"jest": {
20+
"preset": "ts-jest",
21+
"testEnvironment": "node",
22+
"testMatch": ["**/tests/**/*.test.ts"]
23+
}
24+
}

TiaCodegen-ts/src/Blocks/Block.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { CodeBlock } from './CodeBlock';
2+
import { KopCodeHelper } from '../CodeGen/KopCodeHelper';
3+
4+
export class Block {
5+
name: string;
6+
number: number = 0;
7+
private codeBlock: CodeBlock;
8+
title: string | undefined;
9+
titleEnglish: string | undefined;
10+
comment: string | undefined;
11+
commentEnglish: string | undefined;
12+
author: string | undefined;
13+
blockInterface: string;
14+
15+
constructor(name: string, title: string, codeBlock: CodeBlock) {
16+
this.name = name;
17+
this.title = title;
18+
this.codeBlock = codeBlock;
19+
20+
this.blockInterface = `
21+
<Interface>
22+
<Sections xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v2">
23+
<Section Name="Input" />
24+
<Section Name="Output" />
25+
<Section Name="InOut" />
26+
<Section Name="Temp" />
27+
<Section Name="Constant" />
28+
<Section Name="Return">
29+
<Member Name="Ret_Val" Datatype="Void" />
30+
</Section>
31+
</Sections>
32+
</Interface>
33+
`;
34+
}
35+
36+
getCode(): string {
37+
const idRef = { value: 0 };
38+
let code = this.getBlockHeader(idRef);
39+
code += new KopCodeHelper(this.codeBlock).getXml(idRef);
40+
code += this.getBlockFooter(idRef);
41+
return code;
42+
}
43+
44+
getBlockHeader(idRef: { value: number }): string {
45+
const header = `
46+
<SW.Blocks.FC ID="${idRef.value++}">
47+
<AttributeList>
48+
${this.author !== undefined && this.author !== null ? '<HeaderAuthor>' + this.author + '</HeaderAuthor>' : ''}
49+
<HeaderFamily>General</HeaderFamily>
50+
<HeaderVersion>1.0</HeaderVersion>
51+
<MemoryLayout>Optimized</MemoryLayout>
52+
<Name>${this.name}</Name>
53+
${this.number !== 0 ? '<Number>' + this.number + '</Number>' : ''}
54+
<Namespace />
55+
<ProgrammingLanguage>LAD</ProgrammingLanguage>
56+
</AttributeList>
57+
<ObjectList>`;
58+
return header;
59+
}
60+
61+
getBlockFooter(idRef: { value: number }): string {
62+
const footer = `
63+
<MultilingualText ID="${idRef.value++}" CompositionName="Title">
64+
<ObjectList>
65+
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
66+
<AttributeList>
67+
<Culture>de-DE</Culture>
68+
<Text>${this.title}</Text>
69+
</AttributeList>
70+
</MultilingualTextItem>
71+
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
72+
<AttributeList>
73+
<Culture>en-GB</Culture>
74+
<Text>${this.titleEnglish}</Text>
75+
</AttributeList>
76+
</MultilingualTextItem>
77+
</ObjectList>
78+
</MultilingualText>
79+
<MultilingualText ID="${idRef.value++}" CompositionName="Comment">
80+
<ObjectList>
81+
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
82+
<AttributeList>
83+
<Culture>de-DE</Culture>
84+
<Text>${this.comment}</Text>
85+
</AttributeList>
86+
</MultilingualTextItem>
87+
<MultilingualTextItem ID="${idRef.value++}" CompositionName="Items">
88+
<AttributeList>
89+
<Culture>en-GB</Culture>
90+
<Text>${this.commentEnglish}</Text>
91+
</AttributeList>
92+
</MultilingualTextItem>
93+
</ObjectList>
94+
</MultilingualText>
95+
</ObjectList>
96+
</SW.Blocks.FC>`;
97+
return footer;
98+
}
99+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BaseOperationOrSignal } from '../Commands/BaseOperationOrSignal';
2+
3+
export class CodeBlock extends BaseOperationOrSignal {
4+
name: string;
5+
safety: boolean = false;
6+
7+
constructor(name: string = '') {
8+
super();
9+
this.name = name;
10+
}
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { IOperationOrSignal } from '../Interfaces/IOperationOrSignal';
2+
import { BaseOperationOrSignal } from '../Commands/BaseOperationOrSignal';
3+
4+
export class Network extends BaseOperationOrSignal {
5+
networkTitle: string | undefined;
6+
description: string | undefined;
7+
networkTitleEnglish: string | undefined;
8+
descriptionEnglish: string | undefined;
9+
10+
constructor(
11+
networkTitleOrOp?: string | IOperationOrSignal,
12+
networkTitleEnglishOrOp?: string | IOperationOrSignal,
13+
...operationOrSignals: IOperationOrSignal[]
14+
) {
15+
super();
16+
if (typeof networkTitleOrOp === 'string') {
17+
this.networkTitle = networkTitleOrOp;
18+
this.networkTitleEnglish = typeof networkTitleEnglishOrOp === 'string'
19+
? networkTitleEnglishOrOp
20+
: undefined;
21+
if (networkTitleEnglishOrOp !== undefined && typeof networkTitleEnglishOrOp !== 'string') {
22+
this.children.push(networkTitleEnglishOrOp);
23+
}
24+
for (const op of operationOrSignals) {
25+
this.children.push(op);
26+
}
27+
} else {
28+
if (networkTitleOrOp !== undefined) this.children.push(networkTitleOrOp);
29+
if (networkTitleEnglishOrOp !== undefined && typeof networkTitleEnglishOrOp !== 'string') {
30+
this.children.push(networkTitleEnglishOrOp);
31+
}
32+
for (const op of operationOrSignals) {
33+
this.children.push(op);
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)