From 82c82d97bc6d0e93ac66712b454cab54fc3efc18 Mon Sep 17 00:00:00 2001 From: Chad Norvell Date: Thu, 14 Dec 2023 03:29:54 +0000 Subject: [PATCH] pw_ide: Add command to build VSC extension Change-Id: If91f7363b93074afab6ee5c16a5fcdd7547382cc Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/184992 Commit-Queue: Chad Norvell Reviewed-by: Anthony DiGirolamo --- pw_ide/py/pw_ide/cli.py | 5 +++++ pw_ide/py/pw_ide/commands.py | 12 ++++++++++++ pw_ide/py/pw_ide/vscode.py | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/pw_ide/py/pw_ide/cli.py b/pw_ide/py/pw_ide/cli.py index cf910944b3..3862f928bd 100644 --- a/pw_ide/py/pw_ide/cli.py +++ b/pw_ide/py/pw_ide/cli.py @@ -363,6 +363,11 @@ def _build_argument_parser() -> argparse.ArgumentParser: metavar='SETTINGS_TYPE', help='do not update these settings types', ) + parser_vscode.add_argument( + '--build-extension', + action='store_true', + help='build the extension from source', + ) return parser_root diff --git a/pw_ide/py/pw_ide/commands.py b/pw_ide/py/pw_ide/commands.py index 8985c87c8e..01006719b8 100644 --- a/pw_ide/py/pw_ide/commands.py +++ b/pw_ide/py/pw_ide/commands.py @@ -52,6 +52,7 @@ from pw_ide import vscode from pw_ide.vscode import ( + build_extension as build_vscode_extension, VscSettingsManager, VscSettingsType, ) @@ -142,6 +143,7 @@ def cmd_setup( def cmd_vscode( include: Optional[List[VscSettingsType]] = None, exclude: Optional[List[VscSettingsType]] = None, + build_extension: bool = False, reporter: StatusReporter = StatusReporter(), pw_ide_settings: PigweedIdeSettings = PigweedIdeSettings(), ) -> None: @@ -200,6 +202,16 @@ def cmd_vscode( Likewise, it can be enabled by setting that value to true. It is enabled by default. """ + if build_extension: + reporter.info('Building the Visual Studio Code extension') + + try: + build_vscode_extension(Path(env.PW_ROOT)) + except subprocess.CalledProcessError: + reporter.err("Failed! See output for more info.") + else: + reporter.ok('Built successfully!') + if not pw_ide_settings.editor_enabled('vscode'): reporter.wrn( 'Visual Studio Code support is disabled in settings! If this is ' diff --git a/pw_ide/py/pw_ide/vscode.py b/pw_ide/py/pw_ide/vscode.py index cffd35660e..0ced12b477 100644 --- a/pw_ide/py/pw_ide/vscode.py +++ b/pw_ide/py/pw_ide/vscode.py @@ -65,6 +65,8 @@ import os from pathlib import Path import platform +import shutil +import subprocess from typing import Any, Dict, List, Optional, OrderedDict from pw_cli.env import pigweed_environment @@ -424,3 +426,25 @@ class VscSettingsManager(EditorSettingsManager[VscSettingsType]): VscSettingsType.EXTENSIONS: _default_extensions, VscSettingsType.LAUNCH: _default_launch, } + + +def build_extension(pw_root: Path): + """Build the VS Code extension.""" + + license_path = pw_root / 'LICENSE' + icon_path = pw_root.parent / 'icon.png' + + vsc_ext_path = pw_root / 'pw_ide' / 'ts' / 'pigweed-vscode' + temp_license_path = vsc_ext_path / 'LICENSE' + temp_icon_path = vsc_ext_path / 'icon.png' + + shutil.copy(license_path, temp_license_path) + shutil.copy(icon_path, temp_icon_path) + + try: + subprocess.run(['vsce', 'package'], check=True, cwd=vsc_ext_path) + except subprocess.CalledProcessError as e: + raise e + finally: + temp_license_path.unlink() + temp_icon_path.unlink()