1414from continuous_delivery_scripts .utils .configuration import configuration , ConfigurationVariable
1515from continuous_delivery_scripts .utils .logging import log_exception , set_log_level
1616from continuous_delivery_scripts .utils .filesystem_helpers import cd
17- from typing import Optional , Tuple
17+ from typing import Optional , Tuple , Dict
1818
1919logger = logging .getLogger (__name__ )
2020
2121
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 ] ]:
2323 """Versions the project.
2424
2525 Args:
@@ -30,12 +30,12 @@ def version_project(commit_type: CommitType) -> Tuple[bool, Optional[str]]:
3030 (is new version, the new version)
3131 """
3232 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 )
3434 _generate_changelog (new_version , use_news_files )
35- return is_new_version , new_version
35+ return is_new_version , new_version , version_elements
3636
3737
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 ] ]:
3939 """Calculates the version for the release.
4040
4141 eg. "0.1.2"
@@ -66,13 +66,44 @@ def _calculate_version(commit_type: CommitType, use_news_files: bool) -> Tuple[b
6666 # Autoversion second returned value is not actually the new version
6767 # There seem to be a bug in autoversion.
6868 # 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 )
7271 is_new_version = old != new_version
7372 logger .info (":: Determining the new version" )
7473 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+ }
76107
77108
78109def _generate_changelog (version : Optional [str ], use_news_files : bool ) -> None :
@@ -102,7 +133,10 @@ def main() -> None:
102133 set_log_level (args .verbose )
103134
104135 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 )
106140 except Exception as e :
107141 log_exception (logger , e )
108142 sys .exit (1 )
0 commit comments