|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import shutil |
| 17 | +from pathlib import Path |
| 18 | +from typing import Optional |
| 19 | + |
| 20 | +from bcasl import bc_register |
| 21 | +from Plugins_SDK.BcPluginContext import BcPluginBase, PluginMeta, PreCompileContext |
| 22 | +from Plugins_SDK.GeneralContext import ( |
| 23 | + Dialog, |
| 24 | + get_language_code, |
| 25 | + load_plugin_language_file, |
| 26 | + register_i18n_handler, |
| 27 | + register_plugin_translations, |
| 28 | + translate, |
| 29 | +) |
| 30 | + |
| 31 | +# Create instances of Dialog for logging and user interaction |
| 32 | +log = Dialog() |
| 33 | +dialog = Dialog() |
| 34 | + |
| 35 | + |
| 36 | +def _load_i18n() -> None: |
| 37 | + try: |
| 38 | + lang_code = get_language_code() |
| 39 | + data = load_plugin_language_file(__package__, lang_code) |
| 40 | + if isinstance(data, dict) and data: |
| 41 | + register_plugin_translations("outputcleaner", data) |
| 42 | + except Exception: |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +# Load translations now and refresh on language changes |
| 47 | +_load_i18n() |
| 48 | +try: |
| 49 | + register_i18n_handler(lambda _gui, _tr: _load_i18n()) |
| 50 | +except Exception: |
| 51 | + pass |
| 52 | + |
| 53 | +# Plugin metadata |
| 54 | +PLUGIN_META = PluginMeta( |
| 55 | + id="outputcleaner", |
| 56 | + name="Output Cleaner", |
| 57 | + version="1.0.0", |
| 58 | + description="Clean the output directory before compilation", |
| 59 | + author="Samuel Amen Ague", |
| 60 | + tags=["clean", "output"], |
| 61 | + required_bcasl_version="1.0.0", |
| 62 | +) |
| 63 | + |
| 64 | + |
| 65 | +@bc_register |
| 66 | +class OutputCleaner(BcPluginBase): |
| 67 | + """Plugin de nettoyage du dossier output avant compilation. |
| 68 | +
|
| 69 | + Utilise le BuildContext pour identifier le dossier de sortie. |
| 70 | + """ |
| 71 | + |
| 72 | + meta = PLUGIN_META |
| 73 | + |
| 74 | + def __init__(self): |
| 75 | + super().__init__(meta=PLUGIN_META) |
| 76 | + |
| 77 | + def _get_config(self, ctx: PreCompileContext) -> dict: |
| 78 | + try: |
| 79 | + plugins_cfg = ctx.config.get("plugins", {}) |
| 80 | + entry = plugins_cfg.get(self.meta.id, {}) |
| 81 | + return entry.get("config", {}) if isinstance(entry, dict) else {} |
| 82 | + except Exception: |
| 83 | + return {} |
| 84 | + |
| 85 | + def on_pre_compile(self, ctx: PreCompileContext) -> None: |
| 86 | + """Nettoie le dossier output avant la compilation.""" |
| 87 | + try: |
| 88 | + if not ctx.build_context: |
| 89 | + log.log_warn("OutputCleaner: No BuildContext available. Cannot identify output directory.") |
| 90 | + return |
| 91 | + |
| 92 | + output_dir_str = getattr(ctx.build_context, "output_dir", None) |
| 93 | + if not output_dir_str: |
| 94 | + log.log_warn("OutputCleaner: No output_dir defined in BuildContext.") |
| 95 | + return |
| 96 | + |
| 97 | + output_dir = Path(output_dir_str) |
| 98 | + if not output_dir.is_absolute(): |
| 99 | + output_dir = ctx.root / output_dir |
| 100 | + |
| 101 | + if not output_dir.exists(): |
| 102 | + log.log_info(f"OutputCleaner: Output directory does not exist: {output_dir}") |
| 103 | + return |
| 104 | + |
| 105 | + log.log_info(f"OutputCleaner: Cleaning output directory: {output_dir}") |
| 106 | + |
| 107 | + # Simple confirmation if configured |
| 108 | + cfg = self._get_config(ctx) |
| 109 | + if bool(cfg.get("confirm", False)): |
| 110 | + response = dialog.msg_question( |
| 111 | + title="Output Cleaner", |
| 112 | + text=f"Do you want to delete all contents in {output_dir}?", |
| 113 | + default_yes=True, |
| 114 | + ) |
| 115 | + if not response: |
| 116 | + return |
| 117 | + |
| 118 | + # Actually delete the directory and recreate it |
| 119 | + try: |
| 120 | + shutil.rmtree(output_dir) |
| 121 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 122 | + log.log_info(f"OutputCleaner: Successfully cleaned {output_dir}") |
| 123 | + except Exception as e: |
| 124 | + log.log_err(f"OutputCleaner: Failed to clean {output_dir}: {e}") |
| 125 | + |
| 126 | + except Exception as e: |
| 127 | + log.log_err(f"OutputCleaner error: {e}") |
0 commit comments