build.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. """Building the app and initializing all prerequisites."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. import subprocess
  6. import zipfile
  7. from enum import Enum
  8. from pathlib import Path
  9. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  10. from reflex import constants
  11. from reflex.config import get_config
  12. from reflex.utils import console, path_ops, prerequisites, processes
  13. def set_env_json():
  14. """Write the upload url to a REFLEX_JSON."""
  15. path_ops.update_json_file(
  16. constants.ENV_JSON,
  17. {endpoint.name: endpoint.get_url() for endpoint in constants.Endpoint},
  18. )
  19. def set_os_env(**kwargs):
  20. """Set os environment variables.
  21. Args:
  22. kwargs: env key word args.
  23. """
  24. for key, value in kwargs.items():
  25. if not value:
  26. continue
  27. os.environ[key.upper()] = value
  28. def generate_sitemap_config(deploy_url: str):
  29. """Generate the sitemap config file.
  30. Args:
  31. deploy_url: The URL of the deployed app.
  32. """
  33. # Import here to avoid circular imports.
  34. from reflex.compiler import templates
  35. config = json.dumps(
  36. {
  37. "siteUrl": deploy_url,
  38. "generateRobotsTxt": True,
  39. }
  40. )
  41. with open(constants.SITEMAP_CONFIG_FILE, "w") as f:
  42. f.write(templates.SITEMAP_CONFIG(config=config))
  43. class _ComponentName(Enum):
  44. BACKEND = "Backend"
  45. FRONTEND = "Frontend"
  46. def _zip(
  47. component_name: _ComponentName,
  48. target: str,
  49. root_dir: str,
  50. dirs_to_exclude: set[str] | None = None,
  51. files_to_exclude: set[str] | None = None,
  52. ) -> None:
  53. """Zip utility function.
  54. Args:
  55. component_name: The name of the component: backend or frontend.
  56. target: The target zip file.
  57. root_dir: The root directory to zip.
  58. dirs_to_exclude: The directories to exclude.
  59. files_to_exclude: The files to exclude.
  60. """
  61. dirs_to_exclude = dirs_to_exclude or set()
  62. files_to_exclude = files_to_exclude or set()
  63. files_to_zip: list[str] = []
  64. # Traverse the root directory in a top-down manner. In this traversal order,
  65. # we can modify the dirs list in-place to remove directories we don't want to include.
  66. for root, dirs, files in os.walk(root_dir, topdown=True):
  67. # Modify the dirs in-place so excluded and hidden directories are skipped in next traversal.
  68. dirs[:] = [
  69. d
  70. for d in dirs
  71. if (basename := os.path.basename(os.path.normpath(d)))
  72. not in dirs_to_exclude
  73. and not basename.startswith(".")
  74. ]
  75. # Modify the files in-place so the hidden files are excluded.
  76. files[:] = [f for f in files if not f.startswith(".")]
  77. files_to_zip += [
  78. os.path.join(root, file) for file in files if file not in files_to_exclude
  79. ]
  80. # Create a progress bar for zipping the component.
  81. progress = Progress(
  82. *Progress.get_default_columns()[:-1],
  83. MofNCompleteColumn(),
  84. TimeElapsedColumn(),
  85. )
  86. task = progress.add_task(
  87. f"Zipping {component_name.value}:", total=len(files_to_zip)
  88. )
  89. with progress, zipfile.ZipFile(target, "w", zipfile.ZIP_DEFLATED) as zipf:
  90. for file in files_to_zip:
  91. console.debug(f"{target}: {file}")
  92. progress.advance(task)
  93. zipf.write(file, os.path.relpath(file, root_dir))
  94. def export(
  95. backend: bool = True,
  96. frontend: bool = True,
  97. zip: bool = False,
  98. deploy_url: str | None = None,
  99. ):
  100. """Export the app for deployment.
  101. Args:
  102. backend: Whether to zip up the backend app.
  103. frontend: Whether to zip up the frontend app.
  104. zip: Whether to zip the app.
  105. deploy_url: The URL of the deployed app.
  106. """
  107. # Remove the static folder.
  108. path_ops.rm(constants.WEB_STATIC_DIR)
  109. # The export command to run.
  110. command = "export"
  111. if frontend:
  112. # Generate a sitemap if a deploy URL is provided.
  113. if deploy_url is not None:
  114. generate_sitemap_config(deploy_url)
  115. command = "export-sitemap"
  116. checkpoints = [
  117. "Linting and checking ",
  118. "Compiled successfully",
  119. "Route (pages)",
  120. "Collecting page data",
  121. "automatically rendered as static HTML",
  122. 'Copying "static build" directory',
  123. 'Copying "public" directory',
  124. "Finalizing page optimization",
  125. "Export successful",
  126. ]
  127. # Start the subprocess with the progress bar.
  128. process = processes.new_process(
  129. [prerequisites.get_package_manager(), "run", command],
  130. cwd=constants.WEB_DIR,
  131. shell=constants.IS_WINDOWS,
  132. )
  133. processes.show_progress("Creating Production Build", process, checkpoints)
  134. # Zip up the app.
  135. if zip:
  136. files_to_exclude = {constants.FRONTEND_ZIP, constants.BACKEND_ZIP}
  137. if frontend:
  138. _zip(
  139. component_name=_ComponentName.FRONTEND,
  140. target=constants.FRONTEND_ZIP,
  141. root_dir=".web/_static",
  142. files_to_exclude=files_to_exclude,
  143. )
  144. if backend:
  145. _zip(
  146. component_name=_ComponentName.BACKEND,
  147. target=constants.BACKEND_ZIP,
  148. root_dir=".",
  149. dirs_to_exclude={"assets", "__pycache__"},
  150. files_to_exclude=files_to_exclude,
  151. )
  152. def setup_frontend(
  153. root: Path,
  154. disable_telemetry: bool = True,
  155. ):
  156. """Set up the frontend to run the app.
  157. Args:
  158. root: The root path of the project.
  159. disable_telemetry: Whether to disable the Next telemetry.
  160. """
  161. # Copy asset files to public folder.
  162. path_ops.cp(
  163. src=str(root / constants.APP_ASSETS_DIR),
  164. dest=str(root / constants.WEB_ASSETS_DIR),
  165. )
  166. # Set the environment variables in client (env.json).
  167. set_env_json()
  168. # Disable the Next telemetry.
  169. if disable_telemetry:
  170. processes.new_process(
  171. [
  172. prerequisites.get_package_manager(),
  173. "run",
  174. "next",
  175. "telemetry",
  176. "disable",
  177. ],
  178. cwd=constants.WEB_DIR,
  179. stdout=subprocess.DEVNULL,
  180. shell=constants.IS_WINDOWS,
  181. )
  182. def setup_frontend_prod(
  183. root: Path,
  184. disable_telemetry: bool = True,
  185. ):
  186. """Set up the frontend for prod mode.
  187. Args:
  188. root: The root path of the project.
  189. disable_telemetry: Whether to disable the Next telemetry.
  190. """
  191. setup_frontend(root, disable_telemetry)
  192. export(deploy_url=get_config().deploy_url)