@polytric/openws-sdkgen 0.0.2 → 0.0.4

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.
@@ -0,0 +1,147 @@
1
+ interface PipelineContext {
2
+ argv: string[];
3
+ rawInput?: RawInput;
4
+ request?: BuildRequest;
5
+ spec?: OpenWsSpec;
6
+ ir?: IR;
7
+ plan?: PlanStep[];
8
+ }
9
+ interface RawInput {
10
+ spec: string;
11
+ out: string;
12
+ project: string;
13
+ hostRole: string[];
14
+ language: 'csharp' | 'javascript';
15
+ environment: 'unity' | 'node' | 'browser';
16
+ frameworks?: string[];
17
+ }
18
+ interface BuildRequest {
19
+ specPath: string;
20
+ outputPath: string;
21
+ project: string;
22
+ hostRoles: string[];
23
+ target: {
24
+ csharp?: {
25
+ environment: 'unity';
26
+ frameworks?: string[];
27
+ };
28
+ javascript?: {
29
+ environment: 'node' | 'browser';
30
+ frameworks?: string[];
31
+ };
32
+ };
33
+ }
34
+ interface OpenWsSpec {
35
+ name: string;
36
+ description?: string;
37
+ version?: string;
38
+ networks: Record<string, NetworkSpec>;
39
+ }
40
+ interface NetworkSpec {
41
+ name: string;
42
+ description?: string;
43
+ version?: string;
44
+ roles: Record<string, RoleSpec>;
45
+ }
46
+ interface RoleSpec {
47
+ name: string;
48
+ description?: string;
49
+ endpoints?: Endpoint[];
50
+ messages: Record<string, MessageSpec>;
51
+ }
52
+ interface Endpoint {
53
+ scheme: 'ws' | 'wss';
54
+ host: string;
55
+ port: number;
56
+ path: string;
57
+ }
58
+ interface MessageSpec {
59
+ description?: string;
60
+ from?: string[];
61
+ payload: JsonSchema;
62
+ }
63
+ interface JsonSchema {
64
+ type: string;
65
+ description?: string;
66
+ properties?: Record<string, JsonSchema>;
67
+ items?: JsonSchema;
68
+ required?: string[];
69
+ }
70
+ interface IR {
71
+ package: IRPackage;
72
+ networks: IRNetwork[];
73
+ assemblyName?: string;
74
+ }
75
+ interface IRPackage {
76
+ project: string;
77
+ service: string;
78
+ description?: string;
79
+ version?: string;
80
+ }
81
+ interface IRNetwork {
82
+ name: string;
83
+ description?: string;
84
+ version?: string;
85
+ roles: IRRole[];
86
+ handlers: IRHandler[];
87
+ messages: IRMessage[];
88
+ models: IRModel[];
89
+ }
90
+ interface IRRole {
91
+ name: string;
92
+ description?: string;
93
+ isHost: boolean;
94
+ endpoints: Endpoint[];
95
+ }
96
+ interface IRHandler {
97
+ roleName: string;
98
+ handlerName: string;
99
+ description?: string;
100
+ modelClassName?: string;
101
+ messageName?: string;
102
+ methodName?: string;
103
+ }
104
+ interface IRMessage {
105
+ roleName: string;
106
+ handlerName: string;
107
+ description?: string;
108
+ modelClassName?: string;
109
+ messageName?: string;
110
+ methodName?: string;
111
+ }
112
+ interface IRModel {
113
+ scopeName: string;
114
+ modelName: string;
115
+ type: string;
116
+ description?: string;
117
+ properties?: IRProperty[];
118
+ namespace?: string;
119
+ className?: string;
120
+ }
121
+ interface IRProperty {
122
+ type: string;
123
+ scopeName: string;
124
+ modelName: string;
125
+ description?: string;
126
+ required?: boolean;
127
+ items?: {
128
+ type: string;
129
+ scopeName: string;
130
+ modelName: string;
131
+ description?: string;
132
+ };
133
+ propertyName?: string;
134
+ typeName?: string;
135
+ }
136
+ interface PlanStep {
137
+ name: string;
138
+ command: 'copy' | 'render';
139
+ input?: string;
140
+ output: string;
141
+ template?: string;
142
+ getData?: () => unknown;
143
+ }
144
+
145
+ declare function createPlan(ctx: PipelineContext): PipelineContext;
146
+
147
+ export { createPlan as default };
@@ -0,0 +1,147 @@
1
+ interface PipelineContext {
2
+ argv: string[];
3
+ rawInput?: RawInput;
4
+ request?: BuildRequest;
5
+ spec?: OpenWsSpec;
6
+ ir?: IR;
7
+ plan?: PlanStep[];
8
+ }
9
+ interface RawInput {
10
+ spec: string;
11
+ out: string;
12
+ project: string;
13
+ hostRole: string[];
14
+ language: 'csharp' | 'javascript';
15
+ environment: 'unity' | 'node' | 'browser';
16
+ frameworks?: string[];
17
+ }
18
+ interface BuildRequest {
19
+ specPath: string;
20
+ outputPath: string;
21
+ project: string;
22
+ hostRoles: string[];
23
+ target: {
24
+ csharp?: {
25
+ environment: 'unity';
26
+ frameworks?: string[];
27
+ };
28
+ javascript?: {
29
+ environment: 'node' | 'browser';
30
+ frameworks?: string[];
31
+ };
32
+ };
33
+ }
34
+ interface OpenWsSpec {
35
+ name: string;
36
+ description?: string;
37
+ version?: string;
38
+ networks: Record<string, NetworkSpec>;
39
+ }
40
+ interface NetworkSpec {
41
+ name: string;
42
+ description?: string;
43
+ version?: string;
44
+ roles: Record<string, RoleSpec>;
45
+ }
46
+ interface RoleSpec {
47
+ name: string;
48
+ description?: string;
49
+ endpoints?: Endpoint[];
50
+ messages: Record<string, MessageSpec>;
51
+ }
52
+ interface Endpoint {
53
+ scheme: 'ws' | 'wss';
54
+ host: string;
55
+ port: number;
56
+ path: string;
57
+ }
58
+ interface MessageSpec {
59
+ description?: string;
60
+ from?: string[];
61
+ payload: JsonSchema;
62
+ }
63
+ interface JsonSchema {
64
+ type: string;
65
+ description?: string;
66
+ properties?: Record<string, JsonSchema>;
67
+ items?: JsonSchema;
68
+ required?: string[];
69
+ }
70
+ interface IR {
71
+ package: IRPackage;
72
+ networks: IRNetwork[];
73
+ assemblyName?: string;
74
+ }
75
+ interface IRPackage {
76
+ project: string;
77
+ service: string;
78
+ description?: string;
79
+ version?: string;
80
+ }
81
+ interface IRNetwork {
82
+ name: string;
83
+ description?: string;
84
+ version?: string;
85
+ roles: IRRole[];
86
+ handlers: IRHandler[];
87
+ messages: IRMessage[];
88
+ models: IRModel[];
89
+ }
90
+ interface IRRole {
91
+ name: string;
92
+ description?: string;
93
+ isHost: boolean;
94
+ endpoints: Endpoint[];
95
+ }
96
+ interface IRHandler {
97
+ roleName: string;
98
+ handlerName: string;
99
+ description?: string;
100
+ modelClassName?: string;
101
+ messageName?: string;
102
+ methodName?: string;
103
+ }
104
+ interface IRMessage {
105
+ roleName: string;
106
+ handlerName: string;
107
+ description?: string;
108
+ modelClassName?: string;
109
+ messageName?: string;
110
+ methodName?: string;
111
+ }
112
+ interface IRModel {
113
+ scopeName: string;
114
+ modelName: string;
115
+ type: string;
116
+ description?: string;
117
+ properties?: IRProperty[];
118
+ namespace?: string;
119
+ className?: string;
120
+ }
121
+ interface IRProperty {
122
+ type: string;
123
+ scopeName: string;
124
+ modelName: string;
125
+ description?: string;
126
+ required?: boolean;
127
+ items?: {
128
+ type: string;
129
+ scopeName: string;
130
+ modelName: string;
131
+ description?: string;
132
+ };
133
+ propertyName?: string;
134
+ typeName?: string;
135
+ }
136
+ interface PlanStep {
137
+ name: string;
138
+ command: 'copy' | 'render';
139
+ input?: string;
140
+ output: string;
141
+ template?: string;
142
+ getData?: () => unknown;
143
+ }
144
+
145
+ declare function createPlan(ctx: PipelineContext): PipelineContext;
146
+
147
+ export { createPlan as default };
@@ -0,0 +1,192 @@
1
+ // src/plans/dotnet.ts
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+ var __dirname = path.dirname(fileURLToPath(import.meta.url));
5
+ var TEMPLATE_DIR = path.join(__dirname, "../templates/dotnet");
6
+ function pascalCase(str) {
7
+ return str.charAt(0).toUpperCase() + str.slice(1);
8
+ }
9
+ function camelCase(str) {
10
+ return str.charAt(0).toLowerCase() + str.slice(1);
11
+ }
12
+ function createPlan(ctx) {
13
+ const { ir, request } = ctx;
14
+ if (!ir) throw new Error("ir is required");
15
+ if (!request) throw new Error("request is required");
16
+ const assemblyName = `${pascalCase(ir.package.project)}.${pascalCase(ir.package.service)}.Sdk`;
17
+ ir.assemblyName = assemblyName;
18
+ const plan = [
19
+ {
20
+ name: "assembly definition",
21
+ command: "render",
22
+ getData: () => ir,
23
+ template: path.join(TEMPLATE_DIR, "Service.asmdef.ejs"),
24
+ output: path.join(request.outputPath, assemblyName, `${assemblyName}.asmdef`)
25
+ },
26
+ {
27
+ name: "user assembly reference",
28
+ command: "render",
29
+ getData: () => ir,
30
+ template: path.join(TEMPLATE_DIR, "UserService.asmref.ejs"),
31
+ output: path.join(
32
+ request.outputPath,
33
+ `${assemblyName}.User`,
34
+ `${assemblyName}.User.asmref`
35
+ )
36
+ }
37
+ ];
38
+ for (const networkIr of ir.networks) {
39
+ const networkNamespace = `${pascalCase(ir.package.project)}.${pascalCase(ir.package.service)}.${pascalCase(networkIr.name)}`;
40
+ const networkClassName = `${pascalCase(networkIr.name)}Network`;
41
+ const networkOutputPath = path.join(
42
+ request.outputPath,
43
+ assemblyName,
44
+ pascalCase(networkIr.name)
45
+ );
46
+ const userNetworkOutputPath = path.join(
47
+ request.outputPath,
48
+ `${assemblyName}.User`,
49
+ pascalCase(networkIr.name)
50
+ );
51
+ const hostRoles = [];
52
+ const remoteRoles = [];
53
+ for (const role of networkIr.roles) {
54
+ const roleInfo = {
55
+ roleName: role.name,
56
+ className: pascalCase(role.name),
57
+ varName: camelCase(role.name),
58
+ description: role.description || "",
59
+ baseClassName: role.isHost ? "HostRole" : "RemoteRole",
60
+ endpoints: role.endpoints || []
61
+ };
62
+ if (role.isHost) {
63
+ hostRoles.push(roleInfo);
64
+ } else {
65
+ remoteRoles.push(roleInfo);
66
+ }
67
+ }
68
+ const allRoles = [...hostRoles, ...remoteRoles];
69
+ const allModelImports = allRoles.map((role) => `${networkNamespace}.Models.${role.className}`);
70
+ plan.push({
71
+ name: `network ${networkIr.name}`,
72
+ command: "render",
73
+ getData: () => ({
74
+ namespace: networkNamespace,
75
+ networkClassName,
76
+ networkName: networkIr.name,
77
+ description: networkIr.description,
78
+ version: networkIr.version,
79
+ allRoles
80
+ }),
81
+ template: path.join(TEMPLATE_DIR, "Network.cs.ejs"),
82
+ output: path.join(networkOutputPath, `${networkClassName}.cs`)
83
+ });
84
+ for (const modelIr of networkIr.models) {
85
+ modelIr.namespace = `${networkNamespace}.Models.${pascalCase(modelIr.scopeName)}`;
86
+ modelIr.className = pascalCase(modelIr.modelName);
87
+ if (modelIr.properties) {
88
+ for (const propertyIr of modelIr.properties) {
89
+ propertyIr.propertyName = pascalCase(propertyIr.modelName);
90
+ propertyIr.typeName = mapType(propertyIr);
91
+ }
92
+ }
93
+ }
94
+ for (const handlerIr of networkIr.handlers) {
95
+ handlerIr.modelClassName = pascalCase(handlerIr.handlerName);
96
+ handlerIr.messageName = handlerIr.handlerName;
97
+ handlerIr.methodName = pascalCase(handlerIr.handlerName);
98
+ }
99
+ for (const messageIr of networkIr.messages) {
100
+ messageIr.modelClassName = pascalCase(messageIr.handlerName);
101
+ messageIr.messageName = messageIr.handlerName;
102
+ messageIr.methodName = pascalCase(messageIr.handlerName);
103
+ }
104
+ for (const hostRole of hostRoles) {
105
+ const roleHandlers = networkIr.handlers.filter((h) => h.roleName === hostRole.roleName);
106
+ const modelImports = [`${networkNamespace}.Models.${hostRole.className}`];
107
+ plan.push({
108
+ name: `host role ${hostRole.className}`,
109
+ command: "render",
110
+ getData: () => ({
111
+ namespace: `${networkNamespace}.Roles`,
112
+ handlers: roleHandlers,
113
+ remoteRoles,
114
+ modelImports,
115
+ ...hostRole
116
+ }),
117
+ template: path.join(TEMPLATE_DIR, "HostRole.cs.ejs"),
118
+ output: path.join(networkOutputPath, "Roles", `${hostRole.className}.cs`)
119
+ });
120
+ plan.push({
121
+ name: `user host role ${hostRole.className}`,
122
+ command: "render",
123
+ getData: () => ({
124
+ namespace: `${networkNamespace}.Roles`,
125
+ handlers: roleHandlers,
126
+ remoteRoles,
127
+ modelImports: allModelImports,
128
+ ...hostRole
129
+ }),
130
+ template: path.join(TEMPLATE_DIR, "UserHostRole.cs.ejs"),
131
+ output: path.join(userNetworkOutputPath, "Roles", `${hostRole.className}.cs`)
132
+ });
133
+ }
134
+ for (const remoteRole of remoteRoles) {
135
+ const roleMessages = networkIr.messages.filter((m) => m.roleName === remoteRole.roleName);
136
+ const modelImports = [`${networkNamespace}.Models.${remoteRole.className}`];
137
+ plan.push({
138
+ name: `remote role ${remoteRole.className}`,
139
+ command: "render",
140
+ getData: () => ({
141
+ namespace: `${networkNamespace}.Roles`,
142
+ messages: roleMessages,
143
+ modelImports,
144
+ ...remoteRole
145
+ }),
146
+ template: path.join(TEMPLATE_DIR, "RemoteRole.cs.ejs"),
147
+ output: path.join(networkOutputPath, "Roles", `${remoteRole.className}.cs`)
148
+ });
149
+ }
150
+ for (const modelIr of networkIr.models) {
151
+ if (modelIr.type !== "object") continue;
152
+ plan.push({
153
+ name: `model ${modelIr.className}`,
154
+ command: "render",
155
+ getData: () => modelIr,
156
+ template: path.join(TEMPLATE_DIR, "Model.cs.ejs"),
157
+ output: path.join(
158
+ networkOutputPath,
159
+ "Models",
160
+ pascalCase(modelIr.scopeName),
161
+ `${modelIr.className}.cs`
162
+ )
163
+ });
164
+ }
165
+ }
166
+ return {
167
+ ...ctx,
168
+ plan
169
+ };
170
+ }
171
+ function mapType(property) {
172
+ switch (property.type) {
173
+ case "string":
174
+ return "string";
175
+ case "number":
176
+ return "double";
177
+ case "integer":
178
+ return "int";
179
+ case "boolean":
180
+ return "bool";
181
+ case "array":
182
+ if (!property.items) return "List<object>";
183
+ return `List<${mapType(property.items)}>`;
184
+ case "object":
185
+ return pascalCase(property.modelName);
186
+ default:
187
+ return "object";
188
+ }
189
+ }
190
+ export {
191
+ createPlan as default
192
+ };
package/package.json CHANGED
@@ -1,17 +1,13 @@
1
1
  {
2
2
  "name": "@polytric/openws-sdkgen",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "OpenWS SDK generator CLI",
5
- "license": "Apache-2.0",
6
- "author": "Polytric",
7
- "engines": {
8
- "node": ">=18"
9
- },
5
+ "type": "module",
10
6
  "bin": {
11
- "openws-sdkgen": "./src/main.js"
7
+ "openws-sdkgen": "./dist/main.cjs"
12
8
  },
13
9
  "files": [
14
- "src",
10
+ "dist",
15
11
  "LICENSE",
16
12
  "README.md"
17
13
  ],
@@ -23,6 +19,11 @@
23
19
  "unity",
24
20
  "dotnet"
25
21
  ],
22
+ "author": "Polytric",
23
+ "license": "Apache-2.0",
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
26
27
  "repository": {
27
28
  "type": "git",
28
29
  "url": "git+https://github.com/AgeOfLearning/openws.git",
@@ -36,5 +37,17 @@
36
37
  "ejs": "^3.1.10",
37
38
  "yargs": "^18.0.0"
38
39
  },
39
- "scripts": {}
40
+ "devDependencies": {
41
+ "@types/ejs": "^3.1.5",
42
+ "@types/node": "^22.15.29",
43
+ "@types/yargs": "^17.0.33",
44
+ "tsup": "^8.5.1",
45
+ "tsx": "^4.21.0",
46
+ "typescript": "^5.9.3"
47
+ },
48
+ "scripts": {
49
+ "build": "tsup",
50
+ "typecheck": "tsc --noEmit",
51
+ "test:csharp:unity": "node dist/main.cjs --spec ./test/spec.json --out ./generated/dotnet/unity --project Example --hostRole client --language csharp --environment unity"
52
+ }
40
53
  }