44#
55"""Plugin for Golang projects."""
66import logging
7+ import os
78from pathlib import Path
8- from typing import Optional
9+ from typing import Optional , List
10+ from subprocess import check_call
911from continuous_delivery_scripts .utils .language_specifics_base import BaseLanguage , get_language_from_file_name
1012from continuous_delivery_scripts .spdx_report .spdx_project import SpdxProject
13+ from continuous_delivery_scripts .utils .configuration import configuration , ConfigurationVariable
1114
1215logger = logging .getLogger (__name__ )
1316
17+ SRC_DIR = configuration .get_value (ConfigurationVariable .SOURCE_DIR )
18+ ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
19+ ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
20+
21+
22+ def _generate_golds_command_list (output_directory : Path , module : str ) -> List [str ]:
23+ return ["golds" , "-gen" , "-wdpkgs-listing=promoted" , f"-dir={ str (output_directory )} " , "-nouses" , f"{ module } " ]
24+
25+
26+ def _generate_goreleaser_release_command_list (changelog : Path ) -> List [str ]:
27+ return [
28+ "goreleaser" ,
29+ "release" ,
30+ "--rm-dist" ,
31+ "--release-notes" ,
32+ f"{ str (changelog )} " ,
33+ ]
34+
35+
36+ def _generate_goreleaser_check_command_list () -> List [str ]:
37+ return [
38+ "goreleaser" ,
39+ "check" ,
40+ ]
41+
42+
43+ def _install_golds_command_list () -> List [str ]:
44+ return ["go" , "install" , "go101.org/golds@latest" ]
45+
46+
47+ def _install_goreleaser_command_list () -> List [str ]:
48+ return ["go" , "install" , "github.com/goreleaser/goreleaser@latest" ]
49+
50+
51+ def _call_golds (output_directory : Path , module : str ) -> None :
52+ """Calls Golds for generating the docs."""
53+ logger .info ("Installing Golds if missing." )
54+ check_call (_install_golds_command_list ())
55+ logger .info ("Creating Golds documentation." )
56+ check_call (_generate_golds_command_list (output_directory , module ), cwd = SRC_DIR )
57+
58+
59+ def _call_goreleaser_check (version : str ) -> None :
60+ """Calls go releaser check to verify configuration."""
61+ logger .info ("Installing GoReleaser if missing." )
62+ check_call (_install_goreleaser_command_list ())
63+ logger .info ("Checking GoReleaser configuration." )
64+ env = os .environ
65+ env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = version
66+ env [ENVVAR_GORELEASER_GIT_TOKEN ] = configuration .get_value (ConfigurationVariable .GIT_TOKEN )
67+ check_call (_generate_goreleaser_check_command_list (), cwd = SRC_DIR )
68+
69+
70+ def _call_goreleaser_release (version : str ) -> None :
71+ """Calls go releaser release to upload packages."""
72+ logger .info ("Installing GoReleaser if missing." )
73+ check_call (_install_goreleaser_command_list ())
74+ logger .info ("Release package." )
75+ changelogPath = configuration .get_value (ConfigurationVariable .CHANGELOG_FILE_PATH )
76+ env = os .environ
77+ env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = version
78+ env [ENVVAR_GORELEASER_GIT_TOKEN ] = configuration .get_value (ConfigurationVariable .GIT_TOKEN )
79+ check_call (_generate_goreleaser_release_command_list (changelogPath ), cwd = SRC_DIR , env = env )
80+
1481
1582class Go (BaseLanguage ):
1683 """Specific actions for a Golang project."""
@@ -19,22 +86,30 @@ def get_related_language(self) -> str:
1986 """Gets the related language."""
2087 return get_language_from_file_name (__file__ )
2188
22- def package_software (self ) -> None :
89+ def get_version_tag (self , version : str ):
90+ """Gets tag based on version."""
91+ cleansed_version = version .strip ().lstrip ("v" )
92+ return f"v{ cleansed_version } "
93+
94+ def package_software (self , version : str ) -> None :
2395 """No operation."""
24- super ().package_software ()
96+ super ().package_software (version )
97+ _call_goreleaser_check (version )
2598
26- def release_package_to_repository (self ) -> None :
99+ def release_package_to_repository (self , version : str ) -> None :
27100 """No operation."""
28- super ().release_package_to_repository ()
101+ super ().release_package_to_repository (version )
102+ _call_goreleaser_release (version )
29103
30104 def check_credentials (self ) -> None :
31105 """Checks any credentials."""
32106 super ().check_credentials ()
107+ configuration .get_value (ConfigurationVariable .GIT_TOKEN )
33108
34109 def generate_code_documentation (self , output_directory : Path , module_to_document : str ) -> None :
35110 """Generates the code documentation."""
36111 super ().generate_code_documentation (output_directory , module_to_document )
37- # TODO
112+ _call_golds ( output_directory , "./..." )
38113
39114 def can_add_licence_headers (self ) -> bool :
40115 """States that licence headers can be added."""
0 commit comments