Skip to content

Commit

Permalink
add setup script, rename prefix script into local_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Apr 30, 2018
1 parent ad6f16f commit ecacbdf
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 48 deletions.
19 changes: 17 additions & 2 deletions colcon_bash/shell/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from colcon_core.plugin_system import satisfies_version
from colcon_core.plugin_system import SkipExtensionException
from colcon_core.shell import get_colcon_prefix_path
from colcon_core.shell import logger
from colcon_core.shell import ShellExtensionPoint
from colcon_core.shell import use_all_shell_extensions
Expand All @@ -24,7 +25,7 @@ def __init__(self): # noqa: D107
def create_prefix_script(
self, prefix_path, pkg_names, merge_install
): # noqa: D102
prefix_env_path = prefix_path / 'prefix.bash'
prefix_env_path = prefix_path / 'local_setup.bash'
logger.info(
"Creating prefix script '{prefix_env_path}'".format_map(locals()))
expand_template(
Expand All @@ -33,6 +34,19 @@ def create_prefix_script(
{
'pkg_names': pkg_names,
'merge_install': merge_install,
'package_script_no_ext': 'package',
})

prefix_chain_env_path = prefix_path / 'setup.bash'
logger.info(
"Creating prefix chain script '{prefix_chain_env_path}'"
.format_map(locals()))
expand_template(
Path(__file__).parent / 'template' / 'prefix_chain.bash.em',
prefix_chain_env_path,
{
'colcon_prefix_path': get_colcon_prefix_path(skip=prefix_path),
'prefix_script_no_ext': 'local_setup',
})

def create_package_script(
Expand All @@ -47,5 +61,6 @@ def create_package_script(
{
'pkg_name': pkg_name,
'hooks': list(filter(
lambda hook: str(hook[0]).endswith('.bash'), hooks))
lambda hook: str(hook[0]).endswith('.bash'), hooks)),
'package_script_no_ext': 'package',
})
52 changes: 25 additions & 27 deletions colcon_bash/shell/template/package.bash.em
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
# generated from colcon_bash/shell/template/package.bash.em

# This script extends the environment for this package.

# a bash script is able to determine its own path if necessary
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
# the prefix is two levels up from the package specific share directory
_colcon_package_bash_COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`/../.." > /dev/null && pwd)"
else
_colcon_package_bash_COLCON_CURRENT_PREFIX="$COLCON_CURRENT_PREFIX"
fi

# function to source another script with conditional trace output
# first argument: the name of the script file
# first argument: the path of the script
# additional arguments: arguments to the script
colcon_package_source_bash_script() {
# arguments
_colcon_package_source_bash_script="$1"

# source script with conditional trace output
if [ -f "$_colcon_package_source_bash_script" ]; then
_colcon_package_bash_source_script() {
if [ -f "$1" ]; then
if [ -n "$COLCON_TRACE" ]; then
echo ". \"$_colcon_package_source_bash_script\""
echo ". \"$1\""
fi
. $@@
else
echo "not found: \"$_colcon_package_source_bash_script\"" 1>&2
echo "not found: \"$1\"" 1>&2
fi

unset _colcon_package_source_bash_script
}


# a bash script is able to determine its own path if necessary
# the prefix is two levels up from the package specific share directory
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
COLCON_CURRENT_PREFIX=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`/../.." > /dev/null && pwd)
fi
@[if hooks]@
_package_COLCON_CURRENT_PREFIX=$COLCON_CURRENT_PREFIX
@[end if]@

colcon_package_source_bash_script "$COLCON_CURRENT_PREFIX/share/@(pkg_name)/package.sh"
# source sh script of this package
_colcon_package_bash_source_script "$_colcon_package_bash_COLCON_CURRENT_PREFIX/share/@(pkg_name)/@(package_script_no_ext).sh"
@[if hooks]@

COLCON_CURRENT_PREFIX=$_package_COLCON_CURRENT_PREFIX
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced scripts
COLCON_CURRENT_PREFIX="$_colcon_package_bash_COLCON_CURRENT_PREFIX"

# source bash hooks
@[ for hook in hooks]@
colcon_package_source_bash_script "$COLCON_CURRENT_PREFIX/@(hook[0])"@
_colcon_package_bash_source_script "$COLCON_CURRENT_PREFIX/@(hook[0])"@
@[ for hook_arg in hook[1]]@
@(hook_arg)@
@[ end for]
@[ end for]@

unset _package_COLCON_CURRENT_PREFIX
@[end if]@
unset COLCON_CURRENT_PREFIX
unset colcon_package_source_bash_script
@[end if]@

unset _colcon_package_bash_source_script
unset _colcon_package_bash_COLCON_CURRENT_PREFIX
89 changes: 70 additions & 19 deletions colcon_bash/shell/template/prefix.bash.em
Original file line number Diff line number Diff line change
@@ -1,34 +1,85 @@
# generated from colcon_bash/shell/template/prefix.bash.em
@[if pkg_names]@

