解析 Bower 的语义版本语法
Parsing Bower's Semantic Version Syntax
Bower 似乎偏离了 semver spec,因为我有时会看到这样的依赖项(来自 2klic-angular/bower.json):
"dependencies": {
"angulargrid": "s-yadav/angulargrid#^0.4.0"
}
This question 对解释 semver 本身有很大帮助,但对 s-yadav/angulargrid# 部分的解释却没有那么多。
正在查看bower/lib/node_modules/bower-endpoint-parser/index.js
我看到以下代码:
function decompose(endpoint) {
// Note that we allow spaces in targets and sources but they are trimmed
var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/;
var matches = endpoint.match(regExp);
var target;
var error;
if (!matches) {
error = new Error('Invalid endpoint: ' + endpoint);
error.code = 'EINVEND';
throw error;
}
target = trim(matches[3]);
return {
name: trim(matches[1]),
source: trim(matches[2]),
target: isWildcard(target) ? '*' : target
};
}
所以似乎可以使用 # 作为分隔符将存储库源指定为依赖版本的一部分。
但是我无法在 bower 文档中找到任何对此进行描述的内容。
Bowers 对 semver 的解释是否还有其他需要注意的注意事项,或者这是唯一的注意事项,是否足以在 # 处拆分字符串以找到要求表达?
相关代码可以在bower/lib/node_modules/bower-endpoint-parser/index.js中找到json2decomposed
:
function json2decomposed(key, value) {
...
key = trim(key);
value = trim(value);
...
endpoint = key + '=';
split = value.split('#').map(trim);
// If # was found, the source was specified
if (split.length > 1) {
endpoint += (split[0] || key) + '#' + split[1];
// Check if value looks like a source
} else if (isSource(value)) {
endpoint += value + '#*';
// Otherwise use the key as the source
} else {
endpoint += key + '#' + split[0];
}
return decompose(endpoint);
}
所以后来成为 target
的是通过 #
拆分 JSON 依赖数组的值生成的。这个 target
被 resolver, so the actual behaviour depends on the resolver used, but the typical resolver uses node-semver 解析,如果 node-semver 可以解析它的话。否则它使用提交 ID、分支名称、标签等。
所以用'#'拆分字符串,然后修剪空格就可以找到需求表达式,但它可能毕竟不是semver版本。
Bower 似乎偏离了 semver spec,因为我有时会看到这样的依赖项(来自 2klic-angular/bower.json):
"dependencies": {
"angulargrid": "s-yadav/angulargrid#^0.4.0"
}
This question 对解释 semver 本身有很大帮助,但对 s-yadav/angulargrid# 部分的解释却没有那么多。
正在查看bower/lib/node_modules/bower-endpoint-parser/index.js
我看到以下代码:
function decompose(endpoint) {
// Note that we allow spaces in targets and sources but they are trimmed
var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/;
var matches = endpoint.match(regExp);
var target;
var error;
if (!matches) {
error = new Error('Invalid endpoint: ' + endpoint);
error.code = 'EINVEND';
throw error;
}
target = trim(matches[3]);
return {
name: trim(matches[1]),
source: trim(matches[2]),
target: isWildcard(target) ? '*' : target
};
}
所以似乎可以使用 # 作为分隔符将存储库源指定为依赖版本的一部分。
但是我无法在 bower 文档中找到任何对此进行描述的内容。
Bowers 对 semver 的解释是否还有其他需要注意的注意事项,或者这是唯一的注意事项,是否足以在 # 处拆分字符串以找到要求表达?
相关代码可以在bower/lib/node_modules/bower-endpoint-parser/index.js中找到json2decomposed
:
function json2decomposed(key, value) {
...
key = trim(key);
value = trim(value);
...
endpoint = key + '=';
split = value.split('#').map(trim);
// If # was found, the source was specified
if (split.length > 1) {
endpoint += (split[0] || key) + '#' + split[1];
// Check if value looks like a source
} else if (isSource(value)) {
endpoint += value + '#*';
// Otherwise use the key as the source
} else {
endpoint += key + '#' + split[0];
}
return decompose(endpoint);
}
所以后来成为 target
的是通过 #
拆分 JSON 依赖数组的值生成的。这个 target
被 resolver, so the actual behaviour depends on the resolver used, but the typical resolver uses node-semver 解析,如果 node-semver 可以解析它的话。否则它使用提交 ID、分支名称、标签等。
所以用'#'拆分字符串,然后修剪空格就可以找到需求表达式,但它可能毕竟不是semver版本。