| | | 1 | | package domain |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "fmt" |
| | | 5 | | "regexp" |
| | | 6 | | "strconv" |
| | | 7 | | ) |
| | | 8 | | |
| | | 9 | | // Version represents a Neovim release version. The Tag field is the canonical |
| | | 10 | | // form used in GitHub release URLs (e.g. "v0.10.4", "stable", "nightly"). |
| | | 11 | | type Version struct { |
| | | 12 | | Major int |
| | | 13 | | Minor int |
| | | 14 | | Patch int |
| | | 15 | | // Tag is the GitHub release tag, e.g. "stable", "nightly", or "v0.10.4". |
| | | 16 | | Tag string |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | var semverRe = regexp.MustCompile(`^v?(\d+)\.(\d+)\.(\d+)$`) |
| | | 20 | | |
| | | 21 | | // ParseVersion parses a version string. It accepts: |
| | | 22 | | // - "stable" and "nightly" as special tags with zero Major/Minor/Patch |
| | | 23 | | // - semantic version strings like "0.10.4" or "v0.10.4" |
| | | 24 | | func ParseVersion(s string) (Version, error) { |
| | | 25 | | if s == "stable" || s == "nightly" { |
| | | 26 | | return Version{Tag: s}, nil |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | m := semverRe.FindStringSubmatch(s) |
| | | 30 | | if m == nil { |
| | | 31 | | return Version{}, fmt.Errorf("invalid neovim version %q: want \"stable\", \"nightly\", or semver like \"0.10.4\"", s |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | major, _ := strconv.Atoi(m[1]) |
| | | 35 | | minor, _ := strconv.Atoi(m[2]) |
| | | 36 | | patch, _ := strconv.Atoi(m[3]) |
| | | 37 | | |
| | | 38 | | tag := s |
| | | 39 | | if s[0] != 'v' { |
| | | 40 | | tag = "v" + s |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | return Version{Major: major, Minor: minor, Patch: patch, Tag: tag}, nil |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // String returns the canonical version tag. |
| | 4 | 47 | | func (v Version) String() string { |
| | 4 | 48 | | return v.Tag |
| | 4 | 49 | | } |
| | | 50 | | |
| | | 51 | | // AssetName returns the filename of the Neovim release archive for a given platform. |
| | | 52 | | // These match the actual asset names used in Neovim GitHub releases. |
| | 6 | 53 | | func (v Version) AssetName(p Platform) string { |
| | 6 | 54 | | switch p.OS { |
| | 2 | 55 | | case OSDarwin: |
| | 2 | 56 | | // As of Neovim 0.10, GitHub releases ship separate x86_64 and arm64 |
| | 2 | 57 | | // tarballs for macOS rather than a single universal binary. |
| | 1 | 58 | | if p.Arch == ArchARM64 { |
| | 1 | 59 | | return "nvim-macos-arm64.tar.gz" |
| | 1 | 60 | | } |
| | 1 | 61 | | return "nvim-macos-x86_64.tar.gz" |
| | 2 | 62 | | case OSLinux: |
| | 2 | 63 | | switch p.Arch { |
| | 1 | 64 | | case ArchARM64: |
| | 1 | 65 | | return "nvim-linux-arm64.tar.gz" |
| | 1 | 66 | | default: |
| | 1 | 67 | | return "nvim-linux-x86_64.tar.gz" |
| | | 68 | | } |
| | 1 | 69 | | case OSWindows: |
| | 1 | 70 | | return "nvim-win64.zip" |
| | | 71 | | } |
| | 1 | 72 | | return "" |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // BinaryName returns the Neovim executable name for a platform. |
| | | 76 | | func BinaryName(p Platform) string { |
| | | 77 | | if p.OS == OSWindows { |
| | | 78 | | return "nvim.exe" |
| | | 79 | | } |
| | | 80 | | return "nvim" |
| | | 81 | | } |