forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
khamake.ts
286 lines (271 loc) · 6.14 KB
/
khamake.ts
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
// Called from entry point, e.g. Kha/make.js
// This is where options are processed:
// e.g. '-t html5 --server'
"use strict";
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {sys} from './exec';
import * as korepath from './korepath';
import {GraphicsApi} from './GraphicsApi';
import {VrApi} from './VrApi';
import {Options} from './Options';
import {Platform} from './Platform';
import {VisualStudioVersion} from './VisualStudioVersion';
let version = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
if (version < 6) {
console.error('Requires Node.js version 6 or higher.')
process.exit(1);
}
var defaultTarget;
if (os.platform() === "linux") {
defaultTarget = Platform.Linux;
}
else if (os.platform() === "win32") {
defaultTarget = Platform.Windows;
}
else {
defaultTarget = Platform.OSX;
}
var options: Array<any> = [
{
full: 'from',
value: true,
description: 'Location of your project',
default: '.'
},
{
full: 'to',
value: true,
description: 'Build location',
default: 'build'
},
{
full: 'projectfile',
value: true,
description: 'Name of your project file, defaults to "khafile.js"',
default: 'khafile.js'
},
{
full: 'target',
short: 't',
value: true,
description: 'Target platform',
default: defaultTarget
},
{
full: 'vr',
value: true,
description: 'Target VR device',
default: VrApi.None
},
{
full: 'intermediate',
description: 'Intermediate location for object files.',
value: true,
default: '',
hidden: true
},
{
full: 'graphics',
short: 'g',
description: 'Graphics api to use. Possible parameters are direct3d9, direct3d11, direct3d12, metal and opengl.',
value: true,
default: GraphicsApi.Direct3D9
},
{
full: 'visualstudio',
short: 'v',
description: 'Version of Visual Studio to use. Possible parameters are vs2010, vs2012, vs2013 and vs2015.',
value: true,
default: VisualStudioVersion.VS2015
},
{
full: 'kha',
short: 'k',
description: 'Location of Kha directory',
value: true,
default: ''
},
{
full: 'haxe',
description: 'Location of Haxe directory',
value: true,
default: ''
},
{
full: 'ffmpeg',
description: 'Location of ffmpeg executable',
value: true,
default: ''
},
{
full: 'krafix',
description: 'Location of krafix shader compiler',
value: true,
default: ''
},
{
full: 'embedflashassets',
description: 'Embed assets in swf for flash target',
value: false
},
{
full: 'compile',
description: 'Compile executable',
value: false
},
{
full: 'run',
description: 'Run executable',
value: false
},
{
full: 'init',
description: 'Init a Kha project inside the current directory',
value: false
},
{
full: 'name',
description: 'Project name to use when initializing a project',
value: true,
default: 'Project'
},
{
full: 'server',
description: 'Run local http server for html5 target',
value: false
},
{
full: 'port',
description: 'Running port for the server',
value: true,
default: 8080
},
{
full: 'debug',
description: 'Compile in debug mode for native targets.',
value: false
},
{
full: 'silent',
description: 'Silent mode.',
value: false
},
{
full: 'watch',
short: 'w',
description: 'Watch files and recompile on change.',
value: false
}
];
let parsedOptions = new Options();
function printHelp() {
console.log('khamake options:\n');
for (var o in options) {
var option = options[o];
if (option.hidden) continue;
if (option.short) console.log('-' + option.short + ' ' + '--' + option.full);
else console.log('--' + option.full);
console.log(option.description);
console.log();
}
}
function isTarget(target) {
if (target.trim().length < 1) return false;
return true;
}
for (var o in options) {
var option = options[o];
if (option.value) {
parsedOptions[option.full] = option.default;
}
else {
parsedOptions[option.full] = false;
}
}
var args = process.argv;
for (var i = 2; i < args.length; ++i) {
var arg = args[i];
if (arg[0] == '-') {
if (arg[1] == '-') {
if (arg.substr(2) === 'help') {
printHelp();
process.exit(0);
}
for (var o in options) {
var option = options[o];
if (arg.substr(2) === option.full) {
if (option.value) {
++i;
parsedOptions[option.full] = args[i];
}
else {
parsedOptions[option.full] = true;
}
}
}
}
else {
if (arg[1] === 'h') {
printHelp();
process.exit(0);
}
for (var o in options) {
var option = options[o];
if (option.short && arg[1] === option.short) {
if (option.value) {
++i;
parsedOptions[option.full] = args[i];
}
else {
parsedOptions[option.full] = true;
}
}
}
}
}
else {
if (isTarget(arg)) parsedOptions.target = arg;
}
}
if (parsedOptions.graphics === GraphicsApi.OpenGL) {
parsedOptions.graphics = GraphicsApi.OpenGL2;
}
if (parsedOptions.run) {
parsedOptions.compile = true;
}
async function runKhamake() {
try {
await require('./main.js').run(parsedOptions, { info: console.log, error: console.log }, function (name) { });
}
catch (error) {
console.log(error);
}
}
if (parsedOptions.init) {
console.log('Initializing Kha project.\n');
require('./init').run(parsedOptions.name, parsedOptions.from, parsedOptions.projectfile);
console.log('If you want to use the git version of Kha, execute "git init" and "git submodule add https://github.com/ktxsoftware/Kha.git".');
}
else if (parsedOptions.server) {
console.log('Running server on ' + parsedOptions.port);
var nstatic = require('node-static');
var fileServer = new nstatic.Server(path.join(parsedOptions.from,'build', 'html5'), { cache: 0 });
var server = require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
}).resume();
});
server.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
console.log('Error: Port ' + parsedOptions.port + ' is already in use.');
console.log('Please close the competing program (maybe another instance of khamake?)');
console.log('or switch to a different port using the --port argument.');
}
});
server.listen(parsedOptions.port);
}
else {
runKhamake();
}