Skip to content

Commit fff5af2

Browse files
authored
add colcon_cd function (#1)
* add colcon_cd function * handle multiple returned paths gracefully * fix command invocation with zsh
1 parent 390ed13 commit fff5af2

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ dist: trusty
44
sudo: true
55
install:
66
- pip install -U setuptools
7+
# install_requires
8+
- pip install -U git+https://github.com/colcon/colcon-core
9+
- pip install -U git+https://github.com/colcon/colcon-package-information
710
# tests_require
811
- pip install -U flake8-blind-except flake8-builtins flake8-class-newline flake8-comprehensions flake8-deprecated flake8-docstrings flake8-quotes pep8-naming pylint pytest pytest-cov scspell3k
912
script:

appveyor.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ environment:
44
- PYTHON: "C:\\Python36-x64"
55
install:
66
- "%PYTHON%\\python.exe -m pip install -U setuptools"
7+
# install_requires
8+
- "%PYTHON%\\python.exe -m pip install -U git+https://github.com/colcon/colcon-core"
9+
- "%PYTHON%\\python.exe -m pip install -U git+https://github.com/colcon/colcon-package-information"
710
# tests_require
811
- "%PYTHON%\\python.exe -m pip install -U flake8-blind-except flake8-builtins flake8-class-newline flake8-comprehensions flake8-deprecated flake8-docstrings flake8-quotes pep8-naming pylint pytest pytest-cov scspell3k"
912
build: off

colcon.pkg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"hooks": [
3+
"share/colcon_cd/function/colcon_cd.sh"
4+
]
5+
}

function/colcon_cd.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# copied from colcon_cd/function/colcon_cd.sh
2+
3+
colcon_cd() {
4+
if [ $# = 0 ]; then
5+
# change the working directory to the previously saved path
6+
if [ "$_colcon_cd_root" = "" ]; then
7+
echo "No previous path saved. Either call 'colcon_cd <pkgname>' from a" \
8+
"directory where <pkgname> can be found or 'colcon_cd --set' to" \
9+
"directly save the current working directory." 1>&2
10+
return 1
11+
fi
12+
if [ "$_colcon_cd_root" != "$(pwd)" ]; then
13+
cd "$_colcon_cd_root"
14+
fi
15+
16+
elif [ $# = 1 ]; then
17+
if [ "$1" = "--set" ]; then
18+
# store the current working directory for future invocations
19+
_colcon_cd_root="$(pwd)"
20+
echo "Saved the current working directory for future invocations of" \
21+
"'colcon_cd <pkgname>' as well as to return to using 'colcon_cd'." \
22+
"Call 'colcon_cd --reset' to unset the saved path."
23+
return 0
24+
elif [ "$1" = "--reset" ]; then
25+
# unset the save path
26+
unset _colcon_cd_root
27+
return 0
28+
fi
29+
30+
if [ "$_colcon_cd_root" != "" ]; then
31+
# try to find the given package from the saved path
32+
_colcon_cd_pwd="$(pwd)"
33+
cd "$_colcon_cd_root"
34+
35+
_colcon_cd_pkg_path="$(COLCON_LOG_PATH=/dev/null colcon list --packages-select $1 --paths-only 2> /dev/null)"
36+
if [ $? -eq 0 ] && [ "$_colcon_cd_pkg_path" != "" ]; then
37+
# count number of returned paths
38+
if (( $(grep -c . <<< "$_colcon_cd_pkg_path") > 1 )); then
39+
echo "Found in multiple directories:"
40+
echo "$_colcon_cd_pkg_path"
41+
_colcon_cd_pkg_path="$(grep -m 1 . <<< "$_colcon_cd_pkg_path")"
42+
echo "cd to the first one"
43+
fi
44+
cd "$_colcon_cd_pkg_path"
45+
unset _colcon_cd_pkg_path
46+
unset _colcon_cd_pwd
47+
return 0
48+
fi
49+
unset _colcon_cd_pkg_path
50+
51+
cd "$_colcon_cd_pwd"
52+
unset _colcon_cd_pwd
53+
fi
54+
55+
# try to find the given package from the current working directory
56+
_colcon_cd_pkg_path="$(COLCON_LOG_PATH=/dev/null colcon list --packages-select $1 --paths-only 2> /dev/null)"
57+
if [ $? -eq 0 ] && [ "$_colcon_cd_pkg_path" != "" ]; then
58+
if [ "$_colcon_cd_root" = "" ]; then
59+
# store the current working directory for future invocations
60+
_colcon_cd_root="$(pwd)"
61+
echo "Saved the directory '$_colcon_cd_root' for future invocations" \
62+
"of 'colcon_cd <pkgname>' as well as to return to using " \
63+
"'colcon_cd'. Call 'colcon_cd --reset' to unset the saved path."
64+
fi
65+
# count number of returned paths
66+
if (( $(grep -c . <<< "$_colcon_cd_pkg_path") > 1 )); then
67+
echo "Found in multiple directories:"
68+
echo "$_colcon_cd_pkg_path"
69+
_colcon_cd_pkg_path="$(grep -m 1 . <<< "$_colcon_cd_pkg_path")"
70+
echo "cd to the first one"
71+
fi
72+
cd "$_colcon_cd_pkg_path"
73+
unset _colcon_cd_pkg_path
74+
return 0
75+
fi
76+
unset _colcon_cd_pkg_path
77+
78+
if [ "$_colcon_cd_root" != "" ]; then
79+
echo "Could neither find package '$1' from '$_colcon_cd_root' nor from" \
80+
"the current working directory" 1>&2
81+
else
82+
echo "Could not find package '$1' from the current working" \
83+
"directory" 1>&2
84+
fi
85+
return 1
86+
87+
else
88+
echo "'colcon_cd' only supports zero or one arguments" 1>&2
89+
return 1
90+
fi
91+
}

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ long_description = file: README.rst
2424
keywords = colcon
2525

2626
[options]
27+
install_requires =
28+
colcon-core
29+
colcon-package-information
2730
packages = find:
2831
tests_require =
2932
flake8
@@ -42,6 +45,11 @@ tests_require =
4245
scspell3k>=2.2
4346
zip_safe = true
4447

48+
[options.data_files]
49+
# distutils replaces dashes in keys with underscores
50+
share/colcon_cd/function =
51+
function/colcon_cd.sh
52+
4553
[tool:pytest]
4654
filterwarnings =
4755
error

0 commit comments

Comments
 (0)