Debian Codegraph @

util/publish.sh

raw
1 #!/bin/sh
2 ARG=""
3 if test "$1" != "--do-it"; then
4 ARG="--dry-run --allow-dirty"
5 fi
6
7 # Function to check if the crate is already published
8 is_already_published() {
9 local crate_name=$1
10 local crate_version=$2
11
12 # Use the sparse index (meant for automated access, unlike the crates.io
13 # API which rejects requests without a browser-like user agent) and check
14 # whether this exact version has been published.
15 local prefix
16 case ${#crate_name} in
17 1) prefix="1" ;;
18 2) prefix="2" ;;
19 3) prefix="3/${crate_name:0:1}" ;;
20 *) prefix="${crate_name:0:2}/${crate_name:2:2}" ;;
21 esac
22
23 curl -s "https://index.crates.io/$prefix/$crate_name" \
24 | jq -r '.vers' 2>/dev/null \
25 | grep -qx "$crate_version"
26 }
27
28 # Figure out any dependencies between the util via Cargo.toml
29 # We store this as edges in a graph with each line:
30 # [dependent] [dependency]
31 # We use ROOT as a the node that should come before all other nodes.
32 PROGS=$(ls -1d src/uu/*/)
33 PARTIAL_ORDER=""
34 for p in $PROGS; do
35 DEPENDENCIES=$(grep -oE "^uu_[a-z0-9_]+" ${p}Cargo.toml)
36
37 # Turn "src/uu/util/" into "util"
38 p=${p#src/uu/}
39 p=${p%/}
40
41 PARTIAL_ORDER+="$p ROOT\n"
42 while read d; do
43 if [ $d ]; then
44 # Remove "uu_" prefix
45 d=${d#uu_}
46
47 PARTIAL_ORDER+="$p $d\n"
48 fi
49 done <<<"$DEPENDENCIES"
50 done
51
52 # Apply tsort to get the order in which to publish the crates
53 TOTAL_ORDER=$(echo -e $PARTIAL_ORDER | tsort | tac)
54
55 # Remove the ROOT node from the start
56 TOTAL_ORDER=${TOTAL_ORDER#ROOT}
57
58 CRATE_VERSION=$(grep '^version =' Cargo.toml | head -n1 | cut -d '"' -f2)
59
60 set -e
61 CORE_DIRS="src/uucore_procs/ src/uucore/ src/uu/stdbuf/src/libstdbuf/ tests/uutests/ fuzz/uufuzz/"
62 CORE_COUNT=$(echo $CORE_DIRS | wc -w)
63 UTIL_COUNT=$(echo $TOTAL_ORDER | wc -w)
64 TOTAL_COUNT=$((CORE_COUNT + UTIL_COUNT + 1))
65 CURRENT=0
66
67 for dir in $CORE_DIRS; do
68 CURRENT=$((CURRENT + 1))
69 echo "[$CURRENT/$TOTAL_COUNT] Processing: $dir"
70 (
71 cd "$dir"
72 CRATE_NAME=$(grep '^name =' "Cargo.toml" | head -n1 | cut -d '"' -f2)
73 #shellcheck disable=SC2086
74 if ! is_already_published "$CRATE_NAME" "$CRATE_VERSION"; then
75 cargo publish $ARG
76 else
77 echo "Skip: $CRATE_NAME $CRATE_VERSION already published"
78 fi
79 )
80 sleep 2s
81 done
82
83 for p in $TOTAL_ORDER; do
84 CURRENT=$((CURRENT + 1))
85 echo "[$CURRENT/$TOTAL_COUNT] Processing: $p"
86 (
87 cd "src/uu/$p"
88 CRATE_NAME=$(grep '^name =' "Cargo.toml" | head -n1 | cut -d '"' -f2)
89 #shellcheck disable=SC2086
90 if ! is_already_published "$CRATE_NAME" "$CRATE_VERSION"; then
91 cargo publish $ARG
92 else
93 echo "Skip: $CRATE_NAME $CRATE_VERSION already published"
94 fi
95 )
96 done
97
98 CURRENT=$((CURRENT + 1))
99 echo "[$CURRENT/$TOTAL_COUNT] Processing: main coreutils crate"
100 #shellcheck disable=SC2086
101 cargo publish $ARG