# function to source another script with conditional trace output
# first argument: the name of the script file
colcon_prefix_source_bash_script() {
# This script extends the environment with all packages contained in this
# prefix path.

# a bash script is able to determine its own path if necessary
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
_colcon_prefix_bash_COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)"
else
_colcon_prefix_bash_COLCON_CURRENT_PREFIX="$COLCON_CURRENT_PREFIX"
fi

# function to prepend a value to a variable
# which uses colons as separators
# duplicates as well as trailing separators are avoided
# first argument: the name of the result variable
# second argument: the value to be prepended
_colcon_prefix_bash_prepend_unique_value() {
# arguments
_colcon_prefix_source_bash_script="$1"
_listname="$1"
_value="$2"

# source script with conditional trace output
if [ -f "$_colcon_prefix_source_bash_script" ]; then
# get values from variable
eval _values=\"\$$_listname\"
# backup the field separator
_colcon_prefix_bash_prepend_unique_value_IFS="$IFS"
IFS=":"
# start with the new value
_all_values="$_value"
# iterate over existing values in the variable
for _item in $_values; do
# ignore empty strings
if [ -z "$_item" ]; then
continue
fi
# ignore duplicates of _value
if [ "$_item" = "$_value" ]; then
continue
fi
# keep non-duplicate values
_all_values="$_all_values:$_item"
done
unset _item
# restore the field separator
IFS="$_colcon_prefix_bash_prepend_unique_value_IFS"
unset _colcon_prefix_bash_prepend_unique_value_IFS
# export the updated variable
eval export $_listname=\"$_all_values\"
unset _all_values
unset _values
unset _value
unset _listname
}
# add this prefix to the COLCON_PREFIX_PATH
_colcon_prefix_bash_prepend_unique_value COLCON_PREFIX_PATH "$_colcon_prefix_bash_COLCON_CURRENT_PREFIX"
unset _colcon_prefix_bash_prepend_unique_value
@[if pkg_names]@
# function to source another script with conditional trace output
# first argument: the path of the script
_colcon_prefix_bash_source_script() {
if [ -f "$1" ]; then
if [ -n "$COLCON_TRACE" ]; then
echo ". \"$_colcon_prefix_source_bash_script\""
echo ". \"$1\""
fi
. "$_colcon_prefix_source_bash_script"
. "$1"
else
echo "not found: \"$_colcon_prefix_source_bash_script\"" 1>&2
echo "not found: \"$1\"" 1>&2
fi

unset _colcon_prefix_source_bash_script
}

# a bash script is able to determine its own path
_prefix_COLCON_CURRENT_PREFIX=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)

# source packages
@[ for pkg_name in pkg_names]@
COLCON_CURRENT_PREFIX=$_prefix_COLCON_CURRENT_PREFIX@('' if merge_install else ('/' + pkg_name))
colcon_prefix_source_bash_script "$COLCON_CURRENT_PREFIX/share/@(pkg_name)/package.bash"
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX=$_colcon_prefix_bash_COLCON_CURRENT_PREFIX@('' if merge_install else ('/' + pkg_name))
_colcon_prefix_bash_source_script "$COLCON_CURRENT_PREFIX/share/@(pkg_name)/@(package_script_no_ext).bash"

@[ end for]@
unset COLCON_CURRENT_PREFIX
unset _prefix_COLCON_CURRENT_PREFIX
unset colcon_prefix_source_bash_script
unset _colcon_prefix_bash_source_script
@[end if]@

unset _colcon_prefix_bash_COLCON_CURRENT_PREFIX
35 changes: 35 additions & 0 deletions colcon_bash/shell/template/prefix_chain.bash.em
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# generated from colcon_bash/shell/template/prefix_chain.bash.em

# This script extends the environment with the environment of other prefix
# paths which were sourced when this file was generated as well as all packages
# contained in this prefix path.

# function to source another script with conditional trace output
# first argument: the path of the script
_colcon_prefix_chain_bash_source_script() {
if [ -f "$1" ]; then
if [ -n "$COLCON_TRACE" ]; then
echo ". \"$1\""
fi
. "$1"
else
echo "not found: \"$1\"" 1>&2
fi
}
@[if colcon_prefix_path]@

# source chained prefixes
@[ for prefix in reversed(colcon_prefix_path)]@
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX="@(prefix)"
_colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/@(prefix_script_no_ext).bash"
@[ end for]@
@[end if]@

# source this prefix
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)"
_colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/@(prefix_script_no_ext).bash"

unset COLCON_CURRENT_PREFIX
unset _colcon_prefix_chain_bash_source_script

0 comments on commit ecacbdf

Please sign in to comment.