fish-shell/share/functions/__fish_complete_ant_targets.fish

56 lines
2.0 KiB
Fish
Raw Normal View History

function __fish_complete_ant_targets -d "Print list of targets from build.xml and imported files"
2019-01-03 22:03:22 +08:00
function __get_buildfile -d "Get a buildfile that will be used by ant"
set -l tokens $argv # tokens from 'commandline -co'
set -l prev $tokens[1]
set -l buildfile "build.xml"
for token in $argv[2..-1]
switch $prev
case -buildfile -file -f
set buildfile (eval echo $token)
end
set prev $token
end
# return last one
echo $buildfile
end
2019-01-13 20:36:05 +08:00
function __parse_ant_targets_from_projecthelp -d "Parse ant targets from projecthelp"
2019-01-03 22:03:22 +08:00
set -l buildfile $argv[1] # full path to buildfile
2019-01-14 12:08:29 +08:00
set -l targets (ant -p -debug -f $buildfile 2> /dev/null | string match -r '^\s[[:graph:]].*$')
2019-01-03 22:03:22 +08:00
for target in $targets
2019-01-14 12:08:29 +08:00
# Use [[:graph:]] and [[:print:]] to ignore ANSI escape code
2019-01-13 20:36:05 +08:00
set -l tokens (string match -r '^\s([[:graph:]]+)(?:\s+([[:print:]]+))?' "$target")
if [ (count $tokens) -ge 3 ]
echo $tokens[2]\t$tokens[3]
else if [ (count $tokens) -ge 2 ]
echo $tokens[2]
2019-01-03 22:03:22 +08:00
end
end
end
2019-01-13 20:36:05 +08:00
function __get_ant_targets_from_projecthelp -d "Get ant targets from projecthelp"
2019-01-03 22:03:22 +08:00
set -l buildfile $argv[1] # full path to buildfile
2019-01-14 12:08:29 +08:00
if [ \( -z "$XDG_CACHE_HOME" \) -o \( ! -d "$XDG_CACHE_HOME" \) ]
2019-01-17 15:32:57 +08:00
set XDG_CACHE_HOME "$HOME/.cache"
end
2019-01-17 15:32:57 +08:00
2019-01-14 12:08:29 +08:00
set -l cache_dir "$XDG_CACHE_HOME/fish/ant_completions"
2019-01-13 20:36:05 +08:00
mkdir -p $cache_dir
2019-01-14 12:08:29 +08:00
set -l cache_file $cache_dir/(fish_md5 -s $buildfile)
if [ ! -s "$cache_file" ]
2019-01-13 20:36:05 +08:00
# generate cache file if empty
__parse_ant_targets_from_projecthelp $buildfile >$cache_file
2019-01-13 20:36:05 +08:00
end
cat $cache_file
end
2019-01-03 22:03:22 +08:00
2019-01-13 20:36:05 +08:00
set -l tokens $argv
2019-01-18 09:24:14 +08:00
set -l buildfile (realpath -eq $buildfile (__get_buildfile $tokens))
if [ $status -ne 0 ]
2019-01-13 20:36:05 +08:00
return 1 # return nothing if buildfile does not exist
2019-01-03 22:03:22 +08:00
end
2019-01-13 20:36:05 +08:00
__get_ant_targets_from_projecthelp $buildfile
end