Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
221 lines (189 sloc) 5.59 KB
#!/bin/sh
__sm.extension.actions.type()
{
typeset -g action_type
typeset _path _path_type extension shebang binary
_path="${action_path:-}"
case "${action}" in
(*\(\))
action_type="function"
return 0
;;
(*)
if [[ -f "${_path}/${action}" ]]
then
_path_type="$(file "${_path}/${action}")"
else
log debug search "can not find file '${_path}/${action}'"
action_type="dne"
return 0
fi
;;
esac
case "${_path_type}" in
(*sh[[:space:]]script*|*POSIX[[:space:]]shell*|*Bourne*)
action_type="shell"
;;
(*ASCII*)
# Launch with helper DSL, if possible.
extension="${_path//.}"
case "${extension}" in
(shell|zsh|bash|sh)
action_type="shell"
#rb) action_type="ruby" ;;
;;
(*)
read -r shebang < "${_path}/${action}"
case "${shebang}" in
*ruby|*rbx|*jruby|*macruby)
binary="${shebang##*(\#|!)}"
binary="${binary##* }"
action_type="ruby"
;;
(\#!/*/env *)
shebang="${shebang#\#!}"
shebang="${shebang#* }" #search for the binary to env to
if [[ -x "${_path}/${action}" && -x "$( which ${shebang} 2>/dev/null || true )" ]]
then
action_type="binary"
else
log debug search "can not find shebang '${shebang}'"
action_type="noexec"
fi
;;
(\#!/*)
shebang="${shebang#\#!}"
shebang="${shebang% *}"
if [[ -x "${_path}/${action}" && -x "${shebang}" ]]
then
action_type="binary"
else
log debug search "can not find shebang '${shebang}'"
action_type="noexec"
fi
;;
(*)
log debug search "can not find shebang '${shebang}'"
action_type="noexec"
;;
esac
;;
esac
result=$?
;;
(cannot[[:space:]]open)
log debug search "'${_path_type}' file '${_path}/${action}'"
action_type="dne"
;;
(*)
if [[ -x "${_path}/${action}" ]]
then
action_type="binary"
else
log debug search "not executable file '${_path}/${action}'"
action_type="noexec"
fi
;;
esac
return $?
}
__sm.extension.modules.include()
{
typeset _token _namespace module_name
_namespace=""
module_name="${extension}"
__sm.module.include "$module_name" || __sm.log.debug search "extension main module not found: $module_name"
while (( $# > 0 ))
do
_namespace+="/$1"
shift
module_name="${extension}${_namespace}"
__sm.module.include "$module_name" || true
done
}
__sm.extension.action.prerequisites()
{
trace_filter action_pre || set -o xtrace
typeset _pre _name _action _extension
_action="${action// *}"
_extension="${extension// *}"
_pre=(
${_action}_cli
${_action}_initialize
)
__sm.string.contains.word "${_action%%_*}.cli" "${_pre[*]}" || _pre+=(
${_action%%_*}_cli
${_action%%_*}_initialize
)
__sm.string.contains.word "${_extension}.cli" "${_pre[*]}" || _pre+=(
${_extension}_cli
${_extension}_initialize
)
__sm.string.contains.word "__sm.${_action}.cli" "${_pre[*]}" || _pre+=(
__sm.${_action}.cli
__sm.${_action}.initialize
)
__sm.string.contains.word "__sm.${_extension}.cli" "${_pre[*]}" || _pre+=(
__sm.${_extension}.cli
__sm.${_extension}.initialize
)
for _name in ${_pre[@]}
do
if __sm.command.exists ${_name// *}
then
${_name} "${extension_args[@]}"
fi
done
}
__sm.extension.run()
{
trace_filter extension_run || set -o xtrace
typeset -g action
typeset _type _path _namespaces requires
_type="${action_type:-}"
_path="${action_path:-}"
builtin cd "${initial_pwd}"
# Now launch the extension, based on the determined _type.
case "${_type}" in
(function)
_namespaces=${_path##${extension_bin_path}}
__sm.extension.modules.include ${=_namespaces//\// }
action=${action%%\(*}
__sm.extension.action.prerequisites
__sm.actions.call ${action} "${extension_args[@]}" || return $?
;;
(shell)
_namespaces=${_path##${extension_bin_path}}
__sm.extension.modules.include ${=_namespaces//\// }
__sm.extension.action.prerequisites
__sm.actions.source "${_path}/${action}" "${extension_args[@]}" || return $?
;;
(ruby)
# Allow env settings via _initialize
__sm.extension.modules.include ${=_namespaces//\// }
requires=()
for script in functions initialize
do
if [[ -f "${__sm_modules_path}/ruby/core/${script}.rb" ]]
then
requires+=( "-r${__sm_modules_path}/ruby/core/${script}.rb" )
fi
done
exec "${binary:-ruby}" -I"${__sm_modules_path}/ruby" -I"${extension_modules_path}/ruby" \
${requires[@]} "${_path}/${action}"
;;
# (python|lua|javascript)
# ADD "${__sm_modules_path}/${_type}/" to the lib path so the script can require "sm"
# "${_path}"
# ;;
(noexec)
__sm.log.fail "Processing action ${_path#${extension_path}/}${_path:+/}${action} failed; file type is unknown or file is not executable."
;;
(dne)
__sm.log.fail "Processing action ${_path#${extension_path}/}${_path:+/}${action} failed; file does not exist or file is not readable."
;;
(binary|*)
exec "${_path}/${action}" ${extension_action} "${extension_args[@]}"
;;
esac
}
Something went wrong with that request. Please try again.