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-06-07 01:34:55 +00:00
|
|
|
exports.parseAsset = parseAsset;
|
2015-09-09 04:08:45 +00:00
|
|
|
exports.getAssetWithShorthand = getAssetWithShorthand;
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.getArtAsset = getArtAsset;
|
2015-08-08 21:52:47 +00:00
|
|
|
exports.getModuleAsset = getModuleAsset;
|
2015-06-07 01:34:55 +00:00
|
|
|
exports.resolveConfigAsset = resolveConfigAsset;
|
|
|
|
exports.getViewPropertyAsset = getViewPropertyAsset;
|
2015-04-05 07:15:04 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
const ALL_ASSETS = [
|
2015-04-05 07:15:04 +00:00
|
|
|
'art',
|
|
|
|
'menu',
|
|
|
|
'method',
|
2015-04-21 05:24:15 +00:00
|
|
|
'systemMethod',
|
2015-08-08 21:52:47 +00:00
|
|
|
'systemModule',
|
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-09-09 04:08:45 +00:00
|
|
|
function getAssetWithShorthand(spec, defaultType) {
|
|
|
|
if(!_.isString(spec)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('@' === spec[0]) {
|
|
|
|
var asset = parseAsset(spec);
|
|
|
|
assert(_.isString(asset.type));
|
|
|
|
|
|
|
|
return asset;
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type : defaultType,
|
|
|
|
asset : spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: Convert these to getAssetWithShorthand()
|
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
|
|
|
|
2015-08-08 21:52:47 +00:00
|
|
|
function getModuleAsset(module) {
|
|
|
|
if(!_.isString(module)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('@' === module[0]) {
|
|
|
|
var modAsset = parseAsset(module);
|
|
|
|
assert('module' === modAsset.type || 'systemModule' === modAsset.type);
|
|
|
|
|
|
|
|
return modAsset;
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type : 'module',
|
|
|
|
asset : module,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 01:34:55 +00:00
|
|
|
|
|
|
|
function getViewPropertyAsset(src) {
|
|
|
|
if(!_.isString(src) || '@' !== src.charAt(0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseAsset(src);
|
|
|
|
};
|