prerequisites.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import glob
  4. import json
  5. import os
  6. import platform
  7. import re
  8. import subprocess
  9. import sys
  10. from datetime import datetime
  11. from fileinput import FileInput
  12. from pathlib import Path
  13. from types import ModuleType
  14. from typing import Optional
  15. import typer
  16. from packaging import version
  17. from redis import Redis
  18. from reflex import constants
  19. from reflex.config import get_config
  20. from reflex.utils import console, path_ops
  21. def check_node_version(min_version=constants.MIN_NODE_VERSION):
  22. """Check the version of Node.js.
  23. Args:
  24. min_version: The minimum version of Node.js required.
  25. Returns:
  26. Whether the version of Node.js is high enough.
  27. """
  28. try:
  29. # Run the node -v command and capture the output
  30. result = subprocess.run(
  31. ["node", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  32. )
  33. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  34. current_version = version.parse(result.stdout.decode())
  35. # Compare the version numbers
  36. return current_version >= version.parse(min_version)
  37. except Exception:
  38. return False
  39. def get_bun_version() -> Optional[version.Version]:
  40. """Get the version of bun.
  41. Returns:
  42. The version of bun.
  43. """
  44. try:
  45. # Run the bun -v command and capture the output
  46. result = subprocess.run(
  47. [os.path.expandvars(get_config().bun_path), "-v"],
  48. stdout=subprocess.PIPE,
  49. stderr=subprocess.PIPE,
  50. )
  51. return version.parse(result.stdout.decode().strip())
  52. except Exception:
  53. return None
  54. def get_package_manager() -> str:
  55. """Get the package manager executable.
  56. Returns:
  57. The path to the package manager.
  58. Raises:
  59. FileNotFoundError: If bun or npm is not installed.
  60. Exit: If the app directory is invalid.
  61. """
  62. config = get_config()
  63. # Check that the node version is valid.
  64. if not check_node_version():
  65. console.print(
  66. f"[red]Node.js version {constants.MIN_NODE_VERSION} or higher is required to run Reflex."
  67. )
  68. raise typer.Exit()
  69. # On Windows, we use npm instead of bun.
  70. if platform.system() == "Windows" or config.disable_bun:
  71. npm_path = path_ops.which("npm")
  72. if npm_path is None:
  73. raise FileNotFoundError("Reflex requires npm to be installed on Windows.")
  74. return npm_path
  75. # On other platforms, we use bun.
  76. return os.path.expandvars(get_config().bun_path)
  77. def get_app() -> ModuleType:
  78. """Get the app module based on the default config.
  79. Returns:
  80. The app based on the default config.
  81. """
  82. config = get_config()
  83. module = ".".join([config.app_name, config.app_name])
  84. sys.path.insert(0, os.getcwd())
  85. return __import__(module, fromlist=(constants.APP_VAR,))
  86. def get_redis() -> Optional[Redis]:
  87. """Get the redis client.
  88. Returns:
  89. The redis client.
  90. """
  91. config = get_config()
  92. if config.redis_url is None:
  93. return None
  94. redis_url, redis_port = config.redis_url.split(":")
  95. print("Using redis at", config.redis_url)
  96. return Redis(host=redis_url, port=int(redis_port), db=0)
  97. def get_production_backend_url() -> str:
  98. """Get the production backend URL.
  99. Returns:
  100. The production backend URL.
  101. """
  102. config = get_config()
  103. return constants.PRODUCTION_BACKEND_URL.format(
  104. username=config.username,
  105. app_name=config.app_name,
  106. )
  107. def get_default_app_name() -> str:
  108. """Get the default app name.
  109. The default app name is the name of the current directory.
  110. Returns:
  111. The default app name.
  112. """
  113. return os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  114. def create_config(app_name: str):
  115. """Create a new rxconfig file.
  116. Args:
  117. app_name: The name of the app.
  118. """
  119. # Import here to avoid circular imports.
  120. from reflex.compiler import templates
  121. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  122. with open(constants.CONFIG_FILE, "w") as f:
  123. f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
  124. def create_web_directory(root: Path) -> str:
  125. """Creates a web directory in the given root directory
  126. and returns the path to the directory.
  127. Args:
  128. root (Path): The root directory of the project.
  129. Returns:
  130. The path to the web directory.
  131. """
  132. web_dir = str(root / constants.WEB_DIR)
  133. path_ops.cp(constants.WEB_TEMPLATE_DIR, web_dir, overwrite=False)
  134. return web_dir
  135. def initialize_gitignore():
  136. """Initialize the template .gitignore file."""
  137. # The files to add to the .gitignore file.
  138. files = constants.DEFAULT_GITIGNORE
  139. # Subtract current ignored files.
  140. if os.path.exists(constants.GITIGNORE_FILE):
  141. with open(constants.GITIGNORE_FILE, "r") as f:
  142. files |= set([line.strip() for line in f.readlines()])
  143. # Write files to the .gitignore file.
  144. with open(constants.GITIGNORE_FILE, "w") as f:
  145. f.write(f"{(path_ops.join(sorted(files))).lstrip()}")
  146. def initialize_app_directory(app_name: str, template: constants.Template):
  147. """Initialize the app directory on reflex init.
  148. Args:
  149. app_name: The name of the app.
  150. template: The template to use.
  151. """
  152. console.log("Initializing the app directory.")
  153. path_ops.cp(os.path.join(constants.TEMPLATE_DIR, "apps", template.value), app_name)
  154. path_ops.mv(
  155. os.path.join(app_name, template.value + ".py"),
  156. os.path.join(app_name, app_name + constants.PY_EXT),
  157. )
  158. path_ops.cp(constants.ASSETS_TEMPLATE_DIR, constants.APP_ASSETS_DIR)
  159. def initialize_web_directory():
  160. """Initialize the web directory on reflex init."""
  161. console.log("Initializing the web directory.")
  162. path_ops.cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR)
  163. path_ops.mkdir(constants.WEB_ASSETS_DIR)
  164. # update nextJS config based on rxConfig
  165. next_config_file = os.path.join(constants.WEB_DIR, constants.NEXT_CONFIG_FILE)
  166. with open(next_config_file, "r") as file:
  167. lines = file.readlines()
  168. for i, line in enumerate(lines):
  169. if "compress:" in line:
  170. new_line = line.replace(
  171. "true", "true" if get_config().next_compression else "false"
  172. )
  173. lines[i] = new_line
  174. with open(next_config_file, "w") as file:
  175. file.writelines(lines)
  176. # Write the current version of distributed reflex package to a REFLEX_JSON."""
  177. with open(constants.REFLEX_JSON, "w") as f:
  178. reflex_json = {"version": constants.VERSION}
  179. json.dump(reflex_json, f, ensure_ascii=False)
  180. def validate_and_install_bun(initialize=True):
  181. """Check that bun version requirements are met. If they are not,
  182. ask user whether to install required version.
  183. Args:
  184. initialize: whether this function is called on `reflex init` or `reflex run`.
  185. Raises:
  186. Exit: If the bun version is not supported.
  187. """
  188. bun_version = get_bun_version()
  189. if bun_version is not None and (
  190. bun_version < version.parse(constants.MIN_BUN_VERSION)
  191. or bun_version > version.parse(constants.MAX_BUN_VERSION)
  192. ):
  193. console.print(
  194. f"""[red]Bun version {bun_version} is not supported by Reflex. Please change your to bun version to be between {constants.MIN_BUN_VERSION} and {constants.MAX_BUN_VERSION}."""
  195. )
  196. action = console.ask(
  197. "Enter 'yes' to install the latest supported bun version or 'no' to exit.",
  198. choices=["yes", "no"],
  199. default="no",
  200. )
  201. if action == "yes":
  202. remove_existing_bun_installation()
  203. install_bun()
  204. return
  205. else:
  206. raise typer.Exit()
  207. if initialize:
  208. install_bun()
  209. def remove_existing_bun_installation():
  210. """Remove existing bun installation."""
  211. package_manager = get_package_manager()
  212. if os.path.exists(package_manager):
  213. console.log("Removing bun...")
  214. path_ops.rm(os.path.expandvars(constants.BUN_ROOT_PATH))
  215. def install_bun():
  216. """Install bun onto the user's system.
  217. Raises:
  218. FileNotFoundError: if unzip or curl packages are not found.
  219. """
  220. # Bun is not supported on Windows.
  221. if platform.system() == "Windows":
  222. console.log("Skipping bun installation on Windows.")
  223. return
  224. # Only install if bun is not already installed.
  225. if not os.path.exists(get_package_manager()):
  226. console.log("Installing bun...")
  227. # Check if curl is installed
  228. curl_path = path_ops.which("curl")
  229. if curl_path is None:
  230. raise FileNotFoundError("Reflex requires curl to be installed.")
  231. # Check if unzip is installed
  232. unzip_path = path_ops.which("unzip")
  233. if unzip_path is None:
  234. raise FileNotFoundError("Reflex requires unzip to be installed.")
  235. os.system(constants.INSTALL_BUN)
  236. def install_frontend_packages(web_dir: str):
  237. """Installs the base and custom frontend packages
  238. into the given web directory.
  239. Args:
  240. web_dir (str): The directory where the frontend code is located.
  241. """
  242. # Install the frontend packages.
  243. console.rule("[bold]Installing frontend packages")
  244. # Install the base packages.
  245. subprocess.run(
  246. [get_package_manager(), "install"],
  247. cwd=web_dir,
  248. stdout=subprocess.PIPE,
  249. )
  250. # Install the app packages.
  251. packages = get_config().frontend_packages
  252. if len(packages) > 0:
  253. subprocess.run(
  254. [get_package_manager(), "add", *packages],
  255. cwd=web_dir,
  256. stdout=subprocess.PIPE,
  257. )
  258. def is_initialized() -> bool:
  259. """Check whether the app is initialized.
  260. Returns:
  261. Whether the app is initialized in the current directory.
  262. """
  263. return os.path.exists(constants.CONFIG_FILE) and os.path.exists(constants.WEB_DIR)
  264. def is_latest_template() -> bool:
  265. """Whether the app is using the latest template.
  266. Returns:
  267. Whether the app is using the latest template.
  268. """
  269. if not os.path.exists(constants.REFLEX_JSON):
  270. return False
  271. with open(constants.REFLEX_JSON) as f: # type: ignore
  272. app_version = json.load(f)["version"]
  273. return app_version == constants.VERSION
  274. def check_admin_settings():
  275. """Check if admin settings are set and valid for logging in cli app."""
  276. admin_dash = get_config().admin_dash
  277. current_time = datetime.now()
  278. if admin_dash:
  279. if not admin_dash.models:
  280. console.print(
  281. f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard enabled, but no models defined in [bold magenta]rxconfig.py[/bold magenta]. Time: {current_time}"
  282. )
  283. else:
  284. console.print(
  285. f"[yellow][Admin Dashboard][/yellow] Admin enabled, building admin dashboard. Time: {current_time}"
  286. )
  287. console.print(
  288. "Admin dashboard running at: [bold green]http://localhost:8000/admin[/bold green]"
  289. )
  290. def migrate_to_reflex():
  291. """Migration from Pynecone to Reflex."""
  292. # Check if the old config file exists.
  293. if not os.path.exists(constants.OLD_CONFIG_FILE):
  294. return
  295. # Ask the user if they want to migrate.
  296. action = console.ask(
  297. "Pynecone project detected. Automatically upgrade to Reflex?",
  298. choices=["y", "n"],
  299. )
  300. if action == "n":
  301. return
  302. # Rename pcconfig to rxconfig.
  303. console.print(
  304. f"[bold]Renaming {constants.OLD_CONFIG_FILE} to {constants.CONFIG_FILE}"
  305. )
  306. os.rename(constants.OLD_CONFIG_FILE, constants.CONFIG_FILE)
  307. # Find all python files in the app directory.
  308. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  309. file_list = glob.glob(file_pattern, recursive=True)
  310. # Add the config file to the list of files to be migrated.
  311. file_list.append(constants.CONFIG_FILE)
  312. # Migrate all files.
  313. updates = {
  314. "Pynecone": "Reflex",
  315. "pynecone as pc": "reflex as rx",
  316. "pynecone.io": "reflex.dev",
  317. "pynecone": "reflex",
  318. "pc.": "rx.",
  319. "pcconfig": "rxconfig",
  320. }
  321. for file_path in file_list:
  322. with FileInput(file_path, inplace=True) as file:
  323. for line in file:
  324. for old, new in updates.items():
  325. line = line.replace(old, new)
  326. print(line, end="")