-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_proto_compiler_dependency.sh
More file actions
194 lines (158 loc) · 6.05 KB
/
Copy pathupdate_proto_compiler_dependency.sh
File metadata and controls
194 lines (158 loc) · 6.05 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env sh
# This script updates the ondewo-nlu-client-python dependency in the REPO_DIR project
# --- Usage Check ---
if [ "$#" -ne 4 ]; then
echo "Usage: $0 <version> <programming_language> <node_version> <repo>" >&2
exit 1
fi
VERSION=$1
PROGRAMMING_LANGUAGE=$2
NODE_VERSION=$3
REPO=$4
# =============================================
# Script to update ondewo-proto-compiler version
# =============================================
CLEAN_UP="${CLEAN_UP:-true}"
REPO_DIR=$REPO-$PROGRAMMING_LANGUAGE
set -eu # Exit on error and treat unset variables as an error
# --- Colors ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color
# --- Portable colored logging (echo does not interpret \033 escapes under all /bin/sh) ---
log() { printf '%b\n' "$*"; }
# ONDEWO_TMP_DIR="/tmp/ondewo-$(date '+%Y%m%d_%H%M%S_%3N')"
ONDEWO_TMP_DIR="${ONDEWO_TMP_DIR:-/tmp/ondewo}"
TMP_DIR="${ONDEWO_TMP_DIR}/${REPO_DIR}"
log "${BLUE}[INFO]${NC} Updating ${REPO_DIR} to use ondewo-proto-compiler version: ${GREEN}${VERSION}${NC}"
if [ ! -d "$ONDEWO_TMP_DIR" ]; then
log "${YELLOW}[WARN]${NC} Temporary directory ${ONDEWO_TMP_DIR} does not exist, creating it..."
mkdir -p "$ONDEWO_TMP_DIR"
fi
# --- Navigate to temp directory ---
cd "$ONDEWO_TMP_DIR" || {
log "${RED}[ERROR]${NC} Failed to change to ${ONDEWO_TMP_DIR} directory."
exit 1
}
# --- Clean up existing clone if present ---
if [ -d "$TMP_DIR" ]; then
log "${YELLOW}[WARN]${NC} Removing existing temporary directory: ${TMP_DIR}"
rm -rf "$TMP_DIR"
fi
# --- Clone repository ---
log "${BLUE}[INFO]${NC} Cloning repository: git@github.com:ondewo/${REPO_DIR}.git"
git clone "git@github.com:ondewo/${REPO_DIR}.git"
# --- Checkout the desired version ---
cd "$REPO_DIR" || {
log "${RED}[ERROR]${NC} Cannot enter repository directory: $REPO_DIR"
exit 1
}
# --- Set NODE_VERSION in Dockerfile.utils from environment variable ---
if [ -f "Dockerfile.utils" ]; then
if [ -z "${NODE_VERSION:-}" ]; then
log "${RED}[ERROR]${NC} NODE_VERSION environment variable is not set"
exit 1
fi
# -i.bak (not bare -i) keeps this working with both GNU and BSD/macOS sed
sed -i.bak "s/^ENV NODE_VERSION=.*/ENV NODE_VERSION=${NODE_VERSION}/" Dockerfile.utils
rm -f Dockerfile.utils.bak
git add Dockerfile.utils
if ! git diff --cached --quiet; then
git commit -m "Set NODE_VERSION to ${NODE_VERSION} in Dockerfile.utils"
git push
log "${BLUE}[INFO]${NC} Set NODE_VERSION in Dockerfile.utils to ${NODE_VERSION}"
else
log "${YELLOW}[NOOP]${NC} No changes to commit for Dockerfile.utils."
fi
else
log "${RED}[ERROR]${NC} Dockerfile.utils not found in ondewo-proto-compiler directory"
exit 1
fi
log "${BLUE}[INFO]${NC} Updating submodules..."
git submodule update --init --recursive
cd "ondewo-proto-compiler" || {
log "${RED}[ERROR]${NC} Cannot enter submodule directory: ondewo-proto-compiler"
exit 1
}
log "${BLUE}[INFO]${NC} Checking out version: ${GREEN}${VERSION}${NC}"
git checkout "$VERSION"
cd ..
# --- Update the dependency in the project ---
if [ "$PROGRAMMING_LANGUAGE" = "angular" ] || \
[ "$PROGRAMMING_LANGUAGE" = "typescript" ] || \
[ "$PROGRAMMING_LANGUAGE" = "nodejs" ] || \
[ "$PROGRAMMING_LANGUAGE" = "js" ]; then
if [ "$PROGRAMMING_LANGUAGE" = "js" ]; then
IMAGE_DATA_PKG="ondewo-proto-compiler/js/image-data/default-lib-files/package.json"
else
IMAGE_DATA_PKG="ondewo-proto-compiler/${PROGRAMMING_LANGUAGE}/image-data/package.json"
fi
TARGET_PKG="src/package.json"
log "${BLUE}[INFO]${NC} Updating $TARGET_PKG from $IMAGE_DATA_PKG"
if [ ! -f "$IMAGE_DATA_PKG" ]; then
log "${RED}[ERROR]${NC} Missing source package.json: $IMAGE_DATA_PKG"
exit 1
fi
if [ ! -f "$TARGET_PKG" ]; then
log "${RED}[ERROR]${NC} Missing target package.json: $TARGET_PKG"
exit 1
fi
IMAGE_DEPS=$(jq -c '[
.dependencies // {},
.devDependencies // {},
.peerDependencies // {}
] | add' "$IMAGE_DATA_PKG") || { log "${RED}[ERROR]${NC} Failed to parse $IMAGE_DATA_PKG" >&2; exit 1; }
# Explicit template: BSD/macOS mktemp requires a trailing run of X's (bare
# `mktemp` with no operand errors there); GNU accepts the same template.
TMP_PKG=$(mktemp "${TMPDIR:-/tmp}/proto-compiler.XXXXXX")
trap 'rm -f "$TMP_PKG"' EXIT
log "${BLUE}[INFO]${NC} Updating only existing dependency versions..."
jq --argjson imageDeps "$IMAGE_DEPS" '
def update_existing(section):
if .[section] then
.[section] |= with_entries(
if $imageDeps[.key] then
.value = $imageDeps[.key]
else
.
end
)
| if (.[section] | length == 0) then del(.[section]) else . end
else
.
end;
update_existing("dependencies") |
update_existing("devDependencies") |
update_existing("peerDependencies")
' "$TARGET_PKG" > "$TMP_PKG" && mv "$TMP_PKG" "$TARGET_PKG"
if [ -n "$TARGET_PKG" ]; then
log "${BLUE}[INFO]${NC} Adding updated package.json to staging area: ${TARGET_PKG} ..."
git add "${TARGET_PKG}"
fi
else
log "${YELLOW}[WARN]${NC} Programming language '$PROGRAMMING_LANGUAGE' does not require package.json update."
fi
log "${BLUE}[INFO]${NC} Diff of updated git repo ${REPO_DIR}:"
git --no-pager diff .
log "${BLUE}[INFO]${NC} Adding ondewo-proto-compiler submodule..."
git add ondewo-proto-compiler
if ! git diff --cached --quiet; then
log "${BLUE}[INFO]${NC} Changes detected, preparing to commit..."
log "${BLUE}[INFO]${NC} Committing version update..."
git commit -m "Update proto compiler dependency to version $VERSION"
log "${BLUE}[INFO]${NC} Pushing to remote..."
git push
log "${GREEN}[SUCCESS]${NC} Updated $REPO_DIR dependency to ondewo-proto-compiler to version $VERSION."
else
log "${YELLOW}[NOOP]${NC} No changes to commit."
fi
# --- Cleanup ---
if [ "${CLEAN_UP:-true}" = "true" ]; then
log "${BLUE}[INFO]${NC} Cleaning up temporary files for ${TMP_DIR} ..."
rm -rf "${TMP_DIR}"
log "${GREEN}[DONE]${NC} Update process completed successfully for ${TMP_DIR}."
else
log "${YELLOW}[SKIP]${NC} Skipping cleanup as per user request."
fi