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