Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
azahnen committed Jan 7, 2025
1 parent a05579e commit 2566256
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 341 deletions.
72 changes: 0 additions & 72 deletions libxtracfg/js/types/generate.ts

This file was deleted.

32 changes: 32 additions & 0 deletions libxtracfg/js/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { resolve } from "path";

import { generate } from "./ts2x/index.ts";
import { onClass, onNamespace, getAdditional } from "./java.ts";

//TODO: restructure -> xtracfg/bin, xtracfg/lib/*, gen in root?

generate({
source: resolve("./src/index.ts"),
verbose: false,
schema: {
basePath: "./src/gen",
},
go: {
basePath: "../../go",
pkg: "gen",
filePrefixes: { Command: "cmd-", Options: "opts-", Result: "res-" },
},
java: {
basePath: "../../java/src/main/java",
pkg: "de.ii.xtraplatform.cli.gen",
classSuffixes: ["Command", "Options", "Result"],
additionalClasses: getAdditional("de.ii.xtraplatform.cli.gen"),
hooks: {
onNamespace,
onClass,
},
},
ts: {
basePath: "./src/gen",
},
});
Original file line number Diff line number Diff line change
@@ -1,72 +1,56 @@
import { Definition, Generator, Result } from "./common/index.ts";
import { generateClass, generateJava } from "./java/index.ts";
import { ClassGenerator, Definition, Generator } from "./ts2x/index.ts";

//TODO: generalize as java commandHandler option

const suffixNs = ["Command", "Options"];
const commandNs = "Command";
const baseResult = "BaseResult";
const baseResult = "Result";
const failureResult = "FailureResult";

export const generateJavaClasses = (
schema: Definition,
pkg: string,
dataNs: string[]
): Result => {
const commands: string[] = [];
let initCommand: string | undefined;
let baseCommand: string | undefined;

const onNs = (
ns: string,
nsInterface?: string,
nsInterfaceDef?: Definition
) => {
if (ns === commandNs) {
baseCommand = nsInterface;
}
};
const commands: string[] = [];
let initCommand: string | undefined;
let baseCommand: string | undefined;

export const onNamespace = (
ns: string,
nsInterface?: string,
nsInterfaceDef?: Definition
) => {
if (ns === commandNs) {
baseCommand = nsInterface;
}
};

const onClass = (ns: string, name: string, def?: any) => {
if (ns === commandNs) {
if (def?.javaContextInit) {
initCommand = name;
} else {
commands.push(name);
}
export const onClass = (ns: string, name: string, def?: any) => {
if (ns === commandNs) {
if (def?.javaContextInit) {
initCommand = name;
} else {
commands.push(name);
}
};

const result = generateJava(
"Java Backend",
schema,
pkg,
dataNs,
suffixNs,
onNs,
onClass
);
}
};

export const getAdditional = (pkg: string) => (): ClassGenerator[] => {
if (!initCommand) {
throw new Error("No init command found");
}
if (!baseCommand) {
throw new Error("No base command found");
}

result.files.push(
generateClass(
"Handler",
return [
{
name: "Handler",
pkg,
generateHandler(
codeOrGenerator: generateHandler(
commands,
initCommand,
baseCommand,
baseResult,
failureResult
)
)
);

return result;
),
},
];
};

const generateHandler =
Expand Down
4 changes: 2 additions & 2 deletions libxtracfg/js/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"private": true,
"scripts": {
"test": "tsx ./test.ts",
"parse": "tsx ./generate.ts"
"parse": "tsx ./index.ts"
},
"devDependencies": {
"@tsconfig/node22": "^22.0.0",
"tsx": "^4.7.2"
"tsx": "4.19.2"
},
"dependencies": {
"ajv": "^8.17.1",
Expand Down
Loading

0 comments on commit 2566256

Please sign in to comment.