prerequisites.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import glob
  4. import importlib
  5. import inspect
  6. import json
  7. import os
  8. import platform
  9. import random
  10. import re
  11. import stat
  12. import sys
  13. import tempfile
  14. import zipfile
  15. from fileinput import FileInput
  16. from pathlib import Path
  17. from types import ModuleType
  18. from typing import Callable, Optional
  19. import httpx
  20. import pkg_resources
  21. import typer
  22. from alembic.util.exc import CommandError
  23. from packaging import version
  24. from redis.asyncio import Redis
  25. import reflex
  26. from reflex import constants, model
  27. from reflex.compiler import templates
  28. from reflex.config import Config, get_config
  29. from reflex.utils import console, path_ops, processes
  30. def check_latest_package_version(package_name: str):
  31. """Check if the latest version of the package is installed.
  32. Args:
  33. package_name: The name of the package.
  34. """
  35. try:
  36. # Get the latest version from PyPI
  37. current_version = pkg_resources.get_distribution(package_name).version
  38. url = f"https://pypi.org/pypi/{package_name}/json"
  39. response = httpx.get(url)
  40. latest_version = response.json()["info"]["version"]
  41. if version.parse(current_version) < version.parse(latest_version):
  42. console.warn(
  43. f"Your version ({current_version}) of {package_name} is out of date. Upgrade to {latest_version} with 'pip install {package_name} --upgrade'"
  44. )
  45. except Exception:
  46. pass
  47. def check_node_version() -> bool:
  48. """Check the version of Node.js.
  49. Returns:
  50. Whether the version of Node.js is valid.
  51. """
  52. current_version = get_node_version()
  53. if current_version:
  54. # Compare the version numbers
  55. return (
  56. current_version >= version.parse(constants.Node.MIN_VERSION)
  57. if constants.IS_WINDOWS
  58. else current_version == version.parse(constants.Node.VERSION)
  59. )
  60. return False
  61. def get_node_version() -> version.Version | None:
  62. """Get the version of node.
  63. Returns:
  64. The version of node.
  65. """
  66. try:
  67. result = processes.new_process([path_ops.get_node_path(), "-v"], run=True)
  68. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  69. return version.parse(result.stdout) # type: ignore
  70. except (FileNotFoundError, TypeError):
  71. return None
  72. def get_fnm_version() -> version.Version | None:
  73. """Get the version of fnm.
  74. Returns:
  75. The version of FNM.
  76. """
  77. try:
  78. result = processes.new_process([constants.Fnm.EXE, "--version"], run=True)
  79. return version.parse(result.stdout.split(" ")[1]) # type: ignore
  80. except (FileNotFoundError, TypeError):
  81. return None
  82. def get_bun_version() -> version.Version | None:
  83. """Get the version of bun.
  84. Returns:
  85. The version of bun.
  86. """
  87. try:
  88. # Run the bun -v command and capture the output
  89. result = processes.new_process([get_config().bun_path, "-v"], run=True)
  90. return version.parse(result.stdout) # type: ignore
  91. except FileNotFoundError:
  92. return None
  93. def get_install_package_manager() -> str | None:
  94. """Get the package manager executable for installation.
  95. Currently on unix systems, bun is used for installation only.
  96. Returns:
  97. The path to the package manager.
  98. """
  99. # On Windows, we use npm instead of bun.
  100. if constants.IS_WINDOWS:
  101. return get_package_manager()
  102. # On other platforms, we use bun.
  103. return get_config().bun_path
  104. def get_package_manager() -> str | None:
  105. """Get the package manager executable for running app.
  106. Currently on unix systems, npm is used for running the app only.
  107. Returns:
  108. The path to the package manager.
  109. """
  110. npm_path = path_ops.get_npm_path()
  111. if npm_path is not None:
  112. npm_path = str(Path(npm_path).resolve())
  113. return npm_path
  114. def get_app(reload: bool = False) -> ModuleType:
  115. """Get the app module based on the default config.
  116. Args:
  117. reload: Re-import the app module from disk
  118. Returns:
  119. The app based on the default config.
  120. Raises:
  121. RuntimeError: If the app name is not set in the config.
  122. """
  123. os.environ[constants.RELOAD_CONFIG] = str(reload)
  124. config = get_config()
  125. if not config.app_name:
  126. raise RuntimeError(
  127. "Cannot get the app module because `app_name` is not set in rxconfig! "
  128. "If this error occurs in a reflex test case, ensure that `get_app` is mocked."
  129. )
  130. module = ".".join([config.app_name, config.app_name])
  131. sys.path.insert(0, os.getcwd())
  132. app = __import__(module, fromlist=(constants.CompileVars.APP,))
  133. if reload:
  134. from reflex.state import State
  135. # Reset rx.State subclasses to avoid conflict when reloading.
  136. for subclass in tuple(State.class_subclasses):
  137. if subclass.__module__ == module:
  138. State.class_subclasses.remove(subclass)
  139. # Reload the app module.
  140. importlib.reload(app)
  141. return app
  142. def get_compiled_app(reload: bool = False) -> ModuleType:
  143. """Get the app module based on the default config after first compiling it.
  144. Args:
  145. reload: Re-import the app module from disk
  146. Returns:
  147. The compiled app based on the default config.
  148. """
  149. app_module = get_app(reload=reload)
  150. getattr(app_module, constants.CompileVars.APP).compile_()
  151. return app_module
  152. def get_redis() -> Redis | None:
  153. """Get the redis client.
  154. Returns:
  155. The redis client.
  156. """
  157. config = get_config()
  158. if not config.redis_url:
  159. return None
  160. if config.redis_url.startswith(("redis://", "rediss://", "unix://")):
  161. return Redis.from_url(config.redis_url)
  162. console.deprecate(
  163. feature_name="host[:port] style redis urls",
  164. reason="redis-py url syntax is now being used",
  165. deprecation_version="0.3.6",
  166. removal_version="0.4.0",
  167. )
  168. redis_url, has_port, redis_port = config.redis_url.partition(":")
  169. if not has_port:
  170. redis_port = 6379
  171. console.info(f"Using redis at {config.redis_url}")
  172. return Redis(host=redis_url, port=int(redis_port), db=0)
  173. def get_production_backend_url() -> str:
  174. """Get the production backend URL.
  175. Returns:
  176. The production backend URL.
  177. """
  178. config = get_config()
  179. return constants.PRODUCTION_BACKEND_URL.format(
  180. username=config.username,
  181. app_name=config.app_name,
  182. )
  183. def validate_app_name(app_name: str | None = None) -> str:
  184. """Validate the app name.
  185. The default app name is the name of the current directory.
  186. Args:
  187. app_name: the name passed by user during reflex init
  188. Returns:
  189. The app name after validation.
  190. Raises:
  191. Exit: if the app directory name is reflex or if the name is not standard for a python package name.
  192. """
  193. app_name = (
  194. app_name if app_name else os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  195. )
  196. # Make sure the app is not named "reflex".
  197. if app_name == constants.Reflex.MODULE_NAME:
  198. console.error(
  199. f"The app directory cannot be named [bold]{constants.Reflex.MODULE_NAME}[/bold]."
  200. )
  201. raise typer.Exit(1)
  202. # Make sure the app name is standard for a python package name.
  203. if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", app_name):
  204. console.error(
  205. "The app directory name must start with a letter and can contain letters, numbers, and underscores."
  206. )
  207. raise typer.Exit(1)
  208. return app_name
  209. def create_config(app_name: str):
  210. """Create a new rxconfig file.
  211. Args:
  212. app_name: The name of the app.
  213. """
  214. # Import here to avoid circular imports.
  215. from reflex.compiler import templates
  216. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  217. with open(constants.Config.FILE, "w") as f:
  218. console.debug(f"Creating {constants.Config.FILE}")
  219. f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
  220. def initialize_gitignore():
  221. """Initialize the template .gitignore file."""
  222. # The files to add to the .gitignore file.
  223. files = constants.GitIgnore.DEFAULTS
  224. # Subtract current ignored files.
  225. if os.path.exists(constants.GitIgnore.FILE):
  226. with open(constants.GitIgnore.FILE, "r") as f:
  227. files |= set([line.strip() for line in f.readlines()])
  228. # Write files to the .gitignore file.
  229. with open(constants.GitIgnore.FILE, "w") as f:
  230. console.debug(f"Creating {constants.GitIgnore.FILE}")
  231. f.write(f"{(path_ops.join(sorted(files))).lstrip()}")
  232. def initialize_requirements_txt():
  233. """Initialize the requirements.txt file.
  234. If absent, generate one for the user.
  235. If the requirements.txt does not have reflex as dependency,
  236. generate a requirement pinning current version and append to
  237. the requirements.txt file.
  238. """
  239. fp = Path(constants.RequirementsTxt.FILE)
  240. encoding = "utf-8"
  241. if not fp.exists():
  242. fp.touch()
  243. else:
  244. # Detect the encoding of the original file
  245. import charset_normalizer
  246. charset_matches = charset_normalizer.from_path(fp)
  247. maybe_charset_match = charset_matches.best()
  248. if maybe_charset_match is None:
  249. console.debug(f"Unable to detect encoding for {fp}, exiting.")
  250. return
  251. encoding = maybe_charset_match.encoding
  252. console.debug(f"Detected encoding for {fp} as {encoding}.")
  253. try:
  254. other_requirements_exist = False
  255. with open(fp, "r", encoding=encoding) as f:
  256. for req in f.readlines():
  257. # Check if we have a package name that is reflex
  258. if re.match(r"^reflex[^a-zA-Z0-9]", req):
  259. console.debug(f"{fp} already has reflex as dependency.")
  260. return
  261. other_requirements_exist = True
  262. with open(fp, "a", encoding=encoding) as f:
  263. preceding_newline = "\n" if other_requirements_exist else ""
  264. f.write(
  265. f"{preceding_newline}{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  266. )
  267. except Exception:
  268. console.info(f"Unable to check {fp} for reflex dependency.")
  269. def initialize_app_directory(app_name: str, template: constants.Templates.Kind):
  270. """Initialize the app directory on reflex init.
  271. Args:
  272. app_name: The name of the app.
  273. template: The template to use.
  274. """
  275. console.log("Initializing the app directory.")
  276. # Copy the template to the current directory.
  277. template_dir = Path(constants.Templates.Dirs.BASE, "apps", template.value)
  278. # Remove all pyc and __pycache__ dirs in template directory.
  279. for pyc_file in template_dir.glob("**/*.pyc"):
  280. pyc_file.unlink()
  281. for pycache_dir in template_dir.glob("**/__pycache__"):
  282. pycache_dir.rmdir()
  283. for file in template_dir.iterdir():
  284. # Copy the file to current directory but keep the name the same.
  285. path_ops.cp(str(file), file.name)
  286. # Rename the template app to the app name.
  287. path_ops.mv(constants.Templates.Dirs.CODE, app_name)
  288. path_ops.mv(
  289. os.path.join(app_name, template_dir.name + constants.Ext.PY),
  290. os.path.join(app_name, app_name + constants.Ext.PY),
  291. )
  292. # Fix up the imports.
  293. path_ops.find_replace(
  294. app_name,
  295. f"from {constants.Templates.Dirs.CODE}",
  296. f"from {app_name}",
  297. )
  298. def get_project_hash() -> int | None:
  299. """Get the project hash from the reflex.json file if the file exists.
  300. Returns:
  301. project_hash: The app hash.
  302. """
  303. if not os.path.exists(constants.Reflex.JSON):
  304. return None
  305. # Open and read the file
  306. with open(constants.Reflex.JSON, "r") as file:
  307. data = json.load(file)
  308. project_hash = data["project_hash"]
  309. return project_hash
  310. def initialize_web_directory():
  311. """Initialize the web directory on reflex init."""
  312. console.log("Initializing the web directory.")
  313. # Re-use the hash if one is already created, so we don't over-write it when running reflex init
  314. project_hash = get_project_hash()
  315. path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB)
  316. initialize_package_json()
  317. path_ops.mkdir(constants.Dirs.WEB_ASSETS)
  318. update_next_config()
  319. # Initialize the reflex json file.
  320. init_reflex_json(project_hash=project_hash)
  321. def _compile_package_json():
  322. return templates.PACKAGE_JSON.render(
  323. scripts={
  324. "dev": constants.PackageJson.Commands.DEV,
  325. "export": constants.PackageJson.Commands.EXPORT,
  326. "export_sitemap": constants.PackageJson.Commands.EXPORT_SITEMAP,
  327. "prod": constants.PackageJson.Commands.PROD,
  328. },
  329. dependencies=constants.PackageJson.DEPENDENCIES,
  330. dev_dependencies=constants.PackageJson.DEV_DEPENDENCIES,
  331. )
  332. def initialize_package_json():
  333. """Render and write in .web the package.json file."""
  334. output_path = constants.PackageJson.PATH
  335. code = _compile_package_json()
  336. with open(output_path, "w") as file:
  337. file.write(code)
  338. def init_reflex_json(project_hash: int | None):
  339. """Write the hash of the Reflex project to a REFLEX_JSON.
  340. Re-use the hash if one is already created, therefore do not
  341. overwrite it every time we run the reflex init command
  342. .
  343. Args:
  344. project_hash: The app hash.
  345. """
  346. if project_hash is not None:
  347. console.debug(f"Project hash is already set to {project_hash}.")
  348. else:
  349. # Get a random project hash.
  350. project_hash = random.getrandbits(128)
  351. console.debug(f"Setting project hash to {project_hash}.")
  352. # Write the hash and version to the reflex json file.
  353. reflex_json = {
  354. "version": constants.Reflex.VERSION,
  355. "project_hash": project_hash,
  356. }
  357. path_ops.update_json_file(constants.Reflex.JSON, reflex_json)
  358. def update_next_config(export=False):
  359. """Update Next.js config from Reflex config.
  360. Args:
  361. export: if the method run during reflex export.
  362. """
  363. next_config_file = os.path.join(constants.Dirs.WEB, constants.Next.CONFIG_FILE)
  364. next_config = _update_next_config(get_config(), export=export)
  365. with open(next_config_file, "w") as file:
  366. file.write(next_config)
  367. file.write("\n")
  368. def _update_next_config(config, export=False):
  369. next_config = {
  370. "basePath": config.frontend_path or "",
  371. "compress": config.next_compression,
  372. "reactStrictMode": True,
  373. "trailingSlash": True,
  374. }
  375. if export:
  376. next_config["output"] = "export"
  377. next_config["distDir"] = constants.Dirs.STATIC
  378. next_config_json = re.sub(r'"([^"]+)"(?=:)', r"\1", json.dumps(next_config))
  379. return f"module.exports = {next_config_json};"
  380. def remove_existing_bun_installation():
  381. """Remove existing bun installation."""
  382. console.debug("Removing existing bun installation.")
  383. if os.path.exists(get_config().bun_path):
  384. path_ops.rm(constants.Bun.ROOT_PATH)
  385. def download_and_run(url: str, *args, show_status: bool = False, **env):
  386. """Download and run a script.
  387. Args:
  388. url: The url of the script.
  389. args: The arguments to pass to the script.
  390. show_status: Whether to show the status of the script.
  391. env: The environment variables to use.
  392. """
  393. # Download the script
  394. console.debug(f"Downloading {url}")
  395. response = httpx.get(url)
  396. if response.status_code != httpx.codes.OK:
  397. response.raise_for_status()
  398. # Save the script to a temporary file.
  399. script = tempfile.NamedTemporaryFile()
  400. with open(script.name, "w") as f:
  401. f.write(response.text)
  402. # Run the script.
  403. env = {**os.environ, **env}
  404. process = processes.new_process(["bash", f.name, *args], env=env)
  405. show = processes.show_status if show_status else processes.show_logs
  406. show(f"Installing {url}", process)
  407. def download_and_extract_fnm_zip():
  408. """Download and run a script.
  409. Raises:
  410. Exit: If an error occurs while downloading or extracting the FNM zip.
  411. """
  412. # Download the zip file
  413. url = constants.Fnm.INSTALL_URL
  414. console.debug(f"Downloading {url}")
  415. fnm_zip_file = os.path.join(constants.Fnm.DIR, f"{constants.Fnm.FILENAME}.zip")
  416. # Function to download and extract the FNM zip release.
  417. try:
  418. # Download the FNM zip release.
  419. # TODO: show progress to improve UX
  420. with httpx.stream("GET", url, follow_redirects=True) as response:
  421. response.raise_for_status()
  422. with open(fnm_zip_file, "wb") as output_file:
  423. for chunk in response.iter_bytes():
  424. output_file.write(chunk)
  425. # Extract the downloaded zip file.
  426. with zipfile.ZipFile(fnm_zip_file, "r") as zip_ref:
  427. zip_ref.extractall(constants.Fnm.DIR)
  428. console.debug("FNM package downloaded and extracted successfully.")
  429. except Exception as e:
  430. console.error(f"An error occurred while downloading fnm package: {e}")
  431. raise typer.Exit(1) from e
  432. finally:
  433. # Clean up the downloaded zip file.
  434. path_ops.rm(fnm_zip_file)
  435. def install_node():
  436. """Install fnm and nodejs for use by Reflex.
  437. Independent of any existing system installations.
  438. """
  439. if not constants.Fnm.FILENAME:
  440. # fnm only support Linux, macOS and Windows distros.
  441. console.debug("")
  442. return
  443. path_ops.mkdir(constants.Fnm.DIR)
  444. if not os.path.exists(constants.Fnm.EXE):
  445. download_and_extract_fnm_zip()
  446. if constants.IS_WINDOWS:
  447. # Install node
  448. fnm_exe = Path(constants.Fnm.EXE).resolve()
  449. fnm_dir = Path(constants.Fnm.DIR).resolve()
  450. process = processes.new_process(
  451. [
  452. "powershell",
  453. "-Command",
  454. f'& "{fnm_exe}" install {constants.Node.VERSION} --fnm-dir "{fnm_dir}"',
  455. ],
  456. )
  457. else: # All other platforms (Linux, MacOS).
  458. # TODO we can skip installation if check_node_version() checks out
  459. # Add execute permissions to fnm executable.
  460. os.chmod(constants.Fnm.EXE, stat.S_IXUSR)
  461. # Install node.
  462. # Specify arm64 arch explicitly for M1s and M2s.
  463. architecture_arg = (
  464. ["--arch=arm64"]
  465. if platform.system() == "Darwin" and platform.machine() == "arm64"
  466. else []
  467. )
  468. process = processes.new_process(
  469. [
  470. constants.Fnm.EXE,
  471. "install",
  472. *architecture_arg,
  473. constants.Node.VERSION,
  474. "--fnm-dir",
  475. constants.Fnm.DIR,
  476. ],
  477. )
  478. processes.show_status("Installing node", process)
  479. def install_bun():
  480. """Install bun onto the user's system.
  481. Raises:
  482. FileNotFoundError: If required packages are not found.
  483. """
  484. # Bun is not supported on Windows.
  485. if constants.IS_WINDOWS:
  486. console.debug("Skipping bun installation on Windows.")
  487. return
  488. # Skip if bun is already installed.
  489. if os.path.exists(get_config().bun_path) and get_bun_version() == version.parse(
  490. constants.Bun.VERSION
  491. ):
  492. console.debug("Skipping bun installation as it is already installed.")
  493. return
  494. # if unzip is installed
  495. unzip_path = path_ops.which("unzip")
  496. if unzip_path is None:
  497. raise FileNotFoundError("Reflex requires unzip to be installed.")
  498. # Run the bun install script.
  499. download_and_run(
  500. constants.Bun.INSTALL_URL,
  501. f"bun-v{constants.Bun.VERSION}",
  502. BUN_INSTALL=constants.Bun.ROOT_PATH,
  503. )
  504. def _write_cached_procedure_file(payload: str, cache_file: str):
  505. with open(cache_file, "w") as f:
  506. f.write(payload)
  507. def _read_cached_procedure_file(cache_file: str) -> str | None:
  508. if os.path.exists(cache_file):
  509. with open(cache_file, "r") as f:
  510. return f.read()
  511. return None
  512. def _clear_cached_procedure_file(cache_file: str):
  513. if os.path.exists(cache_file):
  514. os.remove(cache_file)
  515. def cached_procedure(cache_file: str, payload_fn: Callable[..., str]):
  516. """Decorator to cache the runs of a procedure on disk. Procedures should not have
  517. a return value.
  518. Args:
  519. cache_file: The file to store the cache payload in.
  520. payload_fn: Function that computes cache payload from function args
  521. Returns:
  522. The decorated function.
  523. """
  524. def _inner_decorator(func):
  525. def _inner(*args, **kwargs):
  526. payload = _read_cached_procedure_file(cache_file)
  527. new_payload = payload_fn(*args, **kwargs)
  528. if payload != new_payload:
  529. _clear_cached_procedure_file(cache_file)
  530. func(*args, **kwargs)
  531. _write_cached_procedure_file(new_payload, cache_file)
  532. return _inner
  533. return _inner_decorator
  534. @cached_procedure(
  535. cache_file=os.path.join(
  536. constants.Dirs.WEB, "reflex.install_frontend_packages.cached"
  537. ),
  538. payload_fn=lambda p, c: f"{repr(sorted(list(p)))},{c.json()}",
  539. )
  540. def install_frontend_packages(packages: set[str], config: Config):
  541. """Installs the base and custom frontend packages.
  542. Args:
  543. packages: A list of package names to be installed.
  544. config: The config object.
  545. Example:
  546. >>> install_frontend_packages(["react", "react-dom"], get_config())
  547. """
  548. # Install the base packages.
  549. process = processes.new_process(
  550. [get_install_package_manager(), "install", "--loglevel", "silly"],
  551. cwd=constants.Dirs.WEB,
  552. shell=constants.IS_WINDOWS,
  553. )
  554. processes.show_status("Installing base frontend packages", process)
  555. if config.tailwind is not None:
  556. # install tailwind and tailwind plugins as dev dependencies.
  557. process = processes.new_process(
  558. [
  559. get_install_package_manager(),
  560. "add",
  561. "-d",
  562. constants.Tailwind.VERSION,
  563. *((config.tailwind or {}).get("plugins", [])),
  564. ],
  565. cwd=constants.Dirs.WEB,
  566. shell=constants.IS_WINDOWS,
  567. )
  568. processes.show_status("Installing tailwind", process)
  569. # Install custom packages defined in frontend_packages
  570. if len(packages) > 0:
  571. process = processes.new_process(
  572. [get_install_package_manager(), "add", *packages],
  573. cwd=constants.Dirs.WEB,
  574. shell=constants.IS_WINDOWS,
  575. )
  576. processes.show_status(
  577. "Installing frontend packages from config and components", process
  578. )
  579. def check_initialized(frontend: bool = True):
  580. """Check that the app is initialized.
  581. Args:
  582. frontend: Whether to check if the frontend is initialized.
  583. Raises:
  584. Exit: If the app is not initialized.
  585. """
  586. has_config = os.path.exists(constants.Config.FILE)
  587. has_reflex_dir = not frontend or os.path.exists(constants.Reflex.DIR)
  588. has_web_dir = not frontend or os.path.exists(constants.Dirs.WEB)
  589. # Check if the app is initialized.
  590. if not (has_config and has_reflex_dir and has_web_dir):
  591. console.error(
  592. f"The app is not initialized. Run [bold]{constants.Reflex.MODULE_NAME} init[/bold] first."
  593. )
  594. raise typer.Exit(1)
  595. # Check that the template is up to date.
  596. if frontend and not is_latest_template():
  597. console.error(
  598. "The base app template has updated. Run [bold]reflex init[/bold] again."
  599. )
  600. raise typer.Exit(1)
  601. # Print a warning for Windows users.
  602. if constants.IS_WINDOWS:
  603. console.warn(
  604. """Windows Subsystem for Linux (WSL) is recommended for improving initial install times."""
  605. )
  606. def is_latest_template() -> bool:
  607. """Whether the app is using the latest template.
  608. Returns:
  609. Whether the app is using the latest template.
  610. """
  611. if not os.path.exists(constants.Reflex.JSON):
  612. return False
  613. with open(constants.Reflex.JSON) as f: # type: ignore
  614. app_version = json.load(f)["version"]
  615. return app_version == constants.Reflex.VERSION
  616. def validate_bun():
  617. """Validate bun if a custom bun path is specified to ensure the bun version meets requirements.
  618. Raises:
  619. Exit: If custom specified bun does not exist or does not meet requirements.
  620. """
  621. # if a custom bun path is provided, make sure its valid
  622. # This is specific to non-FHS OS
  623. bun_path = get_config().bun_path
  624. if bun_path != constants.Bun.DEFAULT_PATH:
  625. bun_version = get_bun_version()
  626. if not bun_version:
  627. console.error(
  628. "Failed to obtain bun version. Make sure the specified bun path in your config is correct."
  629. )
  630. raise typer.Exit(1)
  631. elif bun_version < version.parse(constants.Bun.MIN_VERSION):
  632. console.error(
  633. f"Reflex requires bun version {constants.Bun.VERSION} or higher to run, but the detected version is "
  634. f"{bun_version}. If you have specified a custom bun path in your config, make sure to provide one "
  635. f"that satisfies the minimum version requirement."
  636. )
  637. raise typer.Exit(1)
  638. def validate_frontend_dependencies(init=True):
  639. """Validate frontend dependencies to ensure they meet requirements.
  640. Args:
  641. init: whether running `reflex init`
  642. Raises:
  643. Exit: If the package manager is invalid.
  644. """
  645. if not init:
  646. # we only need to validate the package manager when running app.
  647. # `reflex init` will install the deps anyway(if applied).
  648. package_manager = get_package_manager()
  649. if not package_manager:
  650. console.error(
  651. "Could not find NPM package manager. Make sure you have node installed."
  652. )
  653. raise typer.Exit(1)
  654. if not check_node_version():
  655. node_version = get_node_version()
  656. console.error(
  657. f"Reflex requires node version {constants.Node.MIN_VERSION} or higher to run, but the detected version is {node_version}",
  658. )
  659. raise typer.Exit(1)
  660. if constants.IS_WINDOWS:
  661. return
  662. if init:
  663. # we only need bun for package install on `reflex init`.
  664. validate_bun()
  665. def ensure_reflex_installation_id() -> Optional[int]:
  666. """Ensures that a reflex distinct id has been generated and stored in the reflex directory.
  667. Returns:
  668. Distinct id.
  669. """
  670. try:
  671. initialize_reflex_user_directory()
  672. installation_id_file = os.path.join(constants.Reflex.DIR, "installation_id")
  673. installation_id = None
  674. if os.path.exists(installation_id_file):
  675. try:
  676. with open(installation_id_file, "r") as f:
  677. installation_id = int(f.read())
  678. except Exception:
  679. # If anything goes wrong at all... just regenerate.
  680. # Like what? Examples:
  681. # - file not exists
  682. # - file not readable
  683. # - content not parseable as an int
  684. pass
  685. if installation_id is None:
  686. installation_id = random.getrandbits(128)
  687. with open(installation_id_file, "w") as f:
  688. f.write(str(installation_id))
  689. # If we get here, installation_id is definitely set
  690. return installation_id
  691. except Exception as e:
  692. console.debug(f"Failed to ensure reflex installation id: {e}")
  693. return None
  694. def initialize_reflex_user_directory():
  695. """Initialize the reflex user directory."""
  696. # Create the reflex directory.
  697. path_ops.mkdir(constants.Reflex.DIR)
  698. def initialize_frontend_dependencies():
  699. """Initialize all the frontend dependencies."""
  700. # validate dependencies before install
  701. validate_frontend_dependencies()
  702. # Install the frontend dependencies.
  703. processes.run_concurrently(install_node, install_bun)
  704. # Set up the web directory.
  705. initialize_web_directory()
  706. def check_db_initialized() -> bool:
  707. """Check if the database migrations are initialized.
  708. Returns:
  709. True if alembic is initialized (or if database is not used).
  710. """
  711. if get_config().db_url is not None and not Path(constants.ALEMBIC_CONFIG).exists():
  712. console.error(
  713. "Database is not initialized. Run [bold]reflex db init[/bold] first."
  714. )
  715. return False
  716. return True
  717. def check_schema_up_to_date():
  718. """Check if the sqlmodel metadata matches the current database schema."""
  719. if get_config().db_url is None or not Path(constants.ALEMBIC_CONFIG).exists():
  720. return
  721. with model.Model.get_db_engine().connect() as connection:
  722. try:
  723. if model.Model.alembic_autogenerate(
  724. connection=connection,
  725. write_migration_scripts=False,
  726. ):
  727. console.error(
  728. "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
  729. "to generate migration scripts.",
  730. )
  731. except CommandError as command_error:
  732. if "Target database is not up to date." in str(command_error):
  733. console.error(
  734. f"{command_error} Run [bold]reflex db migrate[/bold] to update database."
  735. )
  736. def prompt_for_template() -> constants.Templates.Kind:
  737. """Prompt the user to specify a template.
  738. Returns:
  739. The template the user selected.
  740. """
  741. # Show the user the URLs of each temlate to preview.
  742. console.print("\nGet started with a template:")
  743. console.print("blank (https://blank-template.reflex.run) - A minimal template.")
  744. console.print(
  745. "sidebar (https://sidebar-template.reflex.run) - A template with a sidebar to navigate pages."
  746. )
  747. console.print("")
  748. # Prompt the user to select a template.
  749. template = console.ask(
  750. "Which template would you like to use?",
  751. choices=[
  752. template.value
  753. for template in constants.Templates.Kind
  754. if template.value != "demo"
  755. ],
  756. default=constants.Templates.Kind.BLANK.value,
  757. )
  758. # Return the template.
  759. return constants.Templates.Kind(template)
  760. def should_show_rx_chakra_migration_instructions() -> bool:
  761. """Should we show the migration instructions for rx.chakra.* => rx.*?.
  762. Returns:
  763. bool: True if we should show the migration instructions.
  764. """
  765. if os.getenv("REFLEX_PROMPT_MIGRATE_TO_RX_CHAKRA") == "yes":
  766. return True
  767. with open(constants.Dirs.REFLEX_JSON, "r") as f:
  768. data = json.load(f)
  769. existing_init_reflex_version = data.get("version", None)
  770. if existing_init_reflex_version is None:
  771. # They clone a reflex app from git for the first time.
  772. # That app may or may not be 0.4 compatible.
  773. # So let's just show these instructions THIS TIME.
  774. return True
  775. if constants.Reflex.VERSION < "0.4":
  776. return False
  777. else:
  778. return existing_init_reflex_version < "0.4"
  779. def show_rx_chakra_migration_instructions():
  780. """Show the migration instructions for rx.chakra.* => rx.*."""
  781. console.log(
  782. "Prior to reflex 0.4.0, rx.* components are based on Chakra UI. They are now based on Radix UI. To stick to Chakra UI, use rx.chakra.*."
  783. )
  784. console.log("")
  785. console.log(
  786. "[bold]Run `reflex script keep-chakra` to automatically update your app."
  787. )
  788. console.log("")
  789. console.log("For more details, please see https://TODO") # TODO add link to docs
  790. def migrate_to_rx_chakra():
  791. """Migrate rx.button => r.chakra.button, etc."""
  792. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  793. file_list = glob.glob(file_pattern, recursive=True)
  794. # Populate with all rx.<x> components that have been moved to rx.chakra.<x>
  795. patterns = {
  796. rf"\brx\.{name}\b": f"rx.chakra.{name}"
  797. for name in _get_rx_chakra_component_to_migrate()
  798. }
  799. for file_path in file_list:
  800. with FileInput(file_path, inplace=True) as file:
  801. for _line_num, line in enumerate(file):
  802. for old, new in patterns.items():
  803. line = re.sub(old, new, line)
  804. print(line, end="")
  805. def _get_rx_chakra_component_to_migrate() -> set[str]:
  806. from reflex.components.chakra import ChakraComponent
  807. rx_chakra_names = set(dir(reflex.chakra))
  808. names_to_migrate = set()
  809. # whitelist names will always be rewritten as rx.chakra.<x>
  810. whitelist = {
  811. "ColorModeIcon",
  812. "MultiSelect",
  813. "MultiSelectOption",
  814. "color_mode_icon",
  815. "multi_select",
  816. "multi_select_option",
  817. }
  818. for rx_chakra_name in sorted(rx_chakra_names):
  819. if rx_chakra_name.startswith("_"):
  820. continue
  821. rx_chakra_object = getattr(reflex.chakra, rx_chakra_name)
  822. try:
  823. if (
  824. inspect.ismethod(rx_chakra_object)
  825. and inspect.isclass(rx_chakra_object.__self__)
  826. and issubclass(rx_chakra_object.__self__, ChakraComponent)
  827. ):
  828. names_to_migrate.add(rx_chakra_name)
  829. elif inspect.isclass(rx_chakra_object) and issubclass(
  830. rx_chakra_object, ChakraComponent
  831. ):
  832. names_to_migrate.add(rx_chakra_name)
  833. elif rx_chakra_name in whitelist:
  834. names_to_migrate.add(rx_chakra_name)
  835. except Exception:
  836. raise
  837. return names_to_migrate
  838. def migrate_to_reflex():
  839. """Migration from Pynecone to Reflex."""
  840. # Check if the old config file exists.
  841. if not os.path.exists(constants.Config.PREVIOUS_FILE):
  842. return
  843. # Ask the user if they want to migrate.
  844. action = console.ask(
  845. "Pynecone project detected. Automatically upgrade to Reflex?",
  846. choices=["y", "n"],
  847. )
  848. if action == "n":
  849. return
  850. # Rename pcconfig to rxconfig.
  851. console.log(
  852. f"[bold]Renaming {constants.Config.PREVIOUS_FILE} to {constants.Config.FILE}"
  853. )
  854. os.rename(constants.Config.PREVIOUS_FILE, constants.Config.FILE)
  855. # Find all python files in the app directory.
  856. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  857. file_list = glob.glob(file_pattern, recursive=True)
  858. # Add the config file to the list of files to be migrated.
  859. file_list.append(constants.Config.FILE)
  860. # Migrate all files.
  861. updates = {
  862. "Pynecone": "Reflex",
  863. "pynecone as pc": "reflex as rx",
  864. "pynecone.io": "reflex.dev",
  865. "pynecone": "reflex",
  866. "pc.": "rx.",
  867. "pcconfig": "rxconfig",
  868. }
  869. for file_path in file_list:
  870. with FileInput(file_path, inplace=True) as file:
  871. for line in file:
  872. for old, new in updates.items():
  873. line = line.replace(old, new)
  874. print(line, end="")