|
1 | 1 | from pathlib import Path |
| 2 | +from sorcery import assigned_names |
| 3 | +from enum import Enum |
2 | 4 |
|
3 | 5 |
|
4 | | -def bump_version_import(module_name: str, package: str = None) -> str: |
| 6 | +class BumpOrderEnum(Enum): |
| 7 | + major, minor, patch = assigned_names() |
| 8 | + micro = patch |
| 9 | + epoch = major |
| 10 | + |
| 11 | + |
| 12 | +def bump_version_import( |
| 13 | + module_name: str, package: str = None, order: BumpOrderEnum = BumpOrderEnum.patch |
| 14 | +) -> str: |
5 | 15 | import importlib |
6 | 16 |
|
7 | 17 | module = importlib.import_module(module_name, package) |
8 | 18 |
|
9 | 19 | version = module.__version__ |
10 | 20 |
|
11 | | - base, _, minor = version.rpartition(".") |
12 | | - return base + "." + str(int(minor) + 1) |
| 21 | + return version_bump(version) |
| 22 | + |
| 23 | + |
| 24 | +def version_partition(version: str) -> str: |
| 25 | + dot_count = version.count(".") |
| 26 | + |
| 27 | + if dot_count == 1: |
| 28 | + major, minor = version.split(".") |
| 29 | + return int(major), int(minor) |
| 30 | + |
| 31 | + elif dot_count >= 2: |
| 32 | + major, minor, patch, *rest = version.split(".") |
| 33 | + |
| 34 | + return int(major), int(minor), int(patch), *rest |
| 35 | + |
| 36 | + return (version,) |
| 37 | + |
| 38 | + |
| 39 | +def version_bump(version: str, order: BumpOrderEnum = BumpOrderEnum.patch) -> str: |
| 40 | + partitioned = version_partition(version) |
| 41 | + |
| 42 | + if len(partitioned) == 1: |
| 43 | + return str(int(partitioned[0]) + 1) |
| 44 | + |
| 45 | + pre = post = "" |
| 46 | + |
| 47 | + if order == BumpOrderEnum.major: |
| 48 | + ordered, *post = partitioned |
| 49 | + post = ".".join([str(p) for p in post]) |
| 50 | + |
| 51 | + elif order == BumpOrderEnum.minor: |
| 52 | + if len(partitioned) == 2: |
| 53 | + pre, ordered = partitioned |
| 54 | + else: |
| 55 | + pre, ordered, *post = partitioned |
| 56 | + post = ".".join([str(p) for p in post]) |
| 57 | + pre = str(pre) |
| 58 | + |
| 59 | + elif order == BumpOrderEnum.patch: |
| 60 | + if len(partitioned) == 2: |
| 61 | + pre, ordered = partitioned |
| 62 | + else: |
| 63 | + major, minor, ordered, *post = partitioned |
| 64 | + pre = ".".join((str(major), str(minor))) |
| 65 | + post = ".".join([str(p) for p in post]) |
| 66 | + pre = str(pre) |
| 67 | + |
| 68 | + if pre != "": |
| 69 | + pre = str(pre) + "." |
| 70 | + |
| 71 | + if post != "": |
| 72 | + post = "." + str(post) |
| 73 | + |
| 74 | + return pre + str(int(ordered) + 1) + post |
13 | 75 |
|
14 | 76 |
|
15 | 77 | def bump_version_regex(file_path: Path, search_regex_str: str) -> str: |
@@ -38,3 +100,13 @@ def bump_version_input() -> str: |
38 | 100 |
|
39 | 101 | if __name__ == "__main__": |
40 | 102 | print(bump_version_import("warg")) |
| 103 | + |
| 104 | + def ujhasduhau(): |
| 105 | + for e in BumpOrderEnum: |
| 106 | + print("__") |
| 107 | + print(version_bump("1", order=e)) |
| 108 | + print(version_bump("1.2", order=e)) |
| 109 | + print(version_bump("1.2.3", order=e)) |
| 110 | + print(version_bump("1.2.3.4", order=e)) |
| 111 | + |
| 112 | + ujhasduhau() |
0 commit comments