Add download function to styler

Can take input in the form user/repo and pulls packages from github into
its package directory. Documented use of new function.

Can download both processors and packages. Styler decides between
both with their naming schemes, searching for `/base16-` or `baseproc16-`
respectively. For example:

username/base16-programname - package
username/baseproc16-programname - processor

If it can not determine it based on name alone, it will assume to be
dealing with a processor but spit out a warning for the user.

Styler processors use same layout as packages, such that package and
processor directories are mirrors of each other. So:

`chriskempson/base16-vim` lies in the packages directory,
`marty-oehme/vim-processor` mirrors it.

This opens up the way for custom processors.
This commit is contained in:
Marty Oehme 2020-01-30 21:12:08 +01:00
parent 4838aecb75
commit acbe002f87

View file

@ -5,6 +5,8 @@ readonly BASE_PATH="${STYLER_DATA_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/sty
readonly PACKAGE_PATH="$BASE_PATH/packages"
readonly PROCESSOR_PATH="$BASE_PATH/processors"
readonly VERSION="0.2.4"
main() {
local cmd=""
local ret=0
@ -16,8 +18,11 @@ main() {
-l | --list | list)
cmd="list"
;;
-d | --download | download)
cmd="download"
;;
-v | --version | version)
printf "Program theming script.\n\n©Marty Oehme\n\nVersion: 0.1.0\n"
printf "Program theming script.\n\n©Marty Oehme\n\nVersion: %s\n" "$VERSION"
exit 0
;;
-h | --help | help | *)
@ -43,6 +48,9 @@ usage() {
"" \
" -s | set Set the theme. Use any valid base16 theme name (without base16- prefix)." \
"" \
" -d | download Download a base16 template into the package directory or download a processor16" \
" into the processor directory. Use user/repo format to automatically pull from github." \
"" \
" -h | help Print out this help." \
"" \
" -v | version Print out program information." \
@ -50,6 +58,13 @@ usage() {
""
}
# base directory should always exist
base_dir_exists_or_create() {
[[ -d "$BASE_PATH" ]] || mkdir "$BASE_PATH"
[[ -d "$PACKAGE_PATH" ]] || mkdir "$PACKAGE_PATH"
[[ -d "$PROCESSOR_PATH" ]] || mkdir "$BASE_PATH"
}
# retrieves all relevant packages from BASE_PATH/packages
# 'relevant' here means they follow github pattern of author/repository
get_packages() {
@ -70,10 +85,16 @@ get_packages() {
# retrieves all processors from BASE_PATH/processors
# 'relevant' here means they follow github pattern of author/repository
get_processors() {
for processor in "$PROCESSOR_PATH"/*; do
[[ -e "$processor" ]] || break
[[ -f "$processor" ]] || break
printf "%s\n" "$(basename -- "$processor")"
for author in "$PROCESSOR_PATH"/*; do
for package in "$author"/*; do
for processor in "$package"/*; do
[[ -e "$processor" ]] || break
[[ -f "$processor" ]] || break
if grep -q -e '/theme_[[:alnum:]]\{1,\}$' <<<"$processor"; then
printf "%s\n" "$(basename -- "$processor")"
fi
done
done
done
}
@ -111,7 +132,7 @@ set_theme() {
appext=$(sed "s|^[[:alnum:]]\{1,\}/base16-||" <<<"$pkg")
# Compares application extension with existing processors and runs the appropriate processor if found
processor="$PROCESSOR_PATH/theme_$appext"
processor=$(find "$PROCESSOR_PATH" -type f -name "theme_$appext")
if [[ -f "$processor" ]]; then
"$processor" "$PACKAGE_PATH" "$pkg" "$theme"
else
@ -140,4 +161,38 @@ list() {
esac
}
download() {
local pkg="$1"
local page="https://github.com"
local repo="$page/$pkg"
[[ -z "$pkg" ]] && {
echo "No package to download passed in. Please provide a package to download in the form user/repository."
exit 1
}
type git >/dev/null 2>&1 || {
echo "git is required to clone base16 package. Please install git."
exit 1
}
base_dir_exists_or_create
if ! git ls-remote --exit-code -h "$repo" >/dev/null; then
echo "Repository $repo not found."
exit 1
fi
# if package has patter name/base16-program, put it in packages; if name/process16-program put it in processors
# if none of the above, assume it's a processor but warn the user
if grep -q -e '^[0-9A-Za-z-]\{1,\}/base16-[0-9A-Za-z-]\{1,\}$' <<<"$pkg"; then
git clone "$repo" "$PACKAGE_PATH/$pkg"
elif grep -q -e '^[0-9A-Za-z-]\{1,\}/process16-[0-9A-Za-z-]\{1,\}$' <<<"$pkg"; then
git clone "$repo" "$PROCESSOR_PATH/$pkg"
else
echo "Package does not fit default naming scheme of packages/processors. Assuming it is a processor but please check manually."
git clone "$repo" "$PROCESSOR_PATH/$pkg"
fi
}
main "$@"