Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions cppython/build/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
from pathlib import Path
from typing import Any

from rich.console import Console

from cppython.core.interface import NoOpInterface
from cppython.core.schema import ProjectConfiguration, SyncData
from cppython.project import Project
from cppython.utility.exception import InstallationVerificationError
from cppython.utility.output import OutputSession


@dataclass
Expand Down Expand Up @@ -89,31 +92,35 @@ def prepare(self) -> BuildPreparationResult:
verbosity=1,
)

# Create the CPPython project
interface = BuildInterface()
project = Project(project_config, interface, pyproject_data)
# Use a headless console on stderr — no spinner in build backend context
console = Console(stderr=True, width=120)

with OutputSession(console, verbose=False) as session:
# Create the CPPython project
interface = BuildInterface()
project = Project(project_config, interface, pyproject_data, session=session)

if not project.enabled:
self.logger.info('CPPython: Project not enabled, skipping preparation')
return BuildPreparationResult()
if not project.enabled:
self.logger.info('CPPython: Project not enabled, skipping preparation')
return BuildPreparationResult()

# Sync and verify — does NOT install dependencies
self.logger.info('CPPython: Verifying C++ dependencies are installed')
# Sync and verify — does NOT install dependencies
self.logger.info('CPPython: Verifying C++ dependencies are installed')

try:
sync_data = project.prepare_build()
except InstallationVerificationError:
self.logger.error(
"CPPython: C++ dependencies not installed. Run 'cppython install' or 'pdm install' before building."
)
raise
try:
sync_data = project.prepare_build()
except InstallationVerificationError:
self.logger.error(
"CPPython: C++ dependencies not installed. Run 'cppython install' or 'pdm install' before building."
)
raise

if sync_data:
self.logger.info('CPPython: Sync data obtained from provider: %s', type(sync_data).__name__)
else:
self.logger.warning('CPPython: No sync data generated')
if sync_data:
self.logger.info('CPPython: Sync data obtained from provider: %s', type(sync_data).__name__)
else:
self.logger.warning('CPPython: No sync data generated')

return BuildPreparationResult(sync_data=sync_data)
return BuildPreparationResult(sync_data=sync_data)


def prepare_build(source_dir: Path) -> BuildPreparationResult:
Expand Down
9 changes: 7 additions & 2 deletions cppython/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def __init__(self, project_configuration: ProjectConfiguration, logger: Logger)
# Informal standard to check for color
force_color = os.getenv('FORCE_COLOR', '1') != '0'

console = Console(
self._console = Console(
force_terminal=force_color,
color_system='auto',
width=120,
Expand All @@ -404,7 +404,7 @@ def __init__(self, project_configuration: ProjectConfiguration, logger: Logger)
)

rich_handler = RichHandler(
console=console,
console=self._console,
rich_tracebacks=True,
show_time=False,
show_path=False,
Expand All @@ -420,6 +420,11 @@ def __init__(self, project_configuration: ProjectConfiguration, logger: Logger)

self._resolver = Resolver(self._project_configuration, self._logger)

@property
def console(self) -> Console:
"""The Rich console instance used for terminal output."""
return self._console

def build(
self,
pep621_configuration: PEP621Configuration,
Expand Down
72 changes: 51 additions & 21 deletions cppython/console/entry.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
"""A Typer CLI for CPPython interfacing"""

import contextlib
from collections.abc import Generator
from importlib.metadata import entry_points
from pathlib import Path
from typing import Annotated

import typer
from rich import print
from rich.console import Console
from rich.syntax import Syntax

from cppython.configuration import ConfigurationLoader
from cppython.console.schema import ConsoleConfiguration, ConsoleInterface
from cppython.core.schema import PluginReport, ProjectConfiguration
from cppython.project import Project
from cppython.utility.output import OutputSession

app = typer.Typer(no_args_is_help=True)

info_app = typer.Typer(no_args_is_help=True, help='Prints project information including plugin configuration, managed files, and templates.')
info_app = typer.Typer(
no_args_is_help=True,
help='Prints project information including plugin configuration, managed files, and templates.',
)
app.add_typer(info_app, name='info')

list_app = typer.Typer(no_args_is_help=True, help='List project entities.')
app.add_typer(list_app, name='list')


def get_enabled_project(context: typer.Context) -> Project:
"""Helper to load and validate an enabled Project from CLI context."""
def _get_configuration(context: typer.Context) -> ConsoleConfiguration:
"""Extract the ConsoleConfiguration object from the CLI context.

Raises:
ValueError: If the configuration object is missing
"""
configuration = context.find_object(ConsoleConfiguration)
if configuration is None:
raise ValueError('The configuration object is missing')
return configuration


def get_enabled_project(context: typer.Context) -> Project:
"""Helper to load and validate an enabled Project from CLI context."""
configuration = _get_configuration(context)

# Use ConfigurationLoader to load and merge all configuration sources
loader = ConfigurationLoader(configuration.project_configuration.project_root)
Expand All @@ -44,6 +61,23 @@ def get_enabled_project(context: typer.Context) -> Project:
return project


@contextlib.contextmanager
def _session_project(context: typer.Context) -> Generator[Project]:
"""Create an enabled Project wrapped in an OutputSession.

Yields the project with its session already attached. The session
(spinner + log file) is torn down when the ``with`` block exits.
"""
project = get_enabled_project(context)
configuration = _get_configuration(context)
verbose = configuration.project_configuration.verbosity > 0
console = Console(width=120)

with OutputSession(console, verbose=verbose) as session:
project.session = session
yield project


def _parse_groups_argument(groups: str | None) -> list[str] | None:
"""Parse pip-style dependency groups from command argument.

Expand Down Expand Up @@ -208,12 +242,10 @@ def install(
Raises:
ValueError: If the configuration object is missing
"""
project = get_enabled_project(context)

# Parse groups from pip-style syntax
group_list = _parse_groups_argument(groups)

project.install(groups=group_list)
with _session_project(context) as project:
project.install(groups=group_list)


@app.command()
Expand All @@ -236,12 +268,10 @@ def update(
Raises:
ValueError: If the configuration object is missing
"""
project = get_enabled_project(context)

# Parse groups from pip-style syntax
group_list = _parse_groups_argument(groups)

project.update(groups=group_list)
with _session_project(context) as project:
project.update(groups=group_list)


@list_app.command()
Expand Down Expand Up @@ -292,8 +322,8 @@ def publish(
Raises:
ValueError: If the configuration object is missing
"""
project = get_enabled_project(context)
project.publish()
with _session_project(context) as project:
project.publish()


@app.command()
Expand All @@ -312,8 +342,8 @@ def build(
context: The CLI configuration object
configuration: Optional named configuration
"""
project = get_enabled_project(context)
project.build(configuration=configuration)
with _session_project(context) as project:
project.build(configuration=configuration)


@app.command()
Expand All @@ -332,8 +362,8 @@ def test(
context: The CLI configuration object
configuration: Optional named configuration
"""
project = get_enabled_project(context)
project.test(configuration=configuration)
with _session_project(context) as project:
project.test(configuration=configuration)


@app.command()
Expand All @@ -352,8 +382,8 @@ def bench(
context: The CLI configuration object
configuration: Optional named configuration
"""
project = get_enabled_project(context)
project.bench(configuration=configuration)
with _session_project(context) as project:
project.bench(configuration=configuration)


@app.command()
Expand All @@ -377,5 +407,5 @@ def run(
target: The name of the build target to run
configuration: Optional named configuration
"""
project = get_enabled_project(context)
project.run(target, configuration=configuration)
with _session_project(context) as project:
project.run(target, configuration=configuration)
36 changes: 31 additions & 5 deletions cppython/plugins/cmake/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The CMake generator implementation"""

import subprocess
from logging import getLogger
from pathlib import Path
from typing import Any

Expand All @@ -13,6 +13,9 @@
from cppython.plugins.cmake.builder import Builder
from cppython.plugins.cmake.resolution import resolve_cmake_data
from cppython.plugins.cmake.schema import CMakeSyncData
from cppython.utility.subprocess import run_subprocess

logger = getLogger('cppython.cmake')


class CMakeGenerator(Generator):
Expand Down Expand Up @@ -136,7 +139,7 @@ def build(self, configuration: str | None = None) -> None:
"""
preset = self._resolve_configuration(configuration)
cmd = [self._cmake_command(), '--build', '--preset', preset]
subprocess.run(cmd, check=True, cwd=self.data.preset_file.parent)
run_subprocess(cmd, cwd=self.data.preset_file.parent, logger=logger)

def test(self, configuration: str | None = None) -> None:
"""Runs tests using ctest with the resolved preset.
Expand All @@ -146,7 +149,7 @@ def test(self, configuration: str | None = None) -> None:
"""
preset = self._resolve_configuration(configuration)
cmd = [self._ctest_command(), '--preset', preset]
subprocess.run(cmd, check=True, cwd=self.data.preset_file.parent)
run_subprocess(cmd, cwd=self.data.preset_file.parent, logger=logger)

def bench(self, configuration: str | None = None) -> None:
"""Runs benchmarks using ctest with the resolved preset.
Expand All @@ -156,7 +159,7 @@ def bench(self, configuration: str | None = None) -> None:
"""
preset = self._resolve_configuration(configuration)
cmd = [self._ctest_command(), '--preset', preset]
subprocess.run(cmd, check=True, cwd=self.data.preset_file.parent)
run_subprocess(cmd, cwd=self.data.preset_file.parent, logger=logger)

def run(self, target: str, configuration: str | None = None) -> None:
"""Runs a built executable by target name.
Expand All @@ -180,7 +183,30 @@ def run(self, target: str, configuration: str | None = None) -> None:
raise FileNotFoundError(f"Could not find executable '{target}' in build directory: {build_path}")

executable = executables[0]
subprocess.run([str(executable)], check=True, cwd=self.data.preset_file.parent)
run_subprocess([str(executable)], cwd=self.data.preset_file.parent, logger=logger)

def list_targets(self) -> list[str]:
"""Lists discovered build targets/executables in the CMake build directory.

Searches the build directory for executable files, excluding common
non-target files.

Returns:
A sorted list of unique target names found.
"""
build_path = self.core_data.cppython_data.build_path

if not build_path.exists():
return []

# Collect executable files from the build directory
targets: set[str] = set()
for candidate in build_path.rglob('*'):
if candidate.is_file() and (candidate.stat().st_mode & 0o111 or candidate.suffix == '.exe'):
# Use the stem (name without extension) as the target name
targets.add(candidate.stem)

return sorted(targets)

def list_targets(self) -> list[str]:
"""Lists discovered build targets/executables in the CMake build directory.
Expand Down
Loading
Loading