|
50 | 50 | validate_display_name, |
51 | 51 | validate_publisher, |
52 | 52 | validate_technical_name, |
| 53 | + write_json, |
| 54 | + write_toml, |
53 | 55 | ) |
54 | 56 |
|
55 | 57 | REMOTE_ENTRY_REGEX = re.compile(r"^remoteEntry\..+\.js$") |
@@ -292,6 +294,7 @@ def app() -> None: |
292 | 294 |
|
293 | 295 | @app.command() |
294 | 296 | def validate() -> None: |
| 297 | + """Validate the extension structure and metadata consistency.""" |
295 | 298 | validate_npm() |
296 | 299 |
|
297 | 300 | cwd = Path.cwd() |
@@ -372,12 +375,167 @@ def validate() -> None: |
372 | 375 | click.secho(" Convention requires: frontend/src/index.tsx", fg="yellow") |
373 | 376 | sys.exit(1) |
374 | 377 |
|
| 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 | + |
375 | 422 | click.secho("✅ Validation successful", fg="green") |
376 | 423 |
|
377 | 424 |
|
| 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 | + |
378 | 535 | @app.command() |
379 | 536 | @click.pass_context |
380 | 537 | def build(ctx: click.Context) -> None: |
| 538 | + """Build extension assets.""" |
381 | 539 | ctx.invoke(validate) |
382 | 540 | cwd = Path.cwd() |
383 | 541 | frontend_dir = cwd / "frontend" |
@@ -413,6 +571,7 @@ def build(ctx: click.Context) -> None: |
413 | 571 | ) |
414 | 572 | @click.pass_context |
415 | 573 | def bundle(ctx: click.Context, output: Path | None) -> None: |
| 574 | + """Package the extension into a .supx file.""" |
416 | 575 | ctx.invoke(build) |
417 | 576 |
|
418 | 577 | cwd = Path.cwd() |
@@ -453,6 +612,7 @@ def bundle(ctx: click.Context, output: Path | None) -> None: |
453 | 612 | @app.command() |
454 | 613 | @click.pass_context |
455 | 614 | def dev(ctx: click.Context) -> None: |
| 615 | + """Automatically rebuild the extension as files change.""" |
456 | 616 | cwd = Path.cwd() |
457 | 617 | frontend_dir = cwd / "frontend" |
458 | 618 | backend_dir = cwd / "backend" |
@@ -647,6 +807,7 @@ def init( |
647 | 807 | frontend_opt: bool | None, |
648 | 808 | backend_opt: bool | None, |
649 | 809 | ) -> None: |
| 810 | + """Scaffold a new extension project.""" |
650 | 811 | # Get extension names with graceful validation |
651 | 812 | names = prompt_for_extension_info(display_name_opt, publisher_opt, name_opt) |
652 | 813 |
|
|
0 commit comments