SemVer.hx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package tools.haxelib;
  2. using Std;
  3. enum Preview {
  4. ALPHA;
  5. BETA;
  6. RC;
  7. REV;
  8. }
  9. class SemVer {
  10. public var major:Int;
  11. public var minor:Int;
  12. public var patch:Int;
  13. public var preview:Null<Preview>;
  14. public var previewNum:Null<Int>;
  15. public function new(major, minor, patch, ?preview, ?previewNum) {
  16. this.major = major;
  17. this.minor = minor;
  18. this.patch = patch;
  19. this.preview = preview;
  20. this.previewNum = previewNum;
  21. }
  22. public function toString():String {
  23. var ret = '$major.$minor.$patch';
  24. if (preview != null) {
  25. ret += '-' + preview.getName().toLowerCase();
  26. if (previewNum != null)
  27. ret += '.' + previewNum;
  28. }
  29. return ret;
  30. }
  31. static var parse = ~/^([0-9]+)\.([0-9]+)\.([0-9]+)(-(alpha|beta|rc|rev)(\.([0-9]+))?)?$/;
  32. static public function ofString(s:String):SemVer
  33. return
  34. if (parse.match(s))
  35. new SemVer(
  36. parse.matched(1).parseInt(),
  37. parse.matched(2).parseInt(),
  38. parse.matched(3).parseInt(),
  39. switch parse.matched(5) {
  40. case 'alpha': ALPHA;
  41. case 'beta': BETA;
  42. case 'rc': RC;
  43. case 'rev': REV;
  44. case v if (v == null): null;
  45. case v: throw 'unrecognized preview tag $v';
  46. },
  47. switch parse.matched(7) {
  48. case v if (v == null): null;
  49. case v: v.parseInt();
  50. }
  51. )
  52. else
  53. throw '$s is not a valid version string (should be major.minor.patch[-(alpha|beta|rc|rev)[.version]]';
  54. }