File size: 3,089 Bytes
c13c67e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
import subprocess
from pathlib import Path

from dotenv import load_dotenv

from lisa_on_cuda.utils import session_logger


load_dotenv()
LOGLEVEL = os.getenv('LOGLEVEL', 'INFO').upper()
session_logger.change_logging(LOGLEVEL)
root_folder = Path(globals().get("__file__", "./_")).absolute().parent.parent.parent
env_project_root_folder = Path(os.getenv("PROJECT_ROOT_FOLDER", root_folder))
env_input_css_path = Path(os.getenv("INPUT_CSS_PATH"))


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, capture_output: bool = True, text: bool = True, check: bool = True) -> None:
    try:
        output_content_home = subprocess.run(
            commands_list,
            capture_output=capture_output,
            text=text,
            check=check
        )
        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(
        project_root_folder: str | Path,
        input_css_path: str | Path,
        output_dist_folder: Path = root_folder / "static" / "dist",
    ) -> None:
    assert_envs([
        str(project_root_folder),
        str(input_css_path)
    ])

    # install deps
    os.chdir(Path(project_root_folder) / "static")
    current_folder = os.getcwd()
    logging.info(f"current_folder:{current_folder}, install pnpm...")
    run_command(["npm", "install", "-g", "npm", "pnpm"])
    logging.info(f"install pnpm dependencies...")
    run_command(["pnpm", "install"])

    # build frontend dist and assert for its correct build
    output_css = str(output_dist_folder / "output.css")
    output_index_html = str(output_dist_folder / "index.html")
    output_dist_folder = str(output_dist_folder)
    logging.info(f"pnpm: build '{output_dist_folder}'...")
    run_command(["pnpm", "build"])
    logging.info(f"pnpm: ls -l {output_index_html}:")
    run_command(["ls", "-l", output_index_html])
    cmd = ["pnpm", "tailwindcss", "-i", str(input_css_path), "-o", output_css]
    logging.info(f"pnpm: {' '.join(cmd)}...")
    run_command(["pnpm", "tailwindcss", "-i", str(input_css_path), "-o", output_css])
    logging.info(f"pnpm: ls -l {output_css}:")
    run_command(["ls", "-l", output_css])
    logging.info(f"end!")


if __name__ == '__main__':
    build_frontend(
        project_root_folder=env_project_root_folder,
        input_css_path=env_input_css_path
    )