Skip to content

Commit c840037

Browse files
committed
feat(release): Add script to append "-cn" to versions
1 parent 305a73a commit c840037

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

.github/scripts/on-release.sh

+2
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ echo "Generating $PACKAGE_JSON_DEV ..."
343343
cat "$PACKAGE_JSON_TEMPLATE" | jq "$jq_arg" > "$OUTPUT_DIR/$PACKAGE_JSON_DEV"
344344
# On MacOS the sed command won't skip the first match. Use gsed instead.
345345
sed '0,/github\.com\//!s|github\.com/|dl.espressif.cn/github_assets/|g' "$OUTPUT_DIR/$PACKAGE_JSON_DEV" > "$OUTPUT_DIR/$PACKAGE_JSON_DEV_CN"
346+
python "$SCRIPTS_DIR/release_append_cn.py" "$OUTPUT_DIR/$PACKAGE_JSON_DEV_CN"
346347
if [ "$RELEASE_PRE" == "false" ]; then
347348
echo "Generating $PACKAGE_JSON_REL ..."
348349
cat "$PACKAGE_JSON_TEMPLATE" | jq "$jq_arg" > "$OUTPUT_DIR/$PACKAGE_JSON_REL"
349350
# On MacOS the sed command won't skip the first match. Use gsed instead.
350351
sed '0,/github\.com\//!s|github\.com/|dl.espressif.cn/github_assets/|g' "$OUTPUT_DIR/$PACKAGE_JSON_REL" > "$OUTPUT_DIR/$PACKAGE_JSON_REL_CN"
352+
python "$SCRIPTS_DIR/release_append_cn.py" "$OUTPUT_DIR/$PACKAGE_JSON_REL_CN"
351353
fi
352354

353355
# Figure out the last release or pre-release

.github/scripts/release_append_cn.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#!/usr/bin/env python3
3+
4+
# Arduino IDE provides by default a package file for the ESP32. This causes version conflicts
5+
# when the user tries to use the JSON file with the Chinese mirrors.
6+
#
7+
# The downside is that the Arduino IDE will always warn the user that updates are available as it
8+
# will consider the version from the Chinese mirrors as a pre-release version.
9+
#
10+
# This script is used to append "-cn" to all versions in the package_esp32_index_cn.json file so that
11+
# the user can select the Chinese mirrors without conflicts.
12+
#
13+
# If Arduino ever stops providing the package_esp32_index.json file by default,
14+
# this script can be removed and the tags reverted.
15+
16+
import json
17+
18+
def append_cn_to_versions(obj):
19+
if isinstance(obj, dict):
20+
# dfu-util comes from arduino.cc and not from the Chinese mirrors, so we skip it
21+
if obj.get("name") == "dfu-util":
22+
return
23+
24+
for key, value in obj.items():
25+
if key == "version" and isinstance(value, str):
26+
if not value.endswith("-cn"):
27+
obj[key] = value + "-cn"
28+
else:
29+
append_cn_to_versions(value)
30+
31+
elif isinstance(obj, list):
32+
for item in obj:
33+
append_cn_to_versions(item)
34+
35+
def process_json_file(input_path, output_path=None):
36+
with open(input_path, "r", encoding="utf-8") as f:
37+
data = json.load(f)
38+
39+
append_cn_to_versions(data)
40+
41+
if output_path is None:
42+
output_path = input_path
43+
44+
with open(output_path, "w", encoding="utf-8") as f:
45+
json.dump(data, f, indent=2)
46+
47+
print(f"Updated JSON written to {output_path}")
48+
49+
if __name__ == "__main__":
50+
import sys
51+
if len(sys.argv) < 2:
52+
print("Usage: python release_append_cn.py input.json [output.json]")
53+
else:
54+
input_file = sys.argv[1]
55+
output_file = sys.argv[2] if len(sys.argv) > 2 else None
56+
process_json_file(input_file, output_file)

0 commit comments

Comments
 (0)