#!/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. cwd=$(pwd) PATH="$cwd/node_modules/.bin:$PATH" help=$(grep "^##?" "$0" | cut -c 5-) eval "$(docopts -h "$help" : "$@")" fail() { echo $help exit 1 } # Check for bash 4+ shopt -s globstar extglob || help awkcmd="awk" if [ "$(uname)" == "Darwin" ]; then awkcmd="gawk" fi # Check for presence of dependencies if (( $(which $awkcmd composer parallel yarn | wc -l) < 4 )); then help; fi read -r -d '' grammarList <<'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 $1 in init) yarn install mkdir -p "$cwd"/deps rm -rf "$cwd"/deps/* cd "$cwd"/deps printf "$grammarList" | 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/* temp=$(mktemp -d) printf "$grammarList" | parallel --colsep ' ' "mkdir -p $temp/{2} && cp -r \"$cwd\"/deps/\$(basename {1})/grammars/* $temp/{2}/" ls -1 $temp/**/!(tree-sitter-*).cson | parallel "csonc --output={.}.json {} && rm {}" rm -rf $temp/**/*.cson mv $temp/* "$cwd"/data/ ;; esac