-
Notifications
You must be signed in to change notification settings - Fork 104
/
asset.js
131 lines (105 loc) · 2.66 KB
/
asset.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
/* jslint node: true */
'use strict';
// ENiGMA½
const Config = require('./config.js').get;
const StatLog = require('./stat_log.js');
// deps
const _ = require('lodash');
const assert = require('assert');
exports.parseAsset = parseAsset;
exports.getAssetWithShorthand = getAssetWithShorthand;
exports.getArtAsset = getArtAsset;
exports.getModuleAsset = getModuleAsset;
exports.resolveConfigAsset = resolveConfigAsset;
exports.resolveSystemStatAsset = resolveSystemStatAsset;
exports.getViewPropertyAsset = getViewPropertyAsset;
const ALL_ASSETS = [
'art',
'menu',
'method',
'userModule',
'systemMethod',
'systemModule',
'prompt',
'config',
'sysStat',
];
const ASSET_RE = new RegExp(
'^@(' + ALL_ASSETS.join('|') + ')' + /:(?:([^:]+):)?([A-Za-z0-9_\-./]+)$/.source
);
function parseAsset(s) {
const m = ASSET_RE.exec(s);
if (m) {
const result = { type: m[1] };
if (m[3]) {
result.asset = m[3];
if (m[2]) {
result.location = m[2];
}
} else {
result.asset = m[2];
}
return result;
}
}
function getAssetWithShorthand(spec, defaultType) {
if (!_.isString(spec)) {
return null;
}
if ('@' === spec[0]) {
const asset = parseAsset(spec);
assert(_.isString(asset.type));
return asset;
}
return {
type: defaultType,
asset: spec,
};
}
function getArtAsset(spec) {
const asset = getAssetWithShorthand(spec, 'art');
if (!asset) {
return null;
}
assert(['art', 'method'].indexOf(asset.type) > -1);
return asset;
}
function getModuleAsset(spec) {
const asset = getAssetWithShorthand(spec, 'systemModule');
if (!asset) {
return null;
}
assert(['userModule', 'systemModule'].includes(asset.type));
return asset;
}
function resolveConfigAsset(spec) {
const asset = parseAsset(spec);
if (asset) {
assert('config' === asset.type);
const path = asset.asset.split('.');
let conf = Config();
for (let i = 0; i < path.length; ++i) {
if (_.isUndefined(conf[path[i]])) {
return spec;
}
conf = conf[path[i]];
}
return conf;
} else {
return spec;
}
}
function resolveSystemStatAsset(spec) {
const asset = parseAsset(spec);
if (!asset) {
return spec;
}
assert('sysStat' === asset.type);
return StatLog.getSystemStat(asset.asset) || spec;
}
function getViewPropertyAsset(src) {
if (!_.isString(src) || '@' !== src.charAt(0)) {
return null;
}
return parseAsset(src);
}