forked from SAP/ui5-language-assistant
/
ui5-model.ts
253 lines (235 loc) · 7.5 KB
/
ui5-model.ts
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
import { map } from "lodash";
import fetch from "node-fetch";
import { resolve } from "path";
import { pathExists, lstat, readJson, writeJson, mkdirs } from "fs-extra";
import { HttpsProxyAgent } from "https-proxy-agent";
import { HttpProxyAgent } from "http-proxy-agent";
import { UI5SemanticModel } from "@ui5-language-assistant/semantic-model-types";
import {
generate,
Json,
TypeNameFix,
} from "@ui5-language-assistant/semantic-model";
import { Fetcher } from "../api";
const DEFAULT_UI5_VERSION = "1.71.14";
export async function getSemanticModel(
modelCachePath: string | undefined
): Promise<UI5SemanticModel> {
return getSemanticModelWithFetcher(fetch, modelCachePath);
}
// This function is exported for testing purposes (using a mock fetcher)
export async function getSemanticModelWithFetcher(
fetcher: Fetcher,
modelCachePath: string | undefined
): Promise<UI5SemanticModel> {
const version = DEFAULT_UI5_VERSION;
const jsonMap: Record<string, Json> = {};
const baseUrl = `https://sapui5.hana.ondemand.com/${version}/test-resources/`;
const suffix = "/designtime/api.json";
const libs = getLibs();
let cacheFolder: string | undefined;
// Note: all cache handling (reading, writing etc) is optional from the user perspective but
// impacts performance, therefore if any errors occur when handling the cache we ignore them but output
// a warning to the user
if (modelCachePath !== undefined) {
cacheFolder = getCacheFolder(modelCachePath, version);
console.log(`${cacheFolder} will be used to cache UI5 resources`);
try {
await mkdirs(cacheFolder);
} catch (err) {
console.warn(
`Could not create folder ${cacheFolder} for caching UI5 resources`,
err
);
cacheFolder = undefined;
}
}
await Promise.all(
map(libs, async (libName) => {
const cacheFilePath = getCacheFilePath(cacheFolder, libName);
let apiJson = await readFromCache(cacheFilePath);
// If the file doesn't exist in the cache (or we couldn't read it), fetch it from the network
if (apiJson === undefined) {
const url = baseUrl + libName.replace(/\./g, "/") + suffix;
let response;
if (
process.env["NO_PROXY"]
?.split(",")
.some((_noProxy) => url.includes(_noProxy))
) {
console.debug(
"Ressource URL is in NO_PROXY env, so no proxy is used."
);
response = await fetcher(url);
} else if (process.env["HTTPS_PROXY"] && url.startsWith("https://")) {
console.debug(
"Ressource URL is https and HTTPS_PROXY env is set, so https proxy is used"
);
const agent = new HttpsProxyAgent(process.env["HTTPS_PROXY"]);
// https://github.com/TooTallNate/node-https-proxy-agent/issues/108
// have to pass agent as any was workaround
response = await fetcher(url, { agent: agent as any });
} else if (process.env["HTTP_PROXY"] && url.startsWith("http://")) {
console.debug(
"Ressource URL is https and HTTP_PROXY env is set, so http proxy is used"
);
const agent = new HttpProxyAgent(process.env["HTTP_PROXY"]);
// https://github.com/TooTallNate/node-https-proxy-agent/issues/108
// have to pass agent as any was workaround
response = await fetcher(url, { agent: agent as any });
} else {
console.debug("Using no proxy at all");
response = await fetcher(url);
}
if (response.ok) {
apiJson = await response.json();
await writeToCache(cacheFilePath, apiJson);
} else {
console.error(`Could not read UI5 resources from ${url}`);
}
}
if (apiJson !== undefined) {
jsonMap[libName] = apiJson;
}
})
);
return generate({
version: version,
libraries: jsonMap,
typeNameFix: getTypeNameFix(),
strict: false,
printValidationErrors: false,
});
}
async function readFromCache(filePath: string | undefined): Promise<unknown> {
if (filePath !== undefined) {
try {
if ((await pathExists(filePath)) && (await lstat(filePath)).isFile()) {
return await readJson(filePath);
}
} catch (err) {
console.warn(
`Could not read file ${filePath} from UI5 resources cache`,
err
);
}
}
return undefined;
}
async function writeToCache(
filePath: string | undefined,
apiJson: unknown
): Promise<void> {
if (filePath !== undefined) {
try {
await writeJson(filePath, apiJson);
} catch (err) {
console.warn(`Could not cache UI5 resources to file ${filePath}`, err);
}
}
}
// Exported for test purposes
export function getCacheFolder(
modelCachePath: string,
version: string
): string {
return resolve(modelCachePath, "ui5-resources-cache", version);
}
// Exported for test purposes
export function getCacheFilePath(
cacheFolder: string | undefined,
libName: string
): string | undefined {
if (cacheFolder === undefined) {
return undefined;
}
return resolve(cacheFolder, libName + ".json");
}
function getTypeNameFix(): TypeNameFix {
const fixes: TypeNameFix = {
Control: "sap.ui.core.Control",
Element: "sap.ui.core.Element",
array: "object[]",
Array: "object[]",
bloolean: "boolean",
"sap.m.PlanningCalendarHeader": undefined,
"sap.m.TimePickerSlider": undefined,
"sap.ui.layout.ResponsiveSplitterPage": undefined,
"sap.gantt.misc.AxisTime": "sap.gantt.misc.AxisTimes",
"sap.gantt.control.Toolbar": undefined,
"sap.gantt.DragOrientation": undefined,
"sap.gantt.simple.GanttHeader": undefined,
"sap.gantt.simple.InnerGanttChart": undefined,
"sap.rules.ui.RuleBase": undefined,
"sap.ui.generic.app.transaction.BaseController": undefined,
"sap.ui.vk.BillboardTextEncoding": undefined,
"sap.ui.vk.BillboardStyle": undefined,
"sap.ui.vk.BillboardBorderLineStyle": undefined,
"sap.ui.vk.BillboardHorizontalAlignment": undefined,
"sap.ui.vk.BillboardCoordinateSpace": undefined,
"sap.ui.vk.DetailViewType": undefined,
"sap.ui.vk.DetailViewShape": undefined,
"sap.ui.vk.tools.HitTestIdMode": undefined,
"sap.ui.vk.tools.CoordinateSystem": undefined,
"sap.ui.vk.AnimationTimeSlider": undefined,
"sap.ui.vk.SelectionMode": undefined,
"sap.ui.vk.RenderMode": undefined,
"sap.viz.ui5.controls.VizRangeSlider": undefined,
};
return fixes;
}
function getLibs(): string[] {
// When we support more version the following libraries should be added:
// "sap.fileviewer"
// "sap.ui.testrecorder"
// "sap.zen.dsh"
// "sap.zen.crosstab"
return [
"sap.ui.core",
"sap.apf",
"sap.ca.scfld.md",
"sap.ca.ui",
"sap.chart",
"sap.collaboration",
"sap.f",
"sap.fe",
"sap.gantt",
"sap.landvisz",
"sap.m",
"sap.makit",
"sap.me",
"sap.ndc",
"sap.ovp",
"sap.rules.ui",
"sap.suite.ui.commons",
"sap.suite.ui.generic.template",
"sap.suite.ui.microchart",
"sap.tnt",
"sap.ui.codeeditor",
"sap.ui.commons",
"sap.ui.comp",
"sap.ui.dt",
"sap.ui.export",
"sap.ui.fl",
"sap.ui.generic.app",
"sap.ui.generic.template",
"sap.ui.integration",
"sap.ui.layout",
"sap.ui.mdc",
"sap.ui.richtexteditor",
"sap.ui.rta",
"sap.ui.suite",
"sap.ui.support",
"sap.ui.table",
"sap.ui.unified",
"sap.ui.ux3",
"sap.ui.vbm",
"sap.ui.vk",
"sap.ui.vtm",
"sap.uiext.inbox",
"sap.ushell",
"sap.uxap",
"sap.viz",
"sap.zen.commons",
];
}