#!/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 coreutils >=8.0 ##? * GNU parallel >=20180922 ##? * jq >=1.6 ##? * yarn >=1.22.10 ##? ##? Arch Linux: ##? ##? ##? pacman -S composer docopts jq parallel yarn ##? ##? ##? macOS: ##? ##? ##? brew install bash composer coreutils docopts jq 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 ##? Builds the language grammars; puts them into data/. ##? ##? needed ##? Lists 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 # Check for presence of dependencies if (( $(which composer jq 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 grammars="franzheidl/atom-applescript atom/language-c atom/language-coffee-script atom/language-csharp atom/language-css miketheman/language-diff atom/language-gfm atom/language-git atom/language-go atom/language-html atom/language-hyperlink atom/language-java atom/language-javascript atom/language-json atom/language-less Azganoth/language-lua-plus burodepeper/language-markdown atom/language-make atom/language-mustache atom/language-objective-c atom/language-perl atom/language-php atom/language-property-list atom/language-python atom/language-ruby atom/language-ruby-on-rails zargony/atom-language-rust atom/language-sass atom/language-shellscript atom/language-sql kelvin13/atom-swift-89 atom/language-text atom/language-todo al3x/atom-language-textile atom/language-typescript atom/language-xml atom/language-yaml" case $subcmd in init) yarn install mkdir -p "$cwd"/deps rm -rf "$cwd"/deps/* cd "$cwd"/deps printf "$grammars" | parallel "git clone --depth 1 https://github.com/{}.git" cd "$cwd" ;; needed) grep -Fv -f <(ls -1 "$cwd"/data/*.json | parallel "basename {} .json") <(ls -1 "$cwd"/data/*.json | parallel "jq -r '.. | select(.include | startswith(\"\$\") or startswith(\"#\") | not)? | .include?' {}" | sort | uniq | tail -n +2) ;; build) rm -rf "$cwd"/data/* # Copy root non-tree-sitter cson and json grammar files to a temporary folder in # subfolders with the repository's name. temp=$(mktemp -d) ls -1 "$cwd"/deps/*/grammars/!(tree-sitter-*).@(cson|json) | parallel " folder=\"$temp/\$(basename \$(dirname \$(dirname {})))\"; mkdir -p \"\$folder\" && cp {} \"\$folder\"/{/}" # Convert all cson files in the temp folder to json files ls -1 $temp/**/*.cson | parallel "csonc --output={.}.json {} && rm {}" # Move all json files to the root of the temp folder with the filename's being # the scopeName property within the json files ls -1 $temp/**/*.json | parallel "mv {} \"$temp/\$(jq -r '.scopeName' {}).json\"" # Finally, move the json files to the data folder mv $temp/*.json "$cwd"/data/ rm -rf $temp ;; esac