bower(和npm)版本的语法是什么? [英] What is the bower (and npm) version syntax?

查看:136
本文介绍了bower(和npm)版本的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鲍尔使我能够使用以下语法指定软件包的版本要求:

"dependencies": {
  "<name>": "<version>",
},

但是我无法找到<version>使用的语法.我知道我可以将版本指定为:

  • 大于使用">1.0.0"
  • 的特定版本
  • 大于或等于版本:">=1.0.0"
  • 或某些范围内:"1.0.0 - 2.0.0".

我还知道有一个通用的语法包含波浪号:"~1.0.0".但是我不确定这是什么意思,它是否与"=1.0.0"相同.

我还想知道我是否能够指定多个非连续版本,例如确切地1.0.3以及大于1.5.0的版本,等等...

概括地说,Bower版本号(和NPM)的语法称为SemVer,它是语义版本控制"的缩写.您可以在Node/npm中的 semver解析器的API上找到Bower和NPM中使用的SemVer详细语法的文档..您可以在 semver.org 未未提及~或其他语法详细信息)的更多信息. >.

有一个超级方便的可视化符号计算器,您可以使用它,使所有这些内容更易于使用和测试.

>

SemVer不仅是一种语法!关于发布API的正确方法,有一些非常有趣的事情要说,这将有助于理解语法的含义.至关重要的是:

一旦确定了公共API,便会以版本号的特定增量传达对它的更改. 考虑X.Y.Z(主要,次要补丁)的版本格式.不影响API的错误修复程序不会增加补丁程序版本,向后兼容的API增加/更改会增加次要版本,向后不兼容的API更改会增加主版本.

因此,您对~的特定问题与该Major.Minor.Patch模式有关. (和相关的插入符号运算符^一样.)您可以使用~将您愿意接受的版本范围缩小到以下任一范围:

  • 随后的补丁程序级别更改为相同的次要版本(不影响API的错误修复" ),或者:
  • 随后的次要级别更改为相同的主要版本(向后兼容的API添加/更改" )

例如:要表明您将在1.2.x树上进行后续的补丁程序级更改,从1.2.0开始,但小于1.3.0,则可以使用:

"angular": "~1.2"
  or:
"angular": "~1.2.0"

这也为您提供与使用.x语法相同的结果:

"angular": "1.2.x"

但是,您可以使用tilde/~语法来更具体:如果您只愿意接受补丁程序级的更改,从1.2.4开始,但仍然小于1.3.0,您将使用:

"angular": "~1.2.4"

如果使用...,则向主要版本向左移动.

"angular": "~1"

...与...相同...

"angular": "1.x"
  or:
"angular": "^1.0.0"

...并匹配高于1.0.0且低于2.0的任何次要或补丁级别的更改:

请注意,上面的最后一个变体:它称为插入符范围" .插入符号看起来很像>,因此您可能会以为它的意思是任何版本大于 1.0.0". (我当然对此有所遗漏.)不!

Caret范围基本上是说您只关心最左边的有效数字(通常是主要版本),并且您会允许进行任何不重要的次要级别或补丁级别更改不会影响最左边的数字.但是,与指定主要版本的代字号范围不同,插入符范围可让您指定精确的次要/音色起点.因此,在^1.0.0 === ~1时,诸如^1.2.3之类的插入符号范围可以让您说您将进行>=1.2.3 && <2.0.0的所有更改.您无法使用波浪号范围来做到这一点.

当您近距离观察时,一切乍一看似乎令人困惑.但是请稍等一会,然后以这种方式进行思考: 插入符号只是让您说出,您最关心的是最左边的有效数字.代字号可以让您说出自己最担心哪个数字最右边. 其余的都是细节.

代字号和插入号的表达能力解释了为什么人们使用它们的次数要比更简单的.x语法多得多:它们只是让您做更多.这就是为什么即使.x都可以使用波浪号的原因.例如,请参阅npm本身:它自己的package.json文件包含许多~2.4.0格式的依赖项,而不是可以使用的2.4.x格式.通过坚持使用~,语法在整个70多个版本依赖项的列表中一直保持一致,无论哪个开始的补丁程序编号是可接受的.

无论如何,SemVer还有更多功能,但是我不会在这里详细说明.在 node semver程序包的自述文件中进行检查.并且在练习并尝试着如何尝试时,请务必使用语义版本控制计算器 . SemVer有效.


RE:非连续版本号:OP的最后一个问题似乎是关于指定非连续版本号/范围(如果我进行了合理的编辑).是的,您可以使用常见的双管道或"运算符:||.像这样:

"angular": "1.2 <= 1.2.9 || >2.0.0"

Bower enables me to specify version requirements for packages using the following syntax:

"dependencies": {
  "<name>": "<version>",
},

But I have not been able to find what is the syntax to use for the <version>. I know that I can specify versions to be:

  • greater than a certain version with ">1.0.0"
  • greater than or equal to a version: ">=1.0.0"
  • or in some range: "1.0.0 - 2.0.0".

I also know that there is a common version syntax containing the tilde: "~1.0.0". But I am not sure what it means and whether it is the same as "=1.0.0".

I am also interested to know whether I am able to specify multiple non-consecutive versions, such as exactly 1.0.3 plus versions greater than 1.5.0, etc...

解决方案

In a nutshell, the syntax for Bower version numbers (and NPM's) is called SemVer, which is short for 'Semantic Versioning'. You can find documentation for the detailed syntax of SemVer as used in Bower and NPM on the API for the semver parser within Node/npm. You can learn more about the underlying spec (which does not mention ~ or other syntax details) at semver.org.

There's a super-handy visual semver calculator you can play with, making all of this much easier to grok and test.

SemVer isn't just a syntax! It has some pretty interesting things to say about the right ways to publish API's, which will help to understand what the syntax means. Crucially:

Once you identify your public API, you communicate changes to it with specific increments to your version number. Consider a version format of X.Y.Z (Major.Minor.Patch). Bug fixes not affecting the API increment the patch version, backwards compatible API additions/changes increment the minor version, and backwards incompatible API changes increment the major version.

So, your specific question about ~ relates to that Major.Minor.Patch schema. (As does the related caret operator ^.) You can use ~ to narrow the range of versions you're willing to accept to either:

  • subsequent patch-level changes to the same minor version ("bug fixes not affecting the API"), or:
  • subsequent minor-level changes to the same major version ("backwards compatible API additions/changes")

For example: to indicate you'll take any subsequent patch-level changes on the 1.2.x tree, starting with 1.2.0, but less than 1.3.0, you could use:

"angular": "~1.2"
  or:
"angular": "~1.2.0"

This also gets you the same results as using the .x syntax:

"angular": "1.2.x"

But, you can use the tilde/~ syntax to be even more specific: if you're only willing to accept patch-level changes starting with 1.2.4, but still less than 1.3.0, you'd use:

"angular": "~1.2.4"

Moving left, towards the major version, if you use...

"angular": "~1"

... it's the same as...

"angular": "1.x"
  or:
"angular": "^1.0.0"

...and matches any minor- or patch-level changes above 1.0.0, and less than 2.0:

Note that last variation above: it's called a 'caret range'. The caret looks an awful lot like a >, so you'd be excused for thinking it means "any version greater than 1.0.0". (I've certainly slipped on that.) Nope!

Caret ranges are basically used to say that you care only about the left-most significant digit - usually the major version - and that you'll permit any minor- or patch-level changes that don't affect that left-most digit. Yet, unlike a tilde range that specifies a major version, caret ranges let you specify a precise minor/patch starting point. So, while ^1.0.0 === ~1, a caret range such as ^1.2.3 lets you say you'll take any changes >=1.2.3 && <2.0.0. You couldn't do that with a tilde range.

That all seems confusing at first, when you look at it up-close. But zoom out for a sec, and think about it this way: the caret simply lets you say that you're most concerned about whatever significant digit is left-most. The tilde lets you say you're most concerned about whichever digit is right-most. The rest is detail.

It's the expressive power of the tilde and the caret that explains why people use them much more than the simpler .x syntax: they simply let you do more. That's why you'll see the tilde used often even where .x would serve. As an example, see npm itself: its own package.json file includes lots of dependencies in ~2.4.0 format, rather than the 2.4.x format it could use. By sticking to ~, the syntax is consistent all the way down a list of 70+ versioned dependencies, regardless of which beginning patch number is acceptable.

Anyway, there's still more to SemVer, but I won't try to detail it all here. Check it out on the node semver package's readme. And be sure to use the semantic versioning calculator while you're practicing and trying to get your head around how SemVer works.


RE: Non-Consecutive Version Numbers: OP's final question seems to be about specifying non-consecutive version numbers/ranges (if I have edited it fairly). Yes, you can do that, using the common double-pipe "or" operator: ||. Like so:

"angular": "1.2 <= 1.2.9 || >2.0.0"

这篇关于bower(和npm)版本的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