11from __future__ import annotations
22
33import json
4+ import os
45import shutil
56import subprocess
67from pathlib import Path
78from typing import TYPE_CHECKING
89from urllib .request import urlopen
910
11+ from natsort import natsorted
12+
1013if TYPE_CHECKING :
1114 from collections .abc import KeysView
1215
@@ -18,22 +21,18 @@ def download_openapi_generator_jar(version: str) -> None:
1821 MVN_BASE_URL
1922 + "/remotecontent?filepath=org/openapitools/openapi-generator-cli/"
2023 + version
21- + "/openapi-generator-cli-"
22- + version
23- + ".jar"
24+ + f"/openapi-generator-cli-{ version } .jar"
2425 )
2526
2627 Path ("openapi-generator.jar" ).unlink (missing_ok = True )
2728
28- print (download_url )
29+ print (f"[ { version } ] URL: { download_url !r } " )
2930 response = urlopen (download_url ) # noqa: S310
3031
3132 if response .status != 200 : # noqa: PLR2004
3233 msg = f"{ response .status } : { download_url } "
3334 raise RuntimeError (msg )
3435
35- print ("Downloading complete" )
36-
3736 with Path ("openapi_generator_cli/openapi-generator.jar" ).open ("wb" ) as openapi_generator_jar :
3837 openapi_generator_jar .write (response .read ())
3938 openapi_generator_jar .close ()
@@ -78,18 +77,43 @@ def get_published_vesions() -> KeysView[str]:
7877 return published_releases .keys ()
7978
8079
81- def publish () -> None :
82- latest_version = get_available_versions ()[0 ]
83- published_versions = get_published_vesions ()
80+ def download_latest_jar_for_test () -> None :
81+ latest_version = natsorted (get_available_versions ())[- 1 ]
82+ print (f"[{ latest_version } ] Downloading..." )
83+ download_openapi_generator_jar (latest_version )
84+ print (f"[{ latest_version } ] Downloaded!" )
85+
86+
87+ def publish (* , dryrun : bool = False ) -> None :
88+ pytest_path = shutil .which ("pytest" )
89+ poetry_path = shutil .which ("poetry" )
90+
91+ unpublished_versions = natsorted (set (get_available_versions ()) - set (get_published_vesions ()))
92+
93+ for publishing_version in unpublished_versions :
94+ print (f"[{ publishing_version } ] Downloading..." )
95+ download_openapi_generator_jar (publishing_version )
96+
97+ print (f"[{ publishing_version } ] Testing..." )
98+ subprocess .check_call ([pytest_path ])
99+
100+ if dryrun :
101+ continue
102+
103+ print (f"[{ publishing_version } ] Building..." )
104+ subprocess .check_call ([poetry_path , "build" , "-v" ])
105+
106+ print (f"[{ publishing_version } ] Publishing to TestPyPI..." )
107+ subprocess .check_call ([poetry_path , "publish" , "-r" , "testpypi" , "-v" ])
108+
109+ print (f"[{ publishing_version } ] Publishing to PyPI..." )
110+ subprocess .check_call ([poetry_path , "publish" , "-v" ])
84111
85- if latest_version not in published_versions :
86- print ("Publishing version " + latest_version )
87- download_openapi_generator_jar (latest_version )
88- poetry_path = shutil .which ("poetry" )
89- subprocess .check_call ([poetry_path , "build" ]) # noqa: S603
90- subprocess .check_call ([poetry_path , "publish" , "-r" , "testpypi" ]) # noqa: S603
91- subprocess .check_call ([poetry_path , "publish" ]) # noqa: S603
112+ print (f"[{ publishing_version } ] Published!" )
92113
93114
94115if __name__ == "__main__" :
95- publish ()
116+ if os .getenv ("DOWNLOAD_LATEST_ONLY" ) == "1" :
117+ download_latest_jar_for_test ()
118+ else :
119+ publish (dryrun = os .getenv ("DRYRUN" ) == "1" )
0 commit comments