forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.ts
189 lines (171 loc) · 5.95 KB
/
Project.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
"use strict";
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as log from './log';
class Library {
libpath: string;
libroot: string;
}
export class Target {
baseTarget: string;
backends: string[];
constructor(baseTarget: string, backends: string[]) {
this.baseTarget = baseTarget;
this.backends = backends;
}
}
export class Project {
name: string;
sources: Array<string>;
defines: Array<string>;
parameters: Array<string>;
scriptdir: string;
static scriptdir: string;
libraries: Array<Library>;
localLibraryPath: string;
windowOptions: any;
targetOptions: any;
assetMatchers: { match: string, options: any }[];
shaderMatchers: { match: string, options: any }[];
customTargets: Map<string, Target>;
constructor(name) {
this.name = name;
this.sources = [];
this.defines = [];
this.parameters = [];
this.scriptdir = Project.scriptdir;
this.libraries = [];
this.localLibraryPath = 'Libraries';
this.assetMatchers = [];
this.shaderMatchers = [];
this.customTargets = new Map();
this.windowOptions = {}
this.targetOptions = {
flash: {},
android: {},
android_native: {}
}
}
/**
* Add all assets matching the match regex relative to the directory containing the current khafile.
* Asset types are infered from the file suffix.
* The regex syntax is very simple: * for anything, ** for anything across directories.
*/
addAssets(match: string, options: any) {
if (!options) options = {};
this.assetMatchers.push({ match: path.resolve(this.scriptdir, match), options: options });
}
addSources(source) {
this.sources.push(source);
}
/**
* Add all shaders matching the match regex relative to the directory containing the current khafile.
* The regex syntax is very simple: * for anything, ** for anything across directories.
*/
addShaders(match: string, options: any) {
if (!options) options = {};
this.shaderMatchers.push({ match: path.resolve(this.scriptdir, match), options: options });
}
addDefine(define) {
this.defines.push(define);
}
addParameter(parameter) {
this.parameters.push(parameter);
}
addTarget(name: string, baseTarget: string, backends: string[]) {
this.customTargets.set(name, new Target(baseTarget, backends));
}
addLibrary(library) {
this.addDefine(library);
let self = this;
function findLibraryDirectory(name: string) {
// Tries to load the default library from inside the kha project.
// e.g. 'Libraries/wyngine'
let libpath = path.join(self.scriptdir, self.localLibraryPath, name);
if (fs.existsSync(libpath) && fs.statSync(libpath).isDirectory()) {
return { libpath: libpath, libroot: self.localLibraryPath + '/' + name };
}
// If the library couldn't be found in Libraries folder, try
// looking in the haxelib folders.
// e.g. addLibrary('hxcpp') => '/usr/lib/haxelib/hxcpp/3,2,193'
try {
libpath = path.join(child_process.execSync('haxelib config', { encoding: 'utf8' }).trim(), name.replace(/\./g, ',').toLowerCase());
}
catch (error) {
libpath = path.join(process.env.HAXEPATH, 'lib', name.toLowerCase());
}
if (fs.existsSync(libpath) && fs.statSync(libpath).isDirectory()) {
if (fs.existsSync(path.join(libpath, '.dev'))) {
//return fs.readFileSync(path.join(libpath, '.dev'), 'utf8');
return { libpath: fs.readFileSync(path.join(libpath, '.dev'), 'utf8'), libroot: libpath};
}
else if (fs.existsSync(path.join(libpath, '.current'))) {
// Get the latest version of the haxelib path,
// e.g. for 'hxcpp', latest version '3,2,193'
let current = fs.readFileSync(path.join(libpath, '.current'), 'utf8');
//return path.join(libpath, current.replace(/\./g, ','));
return { libpath: path.join(libpath, current.replace(/\./g, ',')), libroot: libpath };
}
}
// Show error if library isn't found in Libraries or haxelib folder
log.error('Error: Library ' + name + ' not found.');
log.error('Install it using \'haxelib install ' + name + '\' or add it to the \'Libraries\' folder.');
process.exit(1);
}
let libInfo = findLibraryDirectory(library);
let dir = libInfo.libpath;
if (dir !== '') {
this.libraries.push({
libpath: dir,
libroot: libInfo.libroot
});
// If this is a haxelib library, there must be a haxelib.json
if (fs.existsSync(path.join(dir, 'haxelib.json'))) {
let options = JSON.parse(fs.readFileSync(path.join(dir, 'haxelib.json'), 'utf8'));
// If there is a classPath value, add that directory to be loaded.
// Otherwise, just load the current path.
if (options.classPath) {
// TODO find an example haxelib that has a classPath value
this.sources.push(path.join(dir, options.classPath));
}
else {
// e.g. '/usr/lib/haxelib/hxcpp/3,2,193'
this.sources.push(dir);
}
// If this haxelib has other library dependencies, add them too
if (options.dependencies) {
for (let dependency in options.dependencies) {
if (dependency.toLowerCase() !== 'kha') {
this.addLibrary(dependency);
}
}
}
}
else {
// If there is no haxelib.json file, then just load the library
// by the Sources folder.
// e.g. Libraries/wyngine/Sources
this.sources.push(path.join(dir, 'Sources'));
}
if (fs.existsSync(path.join(dir, 'extraParams.hxml'))) {
let params = fs.readFileSync(path.join(dir, 'extraParams.hxml'), 'utf8');
for (let parameter of params.split('\n')) {
let param = parameter.trim();
if (param !== '') {
if (param.startsWith('-lib')) {
// (DK)
// - '-lib xxx' is for linking a library via haxe, it forces the use of the haxelib version
// - this should be handled by khamake though, as it tracks the dependencies better (local folder or haxelib)
console.log('ignoring', dir + '/extraParams.hxml "' + param + '"');
}
else {
this.addParameter(param);
}
}
}
}
this.addShaders(dir + '/Sources/Shaders/**', {});
}
}
}