forked from PhoenicisOrg/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
362 lines (309 loc) · 11.4 KB
/
script.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const WineEngine = include("engines.wine.engine.implementation");
const {ls, mkdir, fileExists, cat, cp, getFileSize, fileName, lns, remove, touch, writeToFile, createTempFile, createTempDir, chmod, Checksum} = include("utils.functions.filesystem.files");
include("utils.functions.filesystem.extract");
include("utils.functions.net.download");
include("utils.functions.net.resource");
/* exported LATEST_STABLE_VERSION */
const LATEST_STABLE_VERSION = "4.0.1";
/* exported LATEST_DEVELOPMENT_VERSION */
const LATEST_DEVELOPMENT_VERSION = "4.11";
/* exported LATEST_STAGING_VERSION */
const LATEST_STAGING_VERSION = "4.11";
/* exported LATEST_DOS_SUPPORT_VERSION */
const LATEST_DOS_SUPPORT_VERSION = "4.0";
const FilenameUtils = Java.type("org.apache.commons.io.FilenameUtils");
/**
* Wine main prototype
*/
// eslint-disable-next-line no-unused-vars
module.default = class Wine {
constructor() {
this._implementation = new WineEngine();
this.configFactory = Bean("compatibleConfigFileFormatFactory");
this.operatingSystemFetcher = Bean("operatingSystemFetcher");
}
/**
*
* @returns {string} architecture ("x86" or "amd64")
*/
architecture() {
if (fileExists(this.prefixDirectory())) {
const containerConfiguration = this.configFactory.open(this.prefixDirectory() + "/phoenicis.cfg");
const architecture = containerConfiguration.readValue("wineArchitecture", "x86");
return architecture;
} else {
throw new Error(`Wine prefix "${this.prefixDirectory()}" does not exist!`);
}
}
/**
*
* @param {SetupWizard} [wizard]
* @returns {SetupWizard|Wine}
*/
wizard(wizard) {
// get
if (arguments.length == 0) {
return this._implementation.getWizard();
}
// set
this._implementation.setWizard(wizard);
return this;
}
/**
* @param {String} [path]
* @returns {String}
*/
winepath(path) {
return this.run("winepath", ["-w", path], this.prefixDirectory(), true, true);
}
/**
*
* @param {string} [prefix]
* @param {string} [distribution]
* @param {string} [architecture]
* @param {string} [version]
* @returns {string|Wine}
*/
prefix(prefix, distribution, architecture, version) {
// get
if (arguments.length == 0) {
return this._implementation.getWorkingContainer();
}
// set
else if (arguments.length == 1) {
this._implementation.setWorkingContainer(prefix);
} else {
const operatingSystem = this.operatingSystemFetcher.fetchCurrentOperationSystem().getWinePackage();
const subCategory = distribution + "-" + operatingSystem + "-" + architecture;
this._implementation.createContainer(subCategory, version, prefix);
this._implementation.setWorkingContainer(prefix);
}
return this;
}
/**
* returns prefix directory
* @returns {string}
*/
prefixDirectory() {
return this._implementation.getContainerDirectory(this._implementation.getWorkingContainer());
}
/**
* returns the path to the engine binary directory
* if no parameters are given, the Wine version of the current prefix is used
* @param {string} [subCategory] Wine sub-category
* @param {string} [version] Wine version
* @returns {string} path to "wine" binary
*/
binPath(subCategory, version) {
if (0 == arguments.length) {
if (fileExists(this.prefixDirectory())) {
const containerConfiguration = this.configFactory.open(this.prefixDirectory() + "/phoenicis.cfg");
const distribution = containerConfiguration.readValue("wineDistribution", "upstream");
const architecture = containerConfiguration.readValue("wineArchitecture", "x86");
const operatingSystem = this.operatingSystemFetcher.fetchCurrentOperationSystem().getWinePackage();
subCategory = distribution + "-" + operatingSystem + "-" + architecture;
version = containerConfiguration.readValue("wineVersion");
} else {
throw new Error(`Wine prefix "${this.prefixDirectory()}" does not exist!`);
}
}
return this._implementation.getLocalDirectory(subCategory, version) + "/bin/";
}
/**
*
* @param {string} executable
* @param {array} [args = []]
* @param {boolean} [wait=false]
*/
runInsidePrefix(executable, args, wait) {
if (!args) {
args = [];
} else if (typeof args === "string" || args instanceof String) {
args = [args];
}
if (!wait) {
wait = false;
}
return this.run(this.prefixDirectory() + "/drive_c/" + executable, args, this.prefixDirectory(), false, wait);
}
/**
*
* @param {string} executable
* @param {array} [args = []]
* @param {string} [workingDirectory = working container]
* @param {boolean} [captureOutput=false]
* @param {boolean} [wait=false]
* @param {map} [userData=empty]
* @returns {String} output
*/
run(executable, args, workingDirectory, captureOutput, wait, userData) {
if (!args) {
args = [];
} else if (typeof args === "string" || args instanceof String) {
args = [args];
}
if (!workingDirectory) {
workingDirectory =
this._implementation.getContainerDirectory(this._implementation.getWorkingContainer()) + "/drive_c";
}
if (!captureOutput) {
captureOutput = false;
}
if (!wait) {
wait = false;
}
if (!userData) {
userData = [];
}
return this._implementation.run(executable, args, workingDirectory, captureOutput, wait, userData);
}
/**
* uninstall application
* @param {string} name of the application which shall be uninstalled
* @returns {bool} true if an application has been uninstalled, false otherwise
*/
uninstall(application) {
const list = this.run("uninstaller", ["--list"], this.prefixDirectory(), true, true);
const appEscaped = application.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
const re = new RegExp("(.*)\\|\\|\\|.*" + appEscaped);
const uuid = list.match(re);
if (uuid) {
this._implementation.getWizard().wait(tr("Please wait while {0} is uninstalled...", application));
this.run("uninstaller", ["--remove", uuid[1]], this.prefixDirectory(), false, true);
return true;
} else {
print(tr("Could not uninstall {0}!", application));
return false;
}
}
/**
* runs "wineboot"
*
* @returns {Wine} The Wine object
*/
create() {
this.run("wineboot", [], this.prefixDirectory(), false, true);
return this;
}
/**
*
* @returns {string} name of "Program Files"
*/
programFiles() {
const programFilesName = this.run(
"cmd",
["/c", "echo", "%ProgramFiles%"],
this.prefixDirectory(),
true,
true
).trim();
if (programFilesName == "%ProgramFiles%") {
return "Program Files";
} else {
return FilenameUtils.getBaseName(programFilesName);
}
}
/**
* executes wineserver in current prefix
* @param {string} wineserver parameter
*/
wineServer(parameter) {
const workingContainerDirectory = this.prefixDirectory();
if (fileExists(workingContainerDirectory)) {
const containerConfiguration = this.configFactory.open(workingContainerDirectory + "/phoenicis.cfg");
const distribution = containerConfiguration.readValue("wineDistribution", "upstream");
const architecture = containerConfiguration.readValue("wineArchitecture", "x86");
const version = containerConfiguration.readValue("wineVersion");
const operatingSystem = this.operatingSystemFetcher.fetchCurrentOperationSystem().getWinePackage();
const subCategory = distribution + "-" + operatingSystem + "-" + architecture;
const binary = this._implementation.getLocalDirectory(subCategory, version) + "/bin/wineserver";
const ProcessBuilderClass = Java.type("java.lang.ProcessBuilder");
const processBuilder = new ProcessBuilderClass()
.command(Java.to([binary, parameter], "java.lang.String[]"))
.inheritIO();
const environment = processBuilder.environment();
environment.put(
"WINEPREFIX",
this._implementation.getContainerDirectory(this._implementation.getWorkingContainer())
);
const wineServerProcess = processBuilder.start();
wineServerProcess.waitFor();
} else {
throw new Error('Wine prefix "' + this.getWorkingContainer() + '" does not exist!');
}
}
/**
* wait until wineserver finishes
* @returns {Wine}
*/
wait() {
this.wineServer("-w");
return this;
}
/**
* kill wine server
* @returns {Wine}
*/
kill() {
this.wineServer("-k");
return this;
}
/**
*
* @param {string} [architecture = current architecture]
* @returns {string[]}
*/
availableDistributions(architectureName) {
const architecture = architectureName || this._architecture;
const architectureRegExp = new RegExp(architecture);
const wineJson = JSON.parse(this._implementation.getAvailableVersions());
// find all distributions with the right architecture
return wineJson
.filter(distribution => architectureRegExp.test(distribution.name))
.map(distribution => distribution.name.match(/([a-z]+)-/)[1])
.sort();
}
/**
*
* @param {string} [distribution name = current distribution]
* @returns {string[]}
*/
availableVersions(distributionName) {
const fullDistributionName = distributionName || this._fetchFullDistributionName();
const wineJson = JSON.parse(this._implementation.getAvailableVersions());
return wineJson
.filter(distribution => distribution.name == fullDistributionName)
.flatMap(distribution => distribution.packages)
.map(winePackage => winePackage.version)
.sort()
.reverse();
}
/**
*
* @returns {string} system32 directory
*/
system32directory() {
if (fileExists(this.prefixDirectory() + "/drive_c/windows/syswow64")) {
return this.prefixDirectory() + "/drive_c/windows/syswow64";
} else {
return this.prefixDirectory() + "/drive_c/windows/system32";
}
}
/**
*
* @returns {string} system64 directory
*/
system64directory() {
if (fileExists(this.prefixDirectory() + "/drive_c/windows/syswow64")) {
return this.prefixDirectory() + "/drive_c/windows/system32";
}
throw new Error(tr("Prefix seems to be 32bits"));
}
/**
*
* @returns {string} font directory
*/
fontDirectory() {
return this.prefixDirectory() + "/drive_c/windows/Fonts";
}
}