Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Last active October 22, 2020 15:47
Show Gist options
  • Save drewbrokke/d6e4889c0dff1ea4d7c5c31a17cded94 to your computer and use it in GitHub Desktop.
Save drewbrokke/d6e4889c0dff1ea4d7c5c31a17cded94 to your computer and use it in GitHub Desktop.
Quickly `cd` to an OSGi module
# Running this will display a filterable list of modules
# Once you select one, you will navigate to it
# You can call it with an argument to be used as a pre-filter on the module list
# Requires 'fzf', I highly recommend it.
# https://github.com/junegunn/fzf
# In this case, 'fzf' is reading from stdin
# USAGE EXAMPLES
# gm (will present the full list of modules)
# gm segments (will present a list of modules pre-filtered by "segments")
function gm() {
# The optional filter pattern. If none is given, all modules will be shown
local PATTERN
PATTERN="$*"
# Find the root of the Git project
local PROJECT_ROOT
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
# Get the module path
local MODULE_PATH
MODULE_PATH="$(
# Exclude any files that are inside a 'src' folder since we only want module roots
# Exclude any files that are inside a 'samples' folder since those are probably not true sources
# List all *.bnd files from project root
# List all *build.xml files from project root
# List all *pom.xml files from project root
git -C "${PROJECT_ROOT}" ls-files -- \
':!:**/src/**' \
':!:**/samples/**' \
\
'*.bnd' \
'*build.xml' \
'*pom.xml' \
|
# Strip the filename, leaving just the directory name
# This is WAY faster than using "dirname"
#
# Strip the trailing forward-slash
# Delete any empty lines
sed -E \
-e 's,[^/]*$,,g' \
\
-e 's,/$,,g' \
-e '/^$/d' \
|
# Gets rid of any duplicates in case a module has more than one *.bnd file
uniq |
# Pass the results to 'fzf' so the user can choose. Also passes the pattern as a default query.
fzf \
--exit-0 \
--no-multi \
--query "${PATTERN}" \
--select-1 \
;
)"
# If you actually chose a module path, navigate to it. Otherwise do nothing.
if [ -n "${MODULE_PATH}" ]
then
cd "${PROJECT_ROOT}/${MODULE_PATH}" || return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment