14
14
from continuous_delivery_scripts .utils .configuration import configuration , ConfigurationVariable
15
15
from continuous_delivery_scripts .utils .logging import log_exception , set_log_level
16
16
from continuous_delivery_scripts .utils .filesystem_helpers import cd
17
- from typing import Optional , Tuple
17
+ from typing import Optional , Tuple , Dict
18
18
19
19
logger = logging .getLogger (__name__ )
20
20
21
21
22
- def version_project (commit_type : CommitType ) -> Tuple [bool , Optional [str ]]:
22
+ def version_project (commit_type : CommitType ) -> Tuple [bool , Optional [str ], Dict [ str , str ] ]:
23
23
"""Versions the project.
24
24
25
25
Args:
@@ -30,12 +30,12 @@ def version_project(commit_type: CommitType) -> Tuple[bool, Optional[str]]:
30
30
(is new version, the new version)
31
31
"""
32
32
use_news_files = commit_type in [CommitType .BETA , CommitType .RELEASE ]
33
- is_new_version , new_version = _calculate_version (commit_type , use_news_files )
33
+ is_new_version , new_version , version_elements = _calculate_version (commit_type , use_news_files )
34
34
_generate_changelog (new_version , use_news_files )
35
- return is_new_version , new_version
35
+ return is_new_version , new_version , version_elements
36
36
37
37
38
- def _calculate_version (commit_type : CommitType , use_news_files : bool ) -> Tuple [bool , Optional [str ]]:
38
+ def _calculate_version (commit_type : CommitType , use_news_files : bool ) -> Tuple [bool , Optional [str ], Dict [ str , str ] ]:
39
39
"""Calculates the version for the release.
40
40
41
41
eg. "0.1.2"
@@ -66,13 +66,44 @@ def _calculate_version(commit_type: CommitType, use_news_files: bool) -> Tuple[b
66
66
# Autoversion second returned value is not actually the new version
67
67
# There seem to be a bug in autoversion.
68
68
# This is why the following needs to be done to determine the version
69
- for k , v in updates .items ():
70
- if "version" in str (k ).lower ():
71
- new_version = updates [k ]
69
+ version_elements = _get_version_elements (updates )
70
+ new_version = version_elements .get (auto_version_tool .Constants .VERSION_FIELD , new_version )
72
71
is_new_version = old != new_version
73
72
logger .info (":: Determining the new version" )
74
73
logger .info (f"Version: { new_version } " )
75
- return is_new_version , new_version
74
+ return is_new_version , new_version , version_elements
75
+
76
+
77
+ def _update_version_string (
78
+ commit_type : CommitType , new_version : Optional [str ], version_elements : Dict [str , str ]
79
+ ) -> Optional [str ]:
80
+ """Updates the version string for development releases.
81
+
82
+ Args:
83
+ commit_type: commit type
84
+ new_version: the new version
85
+ version_elements: version elements
86
+ """
87
+ if commit_type == CommitType .DEVELOPMENT :
88
+ return "%s-%s.%s" % (
89
+ new_version ,
90
+ auto_version_tool .config .BUILD_TOKEN ,
91
+ version_elements .get (auto_version_tool .Constants .COMMIT_FIELD ),
92
+ )
93
+ return new_version
94
+
95
+
96
+ def _get_version_elements (native_version_elements : Dict [str , str ]) -> Dict [str , str ]:
97
+ """Determines the different version elements.
98
+
99
+ Args:
100
+ native_version_elements: native version elements as understood by autoversion
101
+ """
102
+ return {
103
+ key : native_version_elements [native ]
104
+ for native , key in auto_version_tool .config .key_aliases .items ()
105
+ if native in native_version_elements
106
+ }
76
107
77
108
78
109
def _generate_changelog (version : Optional [str ], use_news_files : bool ) -> None :
@@ -102,7 +133,10 @@ def main() -> None:
102
133
set_log_level (args .verbose )
103
134
104
135
try :
105
- version_project (CommitType .parse (args .release_type ))
136
+ commit_type = CommitType .parse (args .release_type )
137
+ is_new_version , new_version , version_elements = version_project (commit_type )
138
+ version_to_print = _update_version_string (commit_type , new_version , version_elements )
139
+ print (version_to_print )
106
140
except Exception as e :
107
141
log_exception (logger , e )
108
142
sys .exit (1 )
0 commit comments