Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias ls #50

Open
github-actions bot opened this issue May 2, 2022 · 0 comments
Open

alias ls #50

github-actions bot opened this issue May 2, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented May 2, 2022

alias ls

https://github.com/Ackee-Blockchain/woke/blob/0d27de25720142beb9619a89619b7a94c3556af1/woke/woke/x_cli/svm.py#L92

from typing import Tuple, AnyStr
import asyncio

from click import Context
from rich.progress import Progress
import click

from woke.a_config import WokeConfig
from woke.b_svm import SolcVersionManager
from woke.c_regex_parsing.solidity_version import SolidityVersion, SolidityVersionExpr
from .console import console


@click.group(name="svm")
@click.pass_context
def run_svm(ctx: Context):
    """Run Woke solc version manager."""
    config = WokeConfig(woke_root_path=ctx.obj["woke_root_path"])
    config.load_configs()
    ctx.obj["config"] = config


@run_svm.command(name="install")
@click.argument("version_range", nargs=-1)
@click.option(
    "--force", is_flag=True, help="Reinstall the target version if already installed."
)
@click.pass_context
def svm_install(ctx: Context, version_range: Tuple[str], force: bool) -> None:
    """Install the latest solc version matching the given version range."""
    config: WokeConfig = ctx.obj["config"]
    svm = SolcVersionManager(config)
    version_expr = SolidityVersionExpr(" ".join(version_range))

    asyncio.run(run_solc_install(svm, version_expr, force))


async def run_solc_install(
    svm: SolcVersionManager, version_expr: SolidityVersionExpr, force: bool
) -> None:
    version = next(
        version for version in reversed(svm.list_all()) if version in version_expr
    )
    with Progress() as progress:
        task = progress.add_task(f"[green]Downloading solc {version}", total=1)
        await svm.install(
            version,
            force_reinstall=force,
            progress=(lambda x: progress.update(task, completed=x)),
        )
    console.print(f"Installed solc version {version}.")


@run_svm.command(name="switch")
@click.argument("version", nargs=1)
@click.pass_context
def svm_switch(ctx: Context, version: str) -> None:
    """Switch to the target version of solc. Raise an exception if the version is not installed."""
    config: WokeConfig = ctx.obj["config"]
    svm = SolcVersionManager(config)
    parsed_version = SolidityVersion.fromstring(version)

    if not svm.get_path(parsed_version).is_file():
        raise ValueError(f"solc version {parsed_version} is not installed.")

    (config.woke_root_path / ".woke_solc_version").write_text(str(parsed_version))
    console.print(f"Using woke-solc version {version}.")


@run_svm.command(name="use")
@click.argument("version_range", nargs=-1)
@click.option(
    "--force", is_flag=True, help="Reinstall the target version if already installed."
)
@click.pass_context
def svm_use(ctx: Context, version_range: Tuple[str], force: bool) -> None:
    """Install the target solc version and use it as the global version."""
    config: WokeConfig = ctx.obj["config"]
    svm = SolcVersionManager(config)
    version_expr = SolidityVersionExpr(" ".join(version_range))
    version = next(
        version for version in reversed(svm.list_all()) if version in version_expr
    )

    if not svm.get_path(version).is_file():
        asyncio.run(run_solc_install(svm, SolidityVersionExpr(str(version)), force))

    (config.woke_root_path / ".woke_solc_version").write_text(str(version))
    console.print(f"Using woke-solc version {version}.")


@run_svm.command(name="list")  # TODO alias `ls`
@click.option(
    "--all", is_flag=True, help="List all versions including non-installed ones."
)
@click.pass_context
def svm_list(ctx: Context, all: bool) -> None:
    """List installed solc versions."""
    config: WokeConfig = ctx.obj["config"]
    svm = SolcVersionManager(config)
    if all:
        installed = set(svm.list_installed())
        output = "\n".join(
            f"- {version} {'([green]installed[/green])' if version in installed else ''}"
            for version in svm.list_all()
        )
    else:
        output = "\n".join(f"- {version}" for version in svm.list_installed())
    console.print(output)


@run_svm.command(name="remove")  # TODO alias `rm`
@click.argument("version", nargs=1)
@click.option(
    "--ignore-missing",
    is_flag=True,
    help="do not raise an exception if version to be removed is not installed",
)
@click.pass_context
def svm_remove(ctx: Context, version: str, ignore_missing: bool) -> None:
    """Remove the target solc version."""
    config: WokeConfig = ctx.obj["config"]
    svm = SolcVersionManager(config)
    parsed_version = SolidityVersion.fromstring(version)

    if ignore_missing:
        try:
            svm.remove(parsed_version)
            console.print(f"Removed solc version {parsed_version}.")
        except ValueError:
            console.print(f"solc {parsed_version} is not installed.")
    else:
        svm.remove(parsed_version)
        console.print(f"Removed solc version {parsed_version}.")

10efd432c215dca868176400a285a96f69f994e9

@github-actions github-actions bot added the todo label May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants