Reorganize `omf` plugin structure

This updates omf plugin to new architecture, moving
functions to `functions` directory. It also revamps the
separation of concerns between CLI and underlying
implementations, organizing them into directories,
whose are autoloaded in plugin startup.
pull/230/head
Derek Stavis 9 years ago
parent 68f450b4d0
commit 49dda5c2f7

@ -1,5 +0,0 @@
function omf.install_package
for search in $argv
omf.install $search
end
end

@ -1,5 +0,0 @@
# Backwards-compatible wrapper function
# TODO: Remove it after 2015, December 13
function omf.list_installed_packages
omf.packages.list --installed --plugin
end

@ -1,39 +0,0 @@
function omf.new -a option name
switch $option
case "p" "pkg" "pack" "packg" "package"
set option "pkg"
case "t" "th" "the" "thm" "theme" "themes"
set option "themes"
case "*"
echo (omf::err)"$option is not a valid option."(omf::off) 1^&2
return $OMF_INVALID_ARG
end
if not omf.util_valid_package "$name"
echo (omf::err)"$name is not a valid package/theme name"(omf::off) 1^&2
return $OMF_INVALID_ARG
end
if set -l dir (omf.util_mkdir "$option/$name")
cd $dir
set -l github (git config github.user)
test -z "$github"; and set github "{{USER}}"
set -l user (git config user.name)
test -z "$user"; and set user "{{USER}}"
omf.new_from_template "$OMF_PATH/pkg/omf/templates/$option" \
$github $user $name
echo (omf::em)"Switched to $dir"(omf::off)
if test "$option" = themes
omf.theme $name
refresh
end
else
echo (omf::err)"\$OMF_CONFIG and/or \$OMF_PATH undefined."(omf::off) 1^&2
exit $OMF_UNKNOWN_ERR
end
end

@ -1,27 +0,0 @@
function omf.new_from_template -a path github user name
for file in $path/*
if test -d $file
mkdir (basename $file)
pushd (basename $file)
omf.new_from_template $file $github $user $name
else
set -l target (begin
if test (basename $file) = "{{NAME}}.fish"
echo "$name.fish"
else
echo (basename "$file")
end
end)
sed "s/{{USER_NAME}}/$user/;s/{{GITHUB_USER}}/$github/;s/{{NAME}}/$name/" \
$file > $target
echo (omf::em)" create "(omf::off)" "(begin
if test (basename $PWD) = $name
echo ""
else
echo (basename "$PWD")"/"
end
end)$target
end
end
popd >/dev/null ^&2
end

@ -1,44 +0,0 @@
function omf.remove_package
set -l pkg $argv
set -l remove_status 1
if not omf.util_valid_package $pkg
if test $pkg = "omf" -o $pkg = "default"
echo (omf::err)"You can't remove `$pkg`"(omf::off) 1^&2
else
echo (omf::err)"$pkg is not a valid package/theme name"(omf::off) 1^&2
end
return $OMF_INVALID_ARG
end
for path in {$OMF_PATH,$OMF_CONFIG}/{pkg}/$pkg
not test -d $path; and continue
source $path/uninstall.fish ^/dev/null; and emit uninstall_$pkg
omf.bundle.remove "package" $pkg
rm -rf $path
set remove_status $status
end
for path in {$OMF_PATH,$OMF_CONFIG}/{themes}/$pkg
not test -d $path; and continue
if test $pkg = (cat $OMF_CONFIG/theme)
echo default > $OMF_CONFIG/theme
end
omf.bundle.remove "theme" $pkg
rm -rf $path
set remove_status $status
end
if test $remove_status -eq 0
echo (omf::em)"$pkg successfully removed."(omf::off)
else
echo (omf::err)"$pkg could not be found"(omf::off) 1^&2
end
return $remove_status
end

@ -16,10 +16,10 @@ function omf.bundle.install
set name_or_url (echo $record | cut -s -d' ' -f2- | sed 's/ //g')
test -n "$name_or_url"; or continue
set name (omf.package_name $name_or_url)
set name (omf.packages.name $name_or_url)
if not contains $name $packages
omf.install $name_or_url;
omf.packages.install $name_or_url;
and set installed
end
end

@ -11,7 +11,7 @@ function omf.bundle.remove
for record in $bundle_contents
set record_type (echo $record | cut -d' ' -f1)
set record_name_or_url (echo $record | cut -d' ' -f2-)
set record_name (omf.package_name $record_name_or_url)
set record_name (omf.packages.name $record_name_or_url)
if not test "$type" = "$record_type" -a "$name" = "$record_name"
echo "$record_type $record_name_or_url" >> $bundle

@ -0,0 +1,3 @@
function omf.cli.cd -a name
omf.packages.cd $name
end

@ -0,0 +1,10 @@
function omf.cli.describe -a name
switch (count $argv)
case 1
omf.packages.describe $name
return 0
case '*'
echo (omf::err)"Invalid number of arguments"(omf::off)
return 1
end
end

@ -0,0 +1,11 @@
function omf.cli.destroy
echo (omf::err)"This will destroy your Oh My Fish installation!"(omf::off)
read -l decision -p 'echo -n "Are you sure you want to continue? (y/N) "'
switch $decision
case 'y' 'Y'
omf.destroy
case '*'
echo (omf::err)"Aborted!"(omf::off)
end
end

@ -0,0 +1,3 @@
function omf.cli.doctor
omf.doctor
end

@ -1,5 +1,5 @@
function omf.help -a command
switch $command
function omf.cli.help -a command
switch "$command"
case "c" "cd"
echo \n"\
Change directory to root or plugin/theme directory.

@ -0,0 +1,19 @@
function omf.cli.install
set fail_count 0
switch (count $argv)
case 0
omf.bundle.install;
or set fail_count 1
case '*'
for package in $argv
omf.packages.install $package;
and require $package
test $status != 0;
and set fail_count (math $fail_count + 1)
end
end
return $fail_count
end

@ -0,0 +1,14 @@
function omf.cli.list -a type
test -z "$type"; and set type '--installed'
if contains -- $type '--available' '-a' '--database' '-d' '--installed' '-i'
omf.packages.list $type | column
else
echo (omf::err)"Invalid arguments"(omf::off)
echo 'Usage: omf list [ --available | -a ]'
echo ' omf list [ --installed | -i ]'
echo ' omf list [ --database | -d ]'
return $OMF_INVALID_ARG
end
end

@ -0,0 +1,8 @@
function omf.cli.new
if test (count $argv) -ne 2
echo (omf::err)"Package type or name missing"(omf::off) 1^&2
echo "Usage: omf new "(omf::em)"(pkg | theme)"(omf::off)" <name>" 1^&2
return $OMF_MISSING_ARG
end
omf.packages.new $argv
end

@ -0,0 +1,23 @@
function omf.cli.remove -a name
switch (count $argv)
case 1
omf.packages.remove $name
set code $status
switch $code
case 0
echo (omf::em)"$name successfully removed."(omf::off)
case 1
echo (omf::err)"$name could not be removed."(omf::off) 1^&2
case 2
echo (omf::err)"$name could not be found."(omf::off) 1^&2
end
return $code
case '*'
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: omf remove "(omf::em)"<name>"(omf::off) 1^&2
return $OMF_INVALID_ARG
end
end

@ -0,0 +1,10 @@
function omf.cli.submit
switch (count $argv)
case 2
omf.packages.submit $argv
case "*"
echo (omf::err)"Argument missing"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"submit"(omf::off)" "(omf::em)"pkg|themes"(omf::off)"/<name> <url>" 1^&2
return $OMF_MISSING_ARG
end
end

@ -0,0 +1,12 @@
function omf.cli.theme -a name
switch (count $argv)
case 0
omf.cli.themes.list
case 1
omf.theme.set $name
case '*'
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" [<theme name>]" 1^&2
return $OMF_INVALID_ARG
end
end

@ -0,0 +1,11 @@
function omf.cli.themes.list
set -l theme (cat $OMF_CONFIG/theme)
set -l regex_current "(^|[[:space:]])($theme)([[:space:]]|\$)"
set -l highlight_current s/"$regex_current"/"\1"(omf::em)"\2"(omf::off)"\3"/g
echo (omf::under)"Installed:"(omf::off)
omf.packages.list --installed --theme | column | sed -E "$highlight_current"
echo
echo (omf::under)"Available:"(omf::off)
omf.packages.list --available --theme | column
end

@ -0,0 +1,12 @@
function omf.cli.update
if omf.core.update
echo (omf::em)"Oh My Fish is up to date."(omf::off)
else
echo (omf::err)"Oh My Fish failed to update."(omf::off)
echo "Please open a new issue here → "(omf::em)"github.com/oh-my-fish/oh-my-fish/issues"(omf::off)
end
for package in (omf.packages.list --installed)
omf.packages.update $package
end
end

@ -1,3 +1,3 @@
function omf.version
function omf.cli.version
echo "Oh My Fish! $OMF_VERSION"
end

@ -0,0 +1,3 @@
function omf.core.update
omf.repo.pull $OMF_PATH
end

@ -0,0 +1,68 @@
# SYNOPSIS
# Oh My Fish! CLI
#
# OVERVIEW
# Provides options to list, download and remove packages, update
# the framework, create / submit a new package, etc.
function omf -d "Oh My Fish"
if test "x$argv[-1]" = "x--help" -a (count $argv) = 2
set command help
set arguments $argv[1]
else if test (count $argv) -ge 2
set command $argv[1]
set arguments $argv[2..-1]
else if test (count $argv) = 1
set command $argv[1]
set arguments
else
set command help
set arguments
end
switch "$command"
case "-v*" "--v*"
omf.cli.version
case "-h*" "--h*" "help"
omf.cli.help $arguments
case "c" "cd"
omf.cli.cd $arguments
case "d" "describe"
omf.cli.describe $arguments
case "destroy"
omf.cli.destroy
case "doctor"
omf.cli.doctor
case "i" "install" "get"
omf.cli.install $arguments
case "l" "ls" "list"
omf.cli.list $arguments
case "n" "new"
omf.cli.new $arguments
case "r" "rm" "remove" "uninstall"
omf.cli.remove $arguments
case "s" "submit"
omf.cli.submit $arguments
case "t" "theme"
omf.cli.theme $arguments
case "u" "update"
omf.cli.update
case "*"
echo (omf::err)"$argv[1] option not recognized"(omf::off) 1^&2
return $OMF_UNKNOWN_OPT
end
end

@ -1,4 +1,4 @@
function omf.describe -a name
function omf.packages.describe -a name
if test (count $argv) -eq 0
for package in (omf.packages.list --database)
echo $package - (omf.describe $package)

@ -1,11 +1,16 @@
function omf.install -a name_or_url
function __omf.install.success
echo (omf::em)"$argv successfully installed."(omf::off)
end
function __omf.packages.install.success
echo (omf::em)"$argv successfully installed."(omf::off)
end
function __omf.install.error
echo (omf::err)"Could not install $argv."(omf::off) 1^&2
end
function __omf.packages.install.error
echo (omf::err)"Could not install $argv."(omf::off) 1^&2
end
function __omf.packages.install.error.already
echo (omf::err)"Error: $argv already installed."(omf::off) 1^&2
end
function omf.packages.install -a name_or_url
if test \( -e $OMF_PATH/db/themes/$name_or_url \) -o (echo $name_or_url | grep theme-)
set install_type "theme"
@ -19,15 +24,15 @@ function omf.install -a name_or_url
set name $name_or_url
set url (cat $OMF_PATH/db/$parent_path/$name)
else
set name (omf.package_name $name_or_url)
set name (omf.packages.name $name_or_url)
set url $name_or_url
end
if test -e $OMF_PATH/$parent_path/$name
if test "$install_type" = theme
omf.theme $name
omf.theme.set $name
else
echo (omf::err)"Error: $name $install_type already installed."(omf::off) 1^&2
__omf.packages.install.error.already "$install_type $name_or_url"
return $OMF_INVALID_ARG
end
else
@ -36,13 +41,13 @@ function omf.install -a name_or_url
if omf.repo.clone $url $OMF_PATH/$parent_path/$name
omf.bundle.install $OMF_PATH/$parent_path/$name/bundle
omf.bundle.add $install_type $name_or_url
__omf.install.success "$install_type $name"
__omf.packages.install.success "$install_type $name"
if test "$install_type" = theme
omf.theme $name
omf.theme.set $name
end
else
__omf.install.error "$install_type $name"
__omf.packages.install.error "$install_type $name"
return $OMF_UNKNOWN_ERR
end
end

@ -4,6 +4,7 @@ end
function __omf.packages.builtin
echo "omf"
echo "fish-spec"
end
function __omf.packages.list -a type
@ -64,11 +65,11 @@ end
function omf.packages.list -a option type
switch "$option"
case "--available"
case "-a" "--available"
__omf.packages.list.available $type
case "--database"
case "-d" "--database"
__omf.packages.list.database $type
case "--installed"
case "-i" "--installed"
__omf.packages.list.installed $type
case "*"
__omf.packages.list $type

@ -1,3 +1,3 @@
function omf.package_name -a name_or_url
function omf.packages.name -a name_or_url
basename $name_or_url | sed -E 's/^(omf-)?((plugin|pkg|theme)-)?//;s/.git$//'
end

@ -0,0 +1,78 @@
function __omf.packages.new.mkdir -a name
set -l name "$argv[1]"
if test -d "$OMF_CONFIG"
set name "$OMF_CONFIG/$name"
else if test -d "$OMF_PATH"
set name "$OMF_PATH/$name"
end
mkdir -p "$name"
echo $name
end
function __omf.packages.new.from_template -a path github user name
for file in $path/*
if test -d $file
mkdir (basename $file)
pushd (basename $file)
__omf.packages.new.from_template $file $github $user $name
else
set -l target (begin
if test (basename $file) = "{{NAME}}.fish"
echo "$name.fish"
else
echo (basename "$file")
end
end)
sed "s/{{USER_NAME}}/$user/;s/{{GITHUB_USER}}/$github/;s/{{NAME}}/$name/" \
$file > $target
echo (omf::em)" create "(omf::off)" "(begin
if test (basename $PWD) = $name
echo ""
else
echo (basename "$PWD")"/"
end
end)$target
end
end
popd >/dev/null ^&2
end
function omf.packages.new -a option name
switch $option
case "p" "pkg" "pack" "packg" "package"
set option "pkg"
case "t" "th" "the" "thm" "theme" "themes"
set option "themes"
case "*"
echo (omf::err)"$option is not a valid option."(omf::off) 1^&2
return $OMF_INVALID_ARG
end
if not omf.packages.valid_name "$name"
echo (omf::err)"$name is not a valid package/theme name"(omf::off) 1^&2
return $OMF_INVALID_ARG
end
if set -l dir (__omf.packages.new.mkdir "$option/$name")
cd $dir
set -l github (git config github.user)
test -z "$github"; and set github "{{USER}}"
set -l user (git config user.name)
test -z "$user"; and set user "{{USER}}"
__omf.packages.new.from_template "$OMF_PATH/pkg/omf/templates/$option" \
$github $user $name
echo (omf::em)"Switched to $dir"(omf::off)
if test "$option" = themes
omf.theme.set $name
end
else
echo (omf::err)"\$OMF_CONFIG and/or \$OMF_PATH undefined."(omf::off) 1^&2
exit $OMF_UNKNOWN_ERR
end
end

@ -0,0 +1,10 @@
function omf.packages.path -a name
for path in {$OMF_CONFIG,$OMF_PATH}/{themes,pkg}/$name
if test -e $path
echo $path
return 0
end
end
return 1
end

@ -0,0 +1,46 @@
function omf.packages.remove -a pkg
if not omf.packages.valid_name $pkg
echo (omf::err)"$pkg is not a valid package/theme name"(omf::off) 1>&2
return $OMF_INVALID_ARG
end
if test $pkg = "omf" -o $pkg = "default"
echo (omf::err)"You can't remove `$pkg`"(omf::off) 1>&2
return $OMF_INVALID_ARG
end
for path in {$OMF_PATH,$OMF_CONFIG}/pkg/$pkg
test -d $path;
and set found;
or continue
source $path/uninstall.fish ^/dev/null;
and emit uninstall_$pkg
if rm -rf $path
omf.bundle.remove "package" $pkg
return 0
else
return 1
end
end
for path in {$OMF_PATH,$OMF_CONFIG}/themes/$pkg
test -d $path;
and set found;
or continue
test $pkg = (cat $OMF_CONFIG/theme);
and echo default > $OMF_CONFIG/theme
if rm -rf $path
omf.bundle.remove "theme" $pkg
return 0
else
return 1
end
end
set -q found; or return 2
end

@ -5,7 +5,7 @@
# name Name of the package.
# [url] URL to the package repository.
function omf.submit -a name url -d "Submit a package to the registry"
function omf.packages.submit -a name url -d "Submit a package to the registry"
switch (dirname $name)
case pkg
case themes
@ -15,7 +15,7 @@ function omf.submit -a name url -d "Submit a package to the registry"
end
set -l pkg (basename $name)
if not omf.util_valid_package $pkg
if not omf.packages.valid_name $pkg
echo (omf::err)"$pkg is not a valid package/theme name"(omf::off) 1^&2
return $OMF_INVALID_ARG
end

@ -0,0 +1,19 @@
function omf.packages.update -a name
if set target_path (omf.packages.path $name)
# Skip packages outside version control
not test -e $target_path/.git;
and return 0
if omf.repo.pull $target_path
omf.bundle.install $target_path/bundle
echo (omf::em)"$name successfully updated."(omf::off)
return 0
else
echo (omf::err)"Could not update $name."(omf::off) 1^&2
end
else
echo (omf::err)"Could not find $name."(omf::off) 1^&2
end
return 1
end

@ -1,4 +1,4 @@
function omf.util_valid_package -a package
function omf.packages.valid_name -a package
test (echo "$package" | tr "[:upper:]" "[:lower:]") = "omf"; and return 10
test (echo "$package" | tr "[:upper:]" "[:lower:]") = "default"; and return 10
switch $package

@ -1,4 +1,4 @@
function omf.theme -a target_theme
function omf.theme.set -a target_theme
if not contains "$target_theme" (omf.packages.list --installed --theme)
echo (omf::err)"Theme not installed!"(omf::off)
echo Install it using (omf::em)omf install $target_theme(omf::off)

@ -1,3 +1,28 @@
function init -a path --on-event init_omf
autoload $path/cli $path/util
set -g OMF_MISSING_ARG 1
set -g OMF_UNKNOWN_OPT 2
set -g OMF_INVALID_ARG 3
set -g OMF_UNKNOWN_ERR 4
function omf::em
set_color $fish_color_match ^/dev/null; or set_color cyan
end
function omf::dim
set_color $fish_color_autosuggestion ^/dev/null; or set_color 555
end
function omf::err
set_color $fish_color_error ^/dev/null; or set_color red --bold
end
function omf::under
set_color --underline
end
function omf::off
set_color normal
end
autoload $path/functions/{compat,core,packages,themes,bundle,util,repo,cli}
end

@ -0,0 +1 @@
functions/bundle/omf.bundle.install.fish

@ -1,160 +0,0 @@
# SYNOPSIS
# Oh My Fish! CLI
#
# ENV
# OMF_CONFIG Oh My Fish! configuration
#
# OVERVIEW
# Provides options to list, download and remove packages, update
# the framework, create / submit a new package, etc.
set -g OMF_MISSING_ARG 1
set -g OMF_UNKNOWN_OPT 2
set -g OMF_INVALID_ARG 3
set -g OMF_UNKNOWN_ERR 4
function omf::em
set_color $fish_color_match ^/dev/null; or set_color cyan
end
function omf::dim
set_color $fish_color_autosuggestion ^/dev/null; or set_color 555
end
function omf::under
set_color --underline
end
function omf::err
set_color $fish_color_error ^/dev/null; or set_color red --bold
end
function omf::off
set_color normal
end
function omf -d "Oh My Fish"
if test (count $argv) -eq 0
omf.help "main"; and return 0
else
if test "--help" = "$argv[-1]" -a (count $argv) = 2
omf.help $argv[1..-2]; and return 0
end
end
switch $argv[1]
case "-v*" "--v*"
omf.version
case "doctor"
omf.doctor
case "-h*" "--h*" "help"
if test (count $argv) -eq 1
omf.help "main"
else
omf.help $argv[2]
end
case "c" "cd"
switch (count $argv)
case 1
omf.packages.cd
case 2
omf.packages.cd $argv[2]
case "*"
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" <name>" 1^&2
return $OMF_INVALID_ARG
end
case "d" "describe"
if test (count $argv) -eq 1
omf.describe
else
omf.describe $argv[2..-1]
end
case "destroy"
omf.destroy
case "i" "install" "get"
if test (count $argv) -eq 1
omf.bundle.install;
and set installed
else
for package in $argv[2..-1]
omf.install $package;
and set installed
end
end
set -q installed; and refresh
return 0
case "l" "ls" "list"
omf.packages.list --installed | column
case "n" "new"
if test (count $argv) -ne 3
echo (omf::err)"Package type or name missing"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|theme"(omf::off)" <name>" 1^&2
return $OMF_MISSING_ARG
end
omf.new $argv[2..-1]
case "r" "rm" "remove" "uninstall"
if test (count $argv) -ne 2
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" <[package|theme] name>" 1^&2
return $OMF_INVALID_ARG
end
omf.remove_package $argv[2] ; and refresh
case "s" "submit"
switch (count $argv)
case 3
omf.submit $argv[2] $argv[3]
case "*"
echo (omf::err)"Argument missing"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|themes"(omf::off)"/<name> <url>" 1^&2
return $OMF_MISSING_ARG
end
case "t" "theme"
if test (count $argv) -eq 1
set -l theme (cat $OMF_CONFIG/theme)
set -l highlight_current "(^|[[:space:]])($theme)([[:space:]]|\$)"
echo (omf::under)"Installed:"(omf::off)
omf.packages.list --installed --theme | column | sed -E s/"$highlight_current"/"\1"(omf::em)"\2"(omf::off)"\3"/g
echo
echo (omf::under)"Available:"(omf::off)
omf.packages.list --available --theme | column
else if test (count $argv) -eq 2
omf.theme $argv[2]
else
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" [<theme name>]" 1^&2
return $OMF_INVALID_ARG
end
case "u" "update"
echo (omf::em)"Updating Oh My Fish..."(omf::off)
if omf.repo.pull $OMF_PATH
echo (omf::em)"Oh My Fish is up to date."(omf::off)
else
echo (omf::err)"Oh My Fish failed to update."(omf::off)
echo "Please open a new issue here → "(omf::em)"github.com/oh-my-fish/oh-my-fish/issues"(omf::off)
end
for package in (omf.packages.list --installed)
omf.update $package
end
refresh
case "*"
echo (omf::err)"$argv[1] option not recognized"(omf::off) 1^&2
return $OMF_UNKNOWN_OPT
end
end

@ -0,0 +1 @@
functions/packages/omf.packages.install.fish

@ -0,0 +1 @@
functions/packages/omf.packages.list.fish

@ -0,0 +1 @@
functions/packages/omf.packages.name.fish

@ -0,0 +1 @@
functions/repo/omf.repo.pull.fish

@ -1,10 +0,0 @@
function omf.util_mkdir -a name
set -l name "$argv[1]"
if test -d "$OMF_CONFIG"
set name "$OMF_CONFIG/$name"
else if test -d "$OMF_PATH"
set name "$OMF_PATH/$name"
end
mkdir -p "$name"
echo $name
end
Loading…
Cancel
Save