scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 222 5ffeb18b6f85
permissions -rwxr-xr-x
fix license version: GNU GPLv3

#!/usr/bin/perl

# SQL-DK
# Copyright © 2013 František Kučera (frantovo.cz)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# Parses Java source code from STDIN and generates script for BASH completion
# Input (in this order):
#	info/globalcode/sql/dk/Constants.java
#	info/globalcode/sql/dk/formatting/*
#	info/globalcode/sql/dk/CLIParser.java

# TODO: support database/formatter names with spaces

use strict;
use warnings;

my $configDir = "~";

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
		$configDir .= "/$1";
		last;
	}
}

my $databasesFile  = "$configDir/bash-completion/databases";
my $formattersFile = "$configDir/bash-completion/formatters";
my $defaultFormatterFile = "$configDir/bash-completion/default-formatter";
my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";

print '#have sql-dk &&
_sql_dk_bash_completion_find_formatter() {
	local previous
	for token in "$@"; do
		if [ "x$previous" == "x--formatter" ]; then
			echo -n "$token";
			return 0;
		fi
		previous="$token";
	done
	
	if [ -f '.$defaultFormatterFile.' ]; then
		cat '.$defaultFormatterFile.'
	fi
}

_sql_dk_bash_completion_formatter_property_name() {
	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
		local formatter_dir='.$formatterPropertiesDir.'/$formatter
		if [ -d  $formatter_dir ]; then
			ls -1 $formatter_dir;
		fi
	fi
}

_sql_dk_bash_completion_formatter_property_choice() {
	local property="${COMP_WORDS[COMP_CWORD-1]}";
	local formatter_dir='.$formatterPropertiesDir.'/$formatter
	local property_choices_file=$formatter_dir/$property/choices;
	if [ -f $property_choices_file ]; then
		cat $property_choices_file;
	fi
}

_sql_dk_bash_completion() {
	local cur prev formatter

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
	--db | --test-connection | --list-jdbc-properties)
		if [ -f '.$databasesFile.' ]; then
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
			return 0
		fi
		;;
	--formatter | --list-formatter-properties)
		if [ -f '.$formattersFile.' ]; then
			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
		else
			COMPREPLY=( $( compgen -W "
';

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
		print "				$1\n";
	}
	last if (/\/\/\s*bash-completion:options/);
}


print '				" -- $cur ) );
		fi
		return 0
		;;
	--formatter-property)
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) ); 
		return 0;
		;;
	esac;
	
	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
		return 0;
	fi

	COMPREPLY=( $( compgen -W "
';

while (<>) {
	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
		print "			$1\n";
	}
}

print '		" -- $cur ) )
	return 0

}

complete -F _sql_dk_bash_completion sql-dk
';