forked from abaplint/vscode-abap-artifacts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
314 lines (272 loc) · 9.32 KB
/
create.ts
File metadata and controls
314 lines (272 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import * as vscode from "vscode";
import { Utils } from 'vscode-uri';
import { schemas } from "./aff_schemas";
import { JSONSchemaFaker } from "json-schema-faker";
import { affExtras } from "./aff_extras";
async function createFile(path: string, filename: string, content: string) {
const uri = vscode.Uri.joinPath(vscode.Uri.parse(path), filename);
if (await fileExists(uri)) {
vscode.window.showErrorMessage("File already exists!");
return;
}
const uintarray = new TextEncoder().encode(content);
await vscode.workspace.fs.writeFile(uri, uintarray);
await vscode.window.showTextDocument(uri, { preview: false });
}
async function findFolder(uri: vscode.Uri): Promise<string> {
const stat = await vscode.workspace.fs.stat(uri);
if (stat.type === vscode.FileType.File) {
const base = Utils.dirname(uri).path;
return base;
} else {
return uri.path;
}
}
async function fileExists(uri: vscode.Uri): Promise<boolean> {
if (!uri) {
return false;
}
try {
await vscode.workspace.fs.stat(uri);
return true;
} catch {
return false;
}
}
export async function createArtifact(uri: vscode.Uri) {
if (uri === undefined) {
vscode.window.showErrorMessage('Right click folder in explorer view', { modal: true });
return;
}
const foo: { [name: string]: (uri: vscode.Uri) => Promise<void> } = {
"CLAS - Class (abapGit)": createCLAS,
"INTF - Interface (abapGit)": createINTF,
"PROG - Program (abapGit)": createPROG,
"FUGR - Function Group (abapGit)": createFUGR,
};
for (const key of Object.keys(schemas)) {
const schema = schemas[key];
const [name, version] = key.split("-");
foo[name.toUpperCase() + " - " + version + " - " + schema.title + " (AFF)"] = createAff(key);
}
const type = await vscode.window.showQuickPick(Object.keys(foo));
if (type === undefined || type === "") {
return;
}
const fun = foo[type];
if (fun) {
await fun(uri);
}
}
function createAff(key: string) {
const ret = async (uri: vscode.Uri) => {
let name = await vscode.window.showInputBox({ placeHolder: "name" });
if (name === undefined || name === "") {
return;
}
// change namespace,
name = name.trim().replace("/", "(");
name = name.replace("/", ")");
const dir = await findFolder(uri);
const sample = JSONSchemaFaker.generate(schemas[key]) as any;
if (sample?.header?.originalLanguage) {
sample.header.originalLanguage = vscode.workspace.getConfiguration("abap-artifacts").get("defaultLanguage", "en");
}
if (sample?.header?.description) {
sample.header.description = name;
}
if (key === "clas" && vscode.workspace.getConfiguration("abap-artifacts").get("clasNoDescriptions", true)) {
delete sample.descriptions;
}
if (key === "clas") {
sample.fixPointArithmetic = vscode.workspace.getConfiguration("abap-artifacts").get("clasFixPointArithmetic", true);
}
const json = JSON.stringify(sample, null, 2) + "\n";
await createFile(dir, `${name}.${key}.json`, json);
for (const extra of affExtras[key] || []) {
await createFile(dir, `${name}.${key}.${extra.extension}`, extra.contents(name));
}
};
return ret;
}
async function createCLAS(uri: vscode.Uri) {
const name = await vscode.window.showInputBox({ placeHolder: "cl_name" });
if (name === undefined || name === "") {
return;
}
const dir = await findFolder(uri);
const filename = name.replace(/\//g, "#").toLowerCase() + ".clas";
const uriXML = filename + ".xml";
const dataXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>${name.toUpperCase()}</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>${name.toUpperCase()}</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, uriXML, dataXML);
const uriABAP = filename + ".abap";
const dataABAP = `CLASS ${name.toLowerCase()} DEFINITION PUBLIC.
PUBLIC SECTION.
ENDCLASS.
CLASS ${name.toLowerCase()} IMPLEMENTATION.
ENDCLASS.`;
await createFile(dir, uriABAP, dataABAP);
const createTestClass = await vscode.window.showQuickPick(
[{ label: "yes" },
{ label: "no" }],
{ placeHolder: "Add test class include?" }
);
if (createTestClass?.label === "yes") {
const uriTestIncl = filename + ".testclasses" + ".abap";
const dataTestIncl = `*"* use this source file for your ABAP unit test classes`;
await createFile(dir, uriTestIncl, dataTestIncl);
}
}
async function createINTF(uri: vscode.Uri) {
const name = await vscode.window.showInputBox({ placeHolder: "if_name" });
if (name === undefined || name === "") {
return;
}
const dir = await findFolder(uri);
const filename = name.replace(/\//g, "#").toLowerCase() + ".intf";
const uriXML = filename + ".xml";
const dataXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_INTF" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOINTERF>
<CLSNAME>${name.toUpperCase()}</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>${name.toUpperCase()}</DESCRIPT>
<EXPOSURE>2</EXPOSURE>
<STATE>1</STATE>
<UNICODE>X</UNICODE>
</VSEOINTERF>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, uriXML, dataXML);
const uriABAP = filename + ".abap";
const dataABAP = `INTERFACE ${name.toLowerCase()} PUBLIC.
ENDINTERFACE.`;
await createFile(dir, uriABAP, dataABAP);
}
async function createPROG(uri: vscode.Uri) {
const name = await vscode.window.showInputBox({ placeHolder: "zreport" });
if (name === undefined || name === "") {
return;
}
const dir = await findFolder(uri);
const filename = name.toLowerCase() + ".prog";
const uriXML = filename + ".xml";
const dataXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<PROGDIR>
<NAME>${name.toUpperCase()}</NAME>
<DBAPL>S</DBAPL>
<SUBC>1</SUBC>
<FIXPT>X</FIXPT>
<UCCHECK>X</UCCHECK>
</PROGDIR>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, uriXML, dataXML);
const uriABAP = filename + ".abap";
const dataABAP = `REPORT ${name.toLowerCase()}.\n\n`;
await createFile(dir, uriABAP, dataABAP);
}
async function createFUGR(uri: vscode.Uri) {
const groupName = await vscode.window.showInputBox({ placeHolder: "z_fugr" });
if (groupName === undefined || groupName === "") {
return;
}
const moduleName = await vscode.window.showInputBox({ placeHolder: "zfunction_module" });
if (moduleName === undefined || moduleName === "") {
return;
}
const dir = await findFolder(uri);
const filename = groupName.toLowerCase() + ".fugr";
const dataXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_FUGR" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<AREAT>test</AREAT>
<INCLUDES>
<SOBJ_NAME>L${groupName.toUpperCase()}TOP</SOBJ_NAME>
<SOBJ_NAME>SAPL${groupName.toUpperCase()}</SOBJ_NAME>
</INCLUDES>
<FUNCTIONS>
<item>
<FUNCNAME>${moduleName.toUpperCase()}</FUNCNAME>
<SHORT_TEXT>test</SHORT_TEXT>
</item>
</FUNCTIONS>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, filename + ".xml", dataXML);
const mainXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<PROGDIR>
<NAME>SAPL${groupName.toUpperCase()}</NAME>
<DBAPL>S</DBAPL>
<DBNA>D$</DBNA>
<SUBC>F</SUBC>
<APPL>S</APPL>
<RLOAD>E</RLOAD>
<FIXPT>X</FIXPT>
<LDBNAME>D$S</LDBNAME>
<UCCHECK>X</UCCHECK>
</PROGDIR>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, filename + ".sapl" + groupName.toLowerCase() + ".xml", mainXML);
const mainABAP = `
INCLUDE L${groupName.toUpperCase()}TOP.
INCLUDE L${groupName.toUpperCase()}UXX.
`;
await createFile(dir, filename + ".sapl" + groupName.toLowerCase() + ".abap", mainABAP);
const topXML = `<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<PROGDIR>
<NAME>L${groupName.toUpperCase()}TOP</NAME>
<DBAPL>S</DBAPL>
<DBNA>D$</DBNA>
<SUBC>I</SUBC>
<APPL>S</APPL>
<FIXPT>X</FIXPT>
<LDBNAME>D$S</LDBNAME>
<UCCHECK>X</UCCHECK>
</PROGDIR>
</asx:values>
</asx:abap>
</abapGit>`;
await createFile(dir, filename + ".l" + groupName.toLowerCase() + "top.xml", topXML);
const topABAP = `FUNCTION-POOL ${groupName.toLowerCase()}.\n\n`;
await createFile(dir, filename + ".l" + groupName.toLowerCase() + "top.abap", topABAP);
const fmABAP = `FUNCTION ${moduleName.toLowerCase()}.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"----------------------------------------------------------------------
WRITE / 'Hello world'.
ENDFUNCTION.`;
await createFile(dir, filename + "." + moduleName.toLowerCase().replaceAll("/", "#") + ".abap", fmABAP);
}