2015-04-05 07:15:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
var Config = require('./config.js').config;
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2015-04-05 07:15:04 +00:00
|
|
|
exports.parseAsset = parseAsset;
|
2015-04-19 08:13:13 +00:00
|
|
|
exports.getArtAsset = getArtAsset;
|
2015-04-27 02:46:16 +00:00
|
|
|
exports.resolveConfigAsset = resolveConfigAsset;
|
2015-04-05 07:15:04 +00:00
|
|
|
|
|
|
|
var ALL_ASSETS = [
|
|
|
|
'art',
|
|
|
|
'menu',
|
|
|
|
'method',
|
2015-04-21 05:24:15 +00:00
|
|
|
'systemMethod',
|
2015-04-05 07:15:04 +00:00
|
|
|
'prompt',
|
2015-04-27 02:46:16 +00:00
|
|
|
'config',
|
2015-04-05 07:15:04 +00:00
|
|
|
];
|
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
var ASSET_RE = new RegExp('\\@(' + ALL_ASSETS.join('|') + ')\\:([\\w\\d\\.]*)(?:\\/([\\w\\d\\_]+))*');
|
2015-04-05 07:15:04 +00:00
|
|
|
|
|
|
|
function parseAsset(s) {
|
|
|
|
var m = ASSET_RE.exec(s);
|
|
|
|
|
|
|
|
if(m) {
|
|
|
|
var result = { type : m[1] };
|
|
|
|
|
|
|
|
if(m[3]) {
|
|
|
|
result.location = m[2];
|
|
|
|
result.asset = m[3];
|
|
|
|
} else {
|
|
|
|
result.asset = m[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
function getArtAsset(art) {
|
2015-04-19 08:13:13 +00:00
|
|
|
if(!_.isString(art)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('@' === art[0]) {
|
|
|
|
var artAsset = parseAsset(art);
|
|
|
|
assert('art' === artAsset.type || 'method' === artAsset.type);
|
|
|
|
|
|
|
|
return artAsset;
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type : 'art',
|
|
|
|
asset : art,
|
|
|
|
};
|
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
2015-04-27 02:46:16 +00:00
|
|
|
|
|
|
|
function resolveConfigAsset(from) {
|
|
|
|
var asset = parseAsset(from);
|
|
|
|
if(asset) {
|
|
|
|
assert('config' === asset.type);
|
|
|
|
|
|
|
|
var path = asset.asset.split('.');
|
|
|
|
var conf = Config;
|
|
|
|
for(var i = 0; i < path.length; ++i) {
|
|
|
|
if(_.isUndefined(conf[path[i]])) {
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
conf = conf[path[i]];
|
|
|
|
}
|
|
|
|
return conf;
|
|
|
|
} else {
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
}
|