You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.4 KiB
155 lines
4.4 KiB
#!/usr/bin/env bash
|
|
|
|
##? Usage:
|
|
##? run [build|init|needed]
|
|
##? run --help
|
|
##?
|
|
##? Options:
|
|
##? --help Show this help
|
|
##?
|
|
##?
|
|
##? Requirements:
|
|
##?
|
|
##? This script is used to build language grammars from Atom's various
|
|
##? github repositories. They must be built because Atom stupidly uses
|
|
##? CoffeeScript's pointless CSON format and therefore needs to be converted to
|
|
##? JSON to be usable. To build these grammars the following packages must be
|
|
##? installed on a unix-based system:
|
|
##?
|
|
##? * bash >=5.0
|
|
##? * composer >=2.0.6
|
|
##? * docopts >=0.6.3
|
|
##? * GNU awk >=5.1.0
|
|
##? * GNU coreutils >=8.0
|
|
##? * GNU parallel >=20180922
|
|
##? * yarn >=1.22.10
|
|
##?
|
|
##? Arch Linux:
|
|
##? ```
|
|
##? pacman -S composer docopts parallel yarn
|
|
##? ```
|
|
##?
|
|
##? macOS:
|
|
##? ```
|
|
##? brew install bash composer coreutils docopts gawk parallel yarn
|
|
##? echo \"/usr/local/bin/bash\" >> /etc/shells
|
|
##? chsh -s /usr/local/bin/bash
|
|
##? ```
|
|
##?
|
|
##? Building the grammars is then a matter of running the following commands
|
|
##? from the project folder:
|
|
##?
|
|
##? ```
|
|
##? ./run init
|
|
##? ./run build
|
|
##? ```
|
|
##?
|
|
##?
|
|
##? Commands:
|
|
##?
|
|
##? init
|
|
##? Initializes the dependencies necessary to build the language
|
|
##? grammars.
|
|
##?
|
|
##? build
|
|
##? Build the language grammars. Puts them into data/.
|
|
##?
|
|
##? needed
|
|
##? List the language grammars that are included in the current grammars
|
|
##? but are not in the data/ folder.
|
|
##?
|
|
|
|
set -e
|
|
cwd=$(pwd)
|
|
PATH="$cwd/node_modules/.bin:$PATH"
|
|
help=$(grep "^##?" "$0" | cut -c 5-)
|
|
eval "$(docopts -A args -h "$help" : "$@")"
|
|
|
|
fail() {
|
|
echo $help
|
|
exit 1
|
|
}
|
|
|
|
# Check for bash 4+ and set extra glob options
|
|
shopt -s globstar extglob || fail
|
|
|
|
# macOS uses the BSD version of awk which isn't compatible with the linux
|
|
# version, so this script requires Mac users to install the GNU version; it
|
|
# therefore has a different name on macOS.
|
|
awkcmd="awk"
|
|
if [ "$(uname)" == "Darwin" ]; then
|
|
awkcmd="gawk"
|
|
fi
|
|
|
|
# Check for presence of dependencies
|
|
if (( $(which $awkcmd composer parallel yarn | wc -l) < 4 )); then fail; fi
|
|
|
|
# Process the subcommand
|
|
subcmd="build"
|
|
subcmd=$([ "${args[init]}" == "true" ] && echo "init" || echo "$subcmd")
|
|
subcmd=$([ "${args[needed]}" == "true" ] && echo "needed" || echo "$subcmd")
|
|
|
|
# Grammar table of the github repository location followed by its local folder
|
|
# name
|
|
read -r -d '' grammarTable <<'EOF'
|
|
franzheidl/atom-applescript applescript
|
|
atom/language-c c
|
|
atom/language-coffee-script coffeescript
|
|
atom/language-css css
|
|
rmosolgo/language-graphql graphql
|
|
atom/language-html html
|
|
atom/language-java java
|
|
atom/language-javascript javascript
|
|
atom/language-json json
|
|
burodepeper/language-markdown markdown
|
|
Azganoth/language-lua-plus lua
|
|
atom/language-php php
|
|
atom/language-python python
|
|
atom/language-ruby ruby
|
|
atom/language-shellscript shell
|
|
MaxGiting/atom-language-smarty smarty
|
|
atom/language-sql sql
|
|
al3x/atom-language-textile textile
|
|
atom/language-typescript typescript
|
|
atom/language-xml xml
|
|
EOF
|
|
|
|
case $subcmd in
|
|
init)
|
|
yarn install
|
|
mkdir -p "$cwd"/deps
|
|
rm -rf "$cwd"/deps/*
|
|
|
|
cd "$cwd"/deps
|
|
printf "$grammarTable" | parallel --colsep ' ' "git clone https://github.com/{1}.git"
|
|
cd "$cwd"
|
|
;;
|
|
|
|
|
|
needed)
|
|
grep -Fxv -f <(cd "$cwd"/data && ls -1 *.json | sed -e 's/\.json$//g') <(cat "$cwd"/data/*.json | grep -E "\"include\"\s*:\s*\"[^#\$]+\"" | $awkcmd '{ match($0, /"include": "(.+?)"/, arr); if(arr[1] != "") print arr[1] }' | sort -u)
|
|
;;
|
|
|
|
|
|
build | *)
|
|
rm -rf "$cwd"/data/*
|
|
|
|
# Copy grammars folders from all the local language grammar repositories to a
|
|
# temporary folder with the names provided in the table above
|
|
temp=$(mktemp -d)
|
|
printf "$grammarTable" | parallel --colsep ' ' "mkdir -p $temp/{2} &&
|
|
cp -r \"$cwd\"/deps/\$(basename {1})/grammars/* $temp/{2}/"
|
|
|
|
# Convert all relevant CSON files to a format that can actually be used :)
|
|
ls -1 $temp/**/!(tree-sitter-*).cson | parallel "csonc --output={.}.json {} && rm {}"
|
|
|
|
# Remove any remaining tree sitter files; will also remove JSON ones from
|
|
# packages that didn't use CSON
|
|
rm $temp/**/tree-sitter-*
|
|
|
|
# Lastly, move everything over to the project's data folder and delete the
|
|
# temporary folder
|
|
mv $temp/* "$cwd"/data/
|
|
rmdir $temp
|
|
;;
|
|
esac
|