oh-my-fish/lib/require.fish
Derek Willian Stavis a164ebdd5d init: rewrite init process (#260)
* init: rewrite init process

Now use pure globbing to generate 100% valid function and
completion paths, effectively splitting the init process in two
steps, one which paths are added, and other when initialization
is done (sourcing init).

This initialization code introduces a new interface for
`init.fish` hook, which deprecates the previously used event
model. The new interface injects three variables into `init.fish`:
path, package and bundle. This variables can be used by the
package to autoload paths, use bundled files, etc.

Also supports key bindings by sourcing
$OMF_CONFIG/key_bindings.fish and also key_bindings.fish in
packages (plugins and themes) root directories. This is done
when fish_user_key_bindings is called.

* omf: migrate to new init hook

* omf/templates: migrate to new init and uninstall hooks

* docs: document new init and uninstall hooks interface

* README: update new hook interface spec
2016-06-01 01:09:38 -03:00

83 lines
2.0 KiB
Fish

function require
set packages $argv
if test -z "$packages"
echo 'usage: require <name>...'
echo ' require --path <path>...'
echo ' require --no-bundle --path <path>...'
return 1
end
# If bundle should be
if set index (contains -i -- --no-bundle $packages)
set -e packages[$index]
set ignore_bundle
end
# Requiring absolute paths
if set index (contains -i -- --path $packages)
set -e packages[$index]
set package_path $packages
# Requiring specific packages from default paths
else
set package_path {$OMF_PATH,$OMF_CONFIG}/pkg*/$packages
# Exit with error if no package paths were generated
test -z "$package_path"
and return 1
end
set function_path $package_path/functions*
set completion_path $package_path/completions*
set init_path $package_path/init.fish*
# Autoload functions
test -n "$function_path"
and set fish_function_path $fish_function_path[1] \
$function_path \
$fish_function_path[2..-1]
# Autoload completions
test -n "$complete_path"
and set fish_complete_path $fish_complete_path[1] \
$complete_path \
$fish_complete_path[2..-1]
for init in $init_path
emit perf:timer:start $init
set -l IFS '/'
echo $init | read -la components
set path (printf '/%s' $components[1..-2])
contains $path $omf_init_path
and continue
set package $components[-2]
if not set -q ignore_bundle
set bundle $path/bundle
set dependencies
if test -f $bundle
set -l IFS ' '
while read -l type dependency
test "$type" != package
and continue
require "$dependency"
set dependencies $dependencies $dependency
end < $bundle
end
end
source $init $path
emit init_$package $path
set -g omf_init_path $omf_init_path $path
emit perf:timer:finish $init
end
return 0
end