SemVer.hx 1.3 KB

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