|
| 1 | +Import('env') |
| 2 | +import subprocess |
| 3 | +import json |
| 4 | +import re |
| 5 | + |
| 6 | +def get_github_repo(): |
| 7 | + """Extract GitHub repository name from git remote URL. |
| 8 | + |
| 9 | + Uses the remote that the current branch tracks, falling back to 'origin'. |
| 10 | + This handles cases where repositories have multiple remotes or where the |
| 11 | + main remote is not named 'origin'. |
| 12 | + |
| 13 | + Returns: |
| 14 | + str: Repository name in 'owner/repo' format for GitHub repos, |
| 15 | + 'unknown' for non-GitHub repos, missing git CLI, or any errors. |
| 16 | + """ |
| 17 | + try: |
| 18 | + remote_name = 'origin' # Default fallback |
| 19 | + |
| 20 | + # Try to get the remote for the current branch |
| 21 | + try: |
| 22 | + # Get current branch name |
| 23 | + branch_result = subprocess.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], |
| 24 | + capture_output=True, text=True, check=True) |
| 25 | + current_branch = branch_result.stdout.strip() |
| 26 | + |
| 27 | + # Get the remote for the current branch |
| 28 | + remote_result = subprocess.run(['git', 'config', f'branch.{current_branch}.remote'], |
| 29 | + capture_output=True, text=True, check=True) |
| 30 | + tracked_remote = remote_result.stdout.strip() |
| 31 | + |
| 32 | + # Use the tracked remote if we found one |
| 33 | + if tracked_remote: |
| 34 | + remote_name = tracked_remote |
| 35 | + except subprocess.CalledProcessError: |
| 36 | + # If branch config lookup fails, continue with 'origin' as fallback |
| 37 | + pass |
| 38 | + |
| 39 | + # Get the remote URL for the determined remote |
| 40 | + result = subprocess.run(['git', 'remote', 'get-url', remote_name], |
| 41 | + capture_output=True, text=True, check=True) |
| 42 | + remote_url = result.stdout.strip() |
| 43 | + |
| 44 | + # Check if it's a GitHub URL |
| 45 | + if 'github.com' not in remote_url.lower(): |
| 46 | + return None |
| 47 | + |
| 48 | + # Parse GitHub URL patterns: |
| 49 | + # https://github.com/owner/repo.git |
| 50 | + # [email protected]:owner/repo.git |
| 51 | + # https://github.com/owner/repo |
| 52 | + |
| 53 | + # Remove .git suffix if present |
| 54 | + if remote_url.endswith('.git'): |
| 55 | + remote_url = remote_url[:-4] |
| 56 | + |
| 57 | + # Handle HTTPS URLs |
| 58 | + https_match = re.search(r'github\.com/([^/]+/[^/]+)', remote_url, re.IGNORECASE) |
| 59 | + if https_match: |
| 60 | + return https_match.group(1) |
| 61 | + |
| 62 | + # Handle SSH URLs |
| 63 | + ssh_match = re.search(r'github\.com:([^/]+/[^/]+)', remote_url, re.IGNORECASE) |
| 64 | + if ssh_match: |
| 65 | + return ssh_match.group(1) |
| 66 | + |
| 67 | + return None |
| 68 | + |
| 69 | + except FileNotFoundError: |
| 70 | + # Git CLI is not installed or not in PATH |
| 71 | + return None |
| 72 | + except subprocess.CalledProcessError: |
| 73 | + # Git command failed (e.g., not a git repo, no remote, etc.) |
| 74 | + return None |
| 75 | + except Exception: |
| 76 | + # Any other unexpected error |
| 77 | + return None |
| 78 | + |
| 79 | +PACKAGE_FILE = "package.json" |
| 80 | + |
| 81 | +def get_version(): |
| 82 | + try: |
| 83 | + with open(PACKAGE_FILE, "r") as package: |
| 84 | + return json.load(package)["version"] |
| 85 | + except (FileNotFoundError, KeyError, json.JSONDecodeError): |
| 86 | + return None |
| 87 | + |
| 88 | + |
| 89 | +def has_def(cppdefs, name): |
| 90 | + """ Returns true if a given name is set in a CPPDEFINES collection """ |
| 91 | + for f in cppdefs: |
| 92 | + if isinstance(f, tuple): |
| 93 | + f = f[0] |
| 94 | + if f == name: |
| 95 | + return True |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +def add_wled_metadata_flags(env, node): |
| 100 | + cdefs = env["CPPDEFINES"].copy() |
| 101 | + |
| 102 | + if not has_def(cdefs, "WLED_REPO"): |
| 103 | + repo = get_github_repo() |
| 104 | + if repo: |
| 105 | + cdefs.append(("WLED_REPO", f"\\\"{repo}\\\"")) |
| 106 | + |
| 107 | + if not has_def(cdefs, "WLED_VERSION"): |
| 108 | + version = get_version() |
| 109 | + if version: |
| 110 | + cdefs.append(("WLED_VERSION", version)) |
| 111 | + |
| 112 | + # This transforms the node in to a Builder; it cannot be modified again |
| 113 | + return env.Object( |
| 114 | + node, |
| 115 | + CPPDEFINES=cdefs |
| 116 | + ) |
| 117 | + |
| 118 | +env.AddBuildMiddleware( |
| 119 | + add_wled_metadata_flags, |
| 120 | + "*/wled_metadata.cpp" |
| 121 | +) |
0 commit comments