Repository navigation
Expand file tree
/
Copy pathversion-packages.sh
More file actions
executable file
·114 lines (102 loc) · 3.61 KB
/
Copy pathversion-packages.sh
File metadata and controls
executable file
·114 lines (102 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
set -eo pipefail
BASE=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
. "$BASE/tools/includes/check-osx-bash-version.sh"
. "$BASE/tools/includes/chalk-lite.sh"
. "$BASE/tools/includes/changelogger.sh"
# This script updates the composer.json file in the specified directory.
# It will update any monorepo packages to their latest version as declared in their changelog.
#
# Probably will be most useful in the release scripts that branch off, since we:
# a. Want to ship Jetpack with specific versions of packages
# b. Want to preserve @dev in trunk branch
function usage {
cat <<-EOH
usage: $0 [options] <directory>
Most options accepted by \`composer require\` are accepted to pass on
to composer while updating dependencies.
EOH
exit 1
}
DIR=
function check_dir {
if [[ ! -z "$DIR" ]]; then
error "Only one directory may be specified."
return 1
elif [[ -d "$1" ]]; then
DIR="${1%/}"
if [[ ! -f "$DIR/composer.json" ]]; then
error "$DIR does not contain composer.json."
return 1
fi
elif [[ "$1" == */composer.json && -f "$1" ]]; then # DWIM
DIR="$(dirname "$1")"
else
error "Directory $1 does not exist."
return 1
fi
}
COMPOSER_ARGS=()
while [[ $# -gt 0 ]]; do
arg="$1"
shift
case $arg in
-h|--help)
usage
exit
;;
--dev|-V|--version|-d|--working-dir|--working-dir=*)
die "Cannot pass $arg on to composer."
;;
--)
while [[ $# -gt 0 ]]; do
check_dir "$1"
shift
done
;;
-*)
COMPOSER_ARGS+=( "$arg" )
;;
*)
check_dir "$arg"
;;
esac
done
if [[ -z "$DIR" ]]; then
usage
fi
# Remove the monorepo repo from composer.json.
JSON=$(jq --tab 'if .repositories then .repositories |= map( select( .options.monorepo | not ) ) else . end' "$DIR/composer.json")
echo "$JSON" > "$DIR/composer.json"
# Get the list of packages to update, mapped to their target versions.
PACKAGES='{}'
for PKG in "$BASE"/projects/packages/*/composer.json; do
PACKAGES=$(jq -c --arg k "$(jq -r .name "$PKG")" --arg v "$(cd "${PKG%/composer.json}" && changelogger version current --default-first-version)" '.[$k] |= $v' <<<"$PACKAGES")
done
# Update the versions in composer.json, without actually updating them yet.
TO_UPDATE=()
mapfile -t TO_UPDATE < <(jq -r --argjson packages "$PACKAGES" '.require // {} | to_entries[] | select( ( .value | test( "^@dev$|\\.x-dev$" ) ) and $packages[.key] ) | "\(.key)=^\($packages[.key])"' "$DIR/composer.json")
if [[ ${#TO_UPDATE[@]} -gt 0 ]]; then
info "Updating packages: ${TO_UPDATE[*]}..."
composer require "${COMPOSER_ARGS[@]}" --no-update --working-dir="$DIR" -- "${TO_UPDATE[@]}"
fi
TO_UPDATE=()
mapfile -t TO_UPDATE < <(jq -r --argjson packages "$PACKAGES" '.["require-dev"] // {} | to_entries[] | select( ( .value | test( "^@dev$|\\.x-dev$" ) ) and $packages[.key] ) | "\(.key)=^\($packages[.key])"' "$DIR/composer.json")
if [[ ${#TO_UPDATE[@]} -gt 0 ]]; then
info "Updating dev packages: ${TO_UPDATE[*]}..."
composer require "${COMPOSER_ARGS[@]}" --no-update --working-dir="$DIR" --dev -- "${TO_UPDATE[@]}"
fi
# Now update all the deps.
"$BASE/tools/composer-update-monorepo.sh" "${COMPOSER_ARGS[@]}" "$DIR"
# If there's a lock file, check that the locked versions are as expected too.
EXIT=0
if [[ -e "$DIR/composer.lock" ]]; then
TMP="$(composer info --locked --format=json --working-dir="$DIR" | jq -r --argjson packages "$PACKAGES" '.locked[] | select( $packages[.name] ) | [ .name, .version, $packages[.name] ] | @tsv')"
while IFS=$'\t' read -r PKG LOCKVER EXPECTVER; do
if ! pnpm semver -c --range ">=$EXPECTVER" "$LOCKVER" >/dev/null; then
EXIT=1
error "$PKG was not upgraded ($LOCKVER < $EXPECTVER)"
fi
done <<<"$TMP"
fi
exit $EXIT