lisa-on-cuda / scripts /frontend_builder.py
alessandro trinca tornidor
feat: add first frontend_builder.py implementation
f5bd02a
raw
history blame
2.97 kB
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()