Skip to content

Commit fa24103

Browse files
authored
auto generate schemas, proof of concept (#428)
1 parent 05bbe31 commit fa24103

File tree

9 files changed

+507
-1
lines changed

9 files changed

+507
-1
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
root = true
55

66
# ensure the LF line endings
7-
[*.{abap,md,properties,acds,json}]
7+
[*.{abap,md,properties,acds,json,mjs,gitignore}]
88
charset = utf-8
99
end_of_line = lf
1010
insert_final_newline = true

.github/workflows/generate.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Generate Schemas
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
generate:
8+
runs-on: ubuntu-latest
9+
timeout-minutes: 10
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v3
13+
- name: Run
14+
run: |
15+
cd generate
16+
npm ci
17+
npm test

generate/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
downport
3+
output
4+
generated

generate/abap_transpile.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"input_folder": "downport",
3+
"input_filter": [
4+
"intf",
5+
"dtel",
6+
"clas"
7+
],
8+
"output_folder": "output",
9+
"libs": [
10+
{
11+
"url": "https://github.com/open-abap/open-abap-core"
12+
}
13+
],
14+
"write_unit_tests": true,
15+
"write_source_map": true,
16+
"options": {
17+
"ignoreSyntaxCheck": false,
18+
"addFilenames": true,
19+
"addCommonJS": true,
20+
"unknownTypes": "runtimeError",
21+
"extraSetup": "../setup.mjs",
22+
"skip": []
23+
}
24+
}

generate/aff.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as fs from 'node:fs';
2+
import * as path from 'node:path';
3+
import * as child_process from 'node:child_process';
4+
import {initializeABAP} from "./output/init.mjs";
5+
await initializeABAP();
6+
7+
async function run() {
8+
if (fs.existsSync("generated") === false) {
9+
fs.mkdirSync("generated");
10+
}
11+
12+
const types = [];
13+
for (const f of fs.readdirSync("../file-formats/")) {
14+
if (f.length === 4) {
15+
types.push(f.toUpperCase());
16+
}
17+
}
18+
19+
for (const type of types) {
20+
console.log(type);
21+
if (type === "ENHO") {
22+
console.log("\tskip, https://github.com/SAP/abap-file-formats/issues/409");
23+
continue;
24+
}
25+
26+
const result = await abap.Classes["CL_RUN"].run({object_type: new abap.types.String().set(type)});
27+
const filename = "generated" + path.sep + type.toLowerCase() + "-v1.json";
28+
fs.writeFileSync(filename, result.get());
29+
30+
const command = `diff --strip-trailing-cr generated/${type.toLowerCase()}-v1.json ../file-formats/${type.toLowerCase()}/${type.toLowerCase()}-v1.json`;
31+
const output = child_process.execSync(`${command} || true`);
32+
if (output.toString().length > 0) {
33+
console.log(command);
34+
console.log(output.toString());
35+
} else {
36+
console.log("\tOK\n");
37+
}
38+
}
39+
40+
// only run for INTF,
41+
/*
42+
const result = await abap.Classes["CL_RUN"].run({object_type: new abap.types.String().set("INTF")});
43+
fs.writeFileSync("generated" + path.sep + "intf-v1.json", result.get());
44+
*/
45+
}
46+
47+
run();

generate/cl_run.clas.abap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
CLASS cl_run DEFINITION PUBLIC FINAL CREATE PUBLIC.
2+
PUBLIC SECTION.
3+
CLASS-METHODS run
4+
IMPORTING
5+
object_type TYPE string
6+
RETURNING
7+
VALUE(result) TYPE string.
8+
ENDCLASS.
9+
10+
CLASS cl_run IMPLEMENTATION.
11+
12+
METHOD run.
13+
DATA writer TYPE REF TO zcl_aff_writer_json_schema.
14+
DATA generator TYPE REF TO zcl_aff_generator.
15+
DATA string_tab TYPE string_table.
16+
DATA type_name TYPE string.
17+
DATA schema_id TYPE string.
18+
DATA ref TYPE REF TO data.
19+
FIELD-SYMBOLS <row> LIKE LINE OF string_tab.
20+
21+
schema_id = |https://github.com/SAP/abap-file-formats/blob/main/file-formats/{ to_lower( object_type ) }/{ to_lower( object_type ) }-v1.json|.
22+
type_name = to_upper( |ZIF_AFF_{ object_type }_V1=>TY_MAIN| ).
23+
24+
CREATE DATA ref TYPE (type_name).
25+
26+
CREATE OBJECT writer
27+
EXPORTING
28+
schema_id = schema_id.
29+
30+
CREATE OBJECT generator
31+
EXPORTING
32+
writer = writer.
33+
34+
string_tab = generator->generate_type( ref->* ).
35+
36+
* workaround for transpiler/JS keywords
37+
LOOP AT string_tab ASSIGNING <row>.
38+
IF condense( <row> ) CP '"interface_"*'.
39+
REPLACE FIRST OCCURRENCE OF '"interface_"' IN <row> WITH '"interface"'.
40+
ENDIF.
41+
ENDLOOP.
42+
43+
CONCATENATE LINES OF string_tab INTO result SEPARATED BY |\n|.
44+
ENDMETHOD.
45+
46+
ENDCLASS.

0 commit comments

Comments
 (0)