-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·351 lines (292 loc) · 13.1 KB
/
install
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
#!/usr/bin/env node
const { spawn, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
async function command(cmd, args, options) {
if (args == null) args = [];
if (options == null) options = {};
if (options.cwd == null) options.cwd = process.cwd();
if (process.platform == 'win32') {
options.shell = cmd.endsWith('.exe') ? false : true;
args = [].concat(args);
for (let i = 0; i < args.length; i++) {
args[i] = '"' + args[i].replace(/"/g, '""') + '"';
}
}
console.log('\n> ' + cmd + ' ' + args.join(' '));
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, {
stdio: 'inherit',
cwd: options.cwd,
env: process.env,
shell: options.shell
});
child.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Child process exited with code ${code}`));
} else {
resolve(code);
}
});
child.on('error', (error) => {
reject(error);
});
});
}
function walkDirectory(dir, callback) {
// Get the contents of the directory
const entries = fs.readdirSync(dir);
// Iterate through each entry
for (const entry of entries) {
// Create full path
const fullPath = path.join(dir, entry);
// Get file/directory stats
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
// Recursively walk through subdirectories
walkDirectory(fullPath, callback);
} else {
// Call the callback with the file path
callback(fullPath);
}
}
}
function extractCsNamespace(content) {
// Match 'namespace' followed by any valid C# namespace characters
// Handles optional whitespace and the opening brace
const namespaceMatch = content.match(/namespace\s+([\w.]+)\s*{/);
if (namespaceMatch) {
return namespaceMatch[1];
}
return null; // Return null if no namespace found
}
function isLinuxArm64() {
try {
const arch = execSync('uname -m').toString().trim();
return arch == 'aarch64';
} catch (error) {
console.error('Error detecting architecture:', error);
return null;
}
}
// Example usage with async/await
async function main() {
process.chdir(__dirname);
let rawArgs = process.argv.slice(2);
var haxelib = process.platform == 'win32' ? 'haxelib.cmd' : './haxelib';
var haxe = process.platform == 'win32' ? 'haxe.cmd' : './haxe';
let haxeBuildArgs = [];
let buildCpp = rawArgs.indexOf('--cpp') != -1;
let buildCs = rawArgs.indexOf('--cs') != -1;
let buildCsDll = rawArgs.indexOf('--cs-dll') != -1;
let buildJs = rawArgs.indexOf('--js') != -1;
let debug = false;
let i = 0;
while (i < rawArgs.length) {
if (rawArgs[i].startsWith('-D')) {
haxeBuildArgs.push('-D');
haxeBuildArgs.push(rawArgs[i].substring(2));
}
else if (rawArgs[i] == '--debug') {
debug = true;
haxeBuildArgs.push('--debug');
}
i++;
}
// Install dependencies
var haxelibRepoPath = path.join(__dirname, '.haxelib');
if (!fs.existsSync(haxelibRepoPath)) {
fs.mkdirSync(haxelibRepoPath);
}
await command(haxelib, ['dev', 'hxcpp', 'git/hxcpp', '--quiet'], { cwd: __dirname });
await command(haxelib, ['dev', 'hxcs', 'git/hxcs', '--quiet'], { cwd: __dirname });
await command(haxelib, ['dev', 'hscript', 'git/hscript', '--quiet'], { cwd: __dirname });
if (buildCpp) {
let haxeBuildCppArgs = [
'build-cli.hxml',
'--cpp', 'build/cpp',
'-D', 'HXCPP_DEBUG_LINK',
'-D', 'HXCPP_STACK_LINE',
'-D', 'HXCPP_STACK_TRACE',
'-D', 'HXCPP_CHECK_POINTER',
'-D', 'HXCPP_CPP11',
'-D', 'safeMode'
];
if (process.platform == 'darwin') {
console.log('Build loreline for mac');
await command(haxe, haxeBuildCppArgs.concat(haxeBuildArgs).concat([
'-D', 'mac', '-D', 'mac_arm64', '-D', 'no-compilation'
]));
await command('../../haxelib', ['run', 'hxcpp', 'Build.xml', '-DHXCPP_ARM64'].concat(debug ? ['-Ddebug'] : []), { cwd: path.join(__dirname, 'build', 'cpp') });
if (debug) {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli-debug'), path.join(__dirname, 'build/cpp/loreline-arm64'));
}
else {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli'), path.join(__dirname, 'build/cpp/loreline-arm64'));
}
await command(haxe, haxeBuildCppArgs.concat(haxeBuildArgs).concat([
'-D', 'mac', '-D', 'mac_x86_64', '-D', 'no-compilation'
]));
await command('../../haxelib', ['run', 'hxcpp', 'Build.xml', '-DHXCPP_M64', '-DHXCPP_X86_64'].concat(debug ? ['-Ddebug'] : []), { cwd: path.join(__dirname, 'build', 'cpp') });
if (debug) {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli-debug'), path.join(__dirname, 'build/cpp/loreline-x86_64'));
}
else {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli'), path.join(__dirname, 'build/cpp/loreline-x86_64'));
}
if (fs.existsSync(path.join(__dirname, 'loreline'))) {
fs.unlinkSync(path.join(__dirname, 'loreline'));
}
await command('lipo', [
'-create', 'loreline-arm64', 'loreline-x86_64',
'-output', '../../loreline'
], { cwd: path.join(__dirname, 'build', 'cpp') });
}
else if (process.platform == 'win32') {
console.log("Build loreline for windows");
await command(haxe, haxeBuildCppArgs.concat(haxeBuildArgs).concat([
'-D', 'windows', '-D', 'no-compilation'
]));
let haxelibCmd = fs.readFileSync(path.join(__dirname, 'haxelib.cmd'), 'utf8');
haxelibCmd = haxelibCmd.split('/git/').join('/../../git/');
fs.writeFileSync(path.join(__dirname, 'build', 'cpp', 'haxelib.cmd'), haxelibCmd);
await command(haxelib, ['run', 'hxcpp', 'Build.xml', '-DHXCPP_M64', '-DHXCPP_X86_64'].concat(debug ? ['-Ddebug'] : []), { cwd: path.join(__dirname, 'build', 'cpp') });
if (fs.existsSync(path.join(__dirname, 'loreline.exe'))) {
fs.unlinkSync(path.join(__dirname, 'loreline.exe'));
}
if (debug) {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli-debug.exe'), path.join(__dirname, 'loreline.exe'));
}
else {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli.exe'), path.join(__dirname, 'loreline.exe'));
}
}
else {
console.log("Build loreline for linux");
if (isLinuxArm64()) {
await command(haxe, haxeBuildCppArgs.concat(haxeBuildArgs).concat([
'-D', 'linux', '-D', 'no-compilation', '-D', 'linux_arm64'
]));
await command('../../haxelib', ['run', 'hxcpp', 'Build.xml', '-DHXCPP_ARM64'].concat(debug ? ['-Ddebug'] : []), { cwd: path.join(__dirname, 'build', 'cpp') });
}
else {
await command(haxe, haxeBuildCppArgs.concat(haxeBuildArgs).concat([
'-D', 'linux', '-D', 'no-compilation', '-D', 'linux_x86_64'
]));
await command('../../haxelib', ['run', 'hxcpp', 'Build.xml', '-DHXCPP_M64', '-DHXCPP_X86_64'].concat(debug ? ['-Ddebug'] : []), { cwd: path.join(__dirname, 'build', 'cpp') });
}
if (fs.existsSync(path.join(__dirname, 'loreline'))) {
fs.unlinkSync(path.join(__dirname, 'loreline'));
}
if (debug) {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli-debug'), path.join(__dirname, 'loreline'));
}
else {
fs.renameSync(path.join(__dirname, 'build/cpp/Cli'), path.join(__dirname, 'loreline'));
}
}
}
if (buildCs) {
console.log('Export loreline for C#');
if (fs.existsSync('build/cs/Loreline')) {
fs.rmSync('build/cs/Loreline', { recursive: true, force: true });
}
let haxeBuildCsArgs = [
'--class-path', 'src',
'--cs', 'build/cs',
'--library', 'hscript',
'-D', 'no-compilation',
'-D', 'no-root',
'-D', 'real-position',
'-D', 'loreline_use_cs_types',
'-D', 'hscriptPos',
'--main', 'loreline.lib.Library'
];
await command(haxe, haxeBuildCsArgs.concat(haxeBuildArgs).concat([]));
fs.unlinkSync('build/cs/src/loreline/lib/_Library/Library_Fields_.cs');
//fs.unlinkSync('build/cs/src/loreline/Loreline.cs');
fs.unlinkSync('build/cs/hxcs_build.txt');
if (fs.existsSync('cs/Loreline/Internal'))
fs.rmSync('cs/Loreline/Internal', { recursive: true, force: true });
if (fs.existsSync('cs/Loreline/Runtime'))
fs.rmSync('cs/Loreline/Runtime', { recursive: true, force: true });
walkDirectory('build/cs/src', (filePath) => {
if (filePath.endsWith('.cs')) {
const original = fs.readFileSync(filePath, 'utf8');
let content = original;
// Replace any haxe.X with Loreline.Hx.X (with X capitalized)
content = content.replace(
/\bhaxe\.(\w+)\b/g,
(_, identifier) => `Loreline.Internal.${identifier.charAt(0).toUpperCase() + identifier.slice(1)}`
);
content = content.replace(/\bhaxe\b/g, 'Loreline.Internal');
content = content.replace(/\bhscript\b/g, 'Loreline.Internal.Hscript');
content = content.replace(/\bnamespace cs\b/g, 'namespace Loreline.Internal.Cs');
content = content.replace(/\bcs.\b/g, 'Loreline.Internal.Cs.');
content = content.replace(/\bloreline\b/g, 'Loreline.Runtime');
content = content.replace(/\bLoreline\.Internal\./g, 'global::Loreline.Internal.');
content = content.replace(/\bnamespace global::Loreline\.Internal\./g, 'namespace Loreline.Internal.');
content = content.replace(/\bglobal::global::Loreline\.Internal\./g, 'global::Loreline.Internal.');
// Disable warnings related to AOT (the code is still expected to work in that
// situation because it should not rely on actual C# reflection. Uses FieldLookup instead)
content = content.split("#pragma warning disable 109, 114, 219, 429, 168, 162").join("#pragma warning disable 109, 114, 219, 429, 168, 162, IL2026, IL2070, IL2072, IL2060, CS0108");
const namespace = extractCsNamespace(content);
if (namespace == null) {
throw 'Failed to extract namespace for file: ' + filePath;
}
const namespaceParts = namespace.split('.');
let newPath = path.join('cs', namespaceParts[0], namespaceParts[1], path.basename(filePath));
if (!fs.existsSync(path.dirname(newPath))) {
fs.mkdirSync(path.dirname(newPath), { recursive: true });
}
if (fs.existsSync(newPath)) {
throw 'File path collision: ' + newPath;
}
fs.writeFileSync(newPath, content);
}
});
fs.rmSync('build/cs/src', { recursive: true, force: true });
// These identify Loreline
const solutionGuid = '60F34EF9-0A9A-47B7-883F-8EC1F188C262';
const projectGuid = 'F3DEFEF5-8B8F-4D6D-8C2C-1DD7F3E309D4';
if (buildCsDll) {
const csConfig = debug ? 'Debug' : 'Release';
const csPlatform = 'netstandard2.1';
const dllFiles = [
'Loreline.deps.json',
'Loreline.dll',
'Loreline.pdb',
'Loreline.xml'
];
for (let name of dllFiles) {
if (fs.existsSync('cs/' + name)) {
fs.rmSync('cs/' + name, { recursive: true, force: true });
}
}
await command('dotnet', ['build', '-c', csConfig], { cwd: 'cs' });
for (let name of dllFiles) {
fs.copyFileSync(
'build/cs/bin/Loreline/' + csConfig + '/' + csPlatform + '/' + name,
'cs/' + name
);
}
}
}
if (buildJs) {
console.log('Export loreline for Javascript');
let haxeBuildJsArgs = [
'--class-path', 'src',
'--js', 'build/loreline.js',
'-D', 'js-es=6',
'--main', 'loreline.lib.Library'
];
await command(haxe, haxeBuildJsArgs.concat(haxeBuildArgs).concat([]));
}
}
(async () => {
try {
await main();
} catch (error) {
console.error(error);
}
})();