version.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package version
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. )
  7. var (
  8. numberRe = regexp.MustCompile("[0-9]+")
  9. wordRe = regexp.MustCompile("[a-z]+")
  10. )
  11. func GreaterThan(a, b string) bool {
  12. a = stripMetadata(a)
  13. b = stripMetadata(b)
  14. a = strings.TrimLeft(a, "v")
  15. b = strings.TrimLeft(b, "v")
  16. aSplit := periodDashSplit(a)
  17. bSplit := periodDashSplit(b)
  18. if len(bSplit) > len(aSplit) {
  19. return !GreaterThan(b, a) && a != b
  20. }
  21. for i := 0; i < len(aSplit); i++ {
  22. if i == len(bSplit) {
  23. if _, err := strconv.Atoi(aSplit[i]); err == nil {
  24. return true
  25. }
  26. return false
  27. }
  28. aWord := wordRe.FindString(aSplit[i])
  29. bWord := wordRe.FindString(bSplit[i])
  30. if aWord != "" && bWord != "" {
  31. if strings.Compare(aWord, bWord) > 0 {
  32. return true
  33. }
  34. if strings.Compare(bWord, aWord) > 0 {
  35. return false
  36. }
  37. }
  38. aMatch := numberRe.FindString(aSplit[i])
  39. bMatch := numberRe.FindString(bSplit[i])
  40. if aMatch == "" || bMatch == "" {
  41. if strings.Compare(aSplit[i], bSplit[i]) > 0 {
  42. return true
  43. }
  44. if strings.Compare(bSplit[i], aSplit[i]) > 0 {
  45. return false
  46. }
  47. }
  48. aNum, _ := strconv.Atoi(aMatch)
  49. bNum, _ := strconv.Atoi(bMatch)
  50. if aNum > bNum {
  51. return true
  52. }
  53. if bNum > aNum {
  54. return false
  55. }
  56. }
  57. return false
  58. }
  59. func stripMetadata(v string) string {
  60. split := strings.Split(v, "+")
  61. if len(split) > 1 {
  62. return split[0]
  63. }
  64. return v
  65. }
  66. func periodDashSplit(s string) []string {
  67. return strings.FieldsFunc(s, func(r rune) bool {
  68. switch r {
  69. case '.', '-':
  70. return true
  71. }
  72. return false
  73. })
  74. }