mirror of
https://github.com/frappe/bench.git
synced 2025-01-22 22:58:31 +00:00
fix: del node_modules if frappe supports app_cache
This commit is contained in:
parent
d84a05ed2a
commit
9988b8356b
75
bench/app.py
75
bench/app.py
@ -12,6 +12,7 @@ from collections import OrderedDict
|
||||
from datetime import date
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# imports - third party imports
|
||||
@ -170,7 +171,7 @@ class App(AppMeta):
|
||||
branch: str = None,
|
||||
bench: "Bench" = None,
|
||||
soft_link: bool = False,
|
||||
cache_key = None,
|
||||
cache_key=None,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
@ -233,7 +234,7 @@ class App(AppMeta):
|
||||
resolved=False,
|
||||
restart_bench=True,
|
||||
ignore_resolution=False,
|
||||
using_cached=False
|
||||
using_cached=False,
|
||||
):
|
||||
import bench.cli
|
||||
from bench.utils.app import get_app_name
|
||||
@ -292,7 +293,6 @@ class App(AppMeta):
|
||||
required=self.local_resolution,
|
||||
)
|
||||
|
||||
|
||||
"""
|
||||
Get App Cache
|
||||
|
||||
@ -387,9 +387,65 @@ class App(AppMeta):
|
||||
|
||||
def prune_app_directory(self):
|
||||
app_path = self.get_app_path()
|
||||
if can_frappe_use_cached(self):
|
||||
remove_unused_node_modules(app_path)
|
||||
|
||||
|
||||
def can_frappe_use_cached(app: App) -> bool:
|
||||
min_frappe = get_required_frappe_version(app)
|
||||
if not min_frappe:
|
||||
return False
|
||||
|
||||
import semantic_version as sv
|
||||
|
||||
try:
|
||||
return sv.Version(min_frappe) in sv.SimpleSpec(">=15.12.0")
|
||||
except ValueError:
|
||||
# Passed value is not a version string, it's an expression
|
||||
pass
|
||||
|
||||
try:
|
||||
"""
|
||||
15.12.0 is the first version to support USING_CACHED,
|
||||
but there is no way to check the last version without
|
||||
support. So it's not possible to have a ">" filter.
|
||||
|
||||
Hence this excludes the first supported version.
|
||||
"""
|
||||
return sv.Version("15.12.0") not in sv.SimpleSpec(min_frappe)
|
||||
except ValueError:
|
||||
click.secho(
|
||||
f"Invalid value found for frappe version '{min_frappe}'",
|
||||
fg="yellow"
|
||||
)
|
||||
# Invalid expression
|
||||
return False
|
||||
|
||||
|
||||
def get_required_frappe_version(app: App) -> Optional[str]:
|
||||
from bench.utils.app import get_pyproject
|
||||
|
||||
apps_path = os.path.join(os.path.abspath(app.bench.name), "apps")
|
||||
pyproject = get_pyproject(apps_path, app.app_name)
|
||||
|
||||
# Reference: https://github.com/frappe/bench/issues/1524
|
||||
req_frappe = (
|
||||
pyproject.get("tool", {})
|
||||
.get("bench", {})
|
||||
.get("frappe-dependencies", {})
|
||||
.get("frappe")
|
||||
)
|
||||
|
||||
if not req_frappe:
|
||||
click.secho(
|
||||
"Required frappe version not set in pyproject.toml, "
|
||||
"please refer: https://github.com/frappe/bench/issues/1524",
|
||||
fg="yellow",
|
||||
)
|
||||
|
||||
return req_frappe
|
||||
|
||||
|
||||
def remove_unused_node_modules(app_path: Path) -> None:
|
||||
"""
|
||||
Erring a bit the side of caution; since there is no explicit way
|
||||
@ -503,7 +559,9 @@ def get_app(
|
||||
from bench.utils.app import check_existing_dir
|
||||
|
||||
bench = Bench(bench_path)
|
||||
app = App(git_url, branch=branch, bench=bench, soft_link=soft_link, cache_key=cache_key)
|
||||
app = App(
|
||||
git_url, branch=branch, bench=bench, soft_link=soft_link, cache_key=cache_key
|
||||
)
|
||||
git_url = app.url
|
||||
repo_name = app.repo
|
||||
branch = app.tag
|
||||
@ -562,7 +620,12 @@ def get_app(
|
||||
return
|
||||
|
||||
if app.get_cached():
|
||||
app.install(verbose=verbose, skip_assets=skip_assets, restart_bench=restart_bench, using_cached=True)
|
||||
app.install(
|
||||
verbose=verbose,
|
||||
skip_assets=skip_assets,
|
||||
restart_bench=restart_bench,
|
||||
using_cached=True,
|
||||
)
|
||||
return
|
||||
|
||||
dir_already_exists, cloned_path = check_existing_dir(bench_path, repo_name)
|
||||
@ -593,7 +656,6 @@ def get_app(
|
||||
app.set_cache(compress_artifacts)
|
||||
|
||||
|
||||
|
||||
def install_resolved_deps(
|
||||
bench,
|
||||
resolution,
|
||||
@ -602,6 +664,7 @@ def install_resolved_deps(
|
||||
verbose=False,
|
||||
):
|
||||
from bench.utils.app import check_existing_dir
|
||||
|
||||
if "frappe" in resolution:
|
||||
# Terminal dependency
|
||||
del resolution["frappe"]
|
||||
|
@ -4,7 +4,7 @@ import pathlib
|
||||
import re
|
||||
import sys
|
||||
import subprocess
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
from functools import lru_cache
|
||||
|
||||
# imports - module imports
|
||||
@ -230,18 +230,12 @@ def get_app_name(bench_path: str, folder_name: str) -> str:
|
||||
app_name = None
|
||||
apps_path = os.path.join(os.path.abspath(bench_path), "apps")
|
||||
|
||||
pyproject_path = os.path.join(apps_path, folder_name, "pyproject.toml")
|
||||
config_py_path = os.path.join(apps_path, folder_name, "setup.cfg")
|
||||
setup_py_path = os.path.join(apps_path, folder_name, "setup.py")
|
||||
|
||||
if os.path.exists(pyproject_path):
|
||||
try:
|
||||
from tomli import load
|
||||
except ImportError:
|
||||
from tomllib import load
|
||||
|
||||
with open(pyproject_path, "rb") as f:
|
||||
app_name = load(f).get("project", {}).get("name")
|
||||
pyproject = get_pyproject(apps_path, folder_name)
|
||||
if pyproject:
|
||||
app_name = pyproject.get("project", {}).get("name")
|
||||
|
||||
if not app_name and os.path.exists(config_py_path):
|
||||
from setuptools.config import read_configuration
|
||||
@ -261,6 +255,20 @@ def get_app_name(bench_path: str, folder_name: str) -> str:
|
||||
return folder_name
|
||||
|
||||
|
||||
def get_pyproject(apps_path:str, folder_name:str) -> Optional[dict]:
|
||||
pyproject_path = os.path.join(apps_path, folder_name, "pyproject.toml")
|
||||
if not os.path.exists(pyproject_path):
|
||||
return None
|
||||
|
||||
try:
|
||||
from tomli import load
|
||||
except ImportError:
|
||||
from tomllib import load
|
||||
|
||||
with open(pyproject_path, "rb") as f:
|
||||
return load(f)
|
||||
|
||||
|
||||
def check_existing_dir(bench_path, repo_name):
|
||||
cloned_path = os.path.join(bench_path, "apps", repo_name)
|
||||
dir_already_exists = os.path.isdir(cloned_path)
|
||||
|
Loading…
x
Reference in New Issue
Block a user