franta-hg@81: #!/usr/bin/perl franta-hg@81: franta-hg@83: # SQL-DK franta-hg@83: # Copyright © 2013 František Kučera (frantovo.cz) franta-hg@83: # franta-hg@83: # This program is free software: you can redistribute it and/or modify franta-hg@83: # it under the terms of the GNU General Public License as published by franta-hg@250: # the Free Software Foundation, version 3 of the License. franta-hg@83: # franta-hg@83: # This program is distributed in the hope that it will be useful, franta-hg@83: # but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@83: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@83: # GNU General Public License for more details. franta-hg@83: # franta-hg@83: # You should have received a copy of the GNU General Public License franta-hg@83: # along with this program. If not, see . franta-hg@83: franta-hg@83: franta-hg@81: # Parses Java source code from STDIN and generates script for BASH completion franta-hg@81: # Input (in this order): franta-hg@81: # info/globalcode/sql/dk/Constants.java franta-hg@81: # info/globalcode/sql/dk/formatting/* franta-hg@81: # info/globalcode/sql/dk/CLIParser.java franta-hg@81: franta-hg@82: # TODO: support database/formatter names with spaces franta-hg@82: franta-hg@81: use strict; franta-hg@81: use warnings; franta-hg@81: franta-hg@81: my $configDir = "~"; franta-hg@81: franta-hg@81: while (<>) { franta-hg@81: if (/"(.*?)".*? \/\/\s*bash-completion:dir/) { franta-hg@81: $configDir .= "/$1"; franta-hg@81: last; franta-hg@81: } franta-hg@81: } franta-hg@81: franta-hg@82: my $databasesFile = "$configDir/bash-completion/databases"; franta-hg@82: my $formattersFile = "$configDir/bash-completion/formatters"; franta-hg@222: my $defaultFormatterFile = "$configDir/bash-completion/default-formatter"; franta-hg@220: my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties"; franta-hg@82: franta-hg@110: print '#have sql-dk && franta-hg@220: _sql_dk_bash_completion_find_formatter() { franta-hg@220: local previous franta-hg@220: for token in "$@"; do franta-hg@220: if [ "x$previous" == "x--formatter" ]; then franta-hg@220: echo -n "$token"; franta-hg@222: return 0; franta-hg@220: fi franta-hg@220: previous="$token"; franta-hg@220: done franta-hg@222: franta-hg@222: if [ -f '.$defaultFormatterFile.' ]; then franta-hg@222: cat '.$defaultFormatterFile.' franta-hg@222: fi franta-hg@220: } franta-hg@220: franta-hg@222: _sql_dk_bash_completion_formatter_property_name() { franta-hg@220: if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes franta-hg@220: local formatter_dir='.$formatterPropertiesDir.'/$formatter franta-hg@220: if [ -d $formatter_dir ]; then franta-hg@220: ls -1 $formatter_dir; franta-hg@220: fi franta-hg@220: fi franta-hg@220: } franta-hg@220: franta-hg@220: _sql_dk_bash_completion_formatter_property_choice() { franta-hg@220: local property="${COMP_WORDS[COMP_CWORD-1]}"; franta-hg@221: local formatter_dir='.$formatterPropertiesDir.'/$formatter franta-hg@221: local property_choices_file=$formatter_dir/$property/choices; franta-hg@221: if [ -f $property_choices_file ]; then franta-hg@221: cat $property_choices_file; franta-hg@221: fi franta-hg@220: } franta-hg@220: franta-hg@220: _sql_dk_bash_completion() { franta-hg@220: local cur prev formatter franta-hg@81: franta-hg@81: COMPREPLY=() franta-hg@81: cur=${COMP_WORDS[COMP_CWORD]} franta-hg@81: prev=${COMP_WORDS[COMP_CWORD-1]} franta-hg@81: franta-hg@81: case "$prev" in franta-hg@159: --db | --test-connection | --list-jdbc-properties) franta-hg@82: if [ -f '.$databasesFile.' ]; then franta-hg@82: COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) ) franta-hg@82: return 0 franta-hg@82: fi franta-hg@81: ;; franta-hg@209: --formatter | --list-formatter-properties) franta-hg@82: if [ -f '.$formattersFile.' ]; then franta-hg@82: COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) ) franta-hg@82: else franta-hg@82: COMPREPLY=( $( compgen -W " franta-hg@81: '; franta-hg@81: franta-hg@81: while (<>) { franta-hg@81: if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) { franta-hg@82: print " $1\n"; franta-hg@81: } franta-hg@81: last if (/\/\/\s*bash-completion:options/); franta-hg@81: } franta-hg@81: franta-hg@81: franta-hg@82: print ' " -- $cur ) ); franta-hg@82: fi franta-hg@81: return 0 franta-hg@81: ;; franta-hg@220: --formatter-property) franta-hg@220: formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" ); franta-hg@222: COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) ); franta-hg@220: return 0; franta-hg@220: ;; franta-hg@81: esac; franta-hg@220: franta-hg@220: if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then franta-hg@221: formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" ); franta-hg@220: COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); franta-hg@220: return 0; franta-hg@220: fi franta-hg@81: franta-hg@81: COMPREPLY=( $( compgen -W " franta-hg@81: '; franta-hg@81: franta-hg@81: while (<>) { franta-hg@81: if (/"(.*?)".*? \/\/\s*bash-completion:option/) { franta-hg@81: print " $1\n"; franta-hg@81: } franta-hg@81: } franta-hg@81: franta-hg@81: print ' " -- $cur ) ) franta-hg@81: return 0 franta-hg@81: franta-hg@81: } franta-hg@81: franta-hg@220: complete -F _sql_dk_bash_completion sql-dk franta-hg@81: ';