Skip to content

Commit

Permalink
Improve VSCode detection in configure_vscode.py
Browse files Browse the repository at this point in the history
This commit improves the reliability of the `configure_vscode.py` script
on macOS by improving the detection mechanism for the Visual Studio Code
CLI command (`code`). It introduces a fallback mechanism to locate the
`code` executable in common installation path for macOS, addressing the
issue where the VSCode CLI might not be found in PATH variable.

Additionally, the commit refines error handling by providing clearer error
messages for unknown exceptions during the extension installation process.
This ensures that users are better informed about the nature of the error,
facilitating easier troubleshooting.
  • Loading branch information
undergroundwires committed Feb 24, 2024
1 parent 1964524 commit 98845e6
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions scripts/configure_vscode.py
Expand Up @@ -6,10 +6,11 @@

import os
import json
from pathlib import Path
import subprocess
import sys
import re
from typing import Any
from typing import Any, Optional
from shutil import which

VSCODE_SETTINGS_JSON_FILE: str = '.vscode/settings.json'
Expand Down Expand Up @@ -84,19 +85,31 @@ def install_recommended_extensions() -> None:
if not extensions:
print_skip(f"No recommendations found in the {VSCODE_EXTENSIONS_JSON_FILE} file.")
return
vscode_cli_path = which('code') # More reliable than using `code`, especially on Windows.
vscode_cli_path = locate_vscode_cli()
if vscode_cli_path is None:
print_error('Visual Studio Code CLI (`code`) tool not found.')
return
install_vscode_extensions(vscode_cli_path, extensions)
except json.JSONDecodeError:
print_error(f"Invalid JSON in {VSCODE_EXTENSIONS_JSON_FILE}")

def locate_vscode_cli() -> Optional[str]:
vscode_alias = which('code') # More reliable than using `code`, especially on Windows.
if vscode_alias:
return vscode_alias
potential_vscode_cli_paths = [
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code' # macOS VS Code may not register 'code' command in PATH
]
for vscode_cli_candidate_path in potential_vscode_cli_paths:
if Path(vscode_cli_candidate_path).is_file():
return vscode_cli_candidate_path
return None

def remove_json_comments(json_like: str) -> str:
pattern: str = r'(?:"(?:\\.|[^"\\])*"|/\*[\s\S]*?\*/|//.*)|([^:]//.*$)'
return re.sub(
pattern,
lambda m: '' if m.group(1) else m.group(0), json_like, flags=re.MULTILINE,
lambda m: '' if m.group(1) else m.agroup(0), json_like, flags=re.MULTILINE,
)

def install_vscode_extensions(vscode_cli_path: str, extensions: list[str]) -> None:
Expand All @@ -123,6 +136,12 @@ def install_vscode_extensions(vscode_cli_path: str, extensions: list[str]) -> No
f"Visual Studio Code CLI tool not found: {vscode_cli_path}."
f"Could not install extension: {ext}",
]))
except Exception as e: # pylint: disable=broad-except
print_error(' '.join([
f"Failed to install extension '{ext}'.",
f"Attempted using Visual Studio Code CLI at: '{vscode_cli_path}'.",
f"Encountered error: {e}",
]))
total_extensions = len(extensions)
print_installation_results(successful_installations, total_extensions)

Expand Down

0 comments on commit 98845e6

Please sign in to comment.