Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

199 lines (170 sloc) 6.307 kB
#!/bin/sh
__sm.cache.map.check()
{
trace_filter action_detection || set -o xtrace
local _found_actions _method _name _item _candidate _candidates screen_height
_found_actions=()
_method="$1"
_name="$2"
shift || __sm.log.fail "No method and action name was given for search"
shift || __sm.log.fail "No action name was given for search"
__sm.cache.map.find.matching "${_method}" "${_name}"
case "${_method}" in
(only_one)
#TODO: allow user to choice what to do if interactive shell ?
if (( ${#_found_actions[@]} > 1 ))
then
_found_actions=( "${_found_actions[${__sm_array_start}]}" )
fi
;;
esac
if (( ${#_found_actions[@]} == 1 ))
then
_found_actions="${_found_actions#*=}"
action="${_found_actions##*/}"
action_path="${extensions_path}/${extension}/bin/${_found_actions}"
action_path="${action_path%/*}"
extension_args=( "${_rest_params[@]}" )
return 0
elif (( ${#_found_actions[@]} ))
then
_candidates=()
for _candidate in "${_found_actions[@]}"
do
_candidate="${_candidate%%=*}"
_candidates+=( "sm ${extension} ${_candidate//\// } ${_rest_params[@]}\n" )
done
screen_height=${LINES:-$(tput lines 2>/dev/null || echo 20)}
if (( ${#_found_actions[@]} + 4 > screen_height ))
then
__sm.log.error "More than one matching action found;\n\nDid you mean:\n ${_candidates[*]}\n" 2>&1 | less
else
__sm.log.error "More than one matching action found;\n\nDid you mean:\n ${_candidates[*]}\n"
fi
exit 1
else
__sm.log.debug search "no actions found in '${extension}' for '${_name}'"
fi
return 1
}
__sm.extension.actions.find()
{
trace_filter action_detection || set -o xtrace
extensions_path="$1" extension="$2"
shift || __sm.log.fail "Extension path must be given as the first parameter."
shift || __sm.log.fail "Extension name must be given as the second parameter."
local _action_params _action_path _new_action_path _rest_params _param_temp \
_action_params_length _actions_root _map _modules_root _actions_cache
_action_params=("$@")
_rest_params=()
_actions_root="${extensions_path}/${extension}/bin"
_map="${extensions_path}/${extension}/map"
_modules_root="${extensions_path}/${extension}/shell"
_actions_cache=()
__sm.cache.map.filesystem.initialize
__sm.cache.map.actions.initialize
__sm.cache.map.modules.initialize
__sm.log.debug search "actions_cache: ${_actions_cache[*]}"
if [[ ! -f "${_map}" ]]
then
__sm.log.search "missing" "map" "${_map}"
fi
if [[ -f "${_map}" ]] || (( ${#_actions_cache[@]} ))
then
while :
do
_action_path="${_action_params[*]}"
_action_path="${_action_path// //}"
if __sm.cache.map.check only_one "${_action_path}" ||
__sm.cache.map.check allow_many "${_action_path}"
then
return 0
fi
# Not found, go down one level
_action_params_length=${#_action_params[@]}
(( _action_params_length )) || break # finish if no more params
#TODO: account for zsh indexes ( -1 -> -0 )
_param_temp="${_action_params[$(( _action_params_length - 1 ))]}"
_rest_params=( "${_param_temp}" "${_rest_params[@]}" )
if [[ -n "${ZSH_VERSION}" ]];then unsetopt nullglob;else shopt -u nullglob;fi
unset _action_params[$(( _action_params_length - 1 ))]
if [[ -n "${ZSH_VERSION}" ]];then setopt nullglob;else shopt -s nullglob;fi
_action_params=( "${_action_params[@]}" ) # Just in case ... works without it
done
else
__sm.log.error "${extensions_path}/${extension} does not have map or module dependencies with actions"
fi
return 1
}
__sm.extension.action()
{
trace_filter action_detection || set -o xtrace
(( extension_action_calls == 0 )) ||
__sm.log.fail "Cannot call a 2nd time, args: ${extension_args[*]}"
extension_action_calls+=1
unset extension action
#TODO: Should be moved to core/cli?
case "${extension_args[${__sm_array_start}]}" in
(*=*) # exact version specifier
package_name="${extension_args[${__sm_array_start}]}"
export package_version="${package_name##*=}"
export package_name="${package_name%%=*}"
export extension="${package_name}"
extension_args[${__sm_array_start}]="${extension}"
;;
(*:*)
package_name="${extension_args[${__sm_array_start}]}"
export package_version="${package_name##*:}"
export package_name="${package_name%%:*}"
export extension="${package_name}"
extension_args[${__sm_array_start}]="${extension}"
;;
esac
local _extension_found _action_found
_extension_found=0
_action_found=0
if __sm.extension.in.paths __sm.extension.find "${extension_args[@]}"
then
_extension_found=1
__sm.log.search extension "$extension" "$extensions_path"
if __sm.extension.actions.find "$extensions_path" "${extension_args[@]}"
then
_action_found=1
fi
fi
if (( ! _action_found )) &&
__sm.cli.in.path __sm.extension.actions.find "${extension_args[@]}"
then
_action_found=1
fi
if (( _action_found ))
then
__sm.log.search action "$action" "${action_path##${sm_path}\/} params:${extension_args[@]}"
else
if (( _extension_found ))
then
__sm.log.error "Unknown action path: "${extension_args[@]}"\n\nUsage: \n sm extension [namespace] [action].\n"
else
__sm.log.error "Could not find extension: "${extension_args}"\n"
fi
fi
extension_path="$extensions_path/$extension"
extension_modules_path="$extension_path/shell"
extension_bin_path="$extension_path/bin"
extension_config_path="$extension_path/config"
extension_templates_path="$extension_path/templates"
extension_log_path="$log_path/$extension"
paths=$(env | awk -F= -v ORS=' ' '/_path/{print $1}')
flags=$(env | awk -F= -v ORS=' ' '/_flag/{print $1}')
__sm.log.debug search "environment:
action=${action}
action_path=${action_path}
extension_args=${extension_args}
extension_path=${extension_path}
extension_config_path=${extension_config_path}
extension_templates_path=${extension_templates_path}
extension_modules_path=${extension_modules_path}
extension_bin_path=${extension_bin_path}
extension_log_path=${extension_log_path}"
__sm.exports
}
Jump to Line
Something went wrong with that request. Please try again.