|
3 | 3 | import ast |
4 | 4 | import os |
5 | 5 | import re |
| 6 | +import shutil |
6 | 7 | import subprocess |
7 | 8 | import sys |
8 | 9 | from enum import Enum, auto |
|
23 | 24 |
|
24 | 25 | from codeflash.api.cfapi import is_github_app_installed_on_repo |
25 | 26 | from codeflash.cli_cmds.cli_common import apologize_and_exit |
26 | | -from codeflash.cli_cmds.console import console, logger |
| 27 | +from codeflash.cli_cmds.console import console, logger, progress_bar |
27 | 28 | from codeflash.code_utils.compat import LF |
28 | 29 | from codeflash.code_utils.config_parser import parse_config_file |
29 | 30 | from codeflash.code_utils.env_utils import check_formatter_installed, get_codeflash_api_key |
@@ -98,6 +99,8 @@ def init_codeflash() -> None: |
98 | 99 |
|
99 | 100 | install_github_actions(override_formatter_check=True) |
100 | 101 |
|
| 102 | + install_vscode_extension() |
| 103 | + |
101 | 104 | module_string = "" |
102 | 105 | if "setup_info" in locals(): |
103 | 106 | module_string = f" you selected ({setup_info.module_root})" |
@@ -802,6 +805,37 @@ def install_github_actions(override_formatter_check: bool = False) -> None: # n |
802 | 805 | apologize_and_exit() |
803 | 806 |
|
804 | 807 |
|
| 808 | +def install_vscode_extension() -> None: |
| 809 | + vscode_path = shutil.which("code") |
| 810 | + if not vscode_path: |
| 811 | + return |
| 812 | + |
| 813 | + error = "" |
| 814 | + with progress_bar("Installing Codeflash for VSCode…"): |
| 815 | + try: |
| 816 | + result = subprocess.run( |
| 817 | + [vscode_path, "--install-extension", "codeflash.codeflash", "--force"], |
| 818 | + check=True, |
| 819 | + text=True, |
| 820 | + timeout=60, |
| 821 | + capture_output=True, |
| 822 | + ) |
| 823 | + except subprocess.TimeoutExpired: |
| 824 | + error = "Installation timed out." |
| 825 | + except subprocess.CalledProcessError as e: |
| 826 | + error = e.stderr or "Unknown error." |
| 827 | + |
| 828 | + if error: |
| 829 | + click.echo("❌ Failed to install Codeflash for VSCode.") |
| 830 | + click.echo(error.strip()) |
| 831 | + else: |
| 832 | + output = (result.stdout or "").lower() |
| 833 | + if "already installed" in output: |
| 834 | + click.echo("✅ Codeflash for VSCode is already installed.") |
| 835 | + return |
| 836 | + click.echo("✅ Installed the latest version of Codeflash for VSCode.") |
| 837 | + |
| 838 | + |
805 | 839 | def determine_dependency_manager(pyproject_data: dict[str, Any]) -> DependencyManager: # noqa: PLR0911 |
806 | 840 | """Determine which dependency manager is being used based on pyproject.toml contents.""" |
807 | 841 | if (Path.cwd() / "poetry.lock").exists(): |
|
0 commit comments