ScriptCollection 4.3.63__py3-none-any.whl → 4.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ScriptCollection/AnionBuildPlatform.py +205 -205
- ScriptCollection/CertificateUpdater.py +157 -155
- ScriptCollection/Executables.py +4 -2
- ScriptCollection/GeneralUtilities.py +1696 -1696
- ScriptCollection/OCIImages/OCIImageManager.py +278 -278
- ScriptCollection/ProcessesRunner.py +43 -43
- ScriptCollection/ProgramRunnerSudo.py +110 -110
- ScriptCollection/Resources/CultureChooser/CultureChooser.js +0 -0
- ScriptCollection/Resources/CultureChooser/index.html +0 -0
- ScriptCollection/Resources/Dependencies/JRE/Version.txt +0 -0
- ScriptCollection/Resources/Dependencies/OpenAPIGenerator/Version.txt +0 -0
- ScriptCollection/Resources/Dependencies/PlantUML/Version.txt +0 -0
- ScriptCollection/Resources/Fonts/DejaVuSans-Bold.ttf +0 -0
- ScriptCollection/Resources/Fonts/DejaVuSans-BoldOblique.ttf +0 -0
- ScriptCollection/Resources/Fonts/DejaVuSans-Oblique.ttf +0 -0
- ScriptCollection/Resources/Fonts/DejaVuSans.ttf +0 -0
- ScriptCollection/Resources/Fonts/LICENSE.txt +0 -0
- ScriptCollection/Resources/MaintenanceSite/MaintenanceSite.html +0 -0
- ScriptCollection/SCLog.py +115 -115
- ScriptCollection/ScriptCollectionCore.py +4372 -4292
- ScriptCollection/TFCPS/Docker/TFCPS_CodeUnitSpecific_Docker.py +8 -2
- ScriptCollection/TFCPS/DotNet/TFCPS_CodeUnitSpecific_DotNet.py +716 -709
- ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +235 -235
- ScriptCollection/TFCPS/Python/TFCPS_CodeUnitSpecific_Python.py +258 -258
- ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +524 -524
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnit.py +157 -155
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +691 -672
- ScriptCollection/TFCPS/TFCPS_CreateRelease.py +98 -98
- ScriptCollection/TFCPS/TFCPS_Generic.py +44 -44
- ScriptCollection/TFCPS/TFCPS_MergeToMain.py +132 -131
- ScriptCollection/TFCPS/TFCPS_MergeToStable.py +358 -358
- ScriptCollection/TFCPS/TFCPS_PreBuildCodeunitsScript.py +48 -48
- ScriptCollection/TFCPS/TFCPS_Tools_General.py +38 -8
- {scriptcollection-4.3.63.dist-info → scriptcollection-4.4.0.dist-info}/METADATA +1 -1
- {scriptcollection-4.3.63.dist-info → scriptcollection-4.4.0.dist-info}/RECORD +27 -27
- {scriptcollection-4.3.63.dist-info → scriptcollection-4.4.0.dist-info}/WHEEL +0 -0
- {scriptcollection-4.3.63.dist-info → scriptcollection-4.4.0.dist-info}/entry_points.txt +0 -0
- {scriptcollection-4.3.63.dist-info → scriptcollection-4.4.0.dist-info}/top_level.txt +0 -0
|
@@ -1,709 +1,716 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import re
|
|
3
|
-
import shutil
|
|
4
|
-
import uuid
|
|
5
|
-
import json
|
|
6
|
-
from lxml import etree
|
|
7
|
-
import yaml
|
|
8
|
-
from .CertificateGeneratorInformationBase import CertificateGeneratorInformationBase
|
|
9
|
-
from ...GeneralUtilities import GeneralUtilities
|
|
10
|
-
from ...SCLog import LogLevel
|
|
11
|
-
from ..PackageSource import PackageSource
|
|
12
|
-
from ..TFCPS_CodeUnitSpecific_Base import TFCPS_CodeUnitSpecific_Base,TFCPS_CodeUnitSpecific_Base_CLI
|
|
13
|
-
|
|
14
|
-
class TFCPS_CodeUnitSpecific_DotNet_Functions(TFCPS_CodeUnitSpecific_Base):
|
|
15
|
-
|
|
16
|
-
is_library:bool = None
|
|
17
|
-
csproj_file:bool = None
|
|
18
|
-
#the output of the dotnet-cli is localized. The language is set explicitly for the commands whose output gets parsed, because otherwise the parsing would only work on machines which are configured to use english.
|
|
19
|
-
__dotnet_cli_environment_variables:dict = {"DOTNET_CLI_UI_LANGUAGE": "en-US"}
|
|
20
|
-
|
|
21
|
-
def __init__(self,current_file:str,verbosity:LogLevel,targetenvironmenttype:str,use_cache:bool,is_pre_merge:bool):
|
|
22
|
-
super().__init__(current_file, verbosity,targetenvironmenttype,use_cache,is_pre_merge)
|
|
23
|
-
self.csproj_file=os.path.join(self.get_codeunit_folder(), self.get_codeunit_name(), self.get_codeunit_name() + ".csproj")
|
|
24
|
-
self.is_library="<OutputType>Library</OutputType>" in GeneralUtilities.read_text_from_file(self.csproj_file)#TODO do a real check by checking this property using xpath
|
|
25
|
-
#every dotnet-operation of a codeunit (build, test, linting, ...) restores the dependencies of the codeunit, so the sources
|
|
26
|
-
#they are downloaded from have to be available for all of them and not only for the build.
|
|
27
|
-
self.__ensure_declared_package_sources_are_available()
|
|
28
|
-
|
|
29
|
-
@GeneralUtilities.check_arguments
|
|
30
|
-
def __ensure_declared_package_sources_are_available(self) -> None:
|
|
31
|
-
"""Ensures that every NuGet-source which is declared for this machine (see TFCPS_Tools_General.get_declared_package_sources) is
|
|
32
|
-
registered with its current credentials, so a codeunit can also use dependencies which are not available on nuget.org.
|
|
33
|
-
A source which is already registered under the declared name is removed and registered again, so an outdated registration (for
|
|
34
|
-
example one which points to another url) can not let the registration fail. Sources which are not declared are not touched at
|
|
35
|
-
all: the registration is stored in the NuGet-configuration of the current user, which - when the build runs directly on the
|
|
36
|
-
host - is the configuration the user also uses for their own work and which must not be destroyed by a build."""
|
|
37
|
-
package_sources: list[PackageSource] = self.tfcps_Tools_General.get_declared_package_sources("CSharp")
|
|
38
|
-
if len(package_sources) == 0:
|
|
39
|
-
return
|
|
40
|
-
codeunit_folder: str = self.get_codeunit_folder()
|
|
41
|
-
for package_source in package_sources:
|
|
42
|
-
registered_sources: dict[str, str] = self.__get_registered_nuget_sources(codeunit_folder)
|
|
43
|
-
name_of_source_with_same_name: str = None
|
|
44
|
-
name_of_source_with_same_url: str = None
|
|
45
|
-
for registered_name, registered_url in registered_sources.items():
|
|
46
|
-
if registered_name.lower() == package_source.name.lower():
|
|
47
|
-
name_of_source_with_same_name = registered_name
|
|
48
|
-
elif self.__nuget_sources_are_equal(registered_url, package_source.url):
|
|
49
|
-
#the same feed can already be registered under another name (which is the usual case on a machine on which the user
|
|
50
|
-
#configured the feed manually). Such a source is updated instead of being added a second time, because a second
|
|
51
|
-
#registration of the same url would let every restore query the feed twice.
|
|
52
|
-
name_of_source_with_same_url = registered_name
|
|
53
|
-
if name_of_source_with_same_name is not None:
|
|
54
|
-
self.__remove_nuget_source(codeunit_folder, name_of_source_with_same_name)
|
|
55
|
-
if name_of_source_with_same_url is None:
|
|
56
|
-
arguments = ["nuget", "add", "source", package_source.url, "--name", package_source.name]
|
|
57
|
-
else:
|
|
58
|
-
#the credentials are set again even when the source already exists, because a token can have been rotated since it was registered.
|
|
59
|
-
arguments = ["nuget", "update", "source", name_of_source_with_same_url, "--source", package_source.url]
|
|
60
|
-
if package_source.has_credentials():
|
|
61
|
-
arguments = arguments+["--username", package_source.username, "--password", package_source.password]
|
|
62
|
-
if not GeneralUtilities.current_system_is_windows():
|
|
63
|
-
#without this the password can not be encrypted, which lets the command fail on the operating-systems which do not support the encryption.
|
|
64
|
-
arguments.append("--store-password-in-clear-text")
|
|
65
|
-
arguments_for_log = list(arguments)
|
|
66
|
-
if package_source.has_credentials():
|
|
67
|
-
arguments_for_log[arguments_for_log.index(package_source.password)] = "***"#the password must not be written to the build-log
|
|
68
|
-
self._protected_sc.log.log(f"Add NuGet-source \"{package_source.name}\" ({package_source.url})..." if name_of_source_with_same_url is None else f"Update the already registered NuGet-source \"{name_of_source_with_same_url}\" ({package_source.url})...", LogLevel.Debug)
|
|
69
|
-
self._protected_sc.run_program_argsasarray("dotnet", arguments, codeunit_folder, arguments_for_log=arguments_for_log, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
70
|
-
|
|
71
|
-
@GeneralUtilities.check_arguments
|
|
72
|
-
def __remove_nuget_source(self, folder: str, name: str) -> None:
|
|
73
|
-
"""Removes the registration of the NuGet-source with the given name, so it can be registered again with its currently declared url and credentials."""
|
|
74
|
-
self._protected_sc.log.log(f"Remove the outdated registration of the NuGet-source \"{name}\"...", LogLevel.Debug)
|
|
75
|
-
self._protected_sc.run_program_argsasarray("dotnet", ["nuget", "remove", "source", name], folder, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
76
|
-
|
|
77
|
-
@GeneralUtilities.check_arguments
|
|
78
|
-
def __get_registered_nuget_sources(self, folder: str) -> dict[str, str]:
|
|
79
|
-
"""Returns the NuGet-sources which are registered for the given folder as a mapping of their name to their url. The sources are
|
|
80
|
-
read from the output of "dotnet nuget list source", which prints the name (with its state) and the url of a source in two
|
|
81
|
-
consecutive lines."""
|
|
82
|
-
result: dict[str, str] = {}
|
|
83
|
-
list_result = self._protected_sc.run_program_argsasarray("dotnet", ["nuget", "list", "source"], folder, throw_exception_if_exitcode_is_not_zero=False, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
84
|
-
name_pattern = re.compile(r"^\s*\d+\.\s+(.+?)\s+\[[^\[\]]+\]\s*$")
|
|
85
|
-
current_name: str = None
|
|
86
|
-
for line in list_result[1].splitlines():
|
|
87
|
-
match = name_pattern.match(line)
|
|
88
|
-
if match is not None:
|
|
89
|
-
current_name = match.group(1).strip()
|
|
90
|
-
elif current_name is not None and GeneralUtilities.string_has_content(line):
|
|
91
|
-
result[current_name] = line.strip()
|
|
92
|
-
current_name = None
|
|
93
|
-
return result
|
|
94
|
-
|
|
95
|
-
@GeneralUtilities.check_arguments
|
|
96
|
-
def __nuget_sources_are_equal(self, url: str, other_url: str) -> bool:
|
|
97
|
-
"""Compares two urls of package-sources. A trailing slash and the case are ignored because they do not address a different feed."""
|
|
98
|
-
return url.strip().rstrip("/").lower() == other_url.strip().rstrip("/").lower()
|
|
99
|
-
|
|
100
|
-
@GeneralUtilities.check_arguments
|
|
101
|
-
def build(self,runtimes:list[str],generate_open_api_spec:bool) -> None:
|
|
102
|
-
if self.is_library:
|
|
103
|
-
self.standardized_tasks_build_for_dotnet_library_project(runtimes)
|
|
104
|
-
GeneralUtilities.assert_condition(not generate_open_api_spec,"OpenAPI-Specification can not be generated for a library.")
|
|
105
|
-
else:
|
|
106
|
-
self.standardized_tasks_build_for_dotnet_project(runtimes)
|
|
107
|
-
if generate_open_api_spec:
|
|
108
|
-
self.generate_openapi_file(runtimes[0])
|
|
109
|
-
|
|
110
|
-
@GeneralUtilities.check_arguments
|
|
111
|
-
def generate_openapi_file(self, runtime: str) -> None:
|
|
112
|
-
swagger_document_name: str = "APISpecification"
|
|
113
|
-
self._protected_sc.log.log("Generate OpenAPI-specification-file...")
|
|
114
|
-
codeunitname = self.get_codeunit_name()
|
|
115
|
-
repository_folder = self.get_repository_folder()
|
|
116
|
-
codeunit_folder = os.path.join(repository_folder, codeunitname)
|
|
117
|
-
artifacts_folder = os.path.join(codeunit_folder, "Other", "Artifacts")
|
|
118
|
-
GeneralUtilities.ensure_directory_exists(os.path.join(artifacts_folder, "APISpecification"))
|
|
119
|
-
codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(os.path.join(codeunit_folder,f"{codeunitname}.codeunit.xml"))
|
|
120
|
-
|
|
121
|
-
versioned_api_spec_file = f"APISpecification/{codeunitname}.v{codeunit_version}.api.json"
|
|
122
|
-
self._protected_sc.run_program("swagger", f"tofile --output {versioned_api_spec_file} BuildResult_DotNet_{runtime}/{codeunitname}.dll {swagger_document_name}", artifacts_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
123
|
-
api_file: str = os.path.join(artifacts_folder, versioned_api_spec_file)
|
|
124
|
-
|
|
125
|
-
with open(api_file, encoding="utf-8") as api_file_content:
|
|
126
|
-
reloaded_json = json.load(api_file_content)
|
|
127
|
-
reloaded_json = self.__remove_carriage_returns_recursively(reloaded_json)
|
|
128
|
-
json_content: str = json.dumps(reloaded_json, indent=2, ensure_ascii=False).replace("\r\n", "\n").replace("\r", "\n")
|
|
129
|
-
GeneralUtilities.write_text_to_file(api_file, json_content)
|
|
130
|
-
|
|
131
|
-
shutil.copyfile(api_file, os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.latest.api.json"))
|
|
132
|
-
|
|
133
|
-
resources_folder = os.path.join(codeunit_folder, "Other", "Resources")
|
|
134
|
-
GeneralUtilities.ensure_directory_exists(resources_folder)
|
|
135
|
-
resources_apispec_folder = os.path.join(resources_folder, "APISpecification")
|
|
136
|
-
GeneralUtilities.ensure_directory_exists(resources_apispec_folder)
|
|
137
|
-
resource_target_file = os.path.join(resources_apispec_folder, f"{codeunitname}.api.json")
|
|
138
|
-
GeneralUtilities.ensure_file_does_not_exist(resource_target_file)
|
|
139
|
-
shutil.copyfile(api_file, resource_target_file)
|
|
140
|
-
|
|
141
|
-
yamlfile1: str = str(os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.v{codeunit_version}.api.yaml"))
|
|
142
|
-
GeneralUtilities.ensure_file_does_not_exist(yamlfile1)
|
|
143
|
-
|
|
144
|
-
yaml_content: str = yaml.dump(reloaded_json, allow_unicode=True).replace("\r\n", "\n").replace("\r", "\n")
|
|
145
|
-
GeneralUtilities.write_text_to_file(yamlfile1, yaml_content)
|
|
146
|
-
|
|
147
|
-
yamlfile2: str = str(os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.latest.api.yaml"))
|
|
148
|
-
GeneralUtilities.ensure_file_does_not_exist(yamlfile2)
|
|
149
|
-
shutil.copyfile(yamlfile1, yamlfile2)
|
|
150
|
-
|
|
151
|
-
yamlfile3: str = str(os.path.join(resources_apispec_folder, f"{codeunitname}.api.yaml"))
|
|
152
|
-
GeneralUtilities.ensure_file_does_not_exist(yamlfile3)
|
|
153
|
-
shutil.copyfile(yamlfile1, yamlfile3)
|
|
154
|
-
|
|
155
|
-
def __remove_carriage_returns_recursively(self, value):
|
|
156
|
-
if isinstance(value, str):
|
|
157
|
-
return value.replace("\r\n", "\n").replace("\r", "\n")
|
|
158
|
-
if isinstance(value, dict):
|
|
159
|
-
return {self.__remove_carriage_returns_recursively(key): self.__remove_carriage_returns_recursively(item) for key, item in value.items()}
|
|
160
|
-
if isinstance(value, list):
|
|
161
|
-
return [self.__remove_carriage_returns_recursively(item) for item in value]
|
|
162
|
-
return value
|
|
163
|
-
|
|
164
|
-
@GeneralUtilities.check_arguments
|
|
165
|
-
def __standardized_tasks_build_for_dotnet_build(self, csproj_file: str, originaloutputfolder: str, files_to_sign: dict[str, str], commitid: str, runtimes: list[str], target_environmenttype_mapping: dict[str, str], copy_license_file_to_target_folder: bool, repository_folder: str, codeunit_name: str) -> None:
|
|
166
|
-
self._protected_sc.assert_is_git_repository(repository_folder)
|
|
167
|
-
csproj_filename = os.path.basename(csproj_file)
|
|
168
|
-
self._protected_sc.log.log(f"Build {csproj_filename}...")
|
|
169
|
-
dotnet_build_configuration: str = self.get_target_environment_type()
|
|
170
|
-
codeunit_folder = os.path.join(repository_folder, codeunit_name)
|
|
171
|
-
csproj_file_folder = os.path.dirname(csproj_file)
|
|
172
|
-
csproj_file_name = os.path.basename(csproj_file)
|
|
173
|
-
csproj_file_name_without_extension = csproj_file_name.split(".")[0]
|
|
174
|
-
sarif_folder = os.path.join(codeunit_folder, "Other", "Resources", "CodeAnalysisResult")
|
|
175
|
-
GeneralUtilities.ensure_directory_exists(sarif_folder)
|
|
176
|
-
gitkeep_file = os.path.join(sarif_folder, ".gitkeep")
|
|
177
|
-
GeneralUtilities.ensure_file_exists(gitkeep_file)
|
|
178
|
-
for runtime in runtimes:
|
|
179
|
-
outputfolder = originaloutputfolder+runtime
|
|
180
|
-
GeneralUtilities.ensure_directory_does_not_exist(os.path.join(csproj_file_folder, "obj"))
|
|
181
|
-
GeneralUtilities.ensure_directory_does_not_exist(outputfolder)
|
|
182
|
-
self._protected_sc.run_program("dotnet", "clean", csproj_file_folder)
|
|
183
|
-
GeneralUtilities.ensure_directory_exists(outputfolder)
|
|
184
|
-
self._protected_sc.run_program_argsasarray("dotnet", ["restore", "--runtime", runtime], codeunit_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
185
|
-
self._protected_sc.run_program_argsasarray("dotnet", ["build", "--no-restore", csproj_file_name, "-c", dotnet_build_configuration, "-o", outputfolder, "--runtime", runtime], csproj_file_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
186
|
-
if copy_license_file_to_target_folder:
|
|
187
|
-
license_file = os.path.join(repository_folder, "License.txt")
|
|
188
|
-
target = os.path.join(outputfolder, f"{codeunit_name}.License.txt")
|
|
189
|
-
shutil.copyfile(license_file, target)
|
|
190
|
-
if 0 < len(files_to_sign):
|
|
191
|
-
for key, value in files_to_sign.items():
|
|
192
|
-
dll_file = key
|
|
193
|
-
snk_file = value
|
|
194
|
-
dll_file_full = os.path.join(outputfolder, dll_file)
|
|
195
|
-
if os.path.isfile(dll_file_full):
|
|
196
|
-
GeneralUtilities.assert_condition(self._protected_sc.run_program("sn", f"-vf {dll_file}", outputfolder, throw_exception_if_exitcode_is_not_zero=False)[0] == 1, f"Pre-verifying of {dll_file} failed.")
|
|
197
|
-
self._protected_sc.run_program_argsasarray("sn", ["-R", dll_file, snk_file], outputfolder)
|
|
198
|
-
GeneralUtilities.assert_condition(self._protected_sc.run_program("sn", f"-vf {dll_file}", outputfolder, throw_exception_if_exitcode_is_not_zero=False)[0] == 0, f"Verifying of {dll_file} failed.")
|
|
199
|
-
sarif_filename = f"{csproj_file_name_without_extension}.sarif"
|
|
200
|
-
sarif_source_file = os.path.join(sarif_folder, sarif_filename)
|
|
201
|
-
if os.path.exists(sarif_source_file):
|
|
202
|
-
sarif_folder_target = os.path.join(codeunit_folder, "Other", "Artifacts", "CodeAnalysisResult")
|
|
203
|
-
GeneralUtilities.ensure_directory_exists(sarif_folder_target)
|
|
204
|
-
sarif_target_file = os.path.join(sarif_folder_target, sarif_filename)
|
|
205
|
-
GeneralUtilities.ensure_file_does_not_exist(sarif_target_file)
|
|
206
|
-
shutil.copyfile(sarif_source_file, sarif_target_file)
|
|
207
|
-
|
|
208
|
-
@GeneralUtilities.check_arguments
|
|
209
|
-
def standardized_tasks_build_for_dotnet_project(self,runtimes:list[str]) -> None:
|
|
210
|
-
self.__standardized_tasks_build_for_dotnet_project(runtimes)
|
|
211
|
-
|
|
212
|
-
@GeneralUtilities.check_arguments
|
|
213
|
-
def standardized_tasks_build_for_dotnet_library_project(self,runtimes:list[str]) -> None:
|
|
214
|
-
self.__standardized_tasks_build_for_dotnet_project(runtimes)
|
|
215
|
-
self.__standardized_tasks_build_nupkg_for_dotnet_create_package(runtimes)
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
@staticmethod
|
|
219
|
-
@GeneralUtilities.check_arguments
|
|
220
|
-
def get_filestosign_from_commandline_arguments( default_value: dict[str, str]) -> dict[str, str]:
|
|
221
|
-
result_plain =None# TODO TasksForCommonProjectStructure.get_property_from_commandline_arguments(commandline_arguments, "sign")
|
|
222
|
-
if result_plain is None:
|
|
223
|
-
return default_value
|
|
224
|
-
else:
|
|
225
|
-
result: dict[str, str] = dict[str, str]()
|
|
226
|
-
files_tuples = GeneralUtilities.to_list(result_plain, ";")
|
|
227
|
-
for files_tuple in files_tuples:
|
|
228
|
-
splitted = files_tuple.split("=")
|
|
229
|
-
result[splitted[0]] = splitted[1]
|
|
230
|
-
return result
|
|
231
|
-
|
|
232
|
-
@GeneralUtilities.check_arguments
|
|
233
|
-
def __standardized_tasks_build_for_dotnet_project(self,runtimes:list[str]) -> None:
|
|
234
|
-
|
|
235
|
-
target_environment_type: str=self.get_target_environment_type()
|
|
236
|
-
copy_license_file_to_target_folder: bool=True
|
|
237
|
-
codeunitname: str = self.get_codeunit_name()
|
|
238
|
-
|
|
239
|
-
workspace_folder=os.path.join(self.get_codeunit_folder(),"Other","Workspace")
|
|
240
|
-
GeneralUtilities.ensure_directory_does_not_exist(workspace_folder)
|
|
241
|
-
|
|
242
|
-
files_to_sign: dict[str, str] = self.get_filestosign_from_commandline_arguments( dict())
|
|
243
|
-
repository_folder: str = self.get_repository_folder()
|
|
244
|
-
commitid = self._protected_sc.git_get_commit_id(repository_folder)
|
|
245
|
-
outputfolder = GeneralUtilities.resolve_relative_path("./Other/Artifacts", self.get_codeunit_folder())
|
|
246
|
-
codeunit_folder = os.path.join(repository_folder, codeunitname)
|
|
247
|
-
csproj_file = os.path.join(codeunit_folder, codeunitname, codeunitname + ".csproj")
|
|
248
|
-
csproj_test_file = os.path.join(codeunit_folder, codeunitname+"Tests", codeunitname+"Tests.csproj")
|
|
249
|
-
self.__standardized_tasks_build_for_dotnet_build(csproj_file, os.path.join(outputfolder, "BuildResult_DotNet_"), files_to_sign, commitid, runtimes, target_environment_type, copy_license_file_to_target_folder, repository_folder, codeunitname)
|
|
250
|
-
self.__standardized_tasks_build_for_dotnet_build(csproj_test_file, os.path.join(outputfolder, "BuildResultTests_DotNet_"), files_to_sign, commitid, runtimes, target_environment_type, copy_license_file_to_target_folder, repository_folder, codeunitname)
|
|
251
|
-
self.generate_sbom_for_dotnet_project(codeunit_folder)
|
|
252
|
-
self.copy_source_files_to_output_directory()
|
|
253
|
-
|
|
254
|
-
@GeneralUtilities.check_arguments
|
|
255
|
-
def __standardized_tasks_build_nupkg_for_dotnet_create_package(self,runtimes:list[str]) -> None:
|
|
256
|
-
codeunitname: str = self.get_codeunit_name()
|
|
257
|
-
repository_folder: str =self.get_repository_folder()
|
|
258
|
-
build_folder = os.path.join(repository_folder, codeunitname, "Other", "Build")
|
|
259
|
-
outputfolder = GeneralUtilities.resolve_relative_path("./Other/Artifacts/BuildResult_NuGet",self.get_codeunit_folder())
|
|
260
|
-
nuspec_file = os.path.join(build_folder, f"{codeunitname}.nuspec")
|
|
261
|
-
root: etree._ElementTree = etree.parse(nuspec_file)
|
|
262
|
-
# the elements are matched by their local name because the nuspec-namespace can be declared as default-namespace
|
|
263
|
-
# (<package xmlns="...">) as well as with a prefix (<ns0:package xmlns:ns0="...">) and both variants are valid.
|
|
264
|
-
versions = root.xpath("//*[local-name() = 'package']/*[local-name() = 'metadata']/*[local-name() = 'version']/text()")
|
|
265
|
-
if len(versions) == 0:
|
|
266
|
-
raise ValueError(f"Can not determine the version because there is no version-element in \"{nuspec_file}\".")
|
|
267
|
-
current_version = versions[0]
|
|
268
|
-
nupkg_filename = f"{codeunitname}.{current_version}.nupkg"
|
|
269
|
-
nupkg_file = f"{build_folder}/{nupkg_filename}"
|
|
270
|
-
GeneralUtilities.ensure_file_does_not_exist(nupkg_file)
|
|
271
|
-
commit_id = self._protected_sc.git_get_commit_id(repository_folder)
|
|
272
|
-
# Pack the hand-written nuspec using "dotnet pack" (the classic "nuget pack" is not available in the dotnet-only build-container).
|
|
273
|
-
# With NuspecFile the package-content is taken entirely from the nuspec's <files>-list, so no rebuild happens here (the library was already built before).
|
|
274
|
-
self._protected_sc.run_program_argsasarray("dotnet", [
|
|
275
|
-
"pack", self.csproj_file,
|
|
276
|
-
"-c", self.get_target_environment_type(),
|
|
277
|
-
"--no-build", "--no-restore",
|
|
278
|
-
"--output", build_folder,
|
|
279
|
-
f"-p:NuspecFile={nuspec_file}",
|
|
280
|
-
f"-p:NuspecBasePath={build_folder}",
|
|
281
|
-
f"-p:NuspecProperties=commitid={commit_id}",
|
|
282
|
-
"-p:IsPackable=true",
|
|
283
|
-
], build_folder, print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
284
|
-
GeneralUtilities.ensure_directory_does_not_exist(outputfolder)
|
|
285
|
-
GeneralUtilities.ensure_directory_exists(outputfolder)
|
|
286
|
-
os.rename(nupkg_file, f"{outputfolder}/{nupkg_filename}")
|
|
287
|
-
|
|
288
|
-
@GeneralUtilities.check_arguments
|
|
289
|
-
def generate_sbom_for_dotnet_project(self, codeunit_folder: str) -> None:
|
|
290
|
-
self._protected_sc.log.log("Generate SBOM...")
|
|
291
|
-
codeunit_name = os.path.basename(codeunit_folder)
|
|
292
|
-
bomfile_folder = "Other/Artifacts/BOM"
|
|
293
|
-
self._protected_sc.run_program_argsasarray("dotnet", ["CycloneDX", f"{codeunit_name}/{codeunit_name}.csproj", "-o", bomfile_folder], codeunit_folder)
|
|
294
|
-
codeunitversion = self.tfcps_Tools_General.get_version_of_codeunit(os.path.join(codeunit_folder, f"{codeunit_name}.codeunit.xml"))
|
|
295
|
-
target = f"{codeunit_folder}/{bomfile_folder}/{codeunit_name}.{codeunitversion}.sbom.xml"
|
|
296
|
-
GeneralUtilities.ensure_file_does_not_exist(target)
|
|
297
|
-
os.rename(f"{codeunit_folder}/{bomfile_folder}/bom.xml", target)
|
|
298
|
-
self._protected_sc.format_xml_file(target)
|
|
299
|
-
|
|
300
|
-
@GeneralUtilities.check_arguments
|
|
301
|
-
def get_dotnet_build_diagnostics(self) -> list[tuple[LogLevel, str, str | None, int | None]]:
|
|
302
|
-
codeunit_name = self.get_codeunit_name()
|
|
303
|
-
codeunit_folder = self.get_codeunit_folder()
|
|
304
|
-
sln_file = os.path.join(codeunit_folder, codeunit_name + ".sln")
|
|
305
|
-
temp_output_folder = os.path.join(GeneralUtilities.get_temp_folder(), str(uuid.uuid4()))
|
|
306
|
-
GeneralUtilities.ensure_directory_exists(temp_output_folder)
|
|
307
|
-
# Run the build from an absolute temporary working-directory (instead of the codeunit-folder) and with node-reuse
|
|
308
|
-
# disabled. Building a solution with "-o" (which is required here to keep the diagnostics-build isolated from the
|
|
309
|
-
# real build-output) is not officially supported by the .NET-SDK (warning NETSDK1194) and makes MSBuild create an
|
|
310
|
-
# additional "tmp/<guid>"-output-folder relative to the build-process' working-directory. With node-reuse enabled
|
|
311
|
-
# that folder is even created by a reused MSBuild-worker-node that still has a codeunit-subfolder as its working-
|
|
312
|
-
# directory, so it ends up inside the codeunit-folder. Such a folder is usually cleaned up by the SDK, but on a
|
|
313
|
-
# bind-mounted filesystem (for example when the build runs in a Linux-container with the repository mounted from a
|
|
314
|
-
# Windows-host) the cleanup can fail and the folder is left behind. By using an absolute working-directory and
|
|
315
|
-
# forcing fresh worker-nodes (which inherit that working-directory) any such relative "tmp/<guid>"-folder is created
|
|
316
|
-
# below temp_output_folder and removed together with it in the finally-block.
|
|
317
|
-
try:
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
else
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
self.
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
<
|
|
432
|
-
<
|
|
433
|
-
<
|
|
434
|
-
<
|
|
435
|
-
<
|
|
436
|
-
<
|
|
437
|
-
<
|
|
438
|
-
<
|
|
439
|
-
<
|
|
440
|
-
<
|
|
441
|
-
<
|
|
442
|
-
<
|
|
443
|
-
<
|
|
444
|
-
<
|
|
445
|
-
<
|
|
446
|
-
<
|
|
447
|
-
<
|
|
448
|
-
<
|
|
449
|
-
<
|
|
450
|
-
<
|
|
451
|
-
<
|
|
452
|
-
<
|
|
453
|
-
<
|
|
454
|
-
<
|
|
455
|
-
<
|
|
456
|
-
<
|
|
457
|
-
<
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
<
|
|
461
|
-
<
|
|
462
|
-
<
|
|
463
|
-
<
|
|
464
|
-
<
|
|
465
|
-
<\\/PropertyGroup>
|
|
466
|
-
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='
|
|
467
|
-
<DebugType>
|
|
468
|
-
<DebugSymbols>true<\\/DebugSymbols>
|
|
469
|
-
<Optimize>false<\\/Optimize>
|
|
470
|
-
<DefineConstants>TRACE;
|
|
471
|
-
<ErrorReport>
|
|
472
|
-
<\\/PropertyGroup>
|
|
473
|
-
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='
|
|
474
|
-
<DebugType>portable<\\/DebugType>
|
|
475
|
-
<DebugSymbols>true<\\/DebugSymbols>
|
|
476
|
-
<Optimize>false<\\/Optimize>
|
|
477
|
-
<DefineConstants>
|
|
478
|
-
<ErrorReport>none<\\/ErrorReport>
|
|
479
|
-
<\\/PropertyGroup>
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
<
|
|
497
|
-
<
|
|
498
|
-
<
|
|
499
|
-
<
|
|
500
|
-
<
|
|
501
|
-
<
|
|
502
|
-
<
|
|
503
|
-
<
|
|
504
|
-
<
|
|
505
|
-
<
|
|
506
|
-
<
|
|
507
|
-
<
|
|
508
|
-
<
|
|
509
|
-
<
|
|
510
|
-
<
|
|
511
|
-
<
|
|
512
|
-
<
|
|
513
|
-
<
|
|
514
|
-
<
|
|
515
|
-
<
|
|
516
|
-
<
|
|
517
|
-
<
|
|
518
|
-
<
|
|
519
|
-
<
|
|
520
|
-
<
|
|
521
|
-
<
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
<
|
|
525
|
-
<
|
|
526
|
-
<
|
|
527
|
-
<
|
|
528
|
-
<
|
|
529
|
-
<\\/PropertyGroup>
|
|
530
|
-
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='
|
|
531
|
-
<DebugType>
|
|
532
|
-
<DebugSymbols>true<\\/DebugSymbols>
|
|
533
|
-
<Optimize>false<\\/Optimize>
|
|
534
|
-
<DefineConstants>TRACE;
|
|
535
|
-
<ErrorReport>
|
|
536
|
-
<\\/PropertyGroup>
|
|
537
|
-
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='
|
|
538
|
-
<DebugType>portable<\\/DebugType>
|
|
539
|
-
<DebugSymbols>true<\\/DebugSymbols>
|
|
540
|
-
<Optimize>false<\\/Optimize>
|
|
541
|
-
<DefineConstants>
|
|
542
|
-
<ErrorReport>none<\\/ErrorReport>
|
|
543
|
-
<\\/PropertyGroup>
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
self._protected_sc.
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
#
|
|
623
|
-
#
|
|
624
|
-
#
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
GeneralUtilities.
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
def
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
import shutil
|
|
4
|
+
import uuid
|
|
5
|
+
import json
|
|
6
|
+
from lxml import etree
|
|
7
|
+
import yaml
|
|
8
|
+
from .CertificateGeneratorInformationBase import CertificateGeneratorInformationBase
|
|
9
|
+
from ...GeneralUtilities import GeneralUtilities
|
|
10
|
+
from ...SCLog import LogLevel
|
|
11
|
+
from ..PackageSource import PackageSource
|
|
12
|
+
from ..TFCPS_CodeUnitSpecific_Base import TFCPS_CodeUnitSpecific_Base,TFCPS_CodeUnitSpecific_Base_CLI
|
|
13
|
+
|
|
14
|
+
class TFCPS_CodeUnitSpecific_DotNet_Functions(TFCPS_CodeUnitSpecific_Base):
|
|
15
|
+
|
|
16
|
+
is_library:bool = None
|
|
17
|
+
csproj_file:bool = None
|
|
18
|
+
#the output of the dotnet-cli is localized. The language is set explicitly for the commands whose output gets parsed, because otherwise the parsing would only work on machines which are configured to use english.
|
|
19
|
+
__dotnet_cli_environment_variables:dict = {"DOTNET_CLI_UI_LANGUAGE": "en-US"}
|
|
20
|
+
|
|
21
|
+
def __init__(self,current_file:str,verbosity:LogLevel,targetenvironmenttype:str,use_cache:bool,is_pre_merge:bool):
|
|
22
|
+
super().__init__(current_file, verbosity,targetenvironmenttype,use_cache,is_pre_merge)
|
|
23
|
+
self.csproj_file=os.path.join(self.get_codeunit_folder(), self.get_codeunit_name(), self.get_codeunit_name() + ".csproj")
|
|
24
|
+
self.is_library="<OutputType>Library</OutputType>" in GeneralUtilities.read_text_from_file(self.csproj_file)#TODO do a real check by checking this property using xpath
|
|
25
|
+
#every dotnet-operation of a codeunit (build, test, linting, ...) restores the dependencies of the codeunit, so the sources
|
|
26
|
+
#they are downloaded from have to be available for all of them and not only for the build.
|
|
27
|
+
self.__ensure_declared_package_sources_are_available()
|
|
28
|
+
|
|
29
|
+
@GeneralUtilities.check_arguments
|
|
30
|
+
def __ensure_declared_package_sources_are_available(self) -> None:
|
|
31
|
+
"""Ensures that every NuGet-source which is declared for this machine (see TFCPS_Tools_General.get_declared_package_sources) is
|
|
32
|
+
registered with its current credentials, so a codeunit can also use dependencies which are not available on nuget.org.
|
|
33
|
+
A source which is already registered under the declared name is removed and registered again, so an outdated registration (for
|
|
34
|
+
example one which points to another url) can not let the registration fail. Sources which are not declared are not touched at
|
|
35
|
+
all: the registration is stored in the NuGet-configuration of the current user, which - when the build runs directly on the
|
|
36
|
+
host - is the configuration the user also uses for their own work and which must not be destroyed by a build."""
|
|
37
|
+
package_sources: list[PackageSource] = self.tfcps_Tools_General.get_declared_package_sources("CSharp")
|
|
38
|
+
if len(package_sources) == 0:
|
|
39
|
+
return
|
|
40
|
+
codeunit_folder: str = self.get_codeunit_folder()
|
|
41
|
+
for package_source in package_sources:
|
|
42
|
+
registered_sources: dict[str, str] = self.__get_registered_nuget_sources(codeunit_folder)
|
|
43
|
+
name_of_source_with_same_name: str = None
|
|
44
|
+
name_of_source_with_same_url: str = None
|
|
45
|
+
for registered_name, registered_url in registered_sources.items():
|
|
46
|
+
if registered_name.lower() == package_source.name.lower():
|
|
47
|
+
name_of_source_with_same_name = registered_name
|
|
48
|
+
elif self.__nuget_sources_are_equal(registered_url, package_source.url):
|
|
49
|
+
#the same feed can already be registered under another name (which is the usual case on a machine on which the user
|
|
50
|
+
#configured the feed manually). Such a source is updated instead of being added a second time, because a second
|
|
51
|
+
#registration of the same url would let every restore query the feed twice.
|
|
52
|
+
name_of_source_with_same_url = registered_name
|
|
53
|
+
if name_of_source_with_same_name is not None:
|
|
54
|
+
self.__remove_nuget_source(codeunit_folder, name_of_source_with_same_name)
|
|
55
|
+
if name_of_source_with_same_url is None:
|
|
56
|
+
arguments = ["nuget", "add", "source", package_source.url, "--name", package_source.name]
|
|
57
|
+
else:
|
|
58
|
+
#the credentials are set again even when the source already exists, because a token can have been rotated since it was registered.
|
|
59
|
+
arguments = ["nuget", "update", "source", name_of_source_with_same_url, "--source", package_source.url]
|
|
60
|
+
if package_source.has_credentials():
|
|
61
|
+
arguments = arguments+["--username", package_source.username, "--password", package_source.password]
|
|
62
|
+
if not GeneralUtilities.current_system_is_windows():
|
|
63
|
+
#without this the password can not be encrypted, which lets the command fail on the operating-systems which do not support the encryption.
|
|
64
|
+
arguments.append("--store-password-in-clear-text")
|
|
65
|
+
arguments_for_log = list(arguments)
|
|
66
|
+
if package_source.has_credentials():
|
|
67
|
+
arguments_for_log[arguments_for_log.index(package_source.password)] = "***"#the password must not be written to the build-log
|
|
68
|
+
self._protected_sc.log.log(f"Add NuGet-source \"{package_source.name}\" ({package_source.url})..." if name_of_source_with_same_url is None else f"Update the already registered NuGet-source \"{name_of_source_with_same_url}\" ({package_source.url})...", LogLevel.Debug)
|
|
69
|
+
self._protected_sc.run_program_argsasarray("dotnet", arguments, codeunit_folder, arguments_for_log=arguments_for_log, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
70
|
+
|
|
71
|
+
@GeneralUtilities.check_arguments
|
|
72
|
+
def __remove_nuget_source(self, folder: str, name: str) -> None:
|
|
73
|
+
"""Removes the registration of the NuGet-source with the given name, so it can be registered again with its currently declared url and credentials."""
|
|
74
|
+
self._protected_sc.log.log(f"Remove the outdated registration of the NuGet-source \"{name}\"...", LogLevel.Debug)
|
|
75
|
+
self._protected_sc.run_program_argsasarray("dotnet", ["nuget", "remove", "source", name], folder, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
76
|
+
|
|
77
|
+
@GeneralUtilities.check_arguments
|
|
78
|
+
def __get_registered_nuget_sources(self, folder: str) -> dict[str, str]:
|
|
79
|
+
"""Returns the NuGet-sources which are registered for the given folder as a mapping of their name to their url. The sources are
|
|
80
|
+
read from the output of "dotnet nuget list source", which prints the name (with its state) and the url of a source in two
|
|
81
|
+
consecutive lines."""
|
|
82
|
+
result: dict[str, str] = {}
|
|
83
|
+
list_result = self._protected_sc.run_program_argsasarray("dotnet", ["nuget", "list", "source"], folder, throw_exception_if_exitcode_is_not_zero=False, print_live_output=False, env_vars=self.__dotnet_cli_environment_variables)
|
|
84
|
+
name_pattern = re.compile(r"^\s*\d+\.\s+(.+?)\s+\[[^\[\]]+\]\s*$")
|
|
85
|
+
current_name: str = None
|
|
86
|
+
for line in list_result[1].splitlines():
|
|
87
|
+
match = name_pattern.match(line)
|
|
88
|
+
if match is not None:
|
|
89
|
+
current_name = match.group(1).strip()
|
|
90
|
+
elif current_name is not None and GeneralUtilities.string_has_content(line):
|
|
91
|
+
result[current_name] = line.strip()
|
|
92
|
+
current_name = None
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
@GeneralUtilities.check_arguments
|
|
96
|
+
def __nuget_sources_are_equal(self, url: str, other_url: str) -> bool:
|
|
97
|
+
"""Compares two urls of package-sources. A trailing slash and the case are ignored because they do not address a different feed."""
|
|
98
|
+
return url.strip().rstrip("/").lower() == other_url.strip().rstrip("/").lower()
|
|
99
|
+
|
|
100
|
+
@GeneralUtilities.check_arguments
|
|
101
|
+
def build(self,runtimes:list[str],generate_open_api_spec:bool) -> None:
|
|
102
|
+
if self.is_library:
|
|
103
|
+
self.standardized_tasks_build_for_dotnet_library_project(runtimes)
|
|
104
|
+
GeneralUtilities.assert_condition(not generate_open_api_spec,"OpenAPI-Specification can not be generated for a library.")
|
|
105
|
+
else:
|
|
106
|
+
self.standardized_tasks_build_for_dotnet_project(runtimes)
|
|
107
|
+
if generate_open_api_spec:
|
|
108
|
+
self.generate_openapi_file(runtimes[0])
|
|
109
|
+
|
|
110
|
+
@GeneralUtilities.check_arguments
|
|
111
|
+
def generate_openapi_file(self, runtime: str) -> None:
|
|
112
|
+
swagger_document_name: str = "APISpecification"
|
|
113
|
+
self._protected_sc.log.log("Generate OpenAPI-specification-file...")
|
|
114
|
+
codeunitname = self.get_codeunit_name()
|
|
115
|
+
repository_folder = self.get_repository_folder()
|
|
116
|
+
codeunit_folder = os.path.join(repository_folder, codeunitname)
|
|
117
|
+
artifacts_folder = os.path.join(codeunit_folder, "Other", "Artifacts")
|
|
118
|
+
GeneralUtilities.ensure_directory_exists(os.path.join(artifacts_folder, "APISpecification"))
|
|
119
|
+
codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(os.path.join(codeunit_folder,f"{codeunitname}.codeunit.xml"))
|
|
120
|
+
|
|
121
|
+
versioned_api_spec_file = f"APISpecification/{codeunitname}.v{codeunit_version}.api.json"
|
|
122
|
+
self._protected_sc.run_program("swagger", f"tofile --output {versioned_api_spec_file} BuildResult_DotNet_{runtime}/{codeunitname}.dll {swagger_document_name}", artifacts_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
123
|
+
api_file: str = os.path.join(artifacts_folder, versioned_api_spec_file)
|
|
124
|
+
|
|
125
|
+
with open(api_file, encoding="utf-8") as api_file_content:
|
|
126
|
+
reloaded_json = json.load(api_file_content)
|
|
127
|
+
reloaded_json = self.__remove_carriage_returns_recursively(reloaded_json)
|
|
128
|
+
json_content: str = json.dumps(reloaded_json, indent=2, ensure_ascii=False).replace("\r\n", "\n").replace("\r", "\n")
|
|
129
|
+
GeneralUtilities.write_text_to_file(api_file, json_content)
|
|
130
|
+
|
|
131
|
+
shutil.copyfile(api_file, os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.latest.api.json"))
|
|
132
|
+
|
|
133
|
+
resources_folder = os.path.join(codeunit_folder, "Other", "Resources")
|
|
134
|
+
GeneralUtilities.ensure_directory_exists(resources_folder)
|
|
135
|
+
resources_apispec_folder = os.path.join(resources_folder, "APISpecification")
|
|
136
|
+
GeneralUtilities.ensure_directory_exists(resources_apispec_folder)
|
|
137
|
+
resource_target_file = os.path.join(resources_apispec_folder, f"{codeunitname}.api.json")
|
|
138
|
+
GeneralUtilities.ensure_file_does_not_exist(resource_target_file)
|
|
139
|
+
shutil.copyfile(api_file, resource_target_file)
|
|
140
|
+
|
|
141
|
+
yamlfile1: str = str(os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.v{codeunit_version}.api.yaml"))
|
|
142
|
+
GeneralUtilities.ensure_file_does_not_exist(yamlfile1)
|
|
143
|
+
|
|
144
|
+
yaml_content: str = yaml.dump(reloaded_json, allow_unicode=True).replace("\r\n", "\n").replace("\r", "\n")
|
|
145
|
+
GeneralUtilities.write_text_to_file(yamlfile1, yaml_content)
|
|
146
|
+
|
|
147
|
+
yamlfile2: str = str(os.path.join(artifacts_folder, f"APISpecification/{codeunitname}.latest.api.yaml"))
|
|
148
|
+
GeneralUtilities.ensure_file_does_not_exist(yamlfile2)
|
|
149
|
+
shutil.copyfile(yamlfile1, yamlfile2)
|
|
150
|
+
|
|
151
|
+
yamlfile3: str = str(os.path.join(resources_apispec_folder, f"{codeunitname}.api.yaml"))
|
|
152
|
+
GeneralUtilities.ensure_file_does_not_exist(yamlfile3)
|
|
153
|
+
shutil.copyfile(yamlfile1, yamlfile3)
|
|
154
|
+
|
|
155
|
+
def __remove_carriage_returns_recursively(self, value):
|
|
156
|
+
if isinstance(value, str):
|
|
157
|
+
return value.replace("\r\n", "\n").replace("\r", "\n")
|
|
158
|
+
if isinstance(value, dict):
|
|
159
|
+
return {self.__remove_carriage_returns_recursively(key): self.__remove_carriage_returns_recursively(item) for key, item in value.items()}
|
|
160
|
+
if isinstance(value, list):
|
|
161
|
+
return [self.__remove_carriage_returns_recursively(item) for item in value]
|
|
162
|
+
return value
|
|
163
|
+
|
|
164
|
+
@GeneralUtilities.check_arguments
|
|
165
|
+
def __standardized_tasks_build_for_dotnet_build(self, csproj_file: str, originaloutputfolder: str, files_to_sign: dict[str, str], commitid: str, runtimes: list[str], target_environmenttype_mapping: dict[str, str], copy_license_file_to_target_folder: bool, repository_folder: str, codeunit_name: str) -> None:
|
|
166
|
+
self._protected_sc.assert_is_git_repository(repository_folder)
|
|
167
|
+
csproj_filename = os.path.basename(csproj_file)
|
|
168
|
+
self._protected_sc.log.log(f"Build {csproj_filename}...")
|
|
169
|
+
dotnet_build_configuration: str = self.get_target_environment_type()
|
|
170
|
+
codeunit_folder = os.path.join(repository_folder, codeunit_name)
|
|
171
|
+
csproj_file_folder = os.path.dirname(csproj_file)
|
|
172
|
+
csproj_file_name = os.path.basename(csproj_file)
|
|
173
|
+
csproj_file_name_without_extension = csproj_file_name.split(".")[0]
|
|
174
|
+
sarif_folder = os.path.join(codeunit_folder, "Other", "Resources", "CodeAnalysisResult")
|
|
175
|
+
GeneralUtilities.ensure_directory_exists(sarif_folder)
|
|
176
|
+
gitkeep_file = os.path.join(sarif_folder, ".gitkeep")
|
|
177
|
+
GeneralUtilities.ensure_file_exists(gitkeep_file)
|
|
178
|
+
for runtime in runtimes:
|
|
179
|
+
outputfolder = originaloutputfolder+runtime
|
|
180
|
+
GeneralUtilities.ensure_directory_does_not_exist(os.path.join(csproj_file_folder, "obj"))
|
|
181
|
+
GeneralUtilities.ensure_directory_does_not_exist(outputfolder)
|
|
182
|
+
self._protected_sc.run_program("dotnet", "clean", csproj_file_folder)
|
|
183
|
+
GeneralUtilities.ensure_directory_exists(outputfolder)
|
|
184
|
+
self._protected_sc.run_program_argsasarray("dotnet", ["restore", "--runtime", runtime], codeunit_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
185
|
+
self._protected_sc.run_program_argsasarray("dotnet", ["build", "--no-restore", csproj_file_name, "-c", dotnet_build_configuration, "-o", outputfolder, "--runtime", runtime], csproj_file_folder,print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
186
|
+
if copy_license_file_to_target_folder:
|
|
187
|
+
license_file = os.path.join(repository_folder, "License.txt")
|
|
188
|
+
target = os.path.join(outputfolder, f"{codeunit_name}.License.txt")
|
|
189
|
+
shutil.copyfile(license_file, target)
|
|
190
|
+
if 0 < len(files_to_sign):
|
|
191
|
+
for key, value in files_to_sign.items():
|
|
192
|
+
dll_file = key
|
|
193
|
+
snk_file = value
|
|
194
|
+
dll_file_full = os.path.join(outputfolder, dll_file)
|
|
195
|
+
if os.path.isfile(dll_file_full):
|
|
196
|
+
GeneralUtilities.assert_condition(self._protected_sc.run_program("sn", f"-vf {dll_file}", outputfolder, throw_exception_if_exitcode_is_not_zero=False)[0] == 1, f"Pre-verifying of {dll_file} failed.")
|
|
197
|
+
self._protected_sc.run_program_argsasarray("sn", ["-R", dll_file, snk_file], outputfolder)
|
|
198
|
+
GeneralUtilities.assert_condition(self._protected_sc.run_program("sn", f"-vf {dll_file}", outputfolder, throw_exception_if_exitcode_is_not_zero=False)[0] == 0, f"Verifying of {dll_file} failed.")
|
|
199
|
+
sarif_filename = f"{csproj_file_name_without_extension}.sarif"
|
|
200
|
+
sarif_source_file = os.path.join(sarif_folder, sarif_filename)
|
|
201
|
+
if os.path.exists(sarif_source_file):
|
|
202
|
+
sarif_folder_target = os.path.join(codeunit_folder, "Other", "Artifacts", "CodeAnalysisResult")
|
|
203
|
+
GeneralUtilities.ensure_directory_exists(sarif_folder_target)
|
|
204
|
+
sarif_target_file = os.path.join(sarif_folder_target, sarif_filename)
|
|
205
|
+
GeneralUtilities.ensure_file_does_not_exist(sarif_target_file)
|
|
206
|
+
shutil.copyfile(sarif_source_file, sarif_target_file)
|
|
207
|
+
|
|
208
|
+
@GeneralUtilities.check_arguments
|
|
209
|
+
def standardized_tasks_build_for_dotnet_project(self,runtimes:list[str]) -> None:
|
|
210
|
+
self.__standardized_tasks_build_for_dotnet_project(runtimes)
|
|
211
|
+
|
|
212
|
+
@GeneralUtilities.check_arguments
|
|
213
|
+
def standardized_tasks_build_for_dotnet_library_project(self,runtimes:list[str]) -> None:
|
|
214
|
+
self.__standardized_tasks_build_for_dotnet_project(runtimes)
|
|
215
|
+
self.__standardized_tasks_build_nupkg_for_dotnet_create_package(runtimes)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
@staticmethod
|
|
219
|
+
@GeneralUtilities.check_arguments
|
|
220
|
+
def get_filestosign_from_commandline_arguments( default_value: dict[str, str]) -> dict[str, str]:
|
|
221
|
+
result_plain =None# TODO TasksForCommonProjectStructure.get_property_from_commandline_arguments(commandline_arguments, "sign")
|
|
222
|
+
if result_plain is None:
|
|
223
|
+
return default_value
|
|
224
|
+
else:
|
|
225
|
+
result: dict[str, str] = dict[str, str]()
|
|
226
|
+
files_tuples = GeneralUtilities.to_list(result_plain, ";")
|
|
227
|
+
for files_tuple in files_tuples:
|
|
228
|
+
splitted = files_tuple.split("=")
|
|
229
|
+
result[splitted[0]] = splitted[1]
|
|
230
|
+
return result
|
|
231
|
+
|
|
232
|
+
@GeneralUtilities.check_arguments
|
|
233
|
+
def __standardized_tasks_build_for_dotnet_project(self,runtimes:list[str]) -> None:
|
|
234
|
+
|
|
235
|
+
target_environment_type: str=self.get_target_environment_type()
|
|
236
|
+
copy_license_file_to_target_folder: bool=True
|
|
237
|
+
codeunitname: str = self.get_codeunit_name()
|
|
238
|
+
|
|
239
|
+
workspace_folder=os.path.join(self.get_codeunit_folder(),"Other","Workspace")
|
|
240
|
+
GeneralUtilities.ensure_directory_does_not_exist(workspace_folder)
|
|
241
|
+
|
|
242
|
+
files_to_sign: dict[str, str] = self.get_filestosign_from_commandline_arguments( dict())
|
|
243
|
+
repository_folder: str = self.get_repository_folder()
|
|
244
|
+
commitid = self._protected_sc.git_get_commit_id(repository_folder)
|
|
245
|
+
outputfolder = GeneralUtilities.resolve_relative_path("./Other/Artifacts", self.get_codeunit_folder())
|
|
246
|
+
codeunit_folder = os.path.join(repository_folder, codeunitname)
|
|
247
|
+
csproj_file = os.path.join(codeunit_folder, codeunitname, codeunitname + ".csproj")
|
|
248
|
+
csproj_test_file = os.path.join(codeunit_folder, codeunitname+"Tests", codeunitname+"Tests.csproj")
|
|
249
|
+
self.__standardized_tasks_build_for_dotnet_build(csproj_file, os.path.join(outputfolder, "BuildResult_DotNet_"), files_to_sign, commitid, runtimes, target_environment_type, copy_license_file_to_target_folder, repository_folder, codeunitname)
|
|
250
|
+
self.__standardized_tasks_build_for_dotnet_build(csproj_test_file, os.path.join(outputfolder, "BuildResultTests_DotNet_"), files_to_sign, commitid, runtimes, target_environment_type, copy_license_file_to_target_folder, repository_folder, codeunitname)
|
|
251
|
+
self.generate_sbom_for_dotnet_project(codeunit_folder)
|
|
252
|
+
self.copy_source_files_to_output_directory()
|
|
253
|
+
|
|
254
|
+
@GeneralUtilities.check_arguments
|
|
255
|
+
def __standardized_tasks_build_nupkg_for_dotnet_create_package(self,runtimes:list[str]) -> None:
|
|
256
|
+
codeunitname: str = self.get_codeunit_name()
|
|
257
|
+
repository_folder: str =self.get_repository_folder()
|
|
258
|
+
build_folder = os.path.join(repository_folder, codeunitname, "Other", "Build")
|
|
259
|
+
outputfolder = GeneralUtilities.resolve_relative_path("./Other/Artifacts/BuildResult_NuGet",self.get_codeunit_folder())
|
|
260
|
+
nuspec_file = os.path.join(build_folder, f"{codeunitname}.nuspec")
|
|
261
|
+
root: etree._ElementTree = etree.parse(nuspec_file)
|
|
262
|
+
# the elements are matched by their local name because the nuspec-namespace can be declared as default-namespace
|
|
263
|
+
# (<package xmlns="...">) as well as with a prefix (<ns0:package xmlns:ns0="...">) and both variants are valid.
|
|
264
|
+
versions = root.xpath("//*[local-name() = 'package']/*[local-name() = 'metadata']/*[local-name() = 'version']/text()")
|
|
265
|
+
if len(versions) == 0:
|
|
266
|
+
raise ValueError(f"Can not determine the version because there is no version-element in \"{nuspec_file}\".")
|
|
267
|
+
current_version = versions[0]
|
|
268
|
+
nupkg_filename = f"{codeunitname}.{current_version}.nupkg"
|
|
269
|
+
nupkg_file = f"{build_folder}/{nupkg_filename}"
|
|
270
|
+
GeneralUtilities.ensure_file_does_not_exist(nupkg_file)
|
|
271
|
+
commit_id = self._protected_sc.git_get_commit_id(repository_folder)
|
|
272
|
+
# Pack the hand-written nuspec using "dotnet pack" (the classic "nuget pack" is not available in the dotnet-only build-container).
|
|
273
|
+
# With NuspecFile the package-content is taken entirely from the nuspec's <files>-list, so no rebuild happens here (the library was already built before).
|
|
274
|
+
self._protected_sc.run_program_argsasarray("dotnet", [
|
|
275
|
+
"pack", self.csproj_file,
|
|
276
|
+
"-c", self.get_target_environment_type(),
|
|
277
|
+
"--no-build", "--no-restore",
|
|
278
|
+
"--output", build_folder,
|
|
279
|
+
f"-p:NuspecFile={nuspec_file}",
|
|
280
|
+
f"-p:NuspecBasePath={build_folder}",
|
|
281
|
+
f"-p:NuspecProperties=commitid={commit_id}",
|
|
282
|
+
"-p:IsPackable=true",
|
|
283
|
+
], build_folder, print_live_output=self.get_verbosity()==LogLevel.Debug)
|
|
284
|
+
GeneralUtilities.ensure_directory_does_not_exist(outputfolder)
|
|
285
|
+
GeneralUtilities.ensure_directory_exists(outputfolder)
|
|
286
|
+
os.rename(nupkg_file, f"{outputfolder}/{nupkg_filename}")
|
|
287
|
+
|
|
288
|
+
@GeneralUtilities.check_arguments
|
|
289
|
+
def generate_sbom_for_dotnet_project(self, codeunit_folder: str) -> None:
|
|
290
|
+
self._protected_sc.log.log("Generate SBOM...")
|
|
291
|
+
codeunit_name = os.path.basename(codeunit_folder)
|
|
292
|
+
bomfile_folder = "Other/Artifacts/BOM"
|
|
293
|
+
self._protected_sc.run_program_argsasarray("dotnet", ["CycloneDX", f"{codeunit_name}/{codeunit_name}.csproj", "-o", bomfile_folder], codeunit_folder)
|
|
294
|
+
codeunitversion = self.tfcps_Tools_General.get_version_of_codeunit(os.path.join(codeunit_folder, f"{codeunit_name}.codeunit.xml"))
|
|
295
|
+
target = f"{codeunit_folder}/{bomfile_folder}/{codeunit_name}.{codeunitversion}.sbom.xml"
|
|
296
|
+
GeneralUtilities.ensure_file_does_not_exist(target)
|
|
297
|
+
os.rename(f"{codeunit_folder}/{bomfile_folder}/bom.xml", target)
|
|
298
|
+
self._protected_sc.format_xml_file(target)
|
|
299
|
+
|
|
300
|
+
@GeneralUtilities.check_arguments
|
|
301
|
+
def get_dotnet_build_diagnostics(self) -> list[tuple[LogLevel, str, str | None, int | None]]:
|
|
302
|
+
codeunit_name = self.get_codeunit_name()
|
|
303
|
+
codeunit_folder = self.get_codeunit_folder()
|
|
304
|
+
sln_file = os.path.join(codeunit_folder, codeunit_name + ".sln")
|
|
305
|
+
temp_output_folder = os.path.join(GeneralUtilities.get_temp_folder(), str(uuid.uuid4()))
|
|
306
|
+
GeneralUtilities.ensure_directory_exists(temp_output_folder)
|
|
307
|
+
# Run the build from an absolute temporary working-directory (instead of the codeunit-folder) and with node-reuse
|
|
308
|
+
# disabled. Building a solution with "-o" (which is required here to keep the diagnostics-build isolated from the
|
|
309
|
+
# real build-output) is not officially supported by the .NET-SDK (warning NETSDK1194) and makes MSBuild create an
|
|
310
|
+
# additional "tmp/<guid>"-output-folder relative to the build-process' working-directory. With node-reuse enabled
|
|
311
|
+
# that folder is even created by a reused MSBuild-worker-node that still has a codeunit-subfolder as its working-
|
|
312
|
+
# directory, so it ends up inside the codeunit-folder. Such a folder is usually cleaned up by the SDK, but on a
|
|
313
|
+
# bind-mounted filesystem (for example when the build runs in a Linux-container with the repository mounted from a
|
|
314
|
+
# Windows-host) the cleanup can fail and the folder is left behind. By using an absolute working-directory and
|
|
315
|
+
# forcing fresh worker-nodes (which inherit that working-directory) any such relative "tmp/<guid>"-folder is created
|
|
316
|
+
# below temp_output_folder and removed together with it in the finally-block.
|
|
317
|
+
try:
|
|
318
|
+
# The arguments are passed as an array and without quoting them by hand: run_program splits its
|
|
319
|
+
# argument-string with GeneralUtilities.arguments_to_array, which splits at spaces without interpreting
|
|
320
|
+
# quotes, so hand-written quotes end up as part of the argument-value. The value of "-o" would therefore
|
|
321
|
+
# become '"<temp_output_folder>"' (with the quotes), which MSBuild treats as a relative path (it does not
|
|
322
|
+
# begin with a drive-letter anymore) and resolves against the working-directory, so it writes to a path
|
|
323
|
+
# which contains a quote in the middle. On Linux that only creates a strangely named folder, on Windows a
|
|
324
|
+
# quote is not allowed in a path and the msbuild-task GenerateDepsFile fails with an IOException.
|
|
325
|
+
run_result = self._protected_sc.run_program_argsasarray("dotnet", ["build", sln_file, "-nologo", "-v", "minimal", "-o", temp_output_folder], temp_output_folder, throw_exception_if_exitcode_is_not_zero=False, env_vars={"DOTNET_CLI_UI_LANGUAGE": "en-US", "MSBUILDDISABLENODEREUSE": "1"})
|
|
326
|
+
finally:
|
|
327
|
+
GeneralUtilities.ensure_directory_does_not_exist(temp_output_folder)
|
|
328
|
+
diagnostics: list[tuple[LogLevel, str, str | None, int | None]] = []
|
|
329
|
+
pattern = re.compile(r"^\s*(?:(.+?)\((\d+),\d+\): )?(error|warning|message|info) [^:]+: (.+?)(?:\s*\[.+\])?\s*$", re.IGNORECASE)
|
|
330
|
+
for line in GeneralUtilities.string_to_lines(run_result[1] + "\n" + run_result[2]):
|
|
331
|
+
m = pattern.match(line)
|
|
332
|
+
if m:
|
|
333
|
+
file_path = m.group(1)
|
|
334
|
+
line_number = int(m.group(2)) if m.group(2) else None
|
|
335
|
+
level_str = m.group(3).lower()
|
|
336
|
+
message = m.group(4)
|
|
337
|
+
if level_str == "error":
|
|
338
|
+
level = LogLevel.Error
|
|
339
|
+
elif level_str == "warning":
|
|
340
|
+
level = LogLevel.Warning
|
|
341
|
+
else:
|
|
342
|
+
level = LogLevel.Information
|
|
343
|
+
diagnostics.append((level, message, file_path, line_number))
|
|
344
|
+
return diagnostics
|
|
345
|
+
|
|
346
|
+
@GeneralUtilities.check_arguments
|
|
347
|
+
def linting(self) -> None:
|
|
348
|
+
codeunit_name = self.get_codeunit_name()
|
|
349
|
+
codeunit_folder = self.get_codeunit_folder()
|
|
350
|
+
self._protected_sc.normalize_line_endings_of_files_in_folder(codeunit_folder, ["cs"])
|
|
351
|
+
nuspec_file = os.path.join(codeunit_folder, "Other", "Build", f"{codeunit_name}.nuspec")
|
|
352
|
+
if os.path.isfile(nuspec_file):
|
|
353
|
+
self._protected_sc.format_xml_file(nuspec_file)
|
|
354
|
+
self._protected_sc.format_xml_file(os.path.join(codeunit_folder, codeunit_name, codeunit_name + ".csproj"), add_xml_declaration=False)
|
|
355
|
+
self._protected_sc.format_xml_file(os.path.join(codeunit_folder, codeunit_name + "Tests", codeunit_name + "Tests.csproj"), add_xml_declaration=False)
|
|
356
|
+
self.standardized_task_verify_standard_format_csproj_files()
|
|
357
|
+
diagnostics = self.get_dotnet_build_diagnostics()
|
|
358
|
+
has_errors = False
|
|
359
|
+
for (level, message, file, line) in diagnostics:
|
|
360
|
+
location = f" ({file}:{line})" if file else ""
|
|
361
|
+
self._protected_sc.log.log(f"{message}{location}", level)
|
|
362
|
+
if level == LogLevel.Error:#should not occurr on scbuildcodeunits because then the build would have failed already but you can also run this script manually.
|
|
363
|
+
has_errors = True
|
|
364
|
+
if has_errors:
|
|
365
|
+
raise ValueError("Linting-issues occurred.")
|
|
366
|
+
|
|
367
|
+
@GeneralUtilities.check_arguments
|
|
368
|
+
def do_common_tasks(self,current_codeunit_version:str,certificateGeneratorInformation:CertificateGeneratorInformationBase)-> None:
|
|
369
|
+
self.do_common_tasks_base(current_codeunit_version)
|
|
370
|
+
self.update_year_for_dotnet_codeunit()
|
|
371
|
+
codeunit_name =self.get_codeunit_name()
|
|
372
|
+
codeunit_version = self.tfcps_Tools_General.get_version_of_project(self.get_repository_folder()) # Should always be the same as the project-version #TODO make this configurable from outside
|
|
373
|
+
folder_of_current_file =os.path.join(self.get_codeunit_folder(),"Other")
|
|
374
|
+
self._protected_sc.replace_version_in_csproj_file(GeneralUtilities.resolve_relative_path(f"../{codeunit_name}/{codeunit_name}.csproj", folder_of_current_file), codeunit_version)
|
|
375
|
+
self._protected_sc.replace_version_in_csproj_file(GeneralUtilities.resolve_relative_path(f"../{codeunit_name}Tests/{codeunit_name}Tests.csproj", folder_of_current_file), codeunit_version)
|
|
376
|
+
if self.is_library:
|
|
377
|
+
self._protected_sc.replace_version_in_nuspec_file(GeneralUtilities.resolve_relative_path(f"./Build/{codeunit_name}.nuspec", folder_of_current_file), codeunit_version)
|
|
378
|
+
if certificateGeneratorInformation.generate_certificate():
|
|
379
|
+
self.tfcps_Tools_General.set_constants_for_certificate_private_information(self.get_codeunit_folder())
|
|
380
|
+
|
|
381
|
+
@GeneralUtilities.check_arguments
|
|
382
|
+
def standardized_task_verify_standard_format_csproj_files(self) -> bool:
|
|
383
|
+
codeunit_folder=self.get_codeunit_folder()
|
|
384
|
+
repository_folder = os.path.dirname(codeunit_folder)
|
|
385
|
+
codeunit_name = os.path.basename(codeunit_folder)
|
|
386
|
+
codeunit_folder = os.path.join(repository_folder, codeunit_name)
|
|
387
|
+
codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(self.get_codeunit_file())
|
|
388
|
+
|
|
389
|
+
csproj_project_name = codeunit_name
|
|
390
|
+
csproj_file = os.path.join(codeunit_folder, csproj_project_name, csproj_project_name+".csproj")
|
|
391
|
+
result1: tuple[bool, str, list[str]] = self.__standardized_task_verify_standard_format_for_project_csproj_file(csproj_file, codeunit_folder, codeunit_name, codeunit_version)
|
|
392
|
+
if not result1[0]:
|
|
393
|
+
hints: str = "\n".join(result1[2])
|
|
394
|
+
raise ValueError(f"'{csproj_file}' with content '{GeneralUtilities.read_text_from_file(csproj_file)}' does not match the standardized .csproj-file-format which is defined by the regex '{result1[1]}'.\n{hints}")
|
|
395
|
+
self.__check_csproj_urls(csproj_file)
|
|
396
|
+
|
|
397
|
+
test_csproj_project_name = csproj_project_name+"Tests"
|
|
398
|
+
test_csproj_file = os.path.join(codeunit_folder, test_csproj_project_name, test_csproj_project_name+".csproj")
|
|
399
|
+
result2: tuple[bool, str, list[str]] = self.__standardized_task_verify_standard_format_for_test_csproj_file(test_csproj_file, codeunit_name, codeunit_version)
|
|
400
|
+
if not result2[0]:
|
|
401
|
+
hints: str = "\n".join(result2[2])
|
|
402
|
+
raise ValueError(f"'{test_csproj_file}' with content '{GeneralUtilities.read_text_from_file(test_csproj_file)}' does not match the standardized .csproj-file-format which is defined by the regex '{result2[1]}'.\n{hints}")
|
|
403
|
+
self.__check_csproj_urls(test_csproj_file)
|
|
404
|
+
|
|
405
|
+
@GeneralUtilities.check_arguments
|
|
406
|
+
def __check_csproj_urls(self, csproj_file: str) -> None:
|
|
407
|
+
remote_address: str = self.get_remote_address()
|
|
408
|
+
root: etree._ElementTree = etree.parse(csproj_file)
|
|
409
|
+
package_project_url: str = self.__get_unique_csproj_property_value(root, csproj_file, "PackageProjectUrl")
|
|
410
|
+
repository_url: str = self.__get_unique_csproj_property_value(root, csproj_file, "RepositoryUrl")
|
|
411
|
+
if package_project_url != remote_address:
|
|
412
|
+
raise ValueError(f"The PackageProjectUrl-value '{package_project_url}' in '{csproj_file}' is not equal to the remote-address '{remote_address}' which is defined in the ProductInformation.xml-file of the repository.")
|
|
413
|
+
expected_repository_url: str = package_project_url+".git"
|
|
414
|
+
if repository_url != expected_repository_url:
|
|
415
|
+
raise ValueError(f"The RepositoryUrl-value '{repository_url}' in '{csproj_file}' is not equal to the expected value '{expected_repository_url}'.")
|
|
416
|
+
|
|
417
|
+
@GeneralUtilities.check_arguments
|
|
418
|
+
def __get_unique_csproj_property_value(self, root: etree._ElementTree, csproj_file: str, property_name: str) -> str:
|
|
419
|
+
values: list[str] = [str(value).strip() for value in root.xpath(f"/Project/PropertyGroup/{property_name}/text()")]
|
|
420
|
+
if len(values) != 1:
|
|
421
|
+
raise ValueError(f"'{csproj_file}' must contain exactly one {property_name}-element but contains {len(values)} of them.")
|
|
422
|
+
return values[0]
|
|
423
|
+
|
|
424
|
+
def __standardized_task_verify_standard_format_for_project_csproj_file(self, csproj_file: str, codeunit_folder: str, codeunit_name: str, codeunit_version: str) -> tuple[bool, str, str]:
|
|
425
|
+
codeunit_name_regex = re.escape(codeunit_name)
|
|
426
|
+
codeunit_description = self.tfcps_Tools_General.get_codeunit_description(self.get_codeunit_file())
|
|
427
|
+
codeunit_version_regex = re.escape(codeunit_version)
|
|
428
|
+
codeunit_description_regex = re.escape(codeunit_description)
|
|
429
|
+
regex = f"""^<Project Sdk=\\"Microsoft\\.NET\\.Sdk\\">
|
|
430
|
+
<PropertyGroup>
|
|
431
|
+
<TargetFramework>([^<]+)<\\/TargetFramework>
|
|
432
|
+
<Authors>([^<]+)<\\/Authors>
|
|
433
|
+
<Version>{codeunit_version_regex}<\\/Version>
|
|
434
|
+
<AssemblyVersion>{codeunit_version_regex}<\\/AssemblyVersion>
|
|
435
|
+
<FileVersion>{codeunit_version_regex}<\\/FileVersion>
|
|
436
|
+
<SelfContained>false<\\/SelfContained>
|
|
437
|
+
<IsPackable>false<\\/IsPackable>
|
|
438
|
+
<PreserveCompilationContext>false<\\/PreserveCompilationContext>
|
|
439
|
+
<GenerateRuntimeConfigurationFiles>true<\\/GenerateRuntimeConfigurationFiles>
|
|
440
|
+
<Copyright>([^<]+)<\\/Copyright>
|
|
441
|
+
<Description>{codeunit_description_regex}<\\/Description>
|
|
442
|
+
<PackageProjectUrl>https:\\/\\/([^<]+)<\\/PackageProjectUrl>
|
|
443
|
+
<RepositoryUrl>https:\\/\\/([^<]+)\\.git<\\/RepositoryUrl>
|
|
444
|
+
<RootNamespace>([^<]+)\\.Core<\\/RootNamespace>
|
|
445
|
+
<ProduceReferenceAssembly>false<\\/ProduceReferenceAssembly>
|
|
446
|
+
<Nullable>(disable|enable|warnings|annotations)<\\/Nullable>
|
|
447
|
+
<Configurations>Development;QualityCheck;Productive<\\/Configurations>
|
|
448
|
+
<IsTestProject>false<\\/IsTestProject>
|
|
449
|
+
<LangVersion>([^<]+)<\\/LangVersion>
|
|
450
|
+
<PackageRequireLicenseAcceptance>true<\\/PackageRequireLicenseAcceptance>
|
|
451
|
+
<GenerateSerializationAssemblies>Off<\\/GenerateSerializationAssemblies>
|
|
452
|
+
<AppendTargetFrameworkToOutputPath>false<\\/AppendTargetFrameworkToOutputPath>
|
|
453
|
+
<OutputPath>\\.\\.\\\\Other\\\\Artifacts\\\\BuildResult_DotNet_win\\-x64<\\/OutputPath>
|
|
454
|
+
<PlatformTarget>([^<]+)<\\/PlatformTarget>
|
|
455
|
+
<WarningLevel>\\d<\\/WarningLevel>
|
|
456
|
+
<Prefer32Bit>false<\\/Prefer32Bit>
|
|
457
|
+
<SignAssembly>true<\\/SignAssembly>
|
|
458
|
+
<AssemblyOriginatorKeyFile>\\.\\.\\\\\\.\\.\\\\Other\\\\Resources\\\\PublicKeys\\\\StronglyNamedKey\\\\([^<]+)PublicKey\\.snk<\\/AssemblyOriginatorKeyFile>
|
|
459
|
+
<DelaySign>true<\\/DelaySign>
|
|
460
|
+
<NoWarn>([^<]+)<\\/NoWarn>
|
|
461
|
+
<WarningsAsErrors>([^<]+)<\\/WarningsAsErrors>
|
|
462
|
+
<ErrorLog>\\.\\.\\\\Other\\\\Resources\\\\CodeAnalysisResult\\\\{codeunit_name_regex}\\.sarif<\\/ErrorLog>
|
|
463
|
+
<OutputType>([^<]+)<\\/OutputType>
|
|
464
|
+
<DocumentationFile>\\.\\.\\\\Other\\\\Artifacts\\\\MetaInformation\\\\{codeunit_name_regex}\\.xml<\\/DocumentationFile>(\\n|.)*
|
|
465
|
+
<\\/PropertyGroup>
|
|
466
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Development'\\\">
|
|
467
|
+
<DebugType>full<\\/DebugType>
|
|
468
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
469
|
+
<Optimize>false<\\/Optimize>
|
|
470
|
+
<DefineConstants>TRACE;DEBUG;Development<\\/DefineConstants>
|
|
471
|
+
<ErrorReport>prompt<\\/ErrorReport>
|
|
472
|
+
<\\/PropertyGroup>
|
|
473
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='QualityCheck'\\\">
|
|
474
|
+
<DebugType>portable<\\/DebugType>
|
|
475
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
476
|
+
<Optimize>false<\\/Optimize>
|
|
477
|
+
<DefineConstants>TRACE;QualityCheck<\\/DefineConstants>
|
|
478
|
+
<ErrorReport>none<\\/ErrorReport>
|
|
479
|
+
<\\/PropertyGroup>
|
|
480
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Productive'\\\">
|
|
481
|
+
<DebugType>portable<\\/DebugType>
|
|
482
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
483
|
+
<Optimize>false<\\/Optimize>
|
|
484
|
+
<DefineConstants>Productive<\\/DefineConstants>
|
|
485
|
+
<ErrorReport>none<\\/ErrorReport>
|
|
486
|
+
<\\/PropertyGroup>(\\n|.)*
|
|
487
|
+
<\\/Project>\\n?$"""
|
|
488
|
+
result = self.__standardized_task_verify_standard_format_for_csproj_files(regex, csproj_file)
|
|
489
|
+
return (result[0], regex, result[1])
|
|
490
|
+
|
|
491
|
+
def __standardized_task_verify_standard_format_for_test_csproj_file(self, csproj_file: str, codeunit_name: str, codeunit_version: str) -> tuple[bool, str, str]:
|
|
492
|
+
codeunit_name_regex = re.escape(codeunit_name)
|
|
493
|
+
codeunit_version_regex = re.escape(codeunit_version)
|
|
494
|
+
regex = f"""^<Project Sdk=\\"Microsoft\\.NET\\.Sdk\\">
|
|
495
|
+
<PropertyGroup>
|
|
496
|
+
<TargetFramework>([^<]+)<\\/TargetFramework>
|
|
497
|
+
<Authors>([^<]+)<\\/Authors>
|
|
498
|
+
<Version>{codeunit_version_regex}<\\/Version>
|
|
499
|
+
<AssemblyVersion>{codeunit_version_regex}<\\/AssemblyVersion>
|
|
500
|
+
<FileVersion>{codeunit_version_regex}<\\/FileVersion>
|
|
501
|
+
<SelfContained>false<\\/SelfContained>
|
|
502
|
+
<IsPackable>false<\\/IsPackable>
|
|
503
|
+
<PreserveCompilationContext>false<\\/PreserveCompilationContext>
|
|
504
|
+
<GenerateRuntimeConfigurationFiles>true<\\/GenerateRuntimeConfigurationFiles>
|
|
505
|
+
<Copyright>([^<]+)<\\/Copyright>
|
|
506
|
+
<Description>{codeunit_name_regex}Tests is the test-project for {codeunit_name_regex}\\.<\\/Description>
|
|
507
|
+
<PackageProjectUrl>https:\\/\\/([^<]+)<\\/PackageProjectUrl>
|
|
508
|
+
<RepositoryUrl>https:\\/\\/([^<]+)\\.git</RepositoryUrl>
|
|
509
|
+
<RootNamespace>([^<]+)\\.Tests<\\/RootNamespace>
|
|
510
|
+
<ProduceReferenceAssembly>false<\\/ProduceReferenceAssembly>
|
|
511
|
+
<Nullable>(disable|enable|warnings|annotations)<\\/Nullable>
|
|
512
|
+
<Configurations>Development;QualityCheck;Productive<\\/Configurations>
|
|
513
|
+
<IsTestProject>true<\\/IsTestProject>
|
|
514
|
+
<LangVersion>([^<]+)<\\/LangVersion>
|
|
515
|
+
<PackageRequireLicenseAcceptance>true<\\/PackageRequireLicenseAcceptance>
|
|
516
|
+
<GenerateSerializationAssemblies>Off<\\/GenerateSerializationAssemblies>
|
|
517
|
+
<AppendTargetFrameworkToOutputPath>false<\\/AppendTargetFrameworkToOutputPath>
|
|
518
|
+
<OutputPath>\\.\\.\\\\Other\\\\Artifacts\\\\BuildResultTests_DotNet_win\\-x64<\\/OutputPath>
|
|
519
|
+
<PlatformTarget>([^<]+)<\\/PlatformTarget>
|
|
520
|
+
<WarningLevel>\\d<\\/WarningLevel>
|
|
521
|
+
<Prefer32Bit>false<\\/Prefer32Bit>
|
|
522
|
+
<SignAssembly>true<\\/SignAssembly>
|
|
523
|
+
<AssemblyOriginatorKeyFile>\\.\\.\\\\\\.\\.\\\\Other\\\\Resources\\\\PublicKeys\\\\StronglyNamedKey\\\\([^<]+)PublicKey\\.snk<\\/AssemblyOriginatorKeyFile>
|
|
524
|
+
<DelaySign>true<\\/DelaySign>
|
|
525
|
+
<NoWarn>([^<]+)<\\/NoWarn>
|
|
526
|
+
<WarningsAsErrors>([^<]+)<\\/WarningsAsErrors>
|
|
527
|
+
<ErrorLog>\\.\\.\\\\Other\\\\Resources\\\\CodeAnalysisResult\\\\{codeunit_name_regex}Tests\\.sarif<\\/ErrorLog>
|
|
528
|
+
<OutputType>Library<\\/OutputType>(\\n|.)*
|
|
529
|
+
<\\/PropertyGroup>
|
|
530
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Development'\\\">
|
|
531
|
+
<DebugType>full<\\/DebugType>
|
|
532
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
533
|
+
<Optimize>false<\\/Optimize>
|
|
534
|
+
<DefineConstants>TRACE;DEBUG;Development<\\/DefineConstants>
|
|
535
|
+
<ErrorReport>prompt<\\/ErrorReport>
|
|
536
|
+
<\\/PropertyGroup>
|
|
537
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='QualityCheck'\\\">
|
|
538
|
+
<DebugType>portable<\\/DebugType>
|
|
539
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
540
|
+
<Optimize>false<\\/Optimize>
|
|
541
|
+
<DefineConstants>TRACE;QualityCheck<\\/DefineConstants>
|
|
542
|
+
<ErrorReport>none<\\/ErrorReport>
|
|
543
|
+
<\\/PropertyGroup>
|
|
544
|
+
<PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Productive'\\\">
|
|
545
|
+
<DebugType>portable<\\/DebugType>
|
|
546
|
+
<DebugSymbols>true<\\/DebugSymbols>
|
|
547
|
+
<Optimize>false<\\/Optimize>
|
|
548
|
+
<DefineConstants>Productive<\\/DefineConstants>
|
|
549
|
+
<ErrorReport>none<\\/ErrorReport>
|
|
550
|
+
<\\/PropertyGroup>(\\n|.)*
|
|
551
|
+
<\\/Project>\\n?$"""
|
|
552
|
+
result = self.__standardized_task_verify_standard_format_for_csproj_files(regex, csproj_file)
|
|
553
|
+
return (result[0], regex, result[1])
|
|
554
|
+
|
|
555
|
+
def __standardized_task_verify_standard_format_for_csproj_files(self, regex: str, csproj_file: str) -> tuple[bool, list[str]]:
|
|
556
|
+
filename = os.path.basename(csproj_file)
|
|
557
|
+
self._protected_sc.log.log(f"Check {filename}...",LogLevel.Debug)
|
|
558
|
+
file_content = GeneralUtilities.read_text_from_file(csproj_file)
|
|
559
|
+
regex_for_check = regex.replace("\r", GeneralUtilities.empty_string).replace("\n", "\\n")
|
|
560
|
+
file_content = file_content.replace("\r", GeneralUtilities.empty_string)
|
|
561
|
+
match = re.match(regex_for_check, file_content)
|
|
562
|
+
result = match is not None
|
|
563
|
+
hints = None
|
|
564
|
+
if not result:
|
|
565
|
+
hints = self.get_hints_for_csproj(regex, file_content)
|
|
566
|
+
return (result, hints)
|
|
567
|
+
|
|
568
|
+
@GeneralUtilities.check_arguments
|
|
569
|
+
def get_hints_for_csproj(self, regex: str, file_content: str) -> list[str]:
|
|
570
|
+
result: list[str] = []
|
|
571
|
+
regex_lines = GeneralUtilities.string_to_lines(regex)
|
|
572
|
+
file_content_lines = GeneralUtilities.string_to_lines(file_content)
|
|
573
|
+
amount_of_lines = len(file_content_lines)
|
|
574
|
+
if amount_of_lines< len(regex_lines):
|
|
575
|
+
result.append("csproj-file has less lines than the regex requires.")
|
|
576
|
+
return result
|
|
577
|
+
for i in range(35):#you can do this check only for the first 35 lines
|
|
578
|
+
s = file_content_lines[i]
|
|
579
|
+
r = regex_lines[i]
|
|
580
|
+
if not re.match(r, s):
|
|
581
|
+
result.append(f"Line {i+1} does not match: Regex='{r}' String='{s}'")
|
|
582
|
+
return result
|
|
583
|
+
|
|
584
|
+
@GeneralUtilities.check_arguments
|
|
585
|
+
def generate_reference(self, generate_class_reference:bool=False) -> None:
|
|
586
|
+
self.generate_reference_using_docfx(generate_class_reference)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
@GeneralUtilities.check_arguments
|
|
590
|
+
def update_year_for_dotnet_codeunit(self) -> None:
|
|
591
|
+
codeunit_folder:str=self.get_codeunit_folder()
|
|
592
|
+
codeunit_name = os.path.basename(codeunit_folder)
|
|
593
|
+
csproj_file = os.path.join(codeunit_folder, codeunit_name, f"{codeunit_name}.csproj")
|
|
594
|
+
self._protected_sc.update_year_in_copyright_tags(csproj_file)
|
|
595
|
+
csprojtests_file = os.path.join(codeunit_folder, f"{codeunit_name}Tests", f"{codeunit_name}Tests.csproj")
|
|
596
|
+
self._protected_sc.update_year_in_copyright_tags(csprojtests_file)
|
|
597
|
+
nuspec_file = os.path.join(codeunit_folder, "Other", "Build", f"{codeunit_name}.nuspec")
|
|
598
|
+
if os.path.isfile(nuspec_file):
|
|
599
|
+
self._protected_sc.update_year_in_copyright_tags(nuspec_file)
|
|
600
|
+
|
|
601
|
+
@GeneralUtilities.check_arguments
|
|
602
|
+
def run_testcases(self, timeoutInSeconds:int=60*30) -> None:
|
|
603
|
+
self._protected_sc.log.log("Run testcases...")
|
|
604
|
+
dotnet_build_configuration: str = self.get_target_environment_type()
|
|
605
|
+
codeunit_name: str = self.get_codeunit_name()
|
|
606
|
+
|
|
607
|
+
repository_folder: str = self.get_repository_folder().replace("\\", "/")
|
|
608
|
+
coverage_file_folder = os.path.join(repository_folder, codeunit_name, "Other/Artifacts/TestCoverage")
|
|
609
|
+
temp_folder = os.path.join(GeneralUtilities.get_temp_folder(), str(uuid.uuid4()))
|
|
610
|
+
GeneralUtilities.ensure_directory_exists(temp_folder)
|
|
611
|
+
runsettings_file = "runsettings.xml"
|
|
612
|
+
codeunit_folder = f"{repository_folder}/{codeunit_name}"
|
|
613
|
+
GeneralUtilities.ensure_directory_exists(coverage_file_folder)
|
|
614
|
+
target_file = os.path.join(coverage_file_folder, "TestCoverage.xml")
|
|
615
|
+
GeneralUtilities.ensure_file_does_not_exist(target_file)
|
|
616
|
+
|
|
617
|
+
sln_file = os.path.join(codeunit_folder, f"{codeunit_name}.sln")
|
|
618
|
+
args: list[str] = ["test", sln_file, "-c", dotnet_build_configuration, "-o", temp_folder]
|
|
619
|
+
runsettings_path = os.path.join(codeunit_folder, runsettings_file)
|
|
620
|
+
if os.path.isfile(runsettings_path):
|
|
621
|
+
args += ["--settings", runsettings_path]
|
|
622
|
+
# Write the test-results (test-binaries-deployment and coverage) into the system-temp-folder (a subfolder of
|
|
623
|
+
# temp_folder) instead of a relative "./TestResults" inside the codeunit-folder. The relative path would otherwise
|
|
624
|
+
# leave a folder behind in the codeunit-folder (visible e.g. when building inside the mounted Debian-build-container).
|
|
625
|
+
# The whole temp_folder - including these results - is removed in the finally-block below.
|
|
626
|
+
args += ["--results-directory", os.path.join(temp_folder, "TestResults")]
|
|
627
|
+
# Run dotnet-test from an absolute working-directory (a subfolder of temp_folder) with node-reuse disabled, for the
|
|
628
|
+
# same reason as in get_dotnet_build_diagnostics: building the solution with "-o" makes MSBuild create an additional
|
|
629
|
+
# relative "tmp/<guid>"-output-folder, which a reused worker-node would otherwise create inside the codeunit-folder
|
|
630
|
+
# where it can be left behind on a bind-mounted filesystem. All paths passed to dotnet-test are absolute, so the
|
|
631
|
+
# changed working-directory does not affect the test-result or the coverage-output.
|
|
632
|
+
test_working_directory = os.path.join(temp_folder, "WorkingDirectory")
|
|
633
|
+
GeneralUtilities.ensure_directory_exists(test_working_directory)
|
|
634
|
+
try:
|
|
635
|
+
program_output=self._protected_sc.run_program_argsasarray("dotnet", args, test_working_directory, print_live_output=self.get_verbosity()==LogLevel.Debug, timeoutInSeconds=timeoutInSeconds, env_vars={"MSBUILDDISABLENODEREUSE": "1"})
|
|
636
|
+
test_output:str=program_output[1]
|
|
637
|
+
output_lines=program_output[1].split("\n")
|
|
638
|
+
output_lines=[line for line in output_lines if GeneralUtilities.string_has_content(line)]
|
|
639
|
+
generated_coverage_file: str = output_lines[-1].strip()#the cobertura file is printed in the end of the output by the xplat collector
|
|
640
|
+
GeneralUtilities.assert_file_exists(generated_coverage_file)
|
|
641
|
+
shutil.copyfile(generated_coverage_file, target_file)
|
|
642
|
+
finally:
|
|
643
|
+
GeneralUtilities.ensure_directory_does_not_exist(temp_folder)
|
|
644
|
+
|
|
645
|
+
self.__remove_unrelated_package_from_testcoverage_file(target_file, codeunit_name)
|
|
646
|
+
root: etree._ElementTree = etree.parse(target_file)
|
|
647
|
+
source_base_path_in_coverage_file: str = root.xpath("//coverage/sources/source/text()")[0].replace("\\", "/")
|
|
648
|
+
content = GeneralUtilities.read_text_from_file(target_file)
|
|
649
|
+
GeneralUtilities.assert_condition(source_base_path_in_coverage_file.startswith(repository_folder) or repository_folder.startswith(source_base_path_in_coverage_file), f"Unexpected path for coverage. Sourcepath: \"{source_base_path_in_coverage_file}\"; repository: \"{repository_folder}\"")
|
|
650
|
+
content = re.sub('\\\\', '/', content)
|
|
651
|
+
content = re.sub("filename=\"([^\"]+)\"", lambda match: self.__standardized_tasks_run_testcases_for_dotnet_project_helper(source_base_path_in_coverage_file, codeunit_folder, match), content)
|
|
652
|
+
GeneralUtilities.write_text_to_file(target_file, content)
|
|
653
|
+
self.run_testcases_common_post_task(repository_folder, codeunit_name, True, self.get_target_environment_type())
|
|
654
|
+
artifacts_folder = os.path.join(repository_folder, codeunit_name, "Other", "Artifacts")
|
|
655
|
+
for subfolder in GeneralUtilities.get_direct_folders_of_folder(artifacts_folder):
|
|
656
|
+
if os.path.basename(subfolder).startswith("BuildResultTests_DotNet_"):
|
|
657
|
+
GeneralUtilities.ensure_directory_does_not_exist(subfolder)
|
|
658
|
+
|
|
659
|
+
amount_of_ignored_testcases:int=self.__get_amount_of_ignored_testcases(test_output)
|
|
660
|
+
project_has_ignored_testcases:bool=0<amount_of_ignored_testcases
|
|
661
|
+
if project_has_ignored_testcases:
|
|
662
|
+
self._protected_sc.log.log(f"Project '{codeunit_name}' has {amount_of_ignored_testcases} ignored testcases.", LogLevel.Warning)
|
|
663
|
+
|
|
664
|
+
@GeneralUtilities.check_arguments
|
|
665
|
+
def __get_amount_of_ignored_testcases(self, test_output: str) -> int:
|
|
666
|
+
# Ignored (=skipped) testcases are counted in the summary-line which dotnet-test prints for each test-project.
|
|
667
|
+
# The summary of the VSTest-runner looks like "Failed: 0, Passed: 3, Skipped: 1, Total: 4" and the summary of the
|
|
668
|
+
# Microsoft-Testing-Platform-runner looks like "total: 4 / failed: 0 / succeeded: 3 / skipped: 1".
|
|
669
|
+
result: int = 0
|
|
670
|
+
for match in re.finditer(r"skipped:\s*(\d+)", test_output, re.IGNORECASE):
|
|
671
|
+
result = result+int(match.group(1))
|
|
672
|
+
return result
|
|
673
|
+
|
|
674
|
+
@GeneralUtilities.check_arguments
|
|
675
|
+
def __remove_unrelated_package_from_testcoverage_file(self, file: str, codeunit_name: str) -> None:
|
|
676
|
+
root: etree._ElementTree = etree.parse(file)
|
|
677
|
+
packages = root.xpath('//coverage/packages/package')
|
|
678
|
+
for package in packages:
|
|
679
|
+
if package.attrib['name'] != codeunit_name:
|
|
680
|
+
package.getparent().remove(package)
|
|
681
|
+
result = etree.tostring(root).decode("utf-8")
|
|
682
|
+
GeneralUtilities.write_text_to_file(file, result)
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
@GeneralUtilities.check_arguments
|
|
686
|
+
def __standardized_tasks_run_testcases_for_dotnet_project_helper(self, source: str, codeunit_folder: str, match: re.Match) -> str:
|
|
687
|
+
filename = match.group(1)
|
|
688
|
+
file = os.path.join(source, filename)
|
|
689
|
+
GeneralUtilities.assert_condition(file.startswith(codeunit_folder), f"Unexpected path for coverage-file. File: \"{file}\"; codeunitfolder: \"{codeunit_folder}\"")
|
|
690
|
+
filename_relative = f".{file[len(codeunit_folder):]}"
|
|
691
|
+
return f'filename="{filename_relative}"'
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def get_dependencies(self)->dict[str,set[str]]:
|
|
695
|
+
return dict[str,set[str]]()#TODO
|
|
696
|
+
|
|
697
|
+
@GeneralUtilities.check_arguments
|
|
698
|
+
def get_available_versions(self,dependencyname:str)->list[str]:
|
|
699
|
+
return []#TODO
|
|
700
|
+
|
|
701
|
+
def set_dependency_version(self,name:str,new_version:str)->None:
|
|
702
|
+
raise ValueError(f"Operation is not implemented.")
|
|
703
|
+
#csproj_file:str=os.path.join(self.get_codeunit_folder(), self.get_codeunit_name(), self.get_codeunit_name() + ".csproj")
|
|
704
|
+
#self._protected_sc.update_dependencies_of_dotnet_project(csproj_file,[])#TODO set ignored codeunits
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
class TFCPS_CodeUnitSpecific_DotNet_CLI:
|
|
708
|
+
|
|
709
|
+
@staticmethod
|
|
710
|
+
@GeneralUtilities.check_arguments
|
|
711
|
+
def parse(file:str)->TFCPS_CodeUnitSpecific_DotNet_Functions:
|
|
712
|
+
parser=TFCPS_CodeUnitSpecific_Base_CLI.get_base_parser()
|
|
713
|
+
#add custom parameter if desired
|
|
714
|
+
args=parser.parse_args()
|
|
715
|
+
result:TFCPS_CodeUnitSpecific_DotNet_Functions=TFCPS_CodeUnitSpecific_DotNet_Functions(file,LogLevel(int(args.verbosity)),args.targetenvironmenttype,not args.nocache,args.ispremerge)
|
|
716
|
+
return result
|