Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ repos:
name: Compile FAB provider assets
language: node
files: ^providers/fab/.*/www/
entry: ./scripts/ci/pre_commit/compile_fab_assets.py
entry: ./scripts/ci/pre_commit/compile_provider_assets.py fab
pass_filenames: false
additional_dependencies: ['yarn@1.22.21']
- id: compile-ui-assets-dev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations

import argparse
import hashlib
import os
import re
Expand All @@ -32,9 +33,15 @@
# here that are not available in stdlib! You should not import common_precommit_utils.py here because
# They are importing rich library which is not available in the node environment.

FAB_PROVIDER_ROOT_PATH = AIRFLOW_ROOT_PATH / "providers" / "fab"
FAB_PROVIDER_WWW_PATH = FAB_PROVIDER_ROOT_PATH / "src" / "airflow" / "providers" / "fab" / "www"
FAB_PROVIDER_WWW_HASH_FILE = FAB_PROVIDER_ROOT_PATH / "www-hash.txt"
PROVIDERS_ROOT = AIRFLOW_ROOT_PATH / "providers"
PROVIDERS_PATHS = {
"fab": {
"root": PROVIDERS_ROOT / "fab",
"www": PROVIDERS_ROOT / "fab" / "src" / "airflow" / "providers" / "fab" / "www",
"dist": PROVIDERS_ROOT / "fab" / "src" / "airflow" / "providers" / "fab" / "www" / "static" / "dist",
"hash": PROVIDERS_ROOT / "fab" / "www-hash.txt",
},
}


def get_directory_hash(directory: Path, skip_path_regexps: list[str]) -> str:
Expand All @@ -60,18 +67,22 @@ def get_directory_hash(directory: Path, skip_path_regexps: list[str]) -> str:
SKIP_PATH_REGEXPS = [".*/node_modules.*"]


def compile_assets(www_directory: Path):
dist_directory = www_directory / "static" / "dist"
FAB_PROVIDER_WWW_HASH_FILE.parent.mkdir(exist_ok=True, parents=True)
if dist_directory.exists():
old_hash = (
FAB_PROVIDER_WWW_HASH_FILE.read_text().strip() if FAB_PROVIDER_WWW_HASH_FILE.exists() else ""
def compile_assets(provider_name: str):
if provider_name not in PROVIDERS_PATHS:
raise ValueError(
f"Provider '{provider_name}' is not supported. Supported providers: {list(PROVIDERS_PATHS.keys())}"
)
provider_paths = PROVIDERS_PATHS[provider_name]
www_directory = provider_paths["www"]
dist_directory = provider_paths["dist"]
provider_paths["hash"].parent.mkdir(exist_ok=True, parents=True)
if dist_directory.exists():
old_hash = provider_paths["hash"].read_text().strip() if provider_paths["hash"].exists() else ""
new_hash = get_directory_hash(www_directory, skip_path_regexps=SKIP_PATH_REGEXPS)
if new_hash == old_hash:
print(f"The '{www_directory}' directory has not changed! Skip regeneration.")
return
print("The directory has changed, regenerating assets.")
print(f"The directory has changed, regenerating assets in {www_directory}.")
print("Old hash: " + old_hash)
print("New hash: " + new_hash)
else:
Expand All @@ -94,10 +105,17 @@ def compile_assets(www_directory: Path):
sys.exit(result.returncode)
subprocess.check_call(["yarn", "run", "build"], cwd=os.fspath(www_directory), env=env)
new_hash = get_directory_hash(www_directory, skip_path_regexps=SKIP_PATH_REGEXPS)
FAB_PROVIDER_WWW_HASH_FILE.write_text(new_hash + "\n")
provider_paths["hash"].write_text(new_hash + "\n")
print(f"Assets compiled successfully. New hash: {new_hash}")


if __name__ == "__main__":
# Compile assets for fab provider
compile_assets(FAB_PROVIDER_WWW_PATH)
parser = argparse.ArgumentParser(description="Compile provider assets for the specified provider.")
parser.add_argument(
"provider",
type=str,
help="The name of the provider whose assets should be compiled.",
choices=list(PROVIDERS_PATHS.keys()),
)
args = parser.parse_args()
compile_assets(args.provider)