forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.js
223 lines (203 loc) · 6.7 KB
/
Project.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
"use strict";
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const Files = require('./Files.js');
const Paths = require('./Paths.js');
const log = require('./log.js');
function findFiles(dir, match) {
if (path.isAbsolute(match)) {
match = path.relative(dir, match);
}
match = match.replace(/\\/g, '/');
let subdir = '.'
if (match.indexOf('*') >= 0) {
let beforeStar = match.substring(0, match.indexOf('*'));
subdir = beforeStar.substring(0, beforeStar.lastIndexOf('/'));
}
let regex = new RegExp('^' + match.replace(/\./g, "\\.").replace(/\*\*/g, ".?").replace(/\*/g, "[^/]*").replace(/\?/g, '*') + '$', 'g');
let collected = [];
findFiles2(dir, subdir, regex, collected);
return collected;
}
function findFiles2(basedir, dir, regex, collected) {
let dirpath = path.resolve(basedir, dir);
if (!fs.existsSync(dirpath) || !fs.statSync(dirpath).isDirectory()) return;
let files = fs.readdirSync(dirpath);
nextfile: for (let f of files) {
let file = path.resolve(dirpath, f);
if (!fs.existsSync(file) || fs.statSync(file).isDirectory()) continue;
//for (let exclude of this.excludes) {
// if (this.matches(this.stringify(file), exclude)) continue nextfile;
//}
let filename = path.relative(basedir, file).replace(/\\/g, '/');
if (regex.test(filename)) {
collected.push(file.replace(/\\/g, '/'));
}
regex.lastIndex = 0;
}
nextdir: for (let f of files) {
let file = path.resolve(dirpath, f);
if (!fs.existsSync(file) || !fs.statSync(file).isDirectory()) continue;
//for (let exclude of this.excludes) {
// if (this.matchesAllSubdirs(this.basedir.relativize(dir), exclude)) {
// continue nextdir;
// }
//}
findFiles2(basedir, file, regex, collected);
}
}
class Project {
constructor(name) {
this.name = name;
this.assets = [];
this.sources = [];
this.shaders = [];
this.exportedShaders = [];
this.defines = [];
this.parameters = [];
this.scriptdir = Project.scriptdir;
this.libraries = [];
this.windowOptions = {}
this.targetOptions = {
flash : {}
}
}
/**
* 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) {
let files = findFiles(this.scriptdir, match);
for (let f of files) {
let file = path.parse(f);
let name = file.name;
let type = 'blob';
if (file.ext === '.png' || file.ext === '.jpg' || file.ext === '.jpeg') {
type = 'image';
}
else if (file.ext === '.wav') {
type = 'sound';
}
else if (file.ext === '.ttf') {
type = 'font';
}
else if (file.ext === '.mp4' || file.ext === '.webm' || file.ext === '.wmv' || file.ext === '.avi') {
type = 'video';
}
else {
name = file.base;
}
if (!file.name.startsWith('.') && file.name.length > 0) {
this.assets.push({
name: name,
file: f,
type: type
});
}
}
}
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) {
let shaders = findFiles(this.scriptdir, match);
for (let shader of shaders) {
let file = path.parse(shader);
if (!file.name.startsWith('.') && file.ext === '.glsl') {
this.shaders.push({
name: file.name,
files: [shader]
});
}
}
}
addDefine(define) {
this.defines.push(define);
}
addParameter(parameter) {
this.parameters.push(parameter);
}
addLibrary(library) {
let self = this;
function findLibraryDirectory(name) {
// Tries to load the default library from inside the kha project.
// e.g. 'Libraries/wyngine'
let libpath = path.join(self.scriptdir, 'Libraries', name);
if (fs.existsSync(libpath) && fs.statSync(libpath).isDirectory()) {
return libpath;
}
// 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.replaceAll('.', ',').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');
}
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.replaceAll('.', ','));
}
}
// Show error if library isn't found in Libraries or haxelib folder
log.error('Error: Library ' + name + ' not found.');
return '';
}
let dir = findLibraryDirectory(library);
if (dir !== '') {
this.libraries.push(dir);
// 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 !== '') {
this.addParameter(param);
}
}
}
this.addShaders(dir + '/Sources/Shaders/**');
}
}
}
module.exports = Project;