forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions_loader.cpp
75 lines (59 loc) · 2.07 KB
/
extensions_loader.cpp
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
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <regex>
#include <vector>
#include "extensions_loader.h"
#include "settings.h"
#include "helpers.h"
#include "auth/authbasic.h"
#include "api/os/os.h"
using namespace std;
using json = nlohmann::json;
namespace extensions {
vector<string> loadedExtensions;
bool initialized = false;
string __buildExtensionArgs(const string &extensionId) {
string options = "";
options += " --nl-port=" + to_string(settings::getOptionForCurrentMode("port").get<int>());
options += " --nl-token=" + authbasic::getTokenInternal();
options += " --nl-extension-id=" + 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 = regex_replace(command, regex("\\$\\{NL_PATH\\}"), settings::getAppPath());
command += __buildExtensionArgs(extensionId);
os::execCommand(command, "", true); // async
}
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