-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
307 lines (262 loc) · 10.7 KB
/
extension.js
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
/**
* @param {vscode.ExtensionContext} context
*/
const vscode = require('vscode');
const path = require('path');
const { loadPyodide } = require("pyodide");
let globalPyodideInstance = null;
let packagesLoaded = false;
let statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.text = "$(sync~spin) Processing...";
async function getPyodideInstance(context) {
let pyodidePath="file:///"+context.asAbsolutePath(path.join('files','pyodide'));
console.log(pyodidePath);
if (!globalPyodideInstance) {
globalPyodideInstance = await loadPyodide(
{
indexURL: pyodidePath,
}
);
}
return globalPyodideInstance;
}
async function ensurePackagesLoaded(pyodide,context) {
if (!packagesLoaded) {
statusBarItem.show();
// Load micropip
await pyodide.loadPackage(['micropip']);
const micropip = pyodide.pyimport("micropip");
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "six-1.16.0-py2.py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "certifi-2023.7.22-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "idna-3.4-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "antlr4_python3_runtime-4.7.2-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "antlr_denter-1.3.1-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "afmparser-1.0.0-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "uvlparser-1.0.2-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "flamapy-1.1.3-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "flamapy_fm-1.1.3-py3-none-any.whl")));
await micropip.install("file:///"+context.asAbsolutePath(path.join('files','wheels', "flamapy_sat-1.1.7-py3-none-any.whl")));
let flamapy_fm_dist="file:///"+context.asAbsolutePath(path.join('files','wheels', "flamapy_fm_dist-1.6.0-py3-none-any.whl"));
await pyodide.runPythonAsync(`
import micropip
await micropip.install("`+flamapy_fm_dist+`", deps=False)#this is to avoid problems with deps later on
`);
//Let's install Flama starting with the UVLparser
//This function installs the flamapy-fm distribution but without its dependencies as fire wont work on wasm
//Note that the code of python has a different identation to avoid problems with tabs.
packagesLoaded = true;
statusBarItem.hide();
}
}
function getActiveFileContent() {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
return document.getText(); // This returns the entire content of the file
} else {
console.error("No active editor!");
return null;
}
}
async function exec_python(code) {
const pyodide = await getPyodideInstance();
// Ensure packages are loaded
if(packagesLoaded==false){
vscode.window.showWarningMessage('Pyodide and Flama are not yet loaded. Please wait!');
return null
}
await ensurePackagesLoaded(pyodide);
// Example usage:
let uvl_file = getActiveFileContent();
if ( uvl_file== null){
vscode.window.showWarningMessage('No active UVL file. Cannot execute the operation!');
return null
}
pyodide.FS.writeFile("uvlfile.uvl", uvl_file, { encoding: "utf8" });
return pyodide.runPythonAsync(
`
from flamapy.interfaces.python.FLAMAFeatureModel import FLAMAFeatureModel
fm = FLAMAFeatureModel("uvlfile.uvl")
`+code
);
}
class ButtonProvider {
getTreeItem(element) {
return element;
}
getChildren(element) {
if (!element) {
return [
this.createButton('Products', 'flamapy.products'),
this.createButton('Valid', 'flamapy.valid'),
this.createButton('Atomic Sets', 'flamapy.atomic_sets'),
this.createButton('Avg Branching Factor', 'flamapy.average_branching_factor'),
this.createButton('Count Leafs', 'flamapy.count_leafs'),
this.createButton('Estimated #Configurations', 'flamapy.estimated_number_of_products'),
this.createButton('Leaf feature lists', 'flamapy.leaf_features'),
this.createButton('Max Depth', 'flamapy.max_depth'),
this.createButton('Core Features', 'flamapy.core_features'),
this.createButton('Dead Features', 'flamapy.dead_features'),
this.createButton('Error detection', 'flamapy.error_detection'),
this.createButton('False Optional Features', 'flamapy.false_optional_features'),
this.createButton('#Configurations', 'flamapy.products_number')
];
}
return [];
}
createButton(label, command) {
const treeItem = new vscode.TreeItem(label, vscode.TreeItemCollapsibleState.None);
treeItem.command = {
command: command,
title: label,
arguments: []
};
return treeItem;
}
}
function createCommand(id,code,result_header){
let command = vscode.commands.registerCommand(id, function () {
statusBarItem.show();
exec_python(code).then((result) => {
if (result != null) {
const panel = vscode.window.createWebviewPanel(
'resultDisplay',
result_header,
vscode.ViewColumn.Two,
{}
);
panel.webview.html = `<html><body>`+result+`</body></html>`;
}
statusBarItem.hide()
});
});
return command
}
function activate(context) {
const provider = new ButtonProvider();
vscode.window.registerTreeDataProvider('flamaView', provider);
vscode.commands.executeCommand('setContext', 'flamaPackagesLoaded', false);
//This is to start loading pyodide asap
(async () => {
try {
vscode.window.showInformationMessage('Loading Pyodide and Flama packages');
const pyodide = await getPyodideInstance(context);
ensurePackagesLoaded(pyodide,context).then(() => {
vscode.window.showInformationMessage('Done Loading Pyodide and Flama packages');
})
} catch (error) {
console.error("Error initializing Pyodide and Flama:", error);
}
})();
let flamapy_products = createCommand(
'flamapy.products',
`
result=fm.products()
"<br>".join([f'P({i}): {p}' for i, p in enumerate(result, 1)])
`,
'Products Result')
let flamapy_valid = createCommand(
'flamapy.valid',
`
result=fm.valid()
"The feature model is valid? " + str(result)
`,
'Valid Result')
let flamapy_dead_features = createCommand(
'flamapy.dead_features',
`
result=fm.dead_features()
"<br>".join([f'F({i}): {p}' for i, p in enumerate(result, 1)])
` ,
'Dead Features Result')
let flamapy_false_optional_features = createCommand(
'flamapy.false_optional_features',
`
result=fm.false_optional_features()
"<br>".join([f'F({i}): {p}' for i, p in enumerate(result, 1)])
`,
'False Optional Features Result')
let flamapy_core_features = createCommand(
'flamapy.core_features',
`
result=fm.core_features()
"<br>".join([f'F({i}): {p}' for i, p in enumerate(result, 1)])
`,
'Core Features Result')
let flamapy_atomic_sets = createCommand(
'flamapy.atomic_sets',
`
result=fm.atomic_sets()
"<br>".join([f'AS({i}): {p}' for i, p in enumerate(result, 1)])
`,
'Atomix Sets Result')
let flamapy_average_branching_factor = createCommand(
'flamapy.average_branching_factor',
`
result=fm.average_branching_factor()
"<br>"+str(result)
`,
'Avg Branching Factor Result')
let flamapy_count_leafs = createCommand(
'flamapy.count_leafs',
`
result=fm.count_leafs()
"<br>"+str(result)
`,
'Count leafs Result')
let flamapy_estimated_number_of_products = createCommand(
'flamapy.estimated_number_of_products',
`
result=fm.estimated_number_of_products()
"<br>"+str(result)
`,
'Estimated #Configurations Result')
let flamapy_leaf_features = createCommand(
'flamapy.leaf_features',
`
result=fm.leaf_features()
"<br>".join([f'AS({i}): {p}' for i, p in enumerate(result, 1)])
`,
'Leaf Features Result')
let flamapy_max_depth = createCommand(
'flamapy.max_depth',
`
result=fm.max_depth()
"<br>"+str(result)
`,
'Max Depth Result')
let flamapy_error_detection = createCommand(
'flamapy.error_detection',
`
result=fm.max_depth()
"<br>"+str(result)
`,
'Errors Result')
let flamapy_products_number = createCommand(
'flamapy.products_number',
`
result=fm.products_number()
"<br>"+str(result)
`,
'#Configurations Result')
context.subscriptions.push(flamapy_products)
context.subscriptions.push(flamapy_valid)
context.subscriptions.push(flamapy_atomic_sets)
context.subscriptions.push(flamapy_average_branching_factor)
context.subscriptions.push(flamapy_count_leafs)
context.subscriptions.push(flamapy_estimated_number_of_products)
context.subscriptions.push(flamapy_leaf_features)
context.subscriptions.push(flamapy_max_depth)
context.subscriptions.push(flamapy_core_features)
context.subscriptions.push(flamapy_dead_features)
context.subscriptions.push(flamapy_error_detection)
context.subscriptions.push(flamapy_false_optional_features)
context.subscriptions.push(flamapy_products_number)
}
function deactivate() {}
module.exports = {
activate,
deactivate
};