forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions_loader.cpp
More file actions
88 lines (70 loc) · 2.62 KB
/
extensions_loader.cpp
File metadata and controls
88 lines (70 loc) · 2.62 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
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include "extensions_loader.h"
#include "settings.h"
#include "helpers.h"
#include "auth/authbasic.h"
#include "api/os/os.h"
#include "api/fs/fs.h"
using namespace std;
using json = nlohmann::json;
namespace extensions {
vector<string> loadedExtensions;
bool initialized = false;
json __buildExtensionProcessInput(const string &extensionId) {
json options = {
{"nlPort", to_string(settings::getOptionForCurrentMode("port").get<int>())},
{"nlToken", authbasic::getTokenInternal()},
{"nlConnectToken", authbasic::getConnectTokenInternal()},
{"nlExtensionId", extensionId}
};
return options;
}
void init() {
json jExtensions = settings::getOptionForCurrentMode("extensions");
if(jExtensions.is_null())
return;
vector<json> extensions = jExtensions.get<vector<json>>();
for(const json &extension: extensions) {
string commandKeyForOs = "command" + string(NEU_OS_NAME);
if(!helpers::hasField(extension, "id")) {
continue;
}
string extensionId = extension["id"].get<string>();
if(helpers::hasField(extension, "command") || helpers::hasField(extension, commandKeyForOs)) {
string command = helpers::hasField(extension, commandKeyForOs) ? extension[commandKeyForOs].get<string>()
: extension["command"].get<string>();
command = fs::applyPathConstants(command);
os::ChildProcessOptions processOptions;
processOptions.events = false;
processOptions.stdOutHandler = [=](const char *bytes, size_t n){
cout << string(bytes, n) << flush;
};
processOptions.stdErrHandler = [=](const char *bytes, size_t n){
cerr << string(bytes, n) << flush;
};
auto process = os::spawnProcess(command, processOptions);
os::updateSpawnedProcess({process.first, "stdIn", helpers::jsonToString(__buildExtensionProcessInput(extensionId))});
os::updateSpawnedProcess({process.first, "stdInEnd"});
}
extensions::loadOne(extensionId);
}
initialized = true;
}
void loadOne(const string &extensionId) {
loadedExtensions.push_back(extensionId);
}
vector<string> getLoaded() {
return loadedExtensions;
}
bool isLoaded(const string &extensionId) {
return find(loadedExtensions.begin(), loadedExtensions.end(), extensionId)
!= loadedExtensions.end();
}
bool isInitialized() {
return initialized;
}
} // namespace extensions