Commit e4edced2 authored by Adrian DC's avatar Adrian DC
Browse files

feat(mise): avoid '--path' if already the default 'mise use' configuration

parent dfa99438
Loading
Loading
Loading
Loading
+30 −2
Changes for src/features/mise.py: 30 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
from json import loads
from os import environ
from pathlib import Path
from re import MULTILINE, search, sub
from typing import Any, Dict, List, Optional, Tuple

# Components
@@ -27,7 +28,12 @@ class Mise:

    # Arguments
    @staticmethod
    def arguments(config: str) -> List[str]:
    def arguments(config: str, specification: str = '') -> List[str]:

        # Use default configuration
        if config and specification \
                and Path(config).expanduser().resolve() == Mise.target(specification):
            return []

        # Use global configuration
        if config and Path(config).expanduser().resolve() == Mise.config_global():
@@ -85,8 +91,9 @@ class Mise:
            return None

        # Install packages in the configuration file
        specification: str = f'{package}@{version}' # pragma: no cover
        return Commands.mise( # pragma: no cover
            ['use'] + Mise.arguments(config) + [f'{package}@{version}'])
            ['use'] + Mise.arguments(config, specification) + [specification])

    # Configuration
    @staticmethod
@@ -154,6 +161,27 @@ class Mise:
        # Check if running from mise
        return bool(Mise.installation())

    # Target
    @staticmethod
    def target(specification: str) -> Optional[Path]:

        # Detect default configuration file
        output: str = Commands.output(Mise.BINARY, [
            'use',
            '--dry-run',
            specification,
        ], errors=True)
        match = search(
            r'^mise would \w+ (.+) \(',
            sub(r'\x1b\[[0-9;]*m', '', output),
            MULTILINE,
        )
        if match: # pragma: no cover
            return Path(match.group(1)).expanduser().resolve()

        # Fallback
        return None

    # Tool
    @staticmethod
    def tool(path: str) -> Optional[Tuple[str, str]]:
+4 −1
Changes for src/package/updates.py: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -155,7 +155,10 @@ class Updates:
        package_install: str = ''
        if Mise.managed():
            package_install = ' '.join(['mise', 'use'] + [ # pragma: no cover
                quote(argument) for argument in Mise.arguments(Mise.config())
                quote(argument) for argument in Mise.arguments(
                    Mise.config(),
                    f'{Mise.package(name)}@latest',
                )
            ])
        elif Platform.PATH_SEPARATOR + 'pipx' + Platform.PATH_SEPARATOR in Version.path():
            package_install = 'pipx upgrade' # pragma: no cover
+16 −2
Changes for src/system/commands.py: 16 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -69,7 +69,21 @@ class Commands:

    # Output
    @staticmethod
    def output(binary: str, arguments: List[str], quiet: bool = False) -> str:
    def output(
        binary: str,
        arguments: List[str],
        quiet: bool = False,
        errors: bool = False,
    ) -> str:

        # Variables
        stderr: Optional[int] = None

        # Redirect errors
        if errors:
            stderr = subprocess.STDOUT
        elif quiet:
            stderr = subprocess.DEVNULL

        # Get system output
        try:
@@ -77,7 +91,7 @@ class Commands:
                args=[binary] + arguments,
                cwd=None,
                shell=False,
                stderr=subprocess.DEVNULL if quiet else None,
                stderr=stderr,
            ).strip().decode()
        except subprocess.CalledProcessError as err:
            return str(err.output.decode())