ScriptCollection 4.3.64__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.
Files changed (37) hide show
  1. ScriptCollection/AnionBuildPlatform.py +205 -205
  2. ScriptCollection/CertificateUpdater.py +157 -157
  3. ScriptCollection/Executables.py +4 -2
  4. ScriptCollection/GeneralUtilities.py +1696 -1696
  5. ScriptCollection/OCIImages/OCIImageManager.py +278 -278
  6. ScriptCollection/ProcessesRunner.py +43 -43
  7. ScriptCollection/ProgramRunnerSudo.py +110 -110
  8. ScriptCollection/Resources/CultureChooser/CultureChooser.js +0 -0
  9. ScriptCollection/Resources/CultureChooser/index.html +0 -0
  10. ScriptCollection/Resources/Dependencies/JRE/Version.txt +0 -0
  11. ScriptCollection/Resources/Dependencies/OpenAPIGenerator/Version.txt +0 -0
  12. ScriptCollection/Resources/Dependencies/PlantUML/Version.txt +0 -0
  13. ScriptCollection/Resources/Fonts/DejaVuSans-Bold.ttf +0 -0
  14. ScriptCollection/Resources/Fonts/DejaVuSans-BoldOblique.ttf +0 -0
  15. ScriptCollection/Resources/Fonts/DejaVuSans-Oblique.ttf +0 -0
  16. ScriptCollection/Resources/Fonts/DejaVuSans.ttf +0 -0
  17. ScriptCollection/Resources/Fonts/LICENSE.txt +0 -0
  18. ScriptCollection/Resources/MaintenanceSite/MaintenanceSite.html +0 -0
  19. ScriptCollection/SCLog.py +115 -115
  20. ScriptCollection/ScriptCollectionCore.py +4372 -4351
  21. ScriptCollection/TFCPS/DotNet/TFCPS_CodeUnitSpecific_DotNet.py +716 -709
  22. ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +235 -235
  23. ScriptCollection/TFCPS/Python/TFCPS_CodeUnitSpecific_Python.py +258 -258
  24. ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +524 -524
  25. ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnit.py +157 -155
  26. ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +691 -681
  27. ScriptCollection/TFCPS/TFCPS_CreateRelease.py +98 -98
  28. ScriptCollection/TFCPS/TFCPS_Generic.py +44 -44
  29. ScriptCollection/TFCPS/TFCPS_MergeToMain.py +132 -132
  30. ScriptCollection/TFCPS/TFCPS_MergeToStable.py +358 -358
  31. ScriptCollection/TFCPS/TFCPS_PreBuildCodeunitsScript.py +48 -48
  32. ScriptCollection/TFCPS/TFCPS_Tools_General.py +30 -4
  33. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/METADATA +1 -1
  34. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/RECORD +26 -26
  35. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/WHEEL +0 -0
  36. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/entry_points.txt +0 -0
  37. {scriptcollection-4.3.64.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
- run_result = self._protected_sc.run_program("dotnet", f"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"})
319
- finally:
320
- GeneralUtilities.ensure_directory_does_not_exist(temp_output_folder)
321
- diagnostics: list[tuple[LogLevel, str, str | None, int | None]] = []
322
- pattern = re.compile(r"^\s*(?:(.+?)\((\d+),\d+\): )?(error|warning|message|info) [^:]+: (.+?)(?:\s*\[.+\])?\s*$", re.IGNORECASE)
323
- for line in GeneralUtilities.string_to_lines(run_result[1] + "\n" + run_result[2]):
324
- m = pattern.match(line)
325
- if m:
326
- file_path = m.group(1)
327
- line_number = int(m.group(2)) if m.group(2) else None
328
- level_str = m.group(3).lower()
329
- message = m.group(4)
330
- if level_str == "error":
331
- level = LogLevel.Error
332
- elif level_str == "warning":
333
- level = LogLevel.Warning
334
- else:
335
- level = LogLevel.Information
336
- diagnostics.append((level, message, file_path, line_number))
337
- return diagnostics
338
-
339
- @GeneralUtilities.check_arguments
340
- def linting(self) -> None:
341
- codeunit_name = self.get_codeunit_name()
342
- codeunit_folder = self.get_codeunit_folder()
343
- self._protected_sc.normalize_line_endings_of_files_in_folder(codeunit_folder, ["cs"])
344
- nuspec_file = os.path.join(codeunit_folder, "Other", "Build", f"{codeunit_name}.nuspec")
345
- if os.path.isfile(nuspec_file):
346
- self._protected_sc.format_xml_file(nuspec_file)
347
- self._protected_sc.format_xml_file(os.path.join(codeunit_folder, codeunit_name, codeunit_name + ".csproj"), add_xml_declaration=False)
348
- self._protected_sc.format_xml_file(os.path.join(codeunit_folder, codeunit_name + "Tests", codeunit_name + "Tests.csproj"), add_xml_declaration=False)
349
- self.standardized_task_verify_standard_format_csproj_files()
350
- diagnostics = self.get_dotnet_build_diagnostics()
351
- has_errors = False
352
- for (level, message, file, line) in diagnostics:
353
- location = f" ({file}:{line})" if file else ""
354
- self._protected_sc.log.log(f"{message}{location}", level)
355
- 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.
356
- has_errors = True
357
- if has_errors:
358
- raise ValueError("Linting-issues occurred.")
359
-
360
- @GeneralUtilities.check_arguments
361
- def do_common_tasks(self,current_codeunit_version:str,certificateGeneratorInformation:CertificateGeneratorInformationBase)-> None:
362
- self.do_common_tasks_base(current_codeunit_version)
363
- self.update_year_for_dotnet_codeunit()
364
- codeunit_name =self.get_codeunit_name()
365
- 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
366
- folder_of_current_file =os.path.join(self.get_codeunit_folder(),"Other")
367
- self._protected_sc.replace_version_in_csproj_file(GeneralUtilities.resolve_relative_path(f"../{codeunit_name}/{codeunit_name}.csproj", folder_of_current_file), codeunit_version)
368
- 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)
369
- if self.is_library:
370
- self._protected_sc.replace_version_in_nuspec_file(GeneralUtilities.resolve_relative_path(f"./Build/{codeunit_name}.nuspec", folder_of_current_file), codeunit_version)
371
- if certificateGeneratorInformation.generate_certificate():
372
- self.tfcps_Tools_General.set_constants_for_certificate_private_information(self.get_codeunit_folder())
373
-
374
- @GeneralUtilities.check_arguments
375
- def standardized_task_verify_standard_format_csproj_files(self) -> bool:
376
- codeunit_folder=self.get_codeunit_folder()
377
- repository_folder = os.path.dirname(codeunit_folder)
378
- codeunit_name = os.path.basename(codeunit_folder)
379
- codeunit_folder = os.path.join(repository_folder, codeunit_name)
380
- codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(self.get_codeunit_file())
381
-
382
- csproj_project_name = codeunit_name
383
- csproj_file = os.path.join(codeunit_folder, csproj_project_name, csproj_project_name+".csproj")
384
- result1: tuple[bool, str, list[str]] = self.__standardized_task_verify_standard_format_for_project_csproj_file(csproj_file, codeunit_folder, codeunit_name, codeunit_version)
385
- if not result1[0]:
386
- hints: str = "\n".join(result1[2])
387
- 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}")
388
- self.__check_csproj_urls(csproj_file)
389
-
390
- test_csproj_project_name = csproj_project_name+"Tests"
391
- test_csproj_file = os.path.join(codeunit_folder, test_csproj_project_name, test_csproj_project_name+".csproj")
392
- result2: tuple[bool, str, list[str]] = self.__standardized_task_verify_standard_format_for_test_csproj_file(test_csproj_file, codeunit_name, codeunit_version)
393
- if not result2[0]:
394
- hints: str = "\n".join(result2[2])
395
- 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}")
396
- self.__check_csproj_urls(test_csproj_file)
397
-
398
- @GeneralUtilities.check_arguments
399
- def __check_csproj_urls(self, csproj_file: str) -> None:
400
- remote_address: str = self.get_remote_address()
401
- root: etree._ElementTree = etree.parse(csproj_file)
402
- package_project_url: str = self.__get_unique_csproj_property_value(root, csproj_file, "PackageProjectUrl")
403
- repository_url: str = self.__get_unique_csproj_property_value(root, csproj_file, "RepositoryUrl")
404
- if package_project_url != remote_address:
405
- 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.")
406
- expected_repository_url: str = package_project_url+".git"
407
- if repository_url != expected_repository_url:
408
- raise ValueError(f"The RepositoryUrl-value '{repository_url}' in '{csproj_file}' is not equal to the expected value '{expected_repository_url}'.")
409
-
410
- @GeneralUtilities.check_arguments
411
- def __get_unique_csproj_property_value(self, root: etree._ElementTree, csproj_file: str, property_name: str) -> str:
412
- values: list[str] = [str(value).strip() for value in root.xpath(f"/Project/PropertyGroup/{property_name}/text()")]
413
- if len(values) != 1:
414
- raise ValueError(f"'{csproj_file}' must contain exactly one {property_name}-element but contains {len(values)} of them.")
415
- return values[0]
416
-
417
- 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]:
418
- codeunit_name_regex = re.escape(codeunit_name)
419
- codeunit_description = self.tfcps_Tools_General.get_codeunit_description(self.get_codeunit_file())
420
- codeunit_version_regex = re.escape(codeunit_version)
421
- codeunit_description_regex = re.escape(codeunit_description)
422
- regex = f"""^<Project Sdk=\\"Microsoft\\.NET\\.Sdk\\">
423
- <PropertyGroup>
424
- <TargetFramework>([^<]+)<\\/TargetFramework>
425
- <Authors>([^<]+)<\\/Authors>
426
- <Version>{codeunit_version_regex}<\\/Version>
427
- <AssemblyVersion>{codeunit_version_regex}<\\/AssemblyVersion>
428
- <FileVersion>{codeunit_version_regex}<\\/FileVersion>
429
- <SelfContained>false<\\/SelfContained>
430
- <IsPackable>false<\\/IsPackable>
431
- <PreserveCompilationContext>false<\\/PreserveCompilationContext>
432
- <GenerateRuntimeConfigurationFiles>true<\\/GenerateRuntimeConfigurationFiles>
433
- <Copyright>([^<]+)<\\/Copyright>
434
- <Description>{codeunit_description_regex}<\\/Description>
435
- <PackageProjectUrl>https:\\/\\/([^<]+)<\\/PackageProjectUrl>
436
- <RepositoryUrl>https:\\/\\/([^<]+)\\.git<\\/RepositoryUrl>
437
- <RootNamespace>([^<]+)\\.Core<\\/RootNamespace>
438
- <ProduceReferenceAssembly>false<\\/ProduceReferenceAssembly>
439
- <Nullable>(disable|enable|warnings|annotations)<\\/Nullable>
440
- <Configurations>Development;QualityCheck;Productive<\\/Configurations>
441
- <IsTestProject>false<\\/IsTestProject>
442
- <LangVersion>([^<]+)<\\/LangVersion>
443
- <PackageRequireLicenseAcceptance>true<\\/PackageRequireLicenseAcceptance>
444
- <GenerateSerializationAssemblies>Off<\\/GenerateSerializationAssemblies>
445
- <AppendTargetFrameworkToOutputPath>false<\\/AppendTargetFrameworkToOutputPath>
446
- <OutputPath>\\.\\.\\\\Other\\\\Artifacts\\\\BuildResult_DotNet_win\\-x64<\\/OutputPath>
447
- <PlatformTarget>([^<]+)<\\/PlatformTarget>
448
- <WarningLevel>\\d<\\/WarningLevel>
449
- <Prefer32Bit>false<\\/Prefer32Bit>
450
- <SignAssembly>true<\\/SignAssembly>
451
- <AssemblyOriginatorKeyFile>\\.\\.\\\\\\.\\.\\\\Other\\\\Resources\\\\PublicKeys\\\\StronglyNamedKey\\\\([^<]+)PublicKey\\.snk<\\/AssemblyOriginatorKeyFile>
452
- <DelaySign>true<\\/DelaySign>
453
- <NoWarn>([^<]+)<\\/NoWarn>
454
- <WarningsAsErrors>([^<]+)<\\/WarningsAsErrors>
455
- <ErrorLog>\\.\\.\\\\Other\\\\Resources\\\\CodeAnalysisResult\\\\{codeunit_name_regex}\\.sarif<\\/ErrorLog>
456
- <OutputType>([^<]+)<\\/OutputType>
457
- <DocumentationFile>\\.\\.\\\\Other\\\\Artifacts\\\\MetaInformation\\\\{codeunit_name_regex}\\.xml<\\/DocumentationFile>(\\n|.)*
458
- <\\/PropertyGroup>
459
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Development'\\\">
460
- <DebugType>full<\\/DebugType>
461
- <DebugSymbols>true<\\/DebugSymbols>
462
- <Optimize>false<\\/Optimize>
463
- <DefineConstants>TRACE;DEBUG;Development<\\/DefineConstants>
464
- <ErrorReport>prompt<\\/ErrorReport>
465
- <\\/PropertyGroup>
466
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='QualityCheck'\\\">
467
- <DebugType>portable<\\/DebugType>
468
- <DebugSymbols>true<\\/DebugSymbols>
469
- <Optimize>false<\\/Optimize>
470
- <DefineConstants>TRACE;QualityCheck<\\/DefineConstants>
471
- <ErrorReport>none<\\/ErrorReport>
472
- <\\/PropertyGroup>
473
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Productive'\\\">
474
- <DebugType>portable<\\/DebugType>
475
- <DebugSymbols>true<\\/DebugSymbols>
476
- <Optimize>false<\\/Optimize>
477
- <DefineConstants>Productive<\\/DefineConstants>
478
- <ErrorReport>none<\\/ErrorReport>
479
- <\\/PropertyGroup>(\\n|.)*
480
- <\\/Project>\\n?$"""
481
- result = self.__standardized_task_verify_standard_format_for_csproj_files(regex, csproj_file)
482
- return (result[0], regex, result[1])
483
-
484
- def __standardized_task_verify_standard_format_for_test_csproj_file(self, csproj_file: str, codeunit_name: str, codeunit_version: str) -> tuple[bool, str, str]:
485
- codeunit_name_regex = re.escape(codeunit_name)
486
- codeunit_version_regex = re.escape(codeunit_version)
487
- regex = f"""^<Project Sdk=\\"Microsoft\\.NET\\.Sdk\\">
488
- <PropertyGroup>
489
- <TargetFramework>([^<]+)<\\/TargetFramework>
490
- <Authors>([^<]+)<\\/Authors>
491
- <Version>{codeunit_version_regex}<\\/Version>
492
- <AssemblyVersion>{codeunit_version_regex}<\\/AssemblyVersion>
493
- <FileVersion>{codeunit_version_regex}<\\/FileVersion>
494
- <SelfContained>false<\\/SelfContained>
495
- <IsPackable>false<\\/IsPackable>
496
- <PreserveCompilationContext>false<\\/PreserveCompilationContext>
497
- <GenerateRuntimeConfigurationFiles>true<\\/GenerateRuntimeConfigurationFiles>
498
- <Copyright>([^<]+)<\\/Copyright>
499
- <Description>{codeunit_name_regex}Tests is the test-project for {codeunit_name_regex}\\.<\\/Description>
500
- <PackageProjectUrl>https:\\/\\/([^<]+)<\\/PackageProjectUrl>
501
- <RepositoryUrl>https:\\/\\/([^<]+)\\.git</RepositoryUrl>
502
- <RootNamespace>([^<]+)\\.Tests<\\/RootNamespace>
503
- <ProduceReferenceAssembly>false<\\/ProduceReferenceAssembly>
504
- <Nullable>(disable|enable|warnings|annotations)<\\/Nullable>
505
- <Configurations>Development;QualityCheck;Productive<\\/Configurations>
506
- <IsTestProject>true<\\/IsTestProject>
507
- <LangVersion>([^<]+)<\\/LangVersion>
508
- <PackageRequireLicenseAcceptance>true<\\/PackageRequireLicenseAcceptance>
509
- <GenerateSerializationAssemblies>Off<\\/GenerateSerializationAssemblies>
510
- <AppendTargetFrameworkToOutputPath>false<\\/AppendTargetFrameworkToOutputPath>
511
- <OutputPath>\\.\\.\\\\Other\\\\Artifacts\\\\BuildResultTests_DotNet_win\\-x64<\\/OutputPath>
512
- <PlatformTarget>([^<]+)<\\/PlatformTarget>
513
- <WarningLevel>\\d<\\/WarningLevel>
514
- <Prefer32Bit>false<\\/Prefer32Bit>
515
- <SignAssembly>true<\\/SignAssembly>
516
- <AssemblyOriginatorKeyFile>\\.\\.\\\\\\.\\.\\\\Other\\\\Resources\\\\PublicKeys\\\\StronglyNamedKey\\\\([^<]+)PublicKey\\.snk<\\/AssemblyOriginatorKeyFile>
517
- <DelaySign>true<\\/DelaySign>
518
- <NoWarn>([^<]+)<\\/NoWarn>
519
- <WarningsAsErrors>([^<]+)<\\/WarningsAsErrors>
520
- <ErrorLog>\\.\\.\\\\Other\\\\Resources\\\\CodeAnalysisResult\\\\{codeunit_name_regex}Tests\\.sarif<\\/ErrorLog>
521
- <OutputType>Library<\\/OutputType>(\\n|.)*
522
- <\\/PropertyGroup>
523
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Development'\\\">
524
- <DebugType>full<\\/DebugType>
525
- <DebugSymbols>true<\\/DebugSymbols>
526
- <Optimize>false<\\/Optimize>
527
- <DefineConstants>TRACE;DEBUG;Development<\\/DefineConstants>
528
- <ErrorReport>prompt<\\/ErrorReport>
529
- <\\/PropertyGroup>
530
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='QualityCheck'\\\">
531
- <DebugType>portable<\\/DebugType>
532
- <DebugSymbols>true<\\/DebugSymbols>
533
- <Optimize>false<\\/Optimize>
534
- <DefineConstants>TRACE;QualityCheck<\\/DefineConstants>
535
- <ErrorReport>none<\\/ErrorReport>
536
- <\\/PropertyGroup>
537
- <PropertyGroup Condition=\\\"'\\$\\(Configuration\\)'=='Productive'\\\">
538
- <DebugType>portable<\\/DebugType>
539
- <DebugSymbols>true<\\/DebugSymbols>
540
- <Optimize>false<\\/Optimize>
541
- <DefineConstants>Productive<\\/DefineConstants>
542
- <ErrorReport>none<\\/ErrorReport>
543
- <\\/PropertyGroup>(\\n|.)*
544
- <\\/Project>\\n?$"""
545
- result = self.__standardized_task_verify_standard_format_for_csproj_files(regex, csproj_file)
546
- return (result[0], regex, result[1])
547
-
548
- def __standardized_task_verify_standard_format_for_csproj_files(self, regex: str, csproj_file: str) -> tuple[bool, list[str]]:
549
- filename = os.path.basename(csproj_file)
550
- self._protected_sc.log.log(f"Check {filename}...",LogLevel.Debug)
551
- file_content = GeneralUtilities.read_text_from_file(csproj_file)
552
- regex_for_check = regex.replace("\r", GeneralUtilities.empty_string).replace("\n", "\\n")
553
- file_content = file_content.replace("\r", GeneralUtilities.empty_string)
554
- match = re.match(regex_for_check, file_content)
555
- result = match is not None
556
- hints = None
557
- if not result:
558
- hints = self.get_hints_for_csproj(regex, file_content)
559
- return (result, hints)
560
-
561
- @GeneralUtilities.check_arguments
562
- def get_hints_for_csproj(self, regex: str, file_content: str) -> list[str]:
563
- result: list[str] = []
564
- regex_lines = GeneralUtilities.string_to_lines(regex)
565
- file_content_lines = GeneralUtilities.string_to_lines(file_content)
566
- amount_of_lines = len(file_content_lines)
567
- if amount_of_lines< len(regex_lines):
568
- result.append("csproj-file has less lines than the regex requires.")
569
- return result
570
- for i in range(35):#you can do this check only for the first 35 lines
571
- s = file_content_lines[i]
572
- r = regex_lines[i]
573
- if not re.match(r, s):
574
- result.append(f"Line {i+1} does not match: Regex='{r}' String='{s}'")
575
- return result
576
-
577
- @GeneralUtilities.check_arguments
578
- def generate_reference(self, generate_class_reference:bool=False) -> None:
579
- self.generate_reference_using_docfx(generate_class_reference)
580
-
581
-
582
- @GeneralUtilities.check_arguments
583
- def update_year_for_dotnet_codeunit(self) -> None:
584
- codeunit_folder:str=self.get_codeunit_folder()
585
- codeunit_name = os.path.basename(codeunit_folder)
586
- csproj_file = os.path.join(codeunit_folder, codeunit_name, f"{codeunit_name}.csproj")
587
- self._protected_sc.update_year_in_copyright_tags(csproj_file)
588
- csprojtests_file = os.path.join(codeunit_folder, f"{codeunit_name}Tests", f"{codeunit_name}Tests.csproj")
589
- self._protected_sc.update_year_in_copyright_tags(csprojtests_file)
590
- nuspec_file = os.path.join(codeunit_folder, "Other", "Build", f"{codeunit_name}.nuspec")
591
- if os.path.isfile(nuspec_file):
592
- self._protected_sc.update_year_in_copyright_tags(nuspec_file)
593
-
594
- @GeneralUtilities.check_arguments
595
- def run_testcases(self, timeoutInSeconds:int=60*10) -> None:
596
- self._protected_sc.log.log("Run testcases...")
597
- dotnet_build_configuration: str = self.get_target_environment_type()
598
- codeunit_name: str = self.get_codeunit_name()
599
-
600
- repository_folder: str = self.get_repository_folder().replace("\\", "/")
601
- coverage_file_folder = os.path.join(repository_folder, codeunit_name, "Other/Artifacts/TestCoverage")
602
- temp_folder = os.path.join(GeneralUtilities.get_temp_folder(), str(uuid.uuid4()))
603
- GeneralUtilities.ensure_directory_exists(temp_folder)
604
- runsettings_file = "runsettings.xml"
605
- codeunit_folder = f"{repository_folder}/{codeunit_name}"
606
- GeneralUtilities.ensure_directory_exists(coverage_file_folder)
607
- target_file = os.path.join(coverage_file_folder, "TestCoverage.xml")
608
- GeneralUtilities.ensure_file_does_not_exist(target_file)
609
-
610
- sln_file = os.path.join(codeunit_folder, f"{codeunit_name}.sln")
611
- args: list[str] = ["test", sln_file, "-c", dotnet_build_configuration, "-o", temp_folder]
612
- runsettings_path = os.path.join(codeunit_folder, runsettings_file)
613
- if os.path.isfile(runsettings_path):
614
- args += ["--settings", runsettings_path]
615
- # Write the test-results (test-binaries-deployment and coverage) into the system-temp-folder (a subfolder of
616
- # temp_folder) instead of a relative "./TestResults" inside the codeunit-folder. The relative path would otherwise
617
- # leave a folder behind in the codeunit-folder (visible e.g. when building inside the mounted Debian-build-container).
618
- # The whole temp_folder - including these results - is removed in the finally-block below.
619
- args += ["--results-directory", os.path.join(temp_folder, "TestResults")]
620
- # Run dotnet-test from an absolute working-directory (a subfolder of temp_folder) with node-reuse disabled, for the
621
- # same reason as in get_dotnet_build_diagnostics: building the solution with "-o" makes MSBuild create an additional
622
- # relative "tmp/<guid>"-output-folder, which a reused worker-node would otherwise create inside the codeunit-folder
623
- # where it can be left behind on a bind-mounted filesystem. All paths passed to dotnet-test are absolute, so the
624
- # changed working-directory does not affect the test-result or the coverage-output.
625
- test_working_directory = os.path.join(temp_folder, "WorkingDirectory")
626
- GeneralUtilities.ensure_directory_exists(test_working_directory)
627
- try:
628
- 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"})
629
- test_output:str=program_output[1]
630
- output_lines=program_output[1].split("\n")
631
- output_lines=[line for line in output_lines if GeneralUtilities.string_has_content(line)]
632
- generated_coverage_file: str = output_lines[-1].strip()#the cobertura file is printed in the end of the output by the xplat collector
633
- GeneralUtilities.assert_file_exists(generated_coverage_file)
634
- shutil.copyfile(generated_coverage_file, target_file)
635
- finally:
636
- GeneralUtilities.ensure_directory_does_not_exist(temp_folder)
637
-
638
- self.__remove_unrelated_package_from_testcoverage_file(target_file, codeunit_name)
639
- root: etree._ElementTree = etree.parse(target_file)
640
- source_base_path_in_coverage_file: str = root.xpath("//coverage/sources/source/text()")[0].replace("\\", "/")
641
- content = GeneralUtilities.read_text_from_file(target_file)
642
- 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}\"")
643
- content = re.sub('\\\\', '/', content)
644
- 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)
645
- GeneralUtilities.write_text_to_file(target_file, content)
646
- self.run_testcases_common_post_task(repository_folder, codeunit_name, True, self.get_target_environment_type())
647
- artifacts_folder = os.path.join(repository_folder, codeunit_name, "Other", "Artifacts")
648
- for subfolder in GeneralUtilities.get_direct_folders_of_folder(artifacts_folder):
649
- if os.path.basename(subfolder).startswith("BuildResultTests_DotNet_"):
650
- GeneralUtilities.ensure_directory_does_not_exist(subfolder)
651
-
652
- amount_of_ignored_testcases:int=self.__get_amount_of_ignored_testcases(test_output)
653
- project_has_ignored_testcases:bool=0<amount_of_ignored_testcases
654
- if project_has_ignored_testcases:
655
- self._protected_sc.log.log(f"Project '{codeunit_name}' has {amount_of_ignored_testcases} ignored testcases.", LogLevel.Warning)
656
-
657
- @GeneralUtilities.check_arguments
658
- def __get_amount_of_ignored_testcases(self, test_output: str) -> int:
659
- # Ignored (=skipped) testcases are counted in the summary-line which dotnet-test prints for each test-project.
660
- # The summary of the VSTest-runner looks like "Failed: 0, Passed: 3, Skipped: 1, Total: 4" and the summary of the
661
- # Microsoft-Testing-Platform-runner looks like "total: 4 / failed: 0 / succeeded: 3 / skipped: 1".
662
- result: int = 0
663
- for match in re.finditer(r"skipped:\s*(\d+)", test_output, re.IGNORECASE):
664
- result = result+int(match.group(1))
665
- return result
666
-
667
- @GeneralUtilities.check_arguments
668
- def __remove_unrelated_package_from_testcoverage_file(self, file: str, codeunit_name: str) -> None:
669
- root: etree._ElementTree = etree.parse(file)
670
- packages = root.xpath('//coverage/packages/package')
671
- for package in packages:
672
- if package.attrib['name'] != codeunit_name:
673
- package.getparent().remove(package)
674
- result = etree.tostring(root).decode("utf-8")
675
- GeneralUtilities.write_text_to_file(file, result)
676
-
677
-
678
- @GeneralUtilities.check_arguments
679
- def __standardized_tasks_run_testcases_for_dotnet_project_helper(self, source: str, codeunit_folder: str, match: re.Match) -> str:
680
- filename = match.group(1)
681
- file = os.path.join(source, filename)
682
- GeneralUtilities.assert_condition(file.startswith(codeunit_folder), f"Unexpected path for coverage-file. File: \"{file}\"; codeunitfolder: \"{codeunit_folder}\"")
683
- filename_relative = f".{file[len(codeunit_folder):]}"
684
- return f'filename="{filename_relative}"'
685
-
686
-
687
- def get_dependencies(self)->dict[str,set[str]]:
688
- return dict[str,set[str]]()#TODO
689
-
690
- @GeneralUtilities.check_arguments
691
- def get_available_versions(self,dependencyname:str)->list[str]:
692
- return []#TODO
693
-
694
- def set_dependency_version(self,name:str,new_version:str)->None:
695
- raise ValueError(f"Operation is not implemented.")
696
- #csproj_file:str=os.path.join(self.get_codeunit_folder(), self.get_codeunit_name(), self.get_codeunit_name() + ".csproj")
697
- #self._protected_sc.update_dependencies_of_dotnet_project(csproj_file,[])#TODO set ignored codeunits
698
-
699
-
700
- class TFCPS_CodeUnitSpecific_DotNet_CLI:
701
-
702
- @staticmethod
703
- @GeneralUtilities.check_arguments
704
- def parse(file:str)->TFCPS_CodeUnitSpecific_DotNet_Functions:
705
- parser=TFCPS_CodeUnitSpecific_Base_CLI.get_base_parser()
706
- #add custom parameter if desired
707
- args=parser.parse_args()
708
- result:TFCPS_CodeUnitSpecific_DotNet_Functions=TFCPS_CodeUnitSpecific_DotNet_Functions(file,LogLevel(int(args.verbosity)),args.targetenvironmenttype,not args.nocache,args.ispremerge)
709
- return result
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