|
| 1 | +import toml |
| 2 | +import argparse |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +def find_non_path_spacetimedb_deps(dev_deps): |
| 7 | + non_path_spacetimedb = [] |
| 8 | + for name, details in dev_deps.items(): |
| 9 | + if not name.startswith("spacetimedb"): |
| 10 | + continue |
| 11 | + |
| 12 | + if isinstance(details, dict): |
| 13 | + if "path" not in details: |
| 14 | + non_path_spacetimedb.append(name) |
| 15 | + else: |
| 16 | + # String dependency = version from crates.io |
| 17 | + non_path_spacetimedb.append(name) |
| 18 | + return non_path_spacetimedb |
| 19 | + |
| 20 | +def check_cargo_metadata(data): |
| 21 | + package = data.get("package", {}) |
| 22 | + missing_fields = [] |
| 23 | + |
| 24 | + # Accept either license OR license-file |
| 25 | + if "license" not in package and "license-file" not in package: |
| 26 | + missing_fields.append("license/license-file") |
| 27 | + |
| 28 | + if "description" not in package: |
| 29 | + missing_fields.append("description") |
| 30 | + |
| 31 | + return missing_fields |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + parser = argparse.ArgumentParser(description="Check Cargo.toml for metadata and dev-dependencies.") |
| 35 | + parser.add_argument("directory", help="Directory to search for Cargo.toml") |
| 36 | + |
| 37 | + args = parser.parse_args() |
| 38 | + cargo_toml_path = Path(args.directory) / "Cargo.toml" |
| 39 | + |
| 40 | + try: |
| 41 | + if not cargo_toml_path.exists(): |
| 42 | + raise FileNotFoundError(f"{cargo_toml_path} not found.") |
| 43 | + |
| 44 | + data = toml.load(cargo_toml_path) |
| 45 | + |
| 46 | + # Check dev-dependencies |
| 47 | + dev_deps = data.get("dev-dependencies", {}) |
| 48 | + bad_deps = find_non_path_spacetimedb_deps(dev_deps) |
| 49 | + |
| 50 | + # Check license/license-file and description |
| 51 | + missing_fields = check_cargo_metadata(data) |
| 52 | + |
| 53 | + exit_code = 0 |
| 54 | + |
| 55 | + if bad_deps: |
| 56 | + print(f"❌ These dev-dependencies in {cargo_toml_path} must be converted to use `path` in order to not impede crate publishing:") |
| 57 | + for dep in bad_deps: |
| 58 | + print(f" - {dep}") |
| 59 | + exit_code = 1 |
| 60 | + |
| 61 | + if missing_fields: |
| 62 | + print(f"❌ Missing required fields in [package] of {cargo_toml_path}: {', '.join(missing_fields)}") |
| 63 | + exit_code = 1 |
| 64 | + |
| 65 | + if exit_code == 0: |
| 66 | + print(f"✅ {cargo_toml_path} passed all checks.") |
| 67 | + |
| 68 | + sys.exit(exit_code) |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + print(f"⚠️ Error: {e}") |
| 72 | + sys.exit(2) |
0 commit comments