|
| 1 | +#!/bin/zsh |
| 2 | +# update-deps-to-path.sh goal is to replace git dependencies with path dependencies |
| 3 | +# in the target Cargo.toml file. |
| 4 | +# |
| 5 | +# The script will scan the source repos for packages and build a mapping of package name to path. |
| 6 | +# It will then process the target Cargo.toml file line by line and replace git dependencies with path dependencies. |
| 7 | +# |
| 8 | +# The script will output the new Cargo.toml file to stdout. |
| 9 | +# |
| 10 | +# The script will exit with a non-zero status if the target Cargo.toml file is not found. |
| 11 | +# Usage: ./scripts/update-deps-to-path.sh ./Cargo.toml ../polkadot-sdk-otf > Cargo.toml.new |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +TARGET_TOML="${1:?Usage: $0 <target-cargo-toml> <source-repo-1> [source-repo-2] ...}" |
| 16 | +shift |
| 17 | + |
| 18 | +if [[ $# -eq 0 ]]; then |
| 19 | + echo "Error: At least one source repo path required" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Build package name -> path mapping from all source repos |
| 24 | +typeset -A PKG_PATHS |
| 25 | + |
| 26 | +for SOURCE_PATH in "$@"; do |
| 27 | + SOURCE_PATH="$(cd "$SOURCE_PATH" && pwd)" |
| 28 | + echo "Scanning $SOURCE_PATH for packages..." >&2 |
| 29 | + |
| 30 | + for cargo_toml in $(find "$SOURCE_PATH" -name "Cargo.toml" -type f 2>/dev/null); do |
| 31 | + pkg_name=$(yq -p toml -o yaml '.package.name // ""' "$cargo_toml" 2>/dev/null | tr -d '"') |
| 32 | + |
| 33 | + if [[ -n "$pkg_name" && "$pkg_name" != "null" ]]; then |
| 34 | + pkg_dir="$(dirname "$cargo_toml")" |
| 35 | + PKG_PATHS[$pkg_name]="$pkg_dir" |
| 36 | + echo " Found: $pkg_name" >&2 |
| 37 | + fi |
| 38 | + done |
| 39 | +done |
| 40 | + |
| 41 | +echo "Found ${#PKG_PATHS[@]} total packages" >&2 |
| 42 | +echo "" >&2 |
| 43 | + |
| 44 | +# Process target Cargo.toml line by line |
| 45 | +echo "Updating dependencies in $TARGET_TOML..." >&2 |
| 46 | + |
| 47 | +while IFS= read -r line; do |
| 48 | + # Check if this line has a git dependency |
| 49 | + if [[ "$line" =~ ^([a-zA-Z0-9_-]+|\"[^\"]+\")\ *=\ *\{.*git\ *=\ *\" ]]; then |
| 50 | + # Extract package name (handle both quoted and unquoted) |
| 51 | + dep_name=$(echo "$line" | sed -E 's/^"?([a-zA-Z0-9_-]+)"? *=.*/\1/') |
| 52 | + |
| 53 | + # Check for package alias |
| 54 | + if [[ "$line" =~ package\ *=\ *\"([^\"]+)\" ]]; then |
| 55 | + lookup_name="${match[1]}" |
| 56 | + else |
| 57 | + lookup_name="$dep_name" |
| 58 | + fi |
| 59 | + |
| 60 | + # Check if we have this package |
| 61 | + if [[ -n "${PKG_PATHS[$lookup_name]}" ]]; then |
| 62 | + pkg_path="${PKG_PATHS[$lookup_name]}" |
| 63 | + echo " $dep_name -> $pkg_path" >&2 |
| 64 | + |
| 65 | + # Extract features/default-features/package if present |
| 66 | + extras="" |
| 67 | + if [[ "$line" =~ default-features\ *=\ *false ]]; then |
| 68 | + extras="$extras, default-features = false" |
| 69 | + fi |
| 70 | + if [[ "$line" =~ package\ *=\ *\"([^\"]+)\" ]]; then |
| 71 | + extras="$extras, package = \"${match[1]}\"" |
| 72 | + fi |
| 73 | + if [[ "$line" =~ features\ *=\ *\[([^\]]*)\] ]]; then |
| 74 | + extras="$extras, features = [${match[1]}]" |
| 75 | + fi |
| 76 | + |
| 77 | + # Output new line with just path |
| 78 | + echo "${dep_name} = { path = \"${pkg_path}\"${extras} }" |
| 79 | + else |
| 80 | + # Package not found in sources, keep original |
| 81 | + echo "$line" |
| 82 | + fi |
| 83 | + else |
| 84 | + # Not a git dependency, keep as-is |
| 85 | + echo "$line" |
| 86 | + fi |
| 87 | +done < "$TARGET_TOML" |
| 88 | + |
| 89 | +echo "" >&2 |
| 90 | +echo "Done!" >&2 |
0 commit comments