File size: 2,966 Bytes
f5bd02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import logging
import os
import subprocess
from pathlib import Path

from lisa_on_cuda.utils import session_logger

LOGLEVEL = os.getenv('LOGLEVEL', 'INFO').upper()
session_logger.change_logging(LOGLEVEL)


def assert_envs(envs_list):
    for current_env in envs_list:
        try:
            assert current_env is not None and current_env != ""
        except AssertionError as aex:
            logging.error(f"error on assertion for current_env: {current_env}.")
            raise aex


def read_std_out_err(std_out_err, output_type: str, command: list):
    output = std_out_err.split("\n")
    logging.info(f"output type:{output_type} for command:{' '.join(command)}.")
    for line in iter(output):
        logging.info(f"output_content_home stdout:{line.strip()}.")
    logging.info("########")


def run_command(commands_list: list) -> None:
    try:
        output_content_home = subprocess.run(
            commands_list,
            capture_output=True,
            text=True,
            check=True
        )
        read_std_out_err(output_content_home.stdout, "stdout", commands_list)
        read_std_out_err(output_content_home.stderr, "stderr", commands_list)
    except Exception as ex:
        logging.error(f"ex:{ex}.")
        raise ex


def build_frontend() -> None:
    home = os.getenv("HOME")
    root_folder = Path(globals().get("__file__", "./_")).absolute().parent.parent
    project_root_folder = Path(os.getenv("PROJECT_ROOT_FOLDER", root_folder))
    nvm_url_base = os.getenv("NVM_URL_BASE")
    nvm_url_version = os.getenv("NVM_URL_VERSION")
    node_version = os.getenv("NODE_VERSION")

    assert_envs([
        home,
        root_folder,
        project_root_folder,
        nvm_url_base,
        nvm_url_version,
        node_version
    ])

    # compose nvm.sh url
    logging.info(f"NVM_URL_BASE:{nvm_url_base}.")
    logging.info(f"NVM_URL_VERSION:{nvm_url_version}.")
    nvm_url = nvm_url_base.format(nvm_url_version)
    logging.info(f"prepared nvm_url:{nvm_url}.")

    # download and install nodejs
    os.chdir(home)
    run_command(["curl", "-o", "install_nvm.sh", nvm_url])
    run_command(["ls", "-l", f"{home}/install_nvm.sh"])
    run_command(["bash", f"{home}/install_nvm.sh"])
    run_command(["bash", "source", f"{home}/.bashrc"])
    run_command(["nvm.sh", "install", node_version])
    run_command(["npm", "install", "npm", "pnpm"])

    # install deps
    os.chdir(Path(project_root_folder) / "static")
    run_command(["pnpm", "install"])

    # build frontend dist and assert for its correct build
    run_command(["pnpm", "tailwindcss", "-i", "src/input.css", "-o", "dist/output.css"])
    run_command(["pnpm", "build"])
    run_command(["ls", "-l", "./dist"])

    # uninstall node and nvm.sh
    nvm_dir = os.getenv("NVM_DIR")
    assert_envs([nvm_dir])
    run_command(["rm", "-rf", f"{home}/install_nvm.sh", nvm_dir, f"{home}/.npm", f"{home}/.bower"])


if __name__ == '__main__':
    build_frontend()