# Path: makeapp/utils.py
# def run_command(command: str, *, err_msg: str = '') -> List[str]:
#     """Runs a command in a shell process.
# 
#     Returns a list of strings gathered from a command.
# 
#     :param command:
# 
#     :param err_msg: Message to show on error.
# 
#     :raises: CommandError
# 
#     """
#     prc = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True)
# 
#     LOG.debug(f'Run command: `{command}` ...')
# 
#     data = []
# 
#     out, _ = prc.communicate()
# 
#     if isinstance(out, bytes):
#         out = out.decode('utf-8')
# 
#     has_error = prc.returncode
# 
#     for item in out.splitlines():
#         item = item.strip()
# 
#         if not item:
#             continue
# 
#         data.append(item)
# 
#     if has_error:
#         raise CommandError(err_msg or f"Command `{command}` failed: %s" % '\n'.join(data))
# 
#     return data
# 
# def check_command(command: str, hint: str):
#     """Checks whether a command is available.
#     If not - raises an exception.
# 
#     :param command:
#     :param hint:
# 
#     """
#     try:
#         run_command(f'type {command}')
# 
#     except CommandError:
#         raise CommandError(
#             f"Failed to execute '{command}' command. "
#             f"Check {hint} is installed and available.")
# 
# PYTHON_VERSION = sys.version_info


from ..utils import run_command, check_command, PYTHON_VERSION


class DistHelper:
    """Encapsulates Python distribution related logic."""

    python_bin: str = 'python' if PYTHON_VERSION[0] == 3 else 'python3'
    """Name of python binary that'll be used for commands run."""

    check_command(python_bin, 'Python 3+')

    @classmethod
    def ensure(cls):
        """Ensures dist helper is functional."""
        run_command(
            f'{cls.python_bin} -m wheel version',
            err_msg=f"Please install 'wheel' module to proceed: {cls.python_bin} -m pip install wheel")

    @classmethod
    def run_command(cls, command):
        """Basic command runner."""
        return run_command(f'{cls.python_bin} setup.py {command}')

    @classmethod
    def upload(cls):
        """Builds a package and uploads it to PyPI."""

# Path: makeapp/utils.py
# def run_command(command: str, *, err_msg: str = '') -> List[str]:
#     """Runs a command in a shell process.
# 
#     Returns a list of strings gathered from a command.
# 
#     :param command:
# 
#     :param err_msg: Message to show on error.
# 
#     :raises: CommandError
# 
#     """
#     prc = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True)
# 
#     LOG.debug(f'Run command: `{command}` ...')
# 
#     data = []
# 
#     out, _ = prc.communicate()
# 
#     if isinstance(out, bytes):
#         out = out.decode('utf-8')
# 
#     has_error = prc.returncode
# 
#     for item in out.splitlines():
#         item = item.strip()
# 
#         if not item:
#             continue
# 
#         data.append(item)
# 
#     if has_error:
#         raise CommandError(err_msg or f"Command `{command}` failed: %s" % '\n'.join(data))
# 
#     return data
# 
# def check_command(command: str, hint: str):
#     """Checks whether a command is available.
#     If not - raises an exception.
# 
#     :param command:
#     :param hint:
# 
#     """
#     try:
#         run_command(f'type {command}')
# 
#     except CommandError:
#         raise CommandError(
#             f"Failed to execute '{command}' command. "
#             f"Check {hint} is installed and available.")
# 
# PYTHON_VERSION = sys.version_info


# Path: makeapp/helpers/dist.py
from ..utils import run_command, check_command, PYTHON_VERSION



class DistHelper:
    """Encapsulates Python distribution related logic."""

    python_bin: str = 'python' if PYTHON_VERSION[0] == 3 else 'python3'
    """Name of python binary that'll be used for commands run."""

    check_command(python_bin, 'Python 3+')

    @classmethod
    def ensure(cls):
        """Ensures dist helper is functional."""
        run_command(
            f'{cls.python_bin} -m wheel version',
            err_msg=f"Please install 'wheel' module to proceed: {cls.python_bin} -m pip install wheel")

    @classmethod
    def run_command(cls, command):
        """Basic command runner."""
        return run_command(f'{cls.python_bin} setup.py {command}')

    @classmethod
    def upload(cls):
        """Builds a package and uploads it to PyPI."""

        cls.run_command('clean --all sdist bdist_wheel upload')