prerequisites.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import glob
  4. import importlib
  5. import json
  6. import os
  7. import platform
  8. import random
  9. import re
  10. import stat
  11. import sys
  12. import tempfile
  13. import zipfile
  14. from fileinput import FileInput
  15. from pathlib import Path
  16. from types import ModuleType
  17. import httpx
  18. import typer
  19. from alembic.util.exc import CommandError
  20. from packaging import version
  21. from redis.asyncio import Redis
  22. from reflex import constants, model
  23. from reflex.compiler import templates
  24. from reflex.config import Config, get_config
  25. from reflex.utils import console, path_ops, processes
  26. def get_node_version() -> version.Version | None:
  27. """Get the version of node.
  28. Returns:
  29. The version of node.
  30. """
  31. try:
  32. result = processes.new_process([path_ops.get_node_path(), "-v"], run=True)
  33. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  34. return version.parse(result.stdout) # type: ignore
  35. except (FileNotFoundError, TypeError):
  36. return None
  37. def get_fnm_version() -> version.Version | None:
  38. """Get the version of fnm.
  39. Returns:
  40. The version of FNM.
  41. """
  42. try:
  43. result = processes.new_process([constants.Fnm.EXE, "--version"], run=True)
  44. return version.parse(result.stdout.split(" ")[1]) # type: ignore
  45. except (FileNotFoundError, TypeError):
  46. return None
  47. def get_bun_version() -> version.Version | None:
  48. """Get the version of bun.
  49. Returns:
  50. The version of bun.
  51. """
  52. try:
  53. # Run the bun -v command and capture the output
  54. result = processes.new_process([get_config().bun_path, "-v"], run=True)
  55. return version.parse(result.stdout) # type: ignore
  56. except FileNotFoundError:
  57. return None
  58. def get_package_manager() -> str | None:
  59. """Get the package manager executable for installation.
  60. Currently on unix systems, bun is used for installation only.
  61. Returns:
  62. The path to the package manager.
  63. """
  64. # On Windows or lower linux kernels(WSL1), we use npm instead of bun.
  65. if constants.IS_WINDOWS or constants.IS_LINUX and not is_valid_linux():
  66. return get_npm_package_manager()
  67. # On other platforms, we use bun.
  68. return get_config().bun_path
  69. def get_install_package_manager() -> str | None:
  70. """Get package manager to install dependencies.
  71. Returns:
  72. Path to install package manager.
  73. """
  74. if constants.IS_WINDOWS:
  75. return get_npm_package_manager()
  76. return get_config().bun_path
  77. def get_npm_package_manager() -> str | None:
  78. """Get the npm package manager executable for installing and running app
  79. on windows.
  80. Returns:
  81. The path to the package manager.
  82. """
  83. npm_path = path_ops.get_npm_path()
  84. if npm_path is not None:
  85. npm_path = str(Path(npm_path).resolve())
  86. return npm_path
  87. def get_app(reload: bool = False) -> ModuleType:
  88. """Get the app module based on the default config.
  89. Args:
  90. reload: Re-import the app module from disk
  91. Returns:
  92. The app based on the default config.
  93. """
  94. config = get_config()
  95. module = ".".join([config.app_name, config.app_name])
  96. sys.path.insert(0, os.getcwd())
  97. app = __import__(module, fromlist=(constants.CompileVars.APP,))
  98. if reload:
  99. importlib.reload(app)
  100. return app
  101. def get_redis() -> Redis | None:
  102. """Get the redis client.
  103. Returns:
  104. The redis client.
  105. """
  106. config = get_config()
  107. if not config.redis_url:
  108. return None
  109. redis_url, has_port, redis_port = config.redis_url.partition(":")
  110. if not has_port:
  111. redis_port = 6379
  112. console.info(f"Using redis at {config.redis_url}")
  113. return Redis(host=redis_url, port=int(redis_port), db=0)
  114. def get_production_backend_url() -> str:
  115. """Get the production backend URL.
  116. Returns:
  117. The production backend URL.
  118. """
  119. config = get_config()
  120. return constants.PRODUCTION_BACKEND_URL.format(
  121. username=config.username,
  122. app_name=config.app_name,
  123. )
  124. def get_default_app_name() -> str:
  125. """Get the default app name.
  126. The default app name is the name of the current directory.
  127. Returns:
  128. The default app name.
  129. Raises:
  130. Exit: if the app directory name is reflex.
  131. """
  132. app_name = os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  133. # Make sure the app is not named "reflex".
  134. if app_name == constants.Reflex.MODULE_NAME:
  135. console.error(
  136. f"The app directory cannot be named [bold]{constants.Reflex.MODULE_NAME}[/bold]."
  137. )
  138. raise typer.Exit(1)
  139. return app_name
  140. def create_config(app_name: str):
  141. """Create a new rxconfig file.
  142. Args:
  143. app_name: The name of the app.
  144. """
  145. # Import here to avoid circular imports.
  146. from reflex.compiler import templates
  147. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  148. with open(constants.Config.FILE, "w") as f:
  149. console.debug(f"Creating {constants.Config.FILE}")
  150. f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
  151. def initialize_gitignore():
  152. """Initialize the template .gitignore file."""
  153. # The files to add to the .gitignore file.
  154. files = constants.GitIgnore.DEFAULTS
  155. # Subtract current ignored files.
  156. if os.path.exists(constants.GitIgnore.FILE):
  157. with open(constants.GitIgnore.FILE, "r") as f:
  158. files |= set([line.strip() for line in f.readlines()])
  159. # Write files to the .gitignore file.
  160. with open(constants.GitIgnore.FILE, "w") as f:
  161. console.debug(f"Creating {constants.GitIgnore.FILE}")
  162. f.write(f"{(path_ops.join(sorted(files))).lstrip()}")
  163. def initialize_requirements_txt():
  164. """Initialize the requirements.txt file.
  165. If absent, generate one for the user.
  166. If the requirements.txt does not have reflex as dependency,
  167. generate a requirement pinning current version and append to
  168. the requirements.txt file.
  169. """
  170. fp = Path(constants.RequirementsTxt.FILE)
  171. fp.touch(exist_ok=True)
  172. try:
  173. with open(fp, "r") as f:
  174. for req in f.readlines():
  175. # Check if we have a package name that is reflex
  176. if re.match(r"^reflex[^a-zA-Z0-9]", req):
  177. console.debug(f"{fp} already has reflex as dependency.")
  178. return
  179. with open(fp, "a") as f:
  180. f.write(
  181. f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  182. )
  183. except Exception:
  184. console.info(f"Unable to check {fp} for reflex dependency.")
  185. def initialize_app_directory(app_name: str, template: constants.Templates.Kind):
  186. """Initialize the app directory on reflex init.
  187. Args:
  188. app_name: The name of the app.
  189. template: The template to use.
  190. """
  191. console.log("Initializing the app directory.")
  192. # Copy the template to the current directory.
  193. template_dir = Path(constants.Templates.Dirs.BASE, "apps", template.value)
  194. # Remove all pyc and __pycache__ dirs in template directory.
  195. for pyc_file in template_dir.glob("**/*.pyc"):
  196. pyc_file.unlink()
  197. for pycache_dir in template_dir.glob("**/__pycache__"):
  198. pycache_dir.rmdir()
  199. for file in template_dir.iterdir():
  200. # Copy the file to current directory but keep the name the same.
  201. path_ops.cp(str(file), file.name)
  202. # Rename the template app to the app name.
  203. path_ops.mv(constants.Templates.Dirs.CODE, app_name)
  204. path_ops.mv(
  205. os.path.join(app_name, template_dir.name + constants.Ext.PY),
  206. os.path.join(app_name, app_name + constants.Ext.PY),
  207. )
  208. # Fix up the imports.
  209. path_ops.find_replace(
  210. app_name,
  211. f"from {constants.Templates.Dirs.CODE}",
  212. f"from {app_name}",
  213. )
  214. def initialize_web_directory():
  215. """Initialize the web directory on reflex init."""
  216. console.log("Initializing the web directory.")
  217. path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB)
  218. initialize_package_json()
  219. path_ops.mkdir(constants.Dirs.WEB_ASSETS)
  220. # update nextJS config based on rxConfig
  221. next_config_file = os.path.join(constants.Dirs.WEB, constants.Next.CONFIG_FILE)
  222. with open(next_config_file, "r") as file:
  223. next_config = file.read()
  224. next_config = update_next_config(next_config, get_config())
  225. with open(next_config_file, "w") as file:
  226. file.write(next_config)
  227. # Initialize the reflex json file.
  228. init_reflex_json()
  229. def _compile_package_json():
  230. return templates.PACKAGE_JSON.render(
  231. scripts={
  232. "dev": constants.PackageJson.Commands.DEV,
  233. "export": constants.PackageJson.Commands.EXPORT,
  234. "export_sitemap": constants.PackageJson.Commands.EXPORT_SITEMAP,
  235. "prod": constants.PackageJson.Commands.PROD,
  236. },
  237. dependencies=constants.PackageJson.DEPENDENCIES,
  238. dev_dependencies=constants.PackageJson.DEV_DEPENDENCIES,
  239. )
  240. def initialize_package_json():
  241. """Render and write in .web the package.json file."""
  242. output_path = constants.PackageJson.PATH
  243. code = _compile_package_json()
  244. with open(output_path, "w") as file:
  245. file.write(code)
  246. def init_reflex_json():
  247. """Write the hash of the Reflex project to a REFLEX_JSON."""
  248. # Get a random project hash.
  249. project_hash = random.getrandbits(128)
  250. console.debug(f"Setting project hash to {project_hash}.")
  251. # Write the hash and version to the reflex json file.
  252. reflex_json = {
  253. "version": constants.Reflex.VERSION,
  254. "project_hash": project_hash,
  255. }
  256. path_ops.update_json_file(constants.Reflex.JSON, reflex_json)
  257. def update_next_config(next_config: str, config: Config) -> str:
  258. """Update Next.js config from Reflex config. Is its own function for testing.
  259. Args:
  260. next_config: Content of next.config.js.
  261. config: A reflex Config object.
  262. Returns:
  263. The next_config updated from config.
  264. """
  265. next_config = re.sub(
  266. "compress: (true|false)",
  267. f'compress: {"true" if config.next_compression else "false"}',
  268. next_config,
  269. )
  270. next_config = re.sub(
  271. 'basePath: ".*?"',
  272. f'basePath: "{config.frontend_path or ""}"',
  273. next_config,
  274. )
  275. return next_config
  276. def download_and_run(url: str, *args, show_status: bool = False, **env):
  277. """Download and run a script.
  278. Args:
  279. url: The url of the script.
  280. args: The arguments to pass to the script.
  281. show_status: Whether to show the status of the script.
  282. env: The environment variables to use.
  283. """
  284. # Download the script
  285. console.debug(f"Downloading {url}")
  286. response = httpx.get(url)
  287. if response.status_code != httpx.codes.OK:
  288. response.raise_for_status()
  289. # Save the script to a temporary file.
  290. script = tempfile.NamedTemporaryFile()
  291. with open(script.name, "w") as f:
  292. f.write(response.text)
  293. # Run the script.
  294. env = {**os.environ, **env}
  295. process = processes.new_process(["bash", f.name, *args], env=env)
  296. show = processes.show_status if show_status else processes.show_logs
  297. show(f"Installing {url}", process)
  298. def download_and_extract_fnm_zip():
  299. """Download and run a script.
  300. Raises:
  301. Exit: If an error occurs while downloading or extracting the FNM zip.
  302. """
  303. # Download the zip file
  304. url = constants.Fnm.INSTALL_URL
  305. console.debug(f"Downloading {url}")
  306. fnm_zip_file = os.path.join(constants.Fnm.DIR, f"{constants.Fnm.FILENAME}.zip")
  307. # Function to download and extract the FNM zip release.
  308. try:
  309. # Download the FNM zip release.
  310. # TODO: show progress to improve UX
  311. with httpx.stream("GET", url, follow_redirects=True) as response:
  312. response.raise_for_status()
  313. with open(fnm_zip_file, "wb") as output_file:
  314. for chunk in response.iter_bytes():
  315. output_file.write(chunk)
  316. # Extract the downloaded zip file.
  317. with zipfile.ZipFile(fnm_zip_file, "r") as zip_ref:
  318. zip_ref.extractall(constants.Fnm.DIR)
  319. console.debug("FNM package downloaded and extracted successfully.")
  320. except Exception as e:
  321. console.error(f"An error occurred while downloading fnm package: {e}")
  322. raise typer.Exit(1) from e
  323. finally:
  324. # Clean up the downloaded zip file.
  325. path_ops.rm(fnm_zip_file)
  326. def install_node():
  327. """Install fnm and nodejs for use by Reflex."""
  328. if constants.IS_WINDOWS or constants.IS_LINUX and not is_valid_linux():
  329. path_ops.mkdir(constants.Fnm.DIR)
  330. if not os.path.exists(constants.Fnm.EXE):
  331. download_and_extract_fnm_zip()
  332. if constants.IS_WINDOWS:
  333. # Install node
  334. fnm_exe = Path(constants.Fnm.EXE).resolve()
  335. fnm_dir = Path(constants.Fnm.DIR).resolve()
  336. process = processes.new_process(
  337. [
  338. "powershell",
  339. "-Command",
  340. f'& "{fnm_exe}" install {constants.Node.VERSION} --fnm-dir "{fnm_dir}"',
  341. ],
  342. )
  343. else: # All other platforms (Linux, WSL1).
  344. # Add execute permissions to fnm executable.
  345. os.chmod(constants.Fnm.EXE, stat.S_IXUSR)
  346. # Install node.
  347. process = processes.new_process(
  348. [
  349. constants.Fnm.EXE,
  350. "install",
  351. constants.Node.VERSION,
  352. "--fnm-dir",
  353. constants.Fnm.DIR,
  354. ],
  355. )
  356. processes.show_status("Installing node", process)
  357. def install_bun():
  358. """Install bun onto the user's system.
  359. Raises:
  360. FileNotFoundError: If required packages are not found.
  361. """
  362. # Bun is not supported on Windows.
  363. if constants.IS_WINDOWS:
  364. console.debug("Skipping bun installation on Windows.")
  365. return
  366. # Skip if bun is already installed.
  367. if os.path.exists(get_config().bun_path):
  368. console.debug("Skipping bun installation as it is already installed.")
  369. return
  370. # if unzip is installed
  371. unzip_path = path_ops.which("unzip")
  372. if unzip_path is None:
  373. raise FileNotFoundError("Reflex requires unzip to be installed.")
  374. # Run the bun install script.
  375. download_and_run(
  376. constants.Bun.INSTALL_URL,
  377. f"bun-v{constants.Bun.VERSION}",
  378. BUN_INSTALL=constants.Bun.ROOT_PATH,
  379. )
  380. def install_frontend_packages(packages: set[str]):
  381. """Installs the base and custom frontend packages.
  382. Args:
  383. packages: A list of package names to be installed.
  384. Example:
  385. >>> install_frontend_packages(["react", "react-dom"])
  386. """
  387. # Install the base packages.
  388. process = processes.new_process(
  389. [get_install_package_manager(), "install", "--loglevel", "silly"],
  390. cwd=constants.Dirs.WEB,
  391. shell=constants.IS_WINDOWS,
  392. )
  393. processes.show_status("Installing base frontend packages", process)
  394. config = get_config()
  395. if config.tailwind is not None:
  396. # install tailwind and tailwind plugins as dev dependencies.
  397. process = processes.new_process(
  398. [
  399. get_install_package_manager(),
  400. "add",
  401. "-d",
  402. constants.Tailwind.VERSION,
  403. *((config.tailwind or {}).get("plugins", [])),
  404. ],
  405. cwd=constants.Dirs.WEB,
  406. shell=constants.IS_WINDOWS,
  407. )
  408. processes.show_status("Installing tailwind", process)
  409. # Install custom packages defined in frontend_packages
  410. if len(packages) > 0:
  411. process = processes.new_process(
  412. [get_install_package_manager(), "add", *packages],
  413. cwd=constants.Dirs.WEB,
  414. shell=constants.IS_WINDOWS,
  415. )
  416. processes.show_status(
  417. "Installing frontend packages from config and components", process
  418. )
  419. def check_initialized(frontend: bool = True):
  420. """Check that the app is initialized.
  421. Args:
  422. frontend: Whether to check if the frontend is initialized.
  423. Raises:
  424. Exit: If the app is not initialized.
  425. """
  426. has_config = os.path.exists(constants.Config.FILE)
  427. has_reflex_dir = not frontend or os.path.exists(constants.Reflex.DIR)
  428. has_web_dir = not frontend or os.path.exists(constants.Dirs.WEB)
  429. # Check if the app is initialized.
  430. if not (has_config and has_reflex_dir and has_web_dir):
  431. console.error(
  432. f"The app is not initialized. Run [bold]{constants.Reflex.MODULE_NAME} init[/bold] first."
  433. )
  434. raise typer.Exit(1)
  435. # Check that the template is up to date.
  436. if frontend and not is_latest_template():
  437. console.error(
  438. "The base app template has updated. Run [bold]reflex init[/bold] again."
  439. )
  440. raise typer.Exit(1)
  441. # Print a warning for Windows users.
  442. if constants.IS_WINDOWS:
  443. console.warn(
  444. """Windows Subsystem for Linux (WSL) is recommended for improving initial install times."""
  445. )
  446. def is_latest_template() -> bool:
  447. """Whether the app is using the latest template.
  448. Returns:
  449. Whether the app is using the latest template.
  450. """
  451. if not os.path.exists(constants.Reflex.JSON):
  452. return False
  453. with open(constants.Reflex.JSON) as f: # type: ignore
  454. app_version = json.load(f)["version"]
  455. return app_version == constants.Reflex.VERSION
  456. def validate_bun():
  457. """Validate bun if a custom bun path is specified to ensure the bun version meets requirements.
  458. Raises:
  459. Exit: If custom specified bun does not exist or does not meet requirements.
  460. """
  461. # if a custom bun path is provided, make sure its valid
  462. # This is specific to non-FHS OS
  463. bun_path = get_config().bun_path
  464. if bun_path != constants.Bun.DEFAULT_PATH:
  465. bun_version = get_bun_version()
  466. if not bun_version:
  467. console.error(
  468. "Failed to obtain bun version. Make sure the specified bun path in your config is correct."
  469. )
  470. raise typer.Exit(1)
  471. elif bun_version < version.parse(constants.Bun.MIN_VERSION):
  472. console.error(
  473. f"Reflex requires bun version {constants.Bun.VERSION} or higher to run, but the detected version is "
  474. f"{bun_version}. If you have specified a custom bun path in your config, make sure to provide one "
  475. f"that satisfies the minimum version requirement."
  476. )
  477. raise typer.Exit(1)
  478. def validate_node():
  479. """Check the version of Node.js is correct.
  480. Raises:
  481. Exit: If the version of Node.js is incorrect.
  482. """
  483. current_version = get_node_version()
  484. # Check if Node is installed.
  485. if not current_version:
  486. console.error(
  487. "Failed to obtain node version. Make sure node is installed and in your PATH."
  488. )
  489. raise typer.Exit(1)
  490. # Check if the version of Node is correct.
  491. if current_version < version.parse(constants.Node.MIN_VERSION):
  492. console.error(
  493. f"Reflex requires node version {constants.Node.MIN_VERSION} or higher to run, but the detected version is {current_version}."
  494. )
  495. raise typer.Exit(1)
  496. def remove_existing_fnm_dir():
  497. """Remove existing fnm directory on linux and mac."""
  498. if os.path.exists(constants.Fnm.DIR):
  499. console.debug("Removing existing fnm installation.")
  500. path_ops.rm(constants.Fnm.DIR)
  501. def validate_frontend_dependencies():
  502. """Validate frontend dependencies to ensure they meet requirements."""
  503. # Bun only supports linux and Mac. For Non-linux-or-mac, we use node.
  504. validate_bun() if constants.IS_LINUX_OR_MAC else validate_node()
  505. def parse_non_semver_version(version_string: str) -> version.Version | None:
  506. """Parse unsemantic version string and produce
  507. a clean version that confirms to packaging.version.
  508. Args:
  509. version_string: The version string
  510. Returns:
  511. A cleaned semantic packaging.version object.
  512. """
  513. # Remove non-numeric characters from the version string
  514. cleaned_version_string = re.sub(r"[^\d.]+", "", version_string)
  515. try:
  516. parsed_version = version.parse(cleaned_version_string)
  517. return parsed_version
  518. except version.InvalidVersion:
  519. console.debug(f"could not parse version: {version_string}")
  520. return None
  521. def is_valid_linux() -> bool:
  522. """Check if the linux kernel version is valid enough to use bun.
  523. This is typically used run npm at runtime for WSL 1 or lower linux versions.
  524. Returns:
  525. If linux kernel version is valid enough.
  526. """
  527. if not constants.IS_LINUX:
  528. return False
  529. kernel_string = platform.release()
  530. kv = parse_non_semver_version(kernel_string)
  531. return kv.major > 5 or (kv.major == 5 and kv.minor >= 10) if kv else False
  532. def initialize_frontend_dependencies():
  533. """Initialize all the frontend dependencies."""
  534. # Create the reflex directory.
  535. path_ops.mkdir(constants.Reflex.DIR)
  536. # Install the frontend dependencies.
  537. processes.run_concurrently(install_node, install_bun)
  538. # Set up the web directory.
  539. initialize_web_directory()
  540. # remove existing fnm dir on linux and mac
  541. if constants.IS_LINUX_OR_MAC and is_valid_linux():
  542. remove_existing_fnm_dir()
  543. def check_db_initialized() -> bool:
  544. """Check if the database migrations are initialized.
  545. Returns:
  546. True if alembic is initialized (or if database is not used).
  547. """
  548. if get_config().db_url is not None and not Path(constants.ALEMBIC_CONFIG).exists():
  549. console.error(
  550. "Database is not initialized. Run [bold]reflex db init[/bold] first."
  551. )
  552. return False
  553. return True
  554. def check_schema_up_to_date():
  555. """Check if the sqlmodel metadata matches the current database schema."""
  556. if get_config().db_url is None or not Path(constants.ALEMBIC_CONFIG).exists():
  557. return
  558. with model.Model.get_db_engine().connect() as connection:
  559. try:
  560. if model.Model.alembic_autogenerate(
  561. connection=connection,
  562. write_migration_scripts=False,
  563. ):
  564. console.error(
  565. "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
  566. "to generate migration scripts.",
  567. )
  568. except CommandError as command_error:
  569. if "Target database is not up to date." in str(command_error):
  570. console.error(
  571. f"{command_error} Run [bold]reflex db migrate[/bold] to update database."
  572. )
  573. def prompt_for_template() -> constants.Templates.Kind:
  574. """Prompt the user to specify a template.
  575. Returns:
  576. The template the user selected.
  577. """
  578. # Show the user the URLs of each temlate to preview.
  579. console.print("\nGet started with a template:")
  580. console.print("blank (https://blank-template.reflex.run) - A minimal template.")
  581. console.print(
  582. "sidebar (https://sidebar-template.reflex.run) - A template with a sidebar to navigate pages."
  583. )
  584. console.print("")
  585. # Prompt the user to select a template.
  586. template = console.ask(
  587. "Which template would you like to use?",
  588. choices=[
  589. template.value
  590. for template in constants.Templates.Kind
  591. if template.value != "demo"
  592. ],
  593. default=constants.Templates.Kind.BLANK.value,
  594. )
  595. # Return the template.
  596. return constants.Templates.Kind(template)
  597. def migrate_to_reflex():
  598. """Migration from Pynecone to Reflex."""
  599. # Check if the old config file exists.
  600. if not os.path.exists(constants.Config.PREVIOUS_FILE):
  601. return
  602. # Ask the user if they want to migrate.
  603. action = console.ask(
  604. "Pynecone project detected. Automatically upgrade to Reflex?",
  605. choices=["y", "n"],
  606. )
  607. if action == "n":
  608. return
  609. # Rename pcconfig to rxconfig.
  610. console.log(
  611. f"[bold]Renaming {constants.Config.PREVIOUS_FILE} to {constants.Config.FILE}"
  612. )
  613. os.rename(constants.Config.PREVIOUS_FILE, constants.Config.FILE)
  614. # Find all python files in the app directory.
  615. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  616. file_list = glob.glob(file_pattern, recursive=True)
  617. # Add the config file to the list of files to be migrated.
  618. file_list.append(constants.Config.FILE)
  619. # Migrate all files.
  620. updates = {
  621. "Pynecone": "Reflex",
  622. "pynecone as pc": "reflex as rx",
  623. "pynecone.io": "reflex.dev",
  624. "pynecone": "reflex",
  625. "pc.": "rx.",
  626. "pcconfig": "rxconfig",
  627. }
  628. for file_path in file_list:
  629. with FileInput(file_path, inplace=True) as file:
  630. for line in file:
  631. for old, new in updates.items():
  632. line = line.replace(old, new)
  633. print(line, end="")