Skip to content

Commit c1c8eb0

Browse files
authored
Merge branch 'master' into fix-sorting-timetable
2 parents 2350c30 + aa5adb0 commit c1c8eb0

16 files changed

Lines changed: 644 additions & 8 deletions

File tree

‎docs/developer_docs/extensions/development.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ superset-extensions bundle: Packages the extension into a .supx file.
3939
4040
superset-extensions dev: Automatically rebuilds the extension as files change.
4141
42-
superset-extensions validate: Validates the extension structure and metadata.
42+
superset-extensions validate: Validates the extension structure and metadata consistency.
43+
44+
superset-extensions update: Updates derived and generated files in the extension project.
45+
Use --version [<version>] to update the version (prompts if no value given).
46+
Use --license [<license>] to update the license (prompts if no value given).
4347
```
4448

4549
When creating a new extension with `superset-extensions init`, the CLI generates a standardized folder structure:

‎requirements/development.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,8 @@ tabulate==0.9.0
996996
# via
997997
# -c requirements/base-constraint.txt
998998
# apache-superset
999+
tomli-w==1.2.0
1000+
# via apache-superset-extensions-cli
9991001
tomlkit==0.13.3
10001002
# via pylint
10011003
tqdm==4.67.1

‎superset-extensions-cli/pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies = [
4949
"jinja2>=3.1.6",
5050
"semver>=3.0.4",
5151
"tomli>=2.2.1; python_version < '3.11'",
52+
"tomli-w>=1.2.0",
5253
"watchdog>=6.0.0",
5354
]
5455

‎superset-extensions-cli/src/superset_extensions_cli/cli.py‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
validate_display_name,
5151
validate_publisher,
5252
validate_technical_name,
53+
write_json,
54+
write_toml,
5355
)
5456

5557
REMOTE_ENTRY_REGEX = re.compile(r"^remoteEntry\..+\.js$")
@@ -292,6 +294,7 @@ def app() -> None:
292294

293295
@app.command()
294296
def validate() -> None:
297+
"""Validate the extension structure and metadata consistency."""
295298
validate_npm()
296299

297300
cwd = Path.cwd()
@@ -372,12 +375,167 @@ def validate() -> None:
372375
click.secho(" Convention requires: frontend/src/index.tsx", fg="yellow")
373376
sys.exit(1)
374377

378+
# Validate version and license consistency across extension.json, frontend, and backend
379+
mismatches: list[str] = []
380+
frontend_pkg_path = cwd / "frontend" / "package.json"
381+
frontend_pkg = None
382+
if frontend_pkg_path.is_file():
383+
frontend_pkg = read_json(frontend_pkg_path)
384+
if frontend_pkg:
385+
if frontend_pkg.get("version") != extension.version:
386+
mismatches.append(
387+
f" frontend/package.json version: {frontend_pkg.get('version')} "
388+
f"(expected {extension.version})"
389+
)
390+
if extension.license and frontend_pkg.get("license") != extension.license:
391+
mismatches.append(
392+
f" frontend/package.json license: {frontend_pkg.get('license')} "
393+
f"(expected {extension.license})"
394+
)
395+
396+
backend_pyproject_path = cwd / "backend" / "pyproject.toml"
397+
if backend_pyproject_path.is_file():
398+
backend_pyproject = read_toml(backend_pyproject_path)
399+
if backend_pyproject:
400+
project = backend_pyproject.get("project", {})
401+
if project.get("version") != extension.version:
402+
mismatches.append(
403+
f" backend/pyproject.toml version: {project.get('version')} "
404+
f"(expected {extension.version})"
405+
)
406+
if extension.license and project.get("license") != extension.license:
407+
mismatches.append(
408+
f" backend/pyproject.toml license: {project.get('license')} "
409+
f"(expected {extension.license})"
410+
)
411+
412+
if mismatches:
413+
click.secho("❌ Metadata mismatch detected:", err=True, fg="red")
414+
for mismatch in mismatches:
415+
click.secho(mismatch, err=True, fg="red")
416+
click.secho(
417+
"Run `superset-extensions update` to sync from extension.json.",
418+
fg="yellow",
419+
)
420+
sys.exit(1)
421+
375422
click.secho("✅ Validation successful", fg="green")
376423

377424

425+
@app.command()
426+
@click.option(
427+
"--version",
428+
"version_opt",
429+
is_flag=False,
430+
flag_value="__prompt__",
431+
default=None,
432+
help="Set a new version. Prompts for value if none given.",
433+
)
434+
@click.option(
435+
"--license",
436+
"license_opt",
437+
is_flag=False,
438+
flag_value="__prompt__",
439+
default=None,
440+
help="Set a new license. Prompts for value if none given.",
441+
)
442+
def update(version_opt: str | None, license_opt: str | None) -> None:
443+
"""Update derived and generated files in the extension project."""
444+
cwd = Path.cwd()
445+
446+
extension_json_path = cwd / "extension.json"
447+
extension_data = read_json(extension_json_path)
448+
if not extension_data:
449+
click.secho("❌ extension.json not found.", err=True, fg="red")
450+
sys.exit(1)
451+
452+
try:
453+
extension = ExtensionConfig.model_validate(extension_data)
454+
except Exception as e:
455+
click.secho(f"❌ Invalid extension.json: {e}", err=True, fg="red")
456+
sys.exit(1)
457+
458+
# Resolve version: prompt if flag used without value
459+
if version_opt == "__prompt__":
460+
version_opt = click.prompt("Version", default=extension.version)
461+
target_version = (
462+
version_opt
463+
if version_opt and version_opt != extension.version
464+
else extension.version
465+
)
466+
467+
# Resolve license: prompt if flag used without value
468+
if license_opt == "__prompt__":
469+
license_opt = click.prompt("License", default=extension.license or "")
470+
target_license = (
471+
license_opt
472+
if license_opt and license_opt != extension.license
473+
else extension.license
474+
)
475+
476+
updated: list[str] = []
477+
478+
# Update extension.json if version or license changed
479+
ext_changed = False
480+
if version_opt and version_opt != extension.version:
481+
extension_data["version"] = target_version
482+
ext_changed = True
483+
if license_opt and license_opt != extension.license:
484+
extension_data["license"] = target_license
485+
ext_changed = True
486+
if ext_changed:
487+
try:
488+
ExtensionConfig.model_validate(extension_data)
489+
except Exception as e:
490+
click.secho(f"❌ Invalid value: {e}", err=True, fg="red")
491+
sys.exit(1)
492+
write_json(extension_json_path, extension_data)
493+
updated.append("extension.json")
494+
495+
# Update frontend/package.json
496+
frontend_pkg_path = cwd / "frontend" / "package.json"
497+
if frontend_pkg_path.is_file():
498+
frontend_pkg = read_json(frontend_pkg_path)
499+
if frontend_pkg:
500+
pkg_changed = False
501+
if frontend_pkg.get("version") != target_version:
502+
frontend_pkg["version"] = target_version
503+
pkg_changed = True
504+
if target_license and frontend_pkg.get("license") != target_license:
505+
frontend_pkg["license"] = target_license
506+
pkg_changed = True
507+
if pkg_changed:
508+
write_json(frontend_pkg_path, frontend_pkg)
509+
updated.append("frontend/package.json")
510+
511+
# Update backend/pyproject.toml
512+
backend_pyproject_path = cwd / "backend" / "pyproject.toml"
513+
if backend_pyproject_path.is_file():
514+
backend_pyproject = read_toml(backend_pyproject_path)
515+
if backend_pyproject:
516+
project = backend_pyproject.setdefault("project", {})
517+
toml_changed = False
518+
if project.get("version") != target_version:
519+
project["version"] = target_version
520+
toml_changed = True
521+
if target_license and project.get("license") != target_license:
522+
project["license"] = target_license
523+
toml_changed = True
524+
if toml_changed:
525+
write_toml(backend_pyproject_path, backend_pyproject)
526+
updated.append("backend/pyproject.toml")
527+
528+
if updated:
529+
for path in updated:
530+
click.secho(f"✅ Updated {path}", fg="green")
531+
else:
532+
click.secho("✅ All files already up to date.", fg="green")
533+
534+
378535
@app.command()
379536
@click.pass_context
380537
def build(ctx: click.Context) -> None:
538+
"""Build extension assets."""
381539
ctx.invoke(validate)
382540
cwd = Path.cwd()
383541
frontend_dir = cwd / "frontend"
@@ -413,6 +571,7 @@ def build(ctx: click.Context) -> None:
413571
)
414572
@click.pass_context
415573
def bundle(ctx: click.Context, output: Path | None) -> None:
574+
"""Package the extension into a .supx file."""
416575
ctx.invoke(build)
417576

418577
cwd = Path.cwd()
@@ -453,6 +612,7 @@ def bundle(ctx: click.Context, output: Path | None) -> None:
453612
@app.command()
454613
@click.pass_context
455614
def dev(ctx: click.Context) -> None:
615+
"""Automatically rebuild the extension as files change."""
456616
cwd = Path.cwd()
457617
frontend_dir = cwd / "frontend"
458618
backend_dir = cwd / "backend"
@@ -647,6 +807,7 @@ def init(
647807
frontend_opt: bool | None,
648808
backend_opt: bool | None,
649809
) -> None:
810+
"""Scaffold a new extension project."""
650811
# Get extension names with graceful validation
651812
names = prompt_for_extension_info(display_name_opt, publisher_opt, name_opt)
652813

‎superset-extensions-cli/src/superset_extensions_cli/utils.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from pathlib import Path
2222
from typing import Any
2323

24+
import tomli_w
25+
2426
from superset_core.extensions.constants import (
2527
DISPLAY_NAME_PATTERN,
2628
PUBLISHER_PATTERN,
@@ -109,6 +111,14 @@ def read_json(path: Path) -> dict[str, Any] | None:
109111
return json.loads(path.read_text())
110112

111113

114+
def write_json(path: Path, data: dict[str, Any]) -> None:
115+
path.write_text(json.dumps(data, indent=2) + "\n")
116+
117+
118+
def write_toml(path: Path, data: dict[str, Any]) -> None:
119+
path.write_text(tomli_w.dumps(data))
120+
121+
112122
def _normalize_for_identifiers(name: str) -> str:
113123
"""
114124
Normalize display name to clean lowercase words.

‎superset-extensions-cli/tests/conftest.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
from __future__ import annotations
1919

20+
import json
2021
import os
2122
from pathlib import Path
2223

2324
import pytest
25+
import tomli_w
2426
from click.testing import CliRunner
2527

2628

@@ -138,3 +140,69 @@ def _setup(base_path: Path) -> None:
138140
(backend_dir / "__init__.py").write_text("# init")
139141

140142
return _setup
143+
144+
145+
@pytest.fixture
146+
def extension_with_versions():
147+
"""Create an extension directory structure with configurable versions and licenses."""
148+
149+
def _create(
150+
base_path: Path,
151+
ext_version: str = "1.0.0",
152+
frontend_version: str | None = None,
153+
backend_version: str | None = None,
154+
ext_license: str | None = "Apache-2.0",
155+
frontend_license: str | None = None,
156+
backend_license: str | None = None,
157+
) -> None:
158+
extension_json = {
159+
"publisher": "test-org",
160+
"name": "test-extension",
161+
"displayName": "Test Extension",
162+
"version": ext_version,
163+
"permissions": [],
164+
}
165+
if ext_license is not None:
166+
extension_json["license"] = ext_license
167+
(base_path / "extension.json").write_text(json.dumps(extension_json))
168+
169+
if frontend_version is not None:
170+
frontend_dir = base_path / "frontend"
171+
frontend_dir.mkdir(exist_ok=True)
172+
(frontend_dir / "src").mkdir(exist_ok=True)
173+
(frontend_dir / "src" / "index.tsx").write_text("// entry")
174+
pkg = {
175+
"name": "@test-org/test-extension",
176+
"version": frontend_version,
177+
}
178+
if frontend_license is not None:
179+
pkg["license"] = frontend_license
180+
elif ext_license is not None:
181+
pkg["license"] = ext_license
182+
(frontend_dir / "package.json").write_text(json.dumps(pkg, indent=2))
183+
184+
if backend_version is not None:
185+
backend_dir = base_path / "backend"
186+
backend_dir.mkdir(exist_ok=True)
187+
src_dir = backend_dir / "src" / "test_org" / "test_extension"
188+
src_dir.mkdir(parents=True, exist_ok=True)
189+
(src_dir / "entrypoint.py").write_text("# entry")
190+
project = {
191+
"name": "test-org-test-extension",
192+
"version": backend_version,
193+
}
194+
if backend_license is not None:
195+
project["license"] = backend_license
196+
elif ext_license is not None:
197+
project["license"] = ext_license
198+
pyproject = {
199+
"project": project,
200+
"tool": {
201+
"apache_superset_extensions": {
202+
"build": {"include": ["src/**/*.py"]}
203+
}
204+
},
205+
}
206+
(backend_dir / "pyproject.toml").write_text(tomli_w.dumps(pyproject))
207+
208+
return _create

‎superset-extensions-cli/tests/test_cli_build.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_build_command_success_flow(
121121
# Setup mocks
122122
mock_rebuild_frontend.return_value = "remoteEntry.abc123.js"
123123
mock_read_toml.return_value = {
124-
"project": {"name": "test"},
124+
"project": {"name": "test", "version": "1.0.0"},
125125
"tool": {
126126
"apache_superset_extensions": {
127127
"build": {"include": ["src/test_org/test_extension/**/*.py"]}
@@ -162,7 +162,7 @@ def test_build_command_handles_frontend_build_failure(
162162
# Setup mocks
163163
mock_rebuild_frontend.return_value = None # Indicates failure
164164
mock_read_toml.return_value = {
165-
"project": {"name": "test"},
165+
"project": {"name": "test", "version": "1.0.0"},
166166
"tool": {
167167
"apache_superset_extensions": {
168168
"build": {"include": ["src/test_org/test_extension/**/*.py"]}

0 commit comments

Comments
 (0)