scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Sep 2020 12:01:35 +0200
branchv_0
changeset 253 d8442b266ca8
parent 250 aae5009bd0af
permissions -rwxr-xr-x
combatibility with Java 9+, Tested with Java 11; JAXB library
franta-hg@81
     1
#!/usr/bin/perl
franta-hg@81
     2
franta-hg@83
     3
# SQL-DK
franta-hg@83
     4
# Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@83
     5
# 
franta-hg@83
     6
# This program is free software: you can redistribute it and/or modify
franta-hg@83
     7
# it under the terms of the GNU General Public License as published by
franta-hg@250
     8
# the Free Software Foundation, version 3 of the License.
franta-hg@83
     9
# 
franta-hg@83
    10
# This program is distributed in the hope that it will be useful,
franta-hg@83
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@83
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@83
    13
# GNU General Public License for more details.
franta-hg@83
    14
# 
franta-hg@83
    15
# You should have received a copy of the GNU General Public License
franta-hg@83
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@83
    17
franta-hg@83
    18
franta-hg@81
    19
# Parses Java source code from STDIN and generates script for BASH completion
franta-hg@81
    20
# Input (in this order):
franta-hg@81
    21
#	info/globalcode/sql/dk/Constants.java
franta-hg@81
    22
#	info/globalcode/sql/dk/formatting/*
franta-hg@81
    23
#	info/globalcode/sql/dk/CLIParser.java
franta-hg@81
    24
franta-hg@82
    25
# TODO: support database/formatter names with spaces
franta-hg@82
    26
franta-hg@81
    27
use strict;
franta-hg@81
    28
use warnings;
franta-hg@81
    29
franta-hg@81
    30
my $configDir = "~";
franta-hg@81
    31
franta-hg@81
    32
while (<>) {
franta-hg@81
    33
	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
franta-hg@81
    34
		$configDir .= "/$1";
franta-hg@81
    35
		last;
franta-hg@81
    36
	}
franta-hg@81
    37
}
franta-hg@81
    38
franta-hg@82
    39
my $databasesFile  = "$configDir/bash-completion/databases";
franta-hg@82
    40
my $formattersFile = "$configDir/bash-completion/formatters";
franta-hg@222
    41
my $defaultFormatterFile = "$configDir/bash-completion/default-formatter";
franta-hg@220
    42
my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
franta-hg@82
    43
franta-hg@110
    44
print '#have sql-dk &&
franta-hg@220
    45
_sql_dk_bash_completion_find_formatter() {
franta-hg@220
    46
	local previous
franta-hg@220
    47
	for token in "$@"; do
franta-hg@220
    48
		if [ "x$previous" == "x--formatter" ]; then
franta-hg@220
    49
			echo -n "$token";
franta-hg@222
    50
			return 0;
franta-hg@220
    51
		fi
franta-hg@220
    52
		previous="$token";
franta-hg@220
    53
	done
franta-hg@222
    54
	
franta-hg@222
    55
	if [ -f '.$defaultFormatterFile.' ]; then
franta-hg@222
    56
		cat '.$defaultFormatterFile.'
franta-hg@222
    57
	fi
franta-hg@220
    58
}
franta-hg@220
    59
franta-hg@222
    60
_sql_dk_bash_completion_formatter_property_name() {
franta-hg@220
    61
	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
franta-hg@220
    62
		local formatter_dir='.$formatterPropertiesDir.'/$formatter
franta-hg@220
    63
		if [ -d  $formatter_dir ]; then
franta-hg@220
    64
			ls -1 $formatter_dir;
franta-hg@220
    65
		fi
franta-hg@220
    66
	fi
franta-hg@220
    67
}
franta-hg@220
    68
franta-hg@220
    69
_sql_dk_bash_completion_formatter_property_choice() {
franta-hg@220
    70
	local property="${COMP_WORDS[COMP_CWORD-1]}";
franta-hg@221
    71
	local formatter_dir='.$formatterPropertiesDir.'/$formatter
franta-hg@221
    72
	local property_choices_file=$formatter_dir/$property/choices;
franta-hg@221
    73
	if [ -f $property_choices_file ]; then
franta-hg@221
    74
		cat $property_choices_file;
franta-hg@221
    75
	fi
franta-hg@220
    76
}
franta-hg@220
    77
franta-hg@220
    78
_sql_dk_bash_completion() {
franta-hg@220
    79
	local cur prev formatter
franta-hg@81
    80
franta-hg@81
    81
	COMPREPLY=()
franta-hg@81
    82
	cur=${COMP_WORDS[COMP_CWORD]}
franta-hg@81
    83
	prev=${COMP_WORDS[COMP_CWORD-1]}
franta-hg@81
    84
franta-hg@81
    85
	case "$prev" in
franta-hg@159
    86
	--db | --test-connection | --list-jdbc-properties)
franta-hg@82
    87
		if [ -f '.$databasesFile.' ]; then
franta-hg@82
    88
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
franta-hg@82
    89
			return 0
franta-hg@82
    90
		fi
franta-hg@81
    91
		;;
franta-hg@209
    92
	--formatter | --list-formatter-properties)
franta-hg@82
    93
		if [ -f '.$formattersFile.' ]; then
franta-hg@82
    94
			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
franta-hg@82
    95
		else
franta-hg@82
    96
			COMPREPLY=( $( compgen -W "
franta-hg@81
    97
';
franta-hg@81
    98
franta-hg@81
    99
while (<>) {
franta-hg@81
   100
	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
franta-hg@82
   101
		print "				$1\n";
franta-hg@81
   102
	}
franta-hg@81
   103
	last if (/\/\/\s*bash-completion:options/);
franta-hg@81
   104
}
franta-hg@81
   105
franta-hg@81
   106
franta-hg@82
   107
print '				" -- $cur ) );
franta-hg@82
   108
		fi
franta-hg@81
   109
		return 0
franta-hg@81
   110
		;;
franta-hg@220
   111
	--formatter-property)
franta-hg@220
   112
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
franta-hg@222
   113
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) ); 
franta-hg@220
   114
		return 0;
franta-hg@220
   115
		;;
franta-hg@81
   116
	esac;
franta-hg@220
   117
	
franta-hg@220
   118
	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
franta-hg@221
   119
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
franta-hg@220
   120
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
franta-hg@220
   121
		return 0;
franta-hg@220
   122
	fi
franta-hg@81
   123
franta-hg@81
   124
	COMPREPLY=( $( compgen -W "
franta-hg@81
   125
';
franta-hg@81
   126
franta-hg@81
   127
while (<>) {
franta-hg@81
   128
	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
franta-hg@81
   129
		print "			$1\n";
franta-hg@81
   130
	}
franta-hg@81
   131
}
franta-hg@81
   132
franta-hg@81
   133
print '		" -- $cur ) )
franta-hg@81
   134
	return 0
franta-hg@81
   135
franta-hg@81
   136
}
franta-hg@81
   137
franta-hg@220
   138
complete -F _sql_dk_bash_completion sql-dk
franta-hg@81
   139
';