Skip to content

Commit 9fb8e94

Browse files
committed
Implement schema templating (use repository/$apply command)
1 parent a29620d commit 9fb8e94

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

index.js

+48-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,59 @@ var chalk = require("chalk"),
99
temp = require("temp").track(),
1010
_ = require("underscore"),
1111
cson = require("cson"),
12-
plist = require("plist");
12+
plist = require("plist"),
13+
path = require("path");
14+
15+
16+
function readGrammarFile(filename) {
17+
function read(filename, vars) {
18+
var yamlSource = fs.readFileSync(filename, 'utf8');
19+
20+
if (vars) {
21+
yamlSource = yamlSource.replace(
22+
/\$\{(\w+)\}/g,
23+
function(all, name) {
24+
if (name && vars[name]) {
25+
return vars[name];
26+
} else {
27+
return all;
28+
}
29+
});
30+
}
31+
32+
var schema = yaml.safeLoad(yamlSource);
33+
34+
if (schema.repository
35+
&& schema.repository.$apply
36+
&& schema.repository.$apply instanceof Array)
37+
{
38+
var specs = schema.repository.$apply;
39+
for (var i = 0; i < specs.length; i++) {
40+
var spec = specs[i];
41+
42+
var inner = read(path.join(path.dirname(filename),
43+
spec.file),
44+
spec.vars);
45+
46+
_.extend(schema.repository, inner.repository);
47+
}
48+
49+
delete schema.repository.$apply;
50+
}
51+
52+
return schema;
53+
}
54+
55+
return read(filename);
56+
}
1357

1458

1559
function compileGrammar(grammarFile, additionalGrammars) {
1660
function _compile(filename, registry) {
1761
var tmp = temp.openSync();
1862

1963
try {
20-
var yamlSource = fs.readFileSync(filename, 'utf8'),
21-
yamlSchema = yaml.safeLoad(yamlSource);
64+
var yamlSchema = readGrammarFile(filename);
2265

2366
fs.writeSync(tmp.fd, JSON.stringify(yamlSchema));
2467
fs.closeSync(tmp.fd);
@@ -227,8 +270,7 @@ function test(testFiles, grammarFile, options) {
227270

228271

229272
function buildCson(inName, outName) {
230-
var yamlSource = fs.readFileSync(inName, 'utf8'),
231-
yamlSchema = yaml.safeLoad(yamlSource),
273+
var yamlSchema = readGrammarFile(inName),
232274
csonSource = cson.createCSONString(yamlSchema, {indent: 2});
233275

234276
csonSource = '# AUTOGENERATED FROM ' + inName + '\n' + csonSource;
@@ -237,8 +279,7 @@ function buildCson(inName, outName) {
237279

238280

239281
function buildPList(inName, outName) {
240-
var yamlSource = fs.readFileSync(inName, 'utf8'),
241-
yamlSchema = yaml.safeLoad(yamlSource),
282+
var yamlSchema = readGrammarFile(inName),
242283
plistSource = plist.build(yamlSchema);
243284

244285
plistSource = '<!-- AUTOGENERATED FROM ' + inName + ' -->\n' +

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "syntaxdev",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "Unit testing framework for TextMate/Sublime/Atom syntaxes.",
55
"main": "index.js",
66
"author": "Yury Selivanov <yury@magic.io>",

0 commit comments

Comments
 (0)