|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from dataclasses import dataclass |
3 | 4 | from pathlib import Path |
4 | 5 | from typing import Any |
5 | 6 |
|
6 | 7 | import tomlkit |
7 | 8 |
|
8 | 9 | from codeflash.code_utils.config_js import find_package_json, parse_package_json_config |
| 10 | +from codeflash.languages.language_enum import Language |
9 | 11 | from codeflash.lsp.helpers import is_LSP_enabled |
10 | 12 |
|
11 | 13 | PYPROJECT_TOML_CACHE: dict[Path, Path] = {} |
12 | 14 | ALL_CONFIG_FILES: dict[Path, dict[str, Path]] = {} |
13 | 15 |
|
14 | 16 |
|
| 17 | +@dataclass |
| 18 | +class LanguageConfig: |
| 19 | + config: dict[str, Any] |
| 20 | + config_path: Path |
| 21 | + language: Language |
| 22 | + |
| 23 | + |
15 | 24 | def _try_parse_java_build_config() -> tuple[dict[str, Any], Path] | None: |
16 | 25 | """Detect Java project from build files and parse config from pom.xml/gradle.properties. |
17 | 26 |
|
@@ -103,6 +112,149 @@ def find_conftest_files(test_paths: list[Path]) -> list[Path]: |
103 | 112 | return list(list_of_conftest_files) |
104 | 113 |
|
105 | 114 |
|
| 115 | +def normalize_toml_config(config: dict[str, Any], config_file_path: Path) -> dict[str, Any]: |
| 116 | + path_keys = ["module-root", "tests-root", "benchmarks-root"] |
| 117 | + path_list_keys = ["ignore-paths"] |
| 118 | + str_keys = {"pytest-cmd": "pytest", "git-remote": "origin"} |
| 119 | + bool_keys = { |
| 120 | + "override-fixtures": False, |
| 121 | + "disable-telemetry": False, |
| 122 | + "disable-imports-sorting": False, |
| 123 | + "benchmark": False, |
| 124 | + } |
| 125 | + list_str_keys = {"formatter-cmds": []} |
| 126 | + |
| 127 | + for key, default_value in str_keys.items(): |
| 128 | + if key in config: |
| 129 | + config[key] = str(config[key]) |
| 130 | + else: |
| 131 | + config[key] = default_value |
| 132 | + for key, default_value in bool_keys.items(): |
| 133 | + if key in config: |
| 134 | + config[key] = bool(config[key]) |
| 135 | + else: |
| 136 | + config[key] = default_value |
| 137 | + for key in path_keys: |
| 138 | + if key in config: |
| 139 | + config[key] = str((config_file_path.parent / Path(config[key])).resolve()) |
| 140 | + for key, default_value in list_str_keys.items(): |
| 141 | + if key in config: |
| 142 | + config[key] = [str(cmd) for cmd in config[key]] |
| 143 | + else: |
| 144 | + config[key] = default_value |
| 145 | + for key in path_list_keys: |
| 146 | + if key in config: |
| 147 | + config[key] = [str((config_file_path.parent / path).resolve()) for path in config[key]] |
| 148 | + else: |
| 149 | + config[key] = [] |
| 150 | + |
| 151 | + # Convert hyphenated keys to underscored keys |
| 152 | + for key in list(config.keys()): |
| 153 | + if "-" in key: |
| 154 | + config[key.replace("-", "_")] = config[key] |
| 155 | + del config[key] |
| 156 | + |
| 157 | + return config |
| 158 | + |
| 159 | + |
| 160 | +def _parse_java_config_for_dir(dir_path: Path) -> dict[str, Any] | None: |
| 161 | + from codeflash.languages.java.build_config_strategy import parse_java_project_config |
| 162 | + |
| 163 | + return parse_java_project_config(dir_path) |
| 164 | + |
| 165 | + |
| 166 | +_SUBDIR_SKIP = frozenset( |
| 167 | + { |
| 168 | + ".git", |
| 169 | + ".hg", |
| 170 | + ".svn", |
| 171 | + "node_modules", |
| 172 | + ".venv", |
| 173 | + "venv", |
| 174 | + "__pycache__", |
| 175 | + "target", |
| 176 | + "build", |
| 177 | + "dist", |
| 178 | + ".tox", |
| 179 | + ".mypy_cache", |
| 180 | + ".ruff_cache", |
| 181 | + ".pytest_cache", |
| 182 | + } |
| 183 | +) |
| 184 | + |
| 185 | + |
| 186 | +def _check_dir_for_configs(dir_path: Path, configs: list[LanguageConfig], seen_languages: set[Language]) -> None: |
| 187 | + if Language.PYTHON not in seen_languages: |
| 188 | + pyproject = dir_path / "pyproject.toml" |
| 189 | + if pyproject.exists(): |
| 190 | + try: |
| 191 | + with pyproject.open("rb") as f: |
| 192 | + data = tomlkit.parse(f.read()) |
| 193 | + tool = data.get("tool", {}) |
| 194 | + if isinstance(tool, dict) and "codeflash" in tool: |
| 195 | + raw_config = dict(tool["codeflash"]) |
| 196 | + normalized = normalize_toml_config(raw_config, pyproject) |
| 197 | + seen_languages.add(Language.PYTHON) |
| 198 | + configs.append(LanguageConfig(config=normalized, config_path=pyproject, language=Language.PYTHON)) |
| 199 | + except Exception: |
| 200 | + pass |
| 201 | + |
| 202 | + if Language.JAVASCRIPT not in seen_languages: |
| 203 | + package_json = dir_path / "package.json" |
| 204 | + if package_json.exists(): |
| 205 | + try: |
| 206 | + result = parse_package_json_config(package_json) |
| 207 | + if result is not None: |
| 208 | + config, path = result |
| 209 | + seen_languages.add(Language.JAVASCRIPT) |
| 210 | + configs.append(LanguageConfig(config=config, config_path=path, language=Language.JAVASCRIPT)) |
| 211 | + except Exception: |
| 212 | + pass |
| 213 | + |
| 214 | + if Language.JAVA not in seen_languages: |
| 215 | + if ( |
| 216 | + (dir_path / "pom.xml").exists() |
| 217 | + or (dir_path / "build.gradle").exists() |
| 218 | + or (dir_path / "build.gradle.kts").exists() |
| 219 | + ): |
| 220 | + try: |
| 221 | + java_config = _parse_java_config_for_dir(dir_path) |
| 222 | + if java_config is not None: |
| 223 | + seen_languages.add(Language.JAVA) |
| 224 | + configs.append(LanguageConfig(config=java_config, config_path=dir_path, language=Language.JAVA)) |
| 225 | + except Exception: |
| 226 | + pass |
| 227 | + |
| 228 | + |
| 229 | +def find_all_config_files(start_dir: Path | None = None) -> list[LanguageConfig]: |
| 230 | + if start_dir is None: |
| 231 | + start_dir = Path.cwd() |
| 232 | + |
| 233 | + configs: list[LanguageConfig] = [] |
| 234 | + seen_languages: set[Language] = set() |
| 235 | + |
| 236 | + # Walk upward from start_dir to filesystem root (closest config wins per language) |
| 237 | + dir_path = start_dir.resolve() |
| 238 | + while True: |
| 239 | + _check_dir_for_configs(dir_path, configs, seen_languages) |
| 240 | + |
| 241 | + parent = dir_path.parent |
| 242 | + if parent == dir_path: |
| 243 | + break |
| 244 | + dir_path = parent |
| 245 | + |
| 246 | + # Scan immediate subdirectories for monorepo language subprojects |
| 247 | + resolved_start = start_dir.resolve() |
| 248 | + try: |
| 249 | + subdirs = sorted(p for p in resolved_start.iterdir() if p.is_dir() and p.name not in _SUBDIR_SKIP) |
| 250 | + except OSError: |
| 251 | + subdirs = [] |
| 252 | + for subdir in subdirs: |
| 253 | + _check_dir_for_configs(subdir, configs, seen_languages) |
| 254 | + |
| 255 | + return configs |
| 256 | + |
| 257 | + |
106 | 258 | def parse_config_file( |
107 | 259 | config_file_path: Path | None = None, override_formatter_check: bool = False |
108 | 260 | ) -> tuple[dict[str, Any], Path]: |
@@ -174,55 +326,13 @@ def parse_config_file( |
174 | 326 | if config == {} and lsp_mode: |
175 | 327 | return {}, config_file_path |
176 | 328 |
|
177 | | - # Preserve language field if present (important for JS/TS projects) |
178 | | - # default values: |
179 | | - path_keys = ["module-root", "tests-root", "benchmarks-root"] |
180 | | - path_list_keys = ["ignore-paths"] |
181 | | - str_keys = {"pytest-cmd": "pytest", "git-remote": "origin"} |
182 | | - bool_keys = { |
183 | | - "override-fixtures": False, |
184 | | - "disable-telemetry": False, |
185 | | - "disable-imports-sorting": False, |
186 | | - "benchmark": False, |
187 | | - } |
188 | | - # Note: formatter-cmds defaults to empty list. For Python projects, black is typically |
189 | | - # detected by the project detector. For Java projects, no formatter is supported yet. |
190 | | - list_str_keys = {"formatter-cmds": []} |
191 | | - |
192 | | - for key, default_value in str_keys.items(): |
193 | | - if key in config: |
194 | | - config[key] = str(config[key]) |
195 | | - else: |
196 | | - config[key] = default_value |
197 | | - for key, default_value in bool_keys.items(): |
198 | | - if key in config: |
199 | | - config[key] = bool(config[key]) |
200 | | - else: |
201 | | - config[key] = default_value |
202 | | - for key in path_keys: |
203 | | - if key in config: |
204 | | - config[key] = str((Path(config_file_path).parent / Path(config[key])).resolve()) |
205 | | - for key, default_value in list_str_keys.items(): |
206 | | - if key in config: |
207 | | - config[key] = [str(cmd) for cmd in config[key]] |
208 | | - else: |
209 | | - config[key] = default_value |
210 | | - |
211 | | - for key in path_list_keys: |
212 | | - if key in config: |
213 | | - config[key] = [str((Path(config_file_path).parent / path).resolve()) for path in config[key]] |
214 | | - else: |
215 | | - config[key] = [] |
| 329 | + config = normalize_toml_config(config, config_file_path) |
216 | 330 |
|
217 | 331 | # see if this is happening during GitHub actions setup |
218 | | - if config.get("formatter-cmds") and len(config.get("formatter-cmds")) > 0 and not override_formatter_check: |
219 | | - assert config.get("formatter-cmds")[0] != "your-formatter $file", ( |
| 332 | + if config.get("formatter_cmds") and len(config.get("formatter_cmds")) > 0 and not override_formatter_check: |
| 333 | + assert config.get("formatter_cmds")[0] != "your-formatter $file", ( |
220 | 334 | "The formatter command is not set correctly in pyproject.toml. Please set the " |
221 | 335 | "formatter command in the 'formatter-cmds' key. More info - https://docs.codeflash.ai/configuration" |
222 | 336 | ) |
223 | | - for key in list(config.keys()): |
224 | | - if "-" in key: |
225 | | - config[key.replace("-", "_")] = config[key] |
226 | | - del config[key] |
227 | 337 |
|
228 | 338 | return config, config_file_path |
0 commit comments