From af7af376d01a3bcac3eadea1a50968095a643212 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Fri, 5 Jan 2024 14:19:57 -0800 Subject: [PATCH] [REF-1586] [WiP] Use bun as a package manager on windows Although bun support for windows is currently extremely limited/broken, eventually we want to migrate over to it for the installation speed gains over npm. --- reflex/constants/compiler.py | 2 ++ reflex/utils/console.py | 11 ++++++++- reflex/utils/prerequisites.py | 42 +++++++++++++++++------------------ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/reflex/constants/compiler.py b/reflex/constants/compiler.py index f172dfcec..cd563f14f 100644 --- a/reflex/constants/compiler.py +++ b/reflex/constants/compiler.py @@ -26,6 +26,8 @@ class Ext(SimpleNamespace): CSS = ".css" # The extension for zip files. ZIP = ".zip" + # The extension for executable files on Windows. + EXE = ".exe" class CompileVars(SimpleNamespace): diff --git a/reflex/utils/console.py b/reflex/utils/console.py index 1580da706..3fd8804a0 100644 --- a/reflex/utils/console.py +++ b/reflex/utils/console.py @@ -27,6 +27,15 @@ def set_log_level(log_level: LogLevel): _LOG_LEVEL = log_level +def is_debug() -> bool: + """Check if the log level is debug. + + Returns: + True if the log level is debug. + """ + return _LOG_LEVEL <= LogLevel.DEBUG + + def print(msg: str, **kwargs): """Print a message. @@ -44,7 +53,7 @@ def debug(msg: str, **kwargs): msg: The debug message. kwargs: Keyword arguments to pass to the print function. """ - if _LOG_LEVEL <= LogLevel.DEBUG: + if is_debug(): msg_ = f"[blue]Debug: {msg}[/blue]" if progress := kwargs.pop("progress", None): progress.console.print(msg_, **kwargs) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 860a3cba0..343c3b79f 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -145,16 +145,13 @@ def get_bun_version() -> version.Version | None: def get_install_package_manager() -> str | None: """Get the package manager executable for installation. - Currently on unix systems, bun is used for installation only. + Currently, bun is used for installation only. Returns: The path to the package manager. """ - # On Windows, we use npm instead of bun. if constants.IS_WINDOWS: - return get_package_manager() - - # On other platforms, we use bun. + return get_config().bun_path + constants.Ext.EXE return get_config().bun_path @@ -661,11 +658,6 @@ def install_bun(): Raises: FileNotFoundError: If required packages are not found. """ - # Bun is not supported on Windows. - if constants.IS_WINDOWS: - console.debug("Skipping bun installation on Windows.") - return - # Skip if bun is already installed. if os.path.exists(get_config().bun_path) and get_bun_version() == version.parse( constants.Bun.VERSION @@ -674,16 +666,25 @@ def install_bun(): return # if unzip is installed - unzip_path = path_ops.which("unzip") - if unzip_path is None: - raise FileNotFoundError("Reflex requires unzip to be installed.") + if constants.IS_WINDOWS: + processes.new_process( + ["powershell", "-c", f"irm {constants.Bun.INSTALL_URL}.ps1|iex"], + env={"BUN_INSTALL": constants.Bun.ROOT_PATH}, + shell=True, + run=True, + show_logs=console.is_debug(), + ) + else: + unzip_path = path_ops.which("unzip") + if unzip_path is None: + raise FileNotFoundError("Reflex requires unzip to be installed.") - # Run the bun install script. - download_and_run( - constants.Bun.INSTALL_URL, - f"bun-v{constants.Bun.VERSION}", - BUN_INSTALL=constants.Bun.ROOT_PATH, - ) + # Run the bun install script. + download_and_run( + constants.Bun.INSTALL_URL, + f"bun-v{constants.Bun.VERSION}", + BUN_INSTALL=constants.Bun.ROOT_PATH, + ) def _write_cached_procedure_file(payload: str, cache_file: str): @@ -880,9 +881,6 @@ def validate_frontend_dependencies(init=True): ) raise typer.Exit(1) - if constants.IS_WINDOWS: - return - if init: # we only need bun for package install on `reflex init`. validate_bun()