zed-discord-presence/scripts/get_languages.sh

74 lines
2.3 KiB
Bash
Raw Normal View History

2024-07-20 14:10:02 +02:00
#!/bin/bash
fetch_files() {
local repo_owner=$1
local repo_name=$2
2024-08-16 14:02:04 +02:00
curl -Ls "https://api.github.com/repos/$repo_owner/$repo_name/git/trees/HEAD?recursive=1" -H "Authorization: Bearer ${GITHUB_TOKEN}" | jq -c -r '.tree[] | .path'
}
process_language_files() {
local files=$1
local repo_owner=$2
local repo_name=$3
local file_patterns=("${@:4}")
for file in $files; do
local match=false
for pattern in "${file_patterns[@]}"; do
if [[ $file == $pattern ]]; then
match=true
break
fi
done
if ! $match; then
continue
fi
2024-08-16 14:02:04 +02:00
name=$(curl -s "https://raw.githubusercontent.com/$repo_owner/$repo_name/HEAD/${file}" | grep -oP '^name = "\K[^"]*')
id="${file%/config.toml}"
id="${id##*/}"
if [[ $LANGUAGES == *"$name\","* && $LANGUAGE_IDS == *"$name\" = \"$id\","* ]]; then
echo "Skipping language $name with id $id (already added)"
else
echo "Adding language $name with id $id"
LANGUAGES+=" \"$name\",\n"
LANGUAGE_IDS+=" \"$name\" = \"$id\","
fi
done
}
2024-10-03 20:39:00 +02:00
hardcoded_languages() {
LANGUAGES+=" \"edge\",\n"
LANGUAGE_IDS+=" \"edge\" = \"edge\","
}
2024-07-20 14:10:02 +02:00
RESPONSE=$(curl -s "https://api.github.com/repos/zed-industries/zed/git/trees/main?recursive=1" -H "Authorization: Bearer ${GITHUB_TOKEN}")
EXTENSIONS_RESPONSE=$(curl -s "https://raw.githubusercontent.com/zed-industries/extensions/main/.gitmodules")
REPOSITORIES=($(echo "$EXTENSIONS_RESPONSE" | grep -oP 'url = \K.*'))
2024-07-20 14:10:02 +02:00
FILES=$(echo "$RESPONSE" | jq -c -r '.tree[] | .path')
LANGUAGES="languages = [\n"
LANGUAGE_IDS="language_ids = {"
for repository in "${REPOSITORIES[@]}"; do
repo_owner=$(echo "$repository" | cut -d'/' -f4)
repo_name=$(echo "$repository" | cut -d'/' -f5 | sed 's/.git$//')
2024-07-20 14:10:02 +02:00
echo "Checking $repository ($repo_owner/$repo_name)"
files=$(fetch_files "$repo_owner" "$repo_name")
process_language_files "$files" "$repo_owner" "$repo_name" "languages/**/config.toml"
2024-07-20 14:10:02 +02:00
done
echo "Processing zed-industries/zed"
process_language_files "$FILES" "zed-industries" "zed" "crates/languages/src/**/config.toml" "extensions/**/languages/**/config.toml"
2024-10-03 20:39:00 +02:00
hardcoded_languages
2024-07-20 14:10:02 +02:00
LANGUAGES+="]"
LANGUAGE_IDS+="}"
echo -e $LANGUAGES
echo -e $LANGUAGE_IDS