Unify lineAppender
This commit is contained in:
parent
7de674ab3c
commit
4b4948207c
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/jrpcgen",
|
"name": "@hibas123/jrpcgen",
|
||||||
"version": "1.1.3",
|
"version": "1.1.4",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"packageManager": "yarn@3.1.1",
|
"packageManager": "yarn@3.1.1",
|
||||||
|
@ -7,8 +7,7 @@ import {
|
|||||||
} from "../ir";
|
} from "../ir";
|
||||||
|
|
||||||
import { CompileTarget } from "../compile";
|
import { CompileTarget } from "../compile";
|
||||||
|
import { LineAppender } from "../utils";
|
||||||
type lineAppender = (ind: number, line: string | string[]) => void;
|
|
||||||
|
|
||||||
const conversion = {
|
const conversion = {
|
||||||
boolean: "bool",
|
boolean: "bool",
|
||||||
@ -52,13 +51,7 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generateType(definition: TypeDefinition): void {
|
generateType(definition: TypeDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `using System.Text.Json;`);
|
a(0, `using System.Text.Json;`);
|
||||||
a(0, `using System.Text.Json.Serialization;`);
|
a(0, `using System.Text.Json.Serialization;`);
|
||||||
@ -91,17 +84,11 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
|
|||||||
}
|
}
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`${definition.name}.cs`, lines.join("\n"));
|
this.writeFile(`${definition.name}.cs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateEnum(definition: EnumDefinition): void {
|
generateEnum(definition: EnumDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `using System.Text.Json;`);
|
a(0, `using System.Text.Json;`);
|
||||||
a(0, `using System.Text.Json.Serialization;`);
|
a(0, `using System.Text.Json.Serialization;`);
|
||||||
@ -114,17 +101,11 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
|
|||||||
}
|
}
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`${definition.name}.cs`, lines.join("\n"));
|
this.writeFile(`${definition.name}.cs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateServiceClient(definition: ServiceDefinition) {
|
generateServiceClient(definition: ServiceDefinition) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `using System;`);
|
a(0, `using System;`);
|
||||||
a(0, `using System.Text.Json;`);
|
a(0, `using System.Text.Json;`);
|
||||||
@ -202,17 +183,11 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
|
|||||||
// a(0, ``);
|
// a(0, ``);
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`${definition.name}Client.cs`, lines.join("\n"));
|
this.writeFile(`${definition.name}Client.cs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateServiceServer(definition: ServiceDefinition) {
|
generateServiceServer(definition: ServiceDefinition) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `using System;`);
|
a(0, `using System;`);
|
||||||
a(0, `using System.Text.Json;`);
|
a(0, `using System.Text.Json;`);
|
||||||
@ -331,7 +306,7 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
|
|||||||
a(1, `}`);
|
a(1, `}`);
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`${definition.name}Server.cs`, lines.join("\n"));
|
this.writeFile(`${definition.name}Server.cs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateService(definition: ServiceDefinition): void {
|
generateService(definition: ServiceDefinition): void {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { CompileTarget } from "../compile";
|
import { CompileTarget } from "../compile";
|
||||||
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step } from "../ir";
|
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step } from "../ir";
|
||||||
|
import { lineAppender, LineAppender } from "../utils";
|
||||||
type lineAppender = (ind: number, line: string | string[]) => void;
|
|
||||||
|
|
||||||
const conversion = {
|
const conversion = {
|
||||||
boolean: "bool",
|
boolean: "bool",
|
||||||
@ -62,13 +61,8 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generateType(definition: TypeDefinition): void {
|
generateType(definition: TypeDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
if (definition.fields.find((e) => e.map))
|
if (definition.fields.find((e) => e.map))
|
||||||
a(0, `use std::collections::hash_map::HashMap;`);
|
a(0, `use std::collections::hash_map::HashMap;`);
|
||||||
a(0, `use serde::{Deserialize, Serialize};`);
|
a(0, `use serde::{Deserialize, Serialize};`);
|
||||||
@ -92,17 +86,11 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
}
|
}
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`src/${toSnake(definition.name)}.rs`, lines.join("\n"));
|
this.writeFile(`src/${toSnake(definition.name)}.rs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateEnum(definition: EnumDefinition): void {
|
generateEnum(definition: EnumDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `use int_enum::IntEnum;`);
|
a(0, `use int_enum::IntEnum;`);
|
||||||
a(0, `use serde::{Deserialize, Serialize};`);
|
a(0, `use serde::{Deserialize, Serialize};`);
|
||||||
@ -121,17 +109,11 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
}
|
}
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFile(`src/${toSnake(definition.name)}.rs`, lines.join("\n"));
|
this.writeFile(`src/${toSnake(definition.name)}.rs`, getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
private generateServiceClient(definition: ServiceDefinition): void {
|
private generateServiceClient(definition: ServiceDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
const typeToRust = (type: string, array: boolean) => {
|
const typeToRust = (type: string, array: boolean) => {
|
||||||
let rt = toRustType(type);
|
let rt = toRustType(type);
|
||||||
@ -191,20 +173,11 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
a(0, ``);
|
a(0, ``);
|
||||||
|
|
||||||
this.writeFile(
|
this.writeFile(`src/client/${toSnake(definition.name)}.rs`, getResult());
|
||||||
`src/client/${toSnake(definition.name)}.rs`,
|
|
||||||
lines.join("\n")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private generateServiceServer(definition: ServiceDefinition): void {
|
private generateServiceServer(definition: ServiceDefinition): void {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
this.addDependencies(a, definition);
|
this.addDependencies(a, definition);
|
||||||
a(0, `use crate::base_lib::{JRPCServiceHandler,JRPCRequest,Result};`);
|
a(0, `use crate::base_lib::{JRPCServiceHandler,JRPCRequest,Result};`);
|
||||||
@ -301,10 +274,7 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
a(2, `}`);
|
a(2, `}`);
|
||||||
a(1, `}`);
|
a(1, `}`);
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
this.writeFile(
|
this.writeFile(`src/server/${toSnake(definition.name)}.rs`, getResult());
|
||||||
`src/server/${toSnake(definition.name)}.rs`,
|
|
||||||
lines.join("\n")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateService(definition: ServiceDefinition): void {
|
generateService(definition: ServiceDefinition): void {
|
||||||
@ -317,29 +287,13 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private generateLib(steps: Step[]) {
|
private generateLib(steps: Step[]) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
let linesServer: string[] = [];
|
const lc = LineAppender();
|
||||||
const as: lineAppender = (i, t) => {
|
const ls = LineAppender();
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => linesServer.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
let linesClient: string[] = [];
|
const ac = lc.a;
|
||||||
const ac: lineAppender = (i, t) => {
|
const as = ls.a;
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => linesClient.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
a(0, `pub mod base_lib;`);
|
a(0, `pub mod base_lib;`);
|
||||||
a(0, `pub use base_lib::{JRPCServer,Result};`);
|
a(0, `pub use base_lib::{JRPCServer,Result};`);
|
||||||
@ -365,9 +319,9 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.writeFile(`src/lib.rs`, lines.join("\n"));
|
this.writeFile(`src/lib.rs`, getResult());
|
||||||
this.writeFile(`src/server/mod.rs`, linesServer.join("\n"));
|
this.writeFile(`src/server/mod.rs`, ls.getResult());
|
||||||
this.writeFile(`src/client/mod.rs`, linesClient.join("\n"));
|
this.writeFile(`src/client/mod.rs`, lc.getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
finalize(steps: Step[]): void {
|
finalize(steps: Step[]): void {
|
||||||
|
@ -7,10 +7,9 @@ import {
|
|||||||
} from "../ir";
|
} from "../ir";
|
||||||
|
|
||||||
import { CompileTarget } from "../compile";
|
import { CompileTarget } from "../compile";
|
||||||
|
import { LineAppender, lineAppender } from "../utils";
|
||||||
// import * as prettier from "prettier";
|
// import * as prettier from "prettier";
|
||||||
|
|
||||||
type lineAppender = (ind: number, line: string | string[]) => void;
|
|
||||||
|
|
||||||
const conversion = {
|
const conversion = {
|
||||||
boolean: "boolean",
|
boolean: "boolean",
|
||||||
int: "number",
|
int: "number",
|
||||||
@ -73,13 +72,7 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generateType(def: TypeDefinition) {
|
generateType(def: TypeDefinition) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
this.generateImports(a, def);
|
this.generateImports(a, def);
|
||||||
a(0, `export default class ${def.name} {`);
|
a(0, `export default class ${def.name} {`);
|
||||||
@ -168,17 +161,12 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
|
|
||||||
a(0, ``);
|
a(0, ``);
|
||||||
|
|
||||||
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
|
this.writeFormattedFile(this.getFileName(def.name), getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateEnum(def: EnumDefinition) {
|
generateEnum(def: EnumDefinition) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
a(0, this.generateImport("{ VerificationError }", "./ts_base"));
|
a(0, this.generateImport("{ VerificationError }", "./ts_base"));
|
||||||
|
|
||||||
a(0, `enum ${def.name} {`);
|
a(0, `enum ${def.name} {`);
|
||||||
@ -205,7 +193,7 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
a(1, `return data;`);
|
a(1, `return data;`);
|
||||||
a(0, `}`);
|
a(0, `}`);
|
||||||
|
|
||||||
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
|
this.writeFormattedFile(this.getFileName(def.name), getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
generateServiceClient(def: ServiceDefinition) {
|
generateServiceClient(def: ServiceDefinition) {
|
||||||
@ -220,13 +208,7 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
this.getTemplate("ts_service_client.ts")
|
this.getTemplate("ts_service_client.ts")
|
||||||
);
|
);
|
||||||
|
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
this.generateImports(a, def);
|
this.generateImports(a, def);
|
||||||
|
|
||||||
@ -305,18 +287,12 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
|
|
||||||
this.writeFormattedFile(
|
this.writeFormattedFile(
|
||||||
this.getFileName(def.name + "_client"),
|
this.getFileName(def.name + "_client"),
|
||||||
lines.join("\n")
|
getResult()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
generateServiceServer(def: ServiceDefinition) {
|
generateServiceServer(def: ServiceDefinition) {
|
||||||
let lines: string[] = [];
|
const { a, getResult } = LineAppender();
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
this.writeFormattedFile(
|
this.writeFormattedFile(
|
||||||
"service_server.ts",
|
"service_server.ts",
|
||||||
@ -420,7 +396,7 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
|
|
||||||
this.writeFormattedFile(
|
this.writeFormattedFile(
|
||||||
this.getFileName(def.name + "_server"),
|
this.getFileName(def.name + "_server"),
|
||||||
lines.join("\n")
|
getResult()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,30 +410,13 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
finalize(steps: Step[]) {
|
finalize(steps: Step[]) {
|
||||||
let linesClient: string[] = [];
|
const lc = LineAppender();
|
||||||
let linesServer: string[] = [];
|
const ls = LineAppender();
|
||||||
|
|
||||||
const ac: lineAppender = (i, t) => {
|
const ac = lc.a;
|
||||||
if (!Array.isArray(t)) {
|
const as = ls.a;
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => linesClient.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
const as: lineAppender = (i, t) => {
|
const { a, getResult } = LineAppender();
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => linesServer.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
let lines: string[] = [];
|
|
||||||
const a: lineAppender = (i, t) => {
|
|
||||||
if (!Array.isArray(t)) {
|
|
||||||
t = [t];
|
|
||||||
}
|
|
||||||
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
|
|
||||||
};
|
|
||||||
|
|
||||||
let hasService = false;
|
let hasService = false;
|
||||||
steps.forEach(([type, def]) => {
|
steps.forEach(([type, def]) => {
|
||||||
@ -517,17 +476,9 @@ export class TypescriptTarget extends CompileTarget {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.writeFormattedFile(this.getFileName("index"), lines.join("\n"));
|
this.writeFormattedFile(this.getFileName("index"), getResult());
|
||||||
|
this.writeFormattedFile(this.getFileName("index_client"), lc.getResult());
|
||||||
this.writeFormattedFile(
|
this.writeFormattedFile(this.getFileName("index_server"), ls.getResult());
|
||||||
this.getFileName("index_client"),
|
|
||||||
linesClient.join("\n")
|
|
||||||
);
|
|
||||||
|
|
||||||
this.writeFormattedFile(
|
|
||||||
this.getFileName("index_server"),
|
|
||||||
linesServer.join("\n")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user