diff --git a/cli/classes/generator.py b/cli/classes/generator.py index 14a0c0e5..021ff87b 100644 --- a/cli/classes/generator.py +++ b/cli/classes/generator.py @@ -87,9 +87,10 @@ def generate(self) -> Optional[str]: if self.output_settings.verbose: logger.info( "📄", - f"Generated {self.target} pipeline:\n{result}", + f"Generated {self.target} pipeline:", self.output_settings.emoji, ) + print(result) if self.check_syntax: if actual_generator.check(result): logger.info( diff --git a/cli/generators/cli.py b/cli/generators/cli.py index 1d1dfc1a..24b6911e 100644 --- a/cli/generators/cli.py +++ b/cli/generators/cli.py @@ -60,10 +60,8 @@ def add_postfix(self) -> None: Add the postfix to the bash script. E.g. some output settings, the callable functions etc. """ - self.result.append("\n") - self.result.append("# main function") - self.result.append("main () {") - # to enable sourceing the script, we need to skip execution if we do so + self.result.append("\nmain () {") + # to enable sourcing the script, we need to skip execution if we do so # for that, we check if the first parameter is sourcing, which is not ever given to the script elsewhere self.add_line(indentation=2, line='local _current_lifecycle="${1}"') self.add_line(indentation=4, line='if [[ "${_current_lifecycle}" == "aeolus_sourcing" ]]; then') @@ -89,7 +87,6 @@ def handle_always_steps(self, steps: list[str]) -> None: :param steps: to call always :return: CI action """ - self.result.append("\n# always steps") self.result.append("final_aeolus_post_action () " + "{") self.add_line(indentation=2, line="set +e # from now on, we don't exit on errors") self.add_line(indentation=2, line="echo '⚙️ executing final_aeolus_post_action'") @@ -122,18 +119,23 @@ def add_lifecycle_guards(self, name: str, exclusions: Optional[List[Lifecycle]], indentations -= 2 self.add_line(indentation=indentations, line="fi") - def handle_result_list(self, indentation: int, results: List[Result]) -> None: + def handle_result_list(self, indentation: int, results: List[Result], workdir: Optional[str]) -> None: """ Process the results of a step. https://askubuntu.com/a/889746 https://stackoverflow.com/a/8088439 :param indentation: indentation level :param results: list of results + :param workdir: workdir of the step """ + self.add_line(indentation=indentation, line=f'cd "${{{self.initial_directory_variable}}}"') self.add_line(indentation=indentation, line=f"mkdir -p {self.results_directory_variable}") self.add_line(indentation=indentation, line="shopt -s extglob") for result in results: - self.add_line(indentation=indentation, line=f'local _sources="{result.path}"') + source_path: str = result.path + if workdir: + source_path = f"{workdir}/{result.path}" + self.add_line(indentation=indentation, line=f'local _sources="{source_path}"') self.add_line(indentation=indentation, line="local _directory") self.add_line(indentation=indentation, line='_directory=$(dirname "${_sources}")') if result.ignore: @@ -152,7 +154,7 @@ def handle_before_results(self, step: ScriptAction) -> None: return before: List[Result] = [result for result in step.results if result.before] if before: - self.handle_result_list(indentation=2, results=before) + self.handle_result_list(indentation=2, results=before, workdir=step.workdir) def handle_after_results(self, step: ScriptAction) -> None: """ @@ -163,7 +165,7 @@ def handle_after_results(self, step: ScriptAction) -> None: return after: List[Result] = [result for result in step.results if not result.before] if after: - self.handle_result_list(indentation=2, results=after) + self.handle_result_list(indentation=2, results=after, workdir=step.workdir) def handle_step(self, name: str, step: ScriptAction, call: bool) -> None: """ @@ -173,18 +175,14 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None: :param call: whether to call the step or not :return: CI action """ - original_name: Optional[str] = self.metadata.get_original_name_of(name) original_type: Optional[str] = self.metadata.get_meta_for_action(name).get("original_type") - if original_type == "platform": + if original_type == "platform" or (step.platform and step.platform != Target.cli): logger.info( "🔨", "Platform action detected. Skipping...", self.output_settings.emoji, ) return None - self.result.append(f"# step {name}") - self.result.append(f"# generated from step {original_name}") - self.result.append(f"# original type was {original_type}") valid_funtion_name: str = re.sub("[^a-zA-Z]+", "", name) if call: self.functions.append(valid_funtion_name) @@ -212,7 +210,7 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None: self.add_line(indentation=2, line=line) self.handle_after_results(step=step) - self.result.append("}") + self.result.append("}\n") return None def check(self, content: str) -> bool: diff --git a/cli/test/test_generate.py b/cli/test/test_generate.py index edd6b175..01478596 100644 --- a/cli/test/test_generate.py +++ b/cli/test/test_generate.py @@ -50,8 +50,8 @@ def test_generate_valid_cli_script(self) -> None: result: str = cli.generate() self.assertTrue(result.count("#!/usr/bin/env bash") == 1) self.assertTrue("set -e" in result) - # two comments, one echo for execution, one echo in the actual action - self.assertTrue(result.count("internal-action") == 3) + # one echo for execution, one echo in the actual action + self.assertTrue(result.count("internal-action") == 1) # one call to the function and the function itself self.assertTrue(result.count("internalaction") == 2) self.assertTrue(result.count("{") == result.count("}"))