group.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package version
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. )
  7. type ConstraintGroup struct {
  8. constraints []*Constraint
  9. }
  10. // Return a new NewConstrainGroup
  11. func NewConstrainGroup() *ConstraintGroup {
  12. group := new(ConstraintGroup)
  13. return group
  14. }
  15. // Return a new NewConstrainGroup and create the constraints based on a string
  16. //
  17. // Version constraints can be specified in a few different ways:
  18. //
  19. // Exact version: You can specify the exact version of a package, for
  20. // example 1.0.2.
  21. //
  22. // Range: By using comparison operators you can specify ranges of valid versions.
  23. // Valid operators are >, >=, <, <=, !=. An example range would be >=1.0. You can
  24. // define multiple ranges, separated by a comma: >=1.0,<2.0.
  25. //
  26. // Wildcard: You can specify a pattern with a * wildcard. 1.0.* is the equivalent
  27. // of >=1.0,<1.1.
  28. //
  29. // Next Significant Release (Tilde Operator): The ~ operator is best explained by
  30. // example: ~1.2 is equivalent to >=1.2,<2.0, while ~1.2.3 is equivalent to
  31. // >=1.2.3,<1.3. As you can see it is mostly useful for projects respecting
  32. // semantic versioning. A common usage would be to mark the minimum minor
  33. // version you depend on, like ~1.2 (which allows anything up to, but not
  34. // including, 2.0). Since in theory there should be no backwards compatibility
  35. // breaks until 2.0, that works well. Another way of looking at it is that
  36. // using ~ specifies a minimum version, but allows the last digit specified
  37. // to go up.
  38. //
  39. // By default only stable releases are taken into consideration. If you would like
  40. // to also get RC, beta, alpha or dev versions of your dependencies you can do so
  41. // using stability flags. To change that for all packages instead of doing per
  42. // dependency you can also use the minimum-stability setting.
  43. //
  44. // From: http://getcomposer.org/doc/01-basic-usage.md#package-versions
  45. func NewConstrainGroupFromString(name string) *ConstraintGroup {
  46. group := new(ConstraintGroup)
  47. group.fromString(name)
  48. return group
  49. }
  50. // Adds a Contraint to the group
  51. func (self *ConstraintGroup) AddConstraint(constraint ...*Constraint) {
  52. if self.constraints == nil {
  53. self.constraints = make([]*Constraint, 0)
  54. }
  55. self.constraints = append(self.constraints, constraint...)
  56. }
  57. // Return all the constraints
  58. func (self *ConstraintGroup) GetConstraints() []*Constraint {
  59. return self.constraints
  60. }
  61. // Match a given version againts the group
  62. //
  63. // Usage
  64. // c := version.NewConstrainGroupFromString(">2.0,<=3.0")
  65. // c.Match("2.5.0beta")
  66. // Returns: true
  67. //
  68. // c := version.NewConstrainGroupFromString("~1.2.3")
  69. // c.Match("1.2.3.5")
  70. // Returns: true
  71. func (self *ConstraintGroup) Match(version string) bool {
  72. for _, constraint := range self.constraints {
  73. if constraint.Match(version) == false {
  74. return false
  75. }
  76. }
  77. return true
  78. }
  79. func (self *ConstraintGroup) fromString(constraint string) bool {
  80. result := RegFind(`(?i)^([^,\s]*?)@(stable|RC|beta|alpha|dev)$`, constraint)
  81. if result != nil {
  82. constraint = result[1]
  83. if constraint == "" {
  84. constraint = "*"
  85. }
  86. }
  87. result = RegFind(`(?i)^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$`, constraint)
  88. if result != nil {
  89. if result[1] != "" {
  90. constraint = result[1]
  91. }
  92. }
  93. constraints := RegSplit(`\s*,\s*`, strings.Trim(constraint, " "))
  94. if len(constraints) > 1 {
  95. for _, part := range constraints {
  96. self.AddConstraint(self.parseConstraint(part)...)
  97. }
  98. return true
  99. }
  100. self.AddConstraint(self.parseConstraint(constraints[0])...)
  101. return true
  102. }
  103. func (self *ConstraintGroup) parseConstraint(constraint string) []*Constraint {
  104. stabilityModifier := ""
  105. result := RegFind(`(?i)^([^,\s]+?)@(stable|RC|beta|alpha|dev)$`, constraint)
  106. if result != nil {
  107. constraint = result[1]
  108. if result[2] != "stable" {
  109. stabilityModifier = result[2]
  110. }
  111. }
  112. result = RegFind(`^[x*](\.[x*])*$`, constraint)
  113. if result != nil {
  114. return make([]*Constraint, 0)
  115. }
  116. highVersion := ""
  117. lowVersion := ""
  118. result = RegFind(`(?i)^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?`+modifierRegex+`?$`, constraint)
  119. if result != nil {
  120. if len(result) > 4 && result[4] != "" {
  121. last, _ := strconv.Atoi(result[3])
  122. highVersion = result[1] + "." + result[2] + "." + strconv.Itoa(last+1) + ".0-dev"
  123. lowVersion = result[1] + "." + result[2] + "." + result[3] + "." + result[4]
  124. } else if len(result) > 3 && result[3] != "" {
  125. last, _ := strconv.Atoi(result[2])
  126. highVersion = result[1] + "." + strconv.Itoa(last+1) + ".0.0-dev"
  127. lowVersion = result[1] + "." + result[2] + "." + result[3] + ".0"
  128. } else {
  129. last, _ := strconv.Atoi(result[1])
  130. highVersion = strconv.Itoa(last+1) + ".0.0.0-dev"
  131. if len(result) > 2 && result[2] != "" {
  132. lowVersion = result[1] + "." + result[2] + ".0.0"
  133. } else {
  134. lowVersion = result[1] + ".0.0.0"
  135. }
  136. }
  137. if len(result) > 5 && result[5] != "" {
  138. lowVersion = lowVersion + "-" + expandStability(result[5])
  139. }
  140. if len(result) > 6 && result[6] != "" {
  141. lowVersion = lowVersion + result[6]
  142. }
  143. if len(result) > 7 && result[7] != "" {
  144. lowVersion = lowVersion + "-dev"
  145. }
  146. return []*Constraint{
  147. {">=", lowVersion},
  148. {"<", highVersion},
  149. }
  150. }
  151. result = RegFind(`^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$`, constraint)
  152. if result != nil {
  153. if len(result) > 3 && result[3] != "" {
  154. highVersion = result[1] + "." + result[2] + "." + result[3] + ".9999999"
  155. if result[3] == "0" {
  156. last, _ := strconv.Atoi(result[2])
  157. lowVersion = result[1] + "." + strconv.Itoa(last-1) + ".9999999.9999999"
  158. } else {
  159. last, _ := strconv.Atoi(result[3])
  160. lowVersion = result[1] + "." + result[2] + "." + strconv.Itoa(last-1) + ".9999999"
  161. }
  162. } else if len(result) > 2 && result[2] != "" {
  163. highVersion = result[1] + "." + result[2] + ".9999999.9999999"
  164. if result[2] == "0" {
  165. last, _ := strconv.Atoi(result[1])
  166. lowVersion = strconv.Itoa(last-1) + ".9999999.9999999.9999999"
  167. } else {
  168. last, _ := strconv.Atoi(result[2])
  169. lowVersion = result[1] + "." + strconv.Itoa(last-1) + ".9999999.9999999"
  170. }
  171. } else {
  172. highVersion = result[1] + ".9999999.9999999.9999999"
  173. if result[1] == "0" {
  174. return []*Constraint{{"<", highVersion}}
  175. } else {
  176. last, _ := strconv.Atoi(result[1])
  177. lowVersion = strconv.Itoa(last-1) + ".9999999.9999999.9999999"
  178. }
  179. }
  180. return []*Constraint{
  181. {">", lowVersion},
  182. {"<", highVersion},
  183. }
  184. }
  185. // match operators constraints
  186. result = RegFind(`^(<>|!=|>=?|<=?|==?)?\s*(.*)`, constraint)
  187. if result != nil {
  188. version := Normalize(result[2])
  189. if stabilityModifier != "" && parseStability(version) == "stable" {
  190. version = version + "-" + stabilityModifier
  191. } else if result[1] == "<" {
  192. match := RegFind(`(?i)-stable$`, result[2])
  193. if match == nil {
  194. version = version + "-dev"
  195. }
  196. }
  197. if len(result) > 1 && result[1] != "" {
  198. return []*Constraint{{result[1], version}}
  199. } else {
  200. return []*Constraint{{"=", version}}
  201. }
  202. }
  203. return []*Constraint{{constraint, stabilityModifier}}
  204. }
  205. func RegFind(pattern, subject string) []string {
  206. reg := regexp.MustCompile(pattern)
  207. matched := reg.FindAllStringSubmatch(subject, -1)
  208. if matched != nil {
  209. return matched[0]
  210. }
  211. return nil
  212. }
  213. func RegSplit(pattern, subject string) []string {
  214. reg := regexp.MustCompile(pattern)
  215. indexes := reg.FindAllStringIndex(subject, -1)
  216. laststart := 0
  217. result := make([]string, len(indexes)+1)
  218. for i, element := range indexes {
  219. result[i] = subject[laststart:element[0]]
  220. laststart = element[1]
  221. }
  222. result[len(indexes)] = subject[laststart:len(subject)]
  223. return result
  224. }