Commit 753e8b8d authored by Charles Ferguson's avatar Charles Ferguson
Browse files

Add support for expanding to match language files.

We can expand the language names to match the extensions and the
magic recognition for those files. This means than many definitions
become a lot easier to set, as we may just specify which language
files to match.
parent 21dc1c91
......@@ -83,8 +83,66 @@ function config() {
##
# Expand a set of filenames from wildcards.
#
# This can be either:
# * A wildcarded filename in shell style.
# Examples: *.py foo/*.py
#
# * A language name in braces to restrict to that type, followed by a directory to search within
# Examples: {python} {python}foo
#
# @param $@ filenames with wildcards
#
# @return filename per line on stdout
function expand_filenames() {
local specs="$*"
local lang=''
local dir=''
local exts
local magic
local find_match
local spec
for spec in $specs ; do
if [[ "${spec:0:1}" == '{' ]] ; then
# This is a language specification
lang="${spec:1}"
lang="${lang%%\}*}"
dir="${spec#*\}}"
exts="$(call_if_function "${lang}_match_extensions")"
magic="$(call_if_function "${lang}_match_magic")"
# Construct the specification for find
find_match=()
for ext in $exts ; do
if [[ "${#find_match[@]}" != '0' ]] ; then
find_match+=('-o')
fi
find_match+=('-name' "*.$ext")
done
# List all the requested extensions
if [[ "${#find_match[@]}" != '0' ]] ; then
find "./$dir" -type f "${find_match[@]}" \
| (grep -E -v '(/\.|^\.//*ci/)' || true)
fi
# List all the files matching requested magic
if [[ "${magic}" != '' ]] ; then
if [[ "${#find_match[@]}" != '0' ]] ; then
find_match=('!' '(' "${find_match[@]}" ')')
else
find_match=()
fi
# Find files that do not match the extensions, are not in the
# special or hidden directories, and which match the magic
# output.
find "./$dir" -type f -size -100k "${find_match[@]}" -print0 \
| (grep -E -Z -z -v '(/\.|^\.//*ci/)' || true) \
| xargs -0 file -N -n -F':::' /dev/null \
| sed -E "/${magic}/! d; s/::: .*//"
fi
else
(
# If nothing matches a wildcard string, return empty
shopt -s nullglob
......@@ -92,10 +150,13 @@ function expand_filenames() {
# Note: Cannot use 'shopt -s globstar' to allow '**' to be subdirectory match.
# It isn't supported on OSX.
eval echo "$*"
eval echo "$spec"
)
fi
done | sed '/^$/d ; s!//*!/!g ; s!^\./!!' | sort -u
}
##
# Find the tool configuration for a given action.
#
......
......@@ -46,6 +46,16 @@ function perl_active() {
}
##
# Matching functions for filename expansion.
function perl_match_magic() {
echo -n 'Perl script'
}
function perl_match_extensions() {
echo -n 'pl pm'
}
##
# Help messages
function perl_help_lint() {
......
......@@ -94,6 +94,16 @@ function python_active() {
}
##
# Matching functions for filename expansion.
function python_match_magic() {
echo -n 'Python script'
}
function python_match_extensions() {
echo -n 'py'
}
##
# Help messages
function python_help_any() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment