From 8cccfe02748aaf84c7f55caf3c88ff703418fc96 Mon Sep 17 00:00:00 2001 From: Tom Gotsman Date: Fri, 17 Nov 2023 15:39:10 -0800 Subject: [PATCH 1/4] keep app id (project hash) the same even after re-init --- reflex/utils/prerequisites.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 52e8f73175..63bb8acff2 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -278,12 +278,28 @@ def initialize_app_directory(app_name: str, template: constants.Templates.Kind): ) +def get_project_hash_if_exists(): + """Check if the reflex.json file exists in .web folder and + if it does then extract the project_hash number so that we + do not overwrite it every time we run the reflex init command""" + if os.path.exists(constants.Reflex.JSON): + # Open and read the file + with open(constants.Reflex.JSON, 'r') as file: + data = json.load(file) + project_hash = data["project_hash"] + return project_hash + else: + return None + + def initialize_web_directory(): """Initialize the web directory on reflex init.""" console.log("Initializing the web directory.") - path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB) + project_hash = get_project_hash_if_exists() + path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB) + initialize_package_json() path_ops.mkdir(constants.Dirs.WEB_ASSETS) @@ -291,7 +307,7 @@ def initialize_web_directory(): update_next_config() # Initialize the reflex json file. - init_reflex_json() + init_reflex_json(project_hash=project_hash) def _compile_package_json(): @@ -315,11 +331,15 @@ def initialize_package_json(): file.write(code) -def init_reflex_json(): +def init_reflex_json(project_hash): """Write the hash of the Reflex project to a REFLEX_JSON.""" - # Get a random project hash. - project_hash = random.getrandbits(128) - console.debug(f"Setting project hash to {project_hash}.") + + if project_hash != None: + console.debug(f"Project hash is already set to {project_hash}.") + else: + # Get a random project hash. + project_hash = random.getrandbits(128) + console.debug(f"Setting project hash to {project_hash}.") # Write the hash and version to the reflex json file. reflex_json = { From fd2cd94b1af5eb49ed0e60b388b7559f628e0f31 Mon Sep 17 00:00:00 2001 From: Tom Gotsman Date: Mon, 20 Nov 2023 10:47:04 -0800 Subject: [PATCH 2/4] updates for precommit --- reflex/utils/prerequisites.py | 42 +++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 63bb8acff2..44aef17ea0 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -278,28 +278,30 @@ def initialize_app_directory(app_name: str, template: constants.Templates.Kind): ) -def get_project_hash_if_exists(): - """Check if the reflex.json file exists in .web folder and - if it does then extract the project_hash number so that we - do not overwrite it every time we run the reflex init command""" - if os.path.exists(constants.Reflex.JSON): - # Open and read the file - with open(constants.Reflex.JSON, 'r') as file: - data = json.load(file) - project_hash = data["project_hash"] - return project_hash - else: +def get_project_hash() -> int | None: + """Get the project hash from the reflex.json file if the file exists. + + Returns: + project_hash (int): The app hash. + """ + if not os.path.exists(constants.Reflex.JSON): return None + # Open and read the file + with open(constants.Reflex.JSON, "r") as file: + data = json.load(file) + project_hash = data["project_hash"] + return project_hash def initialize_web_directory(): """Initialize the web directory on reflex init.""" console.log("Initializing the web directory.") - project_hash = get_project_hash_if_exists() + # Re-use the hash if one is already created, so we don't over-write it when running reflex init + project_hash = get_project_hash() path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB) - + initialize_package_json() path_ops.mkdir(constants.Dirs.WEB_ASSETS) @@ -331,10 +333,16 @@ def initialize_package_json(): file.write(code) -def init_reflex_json(project_hash): - """Write the hash of the Reflex project to a REFLEX_JSON.""" - - if project_hash != None: +def init_reflex_json(project_hash: int | None): + """Write the hash of the Reflex project to a REFLEX_JSON. + Re-use the hash if one is already created, therefore do not + overwrite it every time we run the reflex init command + . + + Args: + project_hash (int): The app hash. + """ + if project_hash is not None: console.debug(f"Project hash is already set to {project_hash}.") else: # Get a random project hash. From 5a92025ec2ad9048a05f1d53de4ceca5768ef35d Mon Sep 17 00:00:00 2001 From: Tom Gotsman Date: Mon, 20 Nov 2023 11:20:56 -0800 Subject: [PATCH 3/4] minor style update --- reflex/utils/prerequisites.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 44aef17ea0..ba397ebe71 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -335,12 +335,13 @@ def initialize_package_json(): def init_reflex_json(project_hash: int | None): """Write the hash of the Reflex project to a REFLEX_JSON. + Re-use the hash if one is already created, therefore do not overwrite it every time we run the reflex init command . Args: - project_hash (int): The app hash. + project_hash: The app hash. """ if project_hash is not None: console.debug(f"Project hash is already set to {project_hash}.") From 94bc39c31e372c5e2131b8afe293c589db12052d Mon Sep 17 00:00:00 2001 From: Tom Gotsman Date: Mon, 20 Nov 2023 11:24:35 -0800 Subject: [PATCH 4/4] minor style update2 --- reflex/utils/prerequisites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index ba397ebe71..1b12154e83 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -282,7 +282,7 @@ def get_project_hash() -> int | None: """Get the project hash from the reflex.json file if the file exists. Returns: - project_hash (int): The app hash. + project_hash: The app hash. """ if not os.path.exists(constants.Reflex.JSON): return None