|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate that gradle.lockfile files are up-to-date with current dependencies. |
| 3 | +
|
| 4 | +This script uses Gradle's built-in dependency resolution to regenerate lockfiles |
| 5 | +and checks if they differ from the committed versions. |
| 6 | +""" |
| 7 | + |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | + |
| 13 | +def run_command(cmd: list[str], check: bool = True) -> subprocess.CompletedProcess: |
| 14 | + """Run a shell command and return the result.""" |
| 15 | + return subprocess.run( |
| 16 | + cmd, |
| 17 | + capture_output=True, |
| 18 | + text=True, |
| 19 | + check=check, |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def check_for_changes_in_gradle_files() -> bool: |
| 24 | + """Check if any build.gradle or gradle.lockfile files were changed.""" |
| 25 | + try: |
| 26 | + # Get all changed files (staged + unstaged) |
| 27 | + result = run_command(["git", "diff", "--name-only", "HEAD"]) |
| 28 | + unstaged = set(result.stdout.strip().split("\n")) if result.stdout.strip() else set() |
| 29 | + |
| 30 | + result = run_command(["git", "diff", "--name-only", "--cached"]) |
| 31 | + staged = set(result.stdout.strip().split("\n")) if result.stdout.strip() else set() |
| 32 | + |
| 33 | + changed_files = unstaged.union(staged) |
| 34 | + |
| 35 | + # Check if any gradle files were modified |
| 36 | + gradle_files = [ |
| 37 | + f for f in changed_files |
| 38 | + if f.endswith("build.gradle") |
| 39 | + or f.endswith("build.gradle.kts") |
| 40 | + or f.endswith("gradle.lockfile") |
| 41 | + or "gradle.properties" in f |
| 42 | + or "gradle/wrapper" in f |
| 43 | + ] |
| 44 | + |
| 45 | + return len(gradle_files) > 0 |
| 46 | + except subprocess.CalledProcessError: |
| 47 | + # If we can't determine, assume we should check |
| 48 | + return True |
| 49 | + |
| 50 | + |
| 51 | +def regenerate_lockfiles() -> tuple[bool, str]: |
| 52 | + """Regenerate all lockfiles using Gradle. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + Tuple of (success, error_message) |
| 56 | + """ |
| 57 | + print("Regenerating lockfiles to verify they are up-to-date...") |
| 58 | + print("Running: ./gradlew resolveAndLockAll --write-locks") |
| 59 | + |
| 60 | + try: |
| 61 | + result = run_command( |
| 62 | + ["./gradlew", "resolveAndLockAll", "--write-locks", "-x", "generateGitPropertiesGlobal"], |
| 63 | + check=False |
| 64 | + ) |
| 65 | + |
| 66 | + if result.returncode != 0: |
| 67 | + return False, f"Failed to regenerate lockfiles:\n{result.stderr}" |
| 68 | + |
| 69 | + return True, "" |
| 70 | + except Exception as e: |
| 71 | + return False, f"Error running Gradle: {str(e)}" |
| 72 | + |
| 73 | + |
| 74 | +def check_for_lockfile_diffs() -> tuple[bool, list[str]]: |
| 75 | + """Check if any lockfiles have differences after regeneration. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + Tuple of (has_diffs, list_of_changed_files) |
| 79 | + """ |
| 80 | + try: |
| 81 | + result = run_command( |
| 82 | + ["git", "diff", "--name-only", "**gradle.lockfile"], |
| 83 | + check=False |
| 84 | + ) |
| 85 | + |
| 86 | + if result.returncode == 0 and result.stdout.strip(): |
| 87 | + changed_lockfiles = [ |
| 88 | + f for f in result.stdout.strip().split("\n") |
| 89 | + if f.endswith("gradle.lockfile") |
| 90 | + ] |
| 91 | + return True, changed_lockfiles |
| 92 | + |
| 93 | + return False, [] |
| 94 | + except subprocess.CalledProcessError: |
| 95 | + return False, [] |
| 96 | + |
| 97 | + |
| 98 | +def restore_lockfiles(): |
| 99 | + """Restore lockfiles to their original state.""" |
| 100 | + print("Restoring lockfiles to original state...") |
| 101 | + try: |
| 102 | + run_command(["git", "checkout", "**gradle.lockfile"], check=False) |
| 103 | + except: |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | +def main(): |
| 108 | + """Main validation function.""" |
| 109 | + print("Checking gradle lockfile updates...") |
| 110 | + |
| 111 | + # Check if we're in a git repository |
| 112 | + result = run_command(["git", "rev-parse", "--git-dir"], check=False) |
| 113 | + if result.returncode != 0: |
| 114 | + print("Not in a git repository. Skipping lockfile check.") |
| 115 | + return 0 |
| 116 | + |
| 117 | + # Check if any gradle-related files changed |
| 118 | + if not check_for_changes_in_gradle_files(): |
| 119 | + print("✓ No gradle files changed. Skipping lockfile verification.") |
| 120 | + return 0 |
| 121 | + |
| 122 | + # Regenerate lockfiles |
| 123 | + success, error = regenerate_lockfiles() |
| 124 | + if not success: |
| 125 | + print(f"\n❌ ERROR: {error}") |
| 126 | + return 1 |
| 127 | + |
| 128 | + # Check for differences |
| 129 | + has_diffs, changed_files = check_for_lockfile_diffs() |
| 130 | + |
| 131 | + # Always restore lockfiles to original state |
| 132 | + restore_lockfiles() |
| 133 | + |
| 134 | + if has_diffs: |
| 135 | + print("\n❌ ERROR: Dependency lockfiles are out of date!\n") |
| 136 | + print("The following lockfiles need to be updated:\n") |
| 137 | + for file in changed_files: |
| 138 | + print(f" • {file}") |
| 139 | + |
| 140 | + print("\nYour build.gradle changes affect dependency resolution, but the") |
| 141 | + print("corresponding lockfiles were not updated.\n") |
| 142 | + print("To fix this, run:") |
| 143 | + print(" ./gradlew resolveAndLockAll --write-locks\n") |
| 144 | + print("Then commit the updated gradle.lockfile files along with your changes.") |
| 145 | + |
| 146 | + return 1 |
| 147 | + |
| 148 | + print("✓ All gradle lockfiles are up-to-date.") |
| 149 | + return 0 |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + sys.exit(main()) |
0 commit comments