22from dataclasses import dataclass
33from enum import Enum
44from pathlib import Path
5+ from typing import (
6+ Callable ,
7+ Optional ,
8+ )
59
10+ from exasol .toolbox .util .version import Version
611
7- def update_github_yml (template : Path , version : str ) -> None :
12+
13+ def update_github_yml (template : Path , version : Version ) -> None :
814 """Updates versions in GitHub workflows and actions"""
915 with open (template , encoding = "utf-8" ) as file :
1016 content = file .readlines ()
@@ -19,10 +25,7 @@ def update_github_yml(template: Path, version: str) -> None:
1925class Pattern :
2026 match_pattern : str
2127 break_pattern : str
22-
23- @property
24- def version_pattern (self ) -> str :
25- return r"[0-9]+\.[0-9]+\.[0-9]+"
28+ version_pattern : str
2629
2730 @property
2831 def full_pattern (self ) -> str :
@@ -36,24 +39,58 @@ def replace_version(self, line: str, version: str) -> str:
3639 )
3740
3841
39- class ToolboxPattern (Enum ):
40- github = Pattern (
41- match_pattern = "exasol/python-toolbox/.github/[^/]+/[^/]+" ,
42- break_pattern = "@" ,
42+ def full_version_modifier (version : Version ) -> str :
43+ return str (version )
44+
45+
46+ def major_version_modifier (version : Version ) -> str :
47+ return f"v{ version .major } "
48+
49+
50+ class GithubActionsReplacement :
51+ def __init__ (
52+ self , pattern : Pattern , version_string_modifier : Callable [[Version ], str ]
53+ ) -> None :
54+ self .pattern = pattern
55+ self .version_string_modifier = version_string_modifier
56+
57+ def replace_version (self , line : str , version : Version ) -> Optional [str ]:
58+ match = re .search (self .pattern .full_pattern , line )
59+ if match :
60+ return self .pattern .replace_version (
61+ line = line , version = self .version_string_modifier (version )
62+ )
63+ return None
64+
65+
66+ class Replacements (Enum ):
67+ github = GithubActionsReplacement (
68+ pattern = Pattern (
69+ match_pattern = "exasol/python-toolbox/.github/[^/]+/[^/]+" ,
70+ break_pattern = "@" ,
71+ version_pattern = r"[0-9]+\.[0-9]+\.[0-9]+" ,
72+ ),
73+ version_string_modifier = major_version_modifier ,
4374 )
44- pypi = Pattern (
45- match_pattern = "exasol-toolbox" ,
46- break_pattern = "==" ,
75+
76+ pypi = GithubActionsReplacement (
77+ pattern = Pattern (
78+ match_pattern = "exasol-toolbox" ,
79+ break_pattern = "==" ,
80+ version_pattern = r"[0-9]+\.[0-9]+\.[0-9]+" ,
81+ ),
82+ version_string_modifier = full_version_modifier ,
4783 )
4884
4985
50- def _update_line_with_version (line : str , version : str ) -> str :
51- for pattern in ToolboxPattern :
52- match = re .search (pattern .value .full_pattern , line )
53- if match :
54- return pattern .value .replace_version (line = line , version = version )
86+ def _update_line_with_version (line : str , version : Version ) -> str :
87+ for replacement in Replacements :
88+ if replaced_line := replacement .value .replace_version (
89+ line = line , version = version
90+ ):
91+ return replaced_line
5592 return line
5693
5794
58- def update_versions (lines : list [str ], version : str ) -> list [str ]:
95+ def update_versions (lines : list [str ], version : Version ) -> list [str ]:
5996 return [_update_line_with_version (line = line , version = version ) for line in lines ]
0 commit comments