from pathlib import Path

root = Path(__file__).parent.absolute()

import envo

envo.add_source_roots([root])

from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

from envo import Env, Namespace, env_var, logger, run

from env_comm import StickybeakCommEnv as ParentEnv

p = Namespace("p")


class StickybeakCiEnv(ParentEnv):
    class Meta(ParentEnv.Meta):
        stage: str = "ci"
        emoji: str = "⚙"
        load_env_vars = True

    class Environ(ParentEnv.Environ):
        pypi_username: Optional[str] = env_var(raw=True)
        pypi_password: Optional[str] = env_var(raw=True)

    e: Environ

    def init(self) -> None:
        super().init()

    @p.command
    def bootstrap(self, test_apps=True) -> None:
        super().bootstrap(test_apps)

    @p.command
    def test(self) -> None:
        run("pytest --reruns 2 -v tests")

    @p.command
    def build(self) -> None:
        run("poetry build")

    @p.command
    def publish(self) -> None:
        run(f'poetry publish --username "{self.e.pypi_username}" --password "{self.e.pypi_password}"', verbose=False)

    @p.command
    def rstcheck(self) -> None:
        pass
        # run("rstcheck README.rst | tee ./workspace/rstcheck.txt")

    @p.command
    def flake(self) -> None:
        pass
        # run("flake8 . | tee ./workspace/flake8.txt")

    @p.command
    def check_black(self) -> None:
        run("black --check .")

    @p.command
    def check_isort(self) -> None:
        run("black --check .")

    @p.command
    def mypy(self) -> None:
        pass
        run("mypy .")

    @p.command
    def generate_version(self) -> None:
        import toml

        config = toml.load(str(self.meta.root / "pyproject.toml"))
        version: str = config["tool"]["poetry"]["version"]

        version_file = self.meta.root / "stickybeak/__version__.py"
        Path(version_file).touch()

        version_file.write_text(f'__version__ = "{version}"\n')


ThisEnv = StickybeakCiEnv

[(7, 'envo.add_source_roots', 'envo.add_source_roots', False, 'import envo\n'), (16, 'envo.Namespace', 'Namespace', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (26, 'envo.env_var', 'env_var', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (27, 'envo.env_var', 'env_var', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (40, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (44, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (48, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (62, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (66, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (71, 'envo.run', 'run', False, 'from envo import Env, Namespace, env_var, logger, run\n'), (3, 'pathlib.Path', 'Path', False, 'from pathlib import Path\n'), (81, 'pathlib.Path', 'Path', False, 'from pathlib import Path\n')]