alessandro trinca tornidor commited on
Commit
f5bd02a
·
1 Parent(s): 15d84c8

feat: add first frontend_builder.py implementation

Browse files
Files changed (1) hide show
  1. scripts/frontend_builder.py +92 -0
scripts/frontend_builder.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+ import subprocess
4
+ from pathlib import Path
5
+
6
+ from lisa_on_cuda.utils import session_logger
7
+
8
+ LOGLEVEL = os.getenv('LOGLEVEL', 'INFO').upper()
9
+ session_logger.change_logging(LOGLEVEL)
10
+
11
+
12
+ def assert_envs(envs_list):
13
+ for current_env in envs_list:
14
+ try:
15
+ assert current_env is not None and current_env != ""
16
+ except AssertionError as aex:
17
+ logging.error(f"error on assertion for current_env: {current_env}.")
18
+ raise aex
19
+
20
+
21
+ def read_std_out_err(std_out_err, output_type: str, command: list):
22
+ output = std_out_err.split("\n")
23
+ logging.info(f"output type:{output_type} for command:{' '.join(command)}.")
24
+ for line in iter(output):
25
+ logging.info(f"output_content_home stdout:{line.strip()}.")
26
+ logging.info("########")
27
+
28
+
29
+ def run_command(commands_list: list) -> None:
30
+ try:
31
+ output_content_home = subprocess.run(
32
+ commands_list,
33
+ capture_output=True,
34
+ text=True,
35
+ check=True
36
+ )
37
+ read_std_out_err(output_content_home.stdout, "stdout", commands_list)
38
+ read_std_out_err(output_content_home.stderr, "stderr", commands_list)
39
+ except Exception as ex:
40
+ logging.error(f"ex:{ex}.")
41
+ raise ex
42
+
43
+
44
+ def build_frontend() -> None:
45
+ home = os.getenv("HOME")
46
+ root_folder = Path(globals().get("__file__", "./_")).absolute().parent.parent
47
+ project_root_folder = Path(os.getenv("PROJECT_ROOT_FOLDER", root_folder))
48
+ nvm_url_base = os.getenv("NVM_URL_BASE")
49
+ nvm_url_version = os.getenv("NVM_URL_VERSION")
50
+ node_version = os.getenv("NODE_VERSION")
51
+
52
+ assert_envs([
53
+ home,
54
+ root_folder,
55
+ project_root_folder,
56
+ nvm_url_base,
57
+ nvm_url_version,
58
+ node_version
59
+ ])
60
+
61
+ # compose nvm.sh url
62
+ logging.info(f"NVM_URL_BASE:{nvm_url_base}.")
63
+ logging.info(f"NVM_URL_VERSION:{nvm_url_version}.")
64
+ nvm_url = nvm_url_base.format(nvm_url_version)
65
+ logging.info(f"prepared nvm_url:{nvm_url}.")
66
+
67
+ # download and install nodejs
68
+ os.chdir(home)
69
+ run_command(["curl", "-o", "install_nvm.sh", nvm_url])
70
+ run_command(["ls", "-l", f"{home}/install_nvm.sh"])
71
+ run_command(["bash", f"{home}/install_nvm.sh"])
72
+ run_command(["bash", "source", f"{home}/.bashrc"])
73
+ run_command(["nvm.sh", "install", node_version])
74
+ run_command(["npm", "install", "npm", "pnpm"])
75
+
76
+ # install deps
77
+ os.chdir(Path(project_root_folder) / "static")
78
+ run_command(["pnpm", "install"])
79
+
80
+ # build frontend dist and assert for its correct build
81
+ run_command(["pnpm", "tailwindcss", "-i", "src/input.css", "-o", "dist/output.css"])
82
+ run_command(["pnpm", "build"])
83
+ run_command(["ls", "-l", "./dist"])
84
+
85
+ # uninstall node and nvm.sh
86
+ nvm_dir = os.getenv("NVM_DIR")
87
+ assert_envs([nvm_dir])
88
+ run_command(["rm", "-rf", f"{home}/install_nvm.sh", nvm_dir, f"{home}/.npm", f"{home}/.bower"])
89
+
90
+
91
+ if __name__ == '__main__':
92
+ build_frontend()