-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
263 lines (224 loc) · 7.9 KB
/
index.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
"use strict";
const R = require("ramda")
const path = require("path")
const fs = require("fs")
const globby = require("globby")
const minimatch = require("minimatch")
const treey = require("treey")
const stackTrace = require('stack-trace')
const regexBlockBefore = _ => /^{/;
const regexBlockAfter = _ => /}\s*$/;
function fsReadFileSyncCwd(path_relative_to_cwd, base) {
const _base = base || process.cwd();
const full_path = path.isAbsolute(path_relative_to_cwd) ?
path_relative_to_cwd : path.join(_base, path_relative_to_cwd);
return fs.readFileSync(full_path).toString();
}
function _getCallerFileName() {
const stack_files = stackTrace.parse(new Error()).map(({
fileName
}) => fileName);
const caller_file = stack_files.find(filepath =>
filepath &&
(path.basename(filepath) === ".ggyp" ||
minimatch(filepath, "*.{ggyp,ggypi}", {matchBase:true }))
);
return caller_file;
}
function base2local_path(except_relative_path) {
const cwd = process.cwd();
let base = path.dirname(_getCallerFileName());
let result_absolute = path.isAbsolute(except_relative_path) ?
except_relative_path :
path.join(cwd, except_relative_path);
return path.relative(base, result_absolute);
}
function frombase_path(except_relative_path) {
const cwd = process.cwd();
let base = path.dirname(_getCallerFileName());
let result_absolute = path.isAbsolute(except_relative_path) ?
except_relative_path :
path.join(base, except_relative_path);
return path.join(path.relative(base, cwd), path.relative(cwd, result_absolute));
}
function absolute_path(except_relative_path) {
let base = path.dirname(_getCallerFileName());
return path.isAbsolute(except_relative_path) ?
except_relative_path :
path.join(base, except_relative_path);
}
function globby_paths(globby_paths) {
const cwd = process.cwd();
let base = path.dirname(_getCallerFileName() || cwd);
return globby.sync(globby_paths, { cwd: base });
}
const helper_functions = [
["base2local", base2local_path],
["frombase", frombase_path],
["absolute", absolute_path],
["glob", globby_paths]
];
exports.gen = function (config_ggyp, env_vars = {}) {
const config_ggyp_dir = path.dirname(config_ggyp);
const config_ggyp_content = fsReadFileSyncCwd(config_ggyp);
const S = _createTreeyScope(env_vars);
const cwd = process.cwd();
_withProps([
["__S", S], // dont want user using root scope directly in the configure ggyp
["G", S.vars],
["global", (...args) => S.global(...args)],
["target_template", (...args) => S.target_template(...args)],
["$gen", (entry_dir) => {
let base = path.dirname(stackTrace.parse(new Error())[1].fileName);
if (Array.isArray(entry_dir)) {
return entry_dir.forEach(entry => _read_and_run_ggyp(S, entry, config_ggyp_dir));
}
return _read_and_run_ggyp(S, path.join(entry_dir, "./BUILD.ggyp"));
}],
...helper_functions
], `
const scope = {};
__S.on("METHOD_DEFINED", ([name]) => {
scope[name] = (...args) => S[name](...args);
});
with(scope) {
${config_ggyp_content}
}
`.trim(), config_ggyp);
return S.toData(true).then(data => {
// console.log(require("util").inspect(data, { showHidden: true, depth: null }));
const projects = data.$children.filter(([name])=> name === "project");
for (const project of projects) {
const [ _, project_info ] = project;
const {
project_path,
project_name,
project_options,
project_is_includable
} = project_info;
const variables = project_info.variables;
const targets = project_info.$children.filter(([name]) => name === "target");
const json = {};
const project_path_relative_to_base = path.relative(project_path, cwd);
const base_relative_to_project_path = path.relative(cwd, project_path);
const project_path_ext = path.extname(project_name);
const assets_base = path.join(
project_path_relative_to_base, base_relative_to_project_path
);
if (variables && typeof variables === "object"
&& Object.keys(variables).length > 0) {
json.variables = variables;
}
json.targets = targets.length > 0 ? targets.map(([_, target_info]) => {
const _target_info = _rebaseAllPathsBeforeOutput(target_info, assets_base);
return R.filter(Boolean, {
..._target_info,
$children: null
})
}) : void 0;
let suffix = ".gyp";
if (project_is_includable) {
suffix = ".gypi";
}
let json_str = JSON.stringify(json, null, 2);
if (project_info.includes_top) {
json_str = json_str.replace(regexBlockBefore(), function () {
return "{\n" +
" \"includes\": " + JSON.stringify(project_info.includes_top) + ","
})
}
if (project_info.includes_bottom) {
json_str = json_str.replace(regexBlockAfter(), function () {
return " ,\"includes\": " + JSON.stringify(project_info.includes_bottom) +
"\n}\n";
})
}
fs.writeFileSync(
path.join(project_path, `${project_name}${suffix}`),
json_str
);
}
});
}
function _read_and_run_ggyp(S, ggyp_path, base) {
const _base = base || process.cwd();
const full_path = path.isAbsolute(ggyp_path) ?
ggyp_path : path.join(_base, ggyp_path);
const entry_content = fsReadFileSyncCwd(full_path);
_run_ggyp(S, entry_content, full_path);
}
function _run_ggyp(S, code_string, ggyp_path) {
_withProps([
["S", S],
["G", S.vars],
["target_template", (...args) => S.target_template(...args)],
["global", (...args) => S.global(...args)],
["project", (...args) => S.project(...args, ggyp_path)],
["__curr", path.basename(ggyp_path).replace(/\.ggypi?$/, "")],
...helper_functions
], code_string, ggyp_path);
}
function _createTreeyScope(env_vars = {}) {
const S = treey.create();
S.vars = env_vars;
S.def("global", _ => (global_vars) => {
S.vars = global_vars;
})
S.def("project", _ => (project_name, defFn, ggyp_file_path) => {
let _defFn = defFn;
const project_path = path.dirname(ggyp_file_path);
S("project", project => {
project.project_path("");
project.project_name("");
project.variables.ensure_plain_object();
_defFn(project);
project.project_path(project_path);
project.project_name(`${project_name}`);
project.project_is_includable(path.extname(ggyp_file_path) === ".ggypi")
})
});
S.def("target_template", _ => (template_name, defFn) => {
S.def(template_name, scope => (target_name, defTargetFn) => {
scope("target", target => {
target.target_name(target_name)
defFn(target, defTargetFn)
});
})
});
return S;
}
function _withProps(tokenPropsMap, code_string, filename_) {
const cwd = process.cwd();
const filename__ = path.isAbsolute(filename_) ? filename_ : path.join(cwd, filename_);
const token = tokenPropsMap.map(([token]) => token);
const props = tokenPropsMap.map(([token, prop, options = {}]) => prop);
const code_module_string = `
//console.log("code_module_string...", __filename);
module.exports = new Function(
${token.map(token_string => JSON.stringify(token_string)).join(",")},
${JSON.stringify(code_string)}
)
`
const Module = module.constructor;
const m = new Module();
m._compile(code_module_string, filename__);
return m.exports(...props);
}
function _rebaseAllPathsBeforeOutput(target, rebase_prefix) {
return Object.keys(target).reduce((result, key) => {
let value = target[key];
if (Array.isArray(value)) {
if ([
/.?sources$/,
/.?dirs$/,
/.?files$/
].some(regex => regex.test(key))) {
value = value.map(element_path => path.join(rebase_prefix, element_path));
}
} else if (typeof value === "object") {
value = _rebaseAllPathsBeforeOutput(value, rebase_prefix);
}
result[key] = value;
return result;
}, {})
}