diff --git a/Vedvfile b/Vedvfile index f26181b..42e78e7 100644 --- a/Vedvfile +++ b/Vedvfile @@ -30,3 +30,7 @@ RUN sudo pacman -S --noconfirm --needed snapd && \ RUN sudo pacman -S --noconfirm --needed docker docker-compose && \ sudo systemctl enable docker && \ sudo usermod -aG docker "\$USER" +## YZSH +RUN cd "\$(mktemp -d)" && \ + git clone https://github.com/yunielrc/yzsh.git && cd yzsh && \ + make install diff --git a/src/usr/bin/ydf b/src/usr/bin/ydf index 6c85e3c..94c70fe 100755 --- a/src/usr/bin/ydf +++ b/src/usr/bin/ydf @@ -37,6 +37,8 @@ readonly _YDF_LIB_DIR="${E_YDF_TMP_DIR:-"${YDF_TMP_DIR:-"/var/tmp/${USER}/ydf"}" readonly _YDF_CACHE_DIR="${E_YDF_CACHE_DIR:-"${YDF_CACHE_DIR:-"/home/${USER}/.var/cache/ydf"}"}" readonly _YDF_PACKAGE_SERVICE_DEFAULT_OS="${E_YDF_PACKAGE_SERVICE_DEFAULT_OS:-"$YDF_PACKAGE_SERVICE_DEFAULT_OS"}" +readonly _YDF_YZSH_DATA_DIR="${E_YDF_YZSH_DATA_DIR:-"${YDF_YZSH_DATA_DIR:-~/.yzsh}"}" +readonly _YDF_YZSH_GEN_CONFIG_FILE="${E_YDF_YZSH_GEN_CONFIG_FILE:-"${YDF_YZSH_GEN_CONFIG_FILE:-~/.yzsh-gen.env}"}" # CREATE DIRECTORIES ydf::__create_dirs() { @@ -63,7 +65,11 @@ ydf::__source_files() { } ydf::__init_components() { - ydf::package_service::constructor "$_YDF_PACKAGE_SERVICE_DEFAULT_OS" + ydf::package_service::constructor \ + "$_YDF_PACKAGE_SERVICE_DEFAULT_OS" \ + "$_YDF_YZSH_DATA_DIR" \ + "$_YDF_YZSH_GEN_CONFIG_FILE" + } # FUNCTIONS diff --git a/src/usr/lib/ydf/components/package/ydf-package-service.bash b/src/usr/lib/ydf/components/package/ydf-package-service.bash index 7d5f8f7..b57bf35 100644 --- a/src/usr/lib/ydf/components/package/ydf-package-service.bash +++ b/src/usr/lib/ydf/components/package/ydf-package-service.bash @@ -27,8 +27,8 @@ fi # readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='home homeln homecp homecps homecat root rootcp rootcps rootln rootcat flatpack dconf.ini plugin_zsh docker_compose' # readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall pacman yay install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" # readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall apt install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" - -readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='' +# shellcheck disable=SC2016 +readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='plugin_zsh:${pkg_name}.plugin.zsh' readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall install @pacman @yay @flatpak @snap docker_compose:docker-compose.yml postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" # readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" @@ -39,13 +39,17 @@ readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall install @pacman # # Constructor # -# default_os string default os +# default_os string default os +# yzsh_data_dir string yzsh data dir +# yzsh_gen_config_file string yzsh gen config file # # Returns: # 0 on success, non-zero on error. # ydf::package_service::constructor() { readonly __YDF_PACKAGE_SERVICE_DEFAULT_OS="$1" + readonly __YDF_YZSH_DATA_DIR="$2" + readonly __YDF_YZSH_GEN_CONFIG_FILE="$3" } # @@ -176,7 +180,7 @@ ydf::package_service::__instruction_@snap() { } # -# Execute docker-compose.yml instruction +# Execute docker_compose instruction # # Arguments: # pkg_name string package name @@ -188,6 +192,35 @@ ydf::package_service::__instruction_docker_compose() { docker compose up -d } +# +# Execute plugin_zsh instruction +# +# Arguments: +# pkg_name string package name +# +# Returns: +# 0 on success, non-zero on error. +# +ydf::package_service::__instruction_plugin_zsh() { + local -r package_name="$1" + + local -r plugin_name="${package_name}.plugin.zsh" + local -r plugin_file="${PWD}/${plugin_name}" + local -r plugin_dest_file="${__YDF_YZSH_DATA_DIR}/plugins/local/${plugin_name}" + + ln -vsf "$plugin_file" "$plugin_dest_file" || { + err "Creating plugin symlink: ${plugin_dest_file}" + return "$ERR_FILE_CREATION" + } + + if [[ ! -f "$__YDF_YZSH_GEN_CONFIG_FILE" ]] || + ! grep -q "YZSH_PLUGINS+=($package_name)" "$__YDF_YZSH_GEN_CONFIG_FILE"; then + echo "YZSH_PLUGINS+=($package_name)" >>"$__YDF_YZSH_GEN_CONFIG_FILE" + else + ech "Plugin '${package_name}' already added to ${__YDF_YZSH_GEN_CONFIG_FILE}" + fi +} + # # Install a ydotfile package from a directory # @@ -237,10 +270,11 @@ ydf::package_service::install_one_from_dir() { } for _instr in "${instr_arr[@]}"; do + local ifunc_partial_name="${_instr%%:*}" - local ifile_name="${_instr##*:}" + eval local ifile_name="${_instr##*:}" local ifunction="ydf::package_service::__instruction_${ifunc_partial_name}" - + # shellcheck disable=SC2154 if [[ ! -f "./${ifile_name}" ]]; then continue fi diff --git a/src/usr/lib/ydf/errors.bash b/src/usr/lib/ydf/errors.bash index e4c34fb..ac1d58b 100644 --- a/src/usr/lib/ydf/errors.bash +++ b/src/usr/lib/ydf/errors.bash @@ -8,6 +8,7 @@ readonly ERR_INVAL_ARG=67 readonly ERR_MISSING_ARG=68 readonly ERR_NO_DIR=69 readonly ERR_CHANGING_WORKDIR=69 +readonly ERR_FILE_CREATION=70 ## package-service readonly ERR_YPS_GENERAL=80 diff --git a/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh b/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh new file mode 100644 index 0000000..16323c9 --- /dev/null +++ b/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh @@ -0,0 +1,7 @@ +# Aliases +alias hello='Hello from zsh plugin' + +# Functions +hello_func() { + echo 'Hello from zsh plugin' +} diff --git a/tests/fixtures/packages/bandwhich/.pacman b/tests/fixtures/packages/bandwhich/.pacman deleted file mode 100644 index e69de29..0000000 diff --git a/tests/fixtures/packages/bandwhich/bandwhich.plugin.zsh b/tests/fixtures/packages/bandwhich/bandwhich.plugin.zsh deleted file mode 100644 index 6419bcc..0000000 --- a/tests/fixtures/packages/bandwhich/bandwhich.plugin.zsh +++ /dev/null @@ -1,2 +0,0 @@ -# Aliases -alias bandwhich='sudo bandwhich' diff --git a/tests/fixtures/packages/bandwhich/postsetup b/tests/fixtures/packages/bandwhich/postsetup deleted file mode 100644 index aa105be..0000000 --- a/tests/fixtures/packages/bandwhich/postsetup +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env sh -set -eu - -# Configure -# https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file-on-ubuntu-and-centos -echo "${USER} ALL=NOPASSWD:$(which bandwhich)" | sudo tee "/etc/sudoers.d/${USER}-nopasswd-bandwhich" diff --git a/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats b/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats index e7bf831..275678f 100644 --- a/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats +++ b/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats @@ -1,11 +1,19 @@ load test_helper -# setup() { -# -# } +setup() { + + if [[ -f /home/vedv/.yzsh-gen.env ]]; then + rm -f /home/vedv/.yzsh-gen.env + fi + + if [[ -d /home/vedv/.yzsh/plugins/local ]]; then + rm -rf /home/vedv/.yzsh/plugins/local + fi + mkdir /home/vedv/.yzsh/plugins/local +} # teardown() { -# + # } # Tests for ydf package @@ -267,3 +275,21 @@ com.github.tchx84.Flatseal: postinstall succeed" assert_success assert [ -n "$output" ] } + +# Tests for ydf package install +@test "ydf package install ./10ydfplugin Should succeed" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages/10ydfplugin" + + run ydf package install "$_package_dir" + + assert_success + assert_output "'/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' -> '/home/vedv/ydf/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh'" + + assert [ -L '/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' ] + assert [ -f '/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' ] + + run grep "YZSH_PLUGINS+=(10ydfplugin)" "$YDF_YZSH_GEN_CONFIG_FILE" + + assert_success + assert_output "YZSH_PLUGINS+=(10ydfplugin)" +} diff --git a/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats b/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats index 9665b7b..71d451a 100644 --- a/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats +++ b/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats @@ -1,12 +1,28 @@ load test_helper setup() { - ydf::package_service::constructor "$YDF_PACKAGE_SERVICE_DEFAULT_OS" - export __YDF_PACKAGE_SERVICE_DEFAULT_OS + ydf::package_service::constructor \ + "$YDF_PACKAGE_SERVICE_DEFAULT_OS" \ + "$YDF_YZSH_DATA_DIR" \ + "$YDF_YZSH_GEN_CONFIG_FILE" + + export __YDF_PACKAGE_SERVICE_DEFAULT_OS \ + __YDF_YZSH_DATA_DIR \ + __YDF_YZSH_GEN_CONFIG_FILE + + + if [[ -f /home/vedv/.yzsh-gen.env ]]; then + rm -f /home/vedv/.yzsh-gen.env + fi + + if [[ -d /home/vedv/.yzsh/plugins/local ]]; then + rm -rf /home/vedv/.yzsh/plugins/local + fi + mkdir /home/vedv/.yzsh/plugins/local } # teardown() { -# + # } @@ -284,3 +300,41 @@ docker_compose" assert_success assert [ -n "$output" ] } + +# Tests for ydf::package_service::__instruction_plugin_zsh() +@test "ydf::package_service::__instruction_plugin_zsh() Should fail if ln fails" { + cd "${TEST_FIXTURES_DIR}/packages/10ydfplugin" + + ln() { + assert_equal "$*" "-vsf /home/vedv/ydf/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh /home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh" + return 1 + } + + run ydf::package_service::__instruction_plugin_zsh '10ydfplugin' + + assert_failure + assert_output "ERROR> Creating plugin symlink: /home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh" +} + +@test "ydf::package_service::__instruction_plugin_zsh() Should add plugin" { + cd "${TEST_FIXTURES_DIR}/packages/10ydfplugin" + + run ydf::package_service::__instruction_plugin_zsh '10ydfplugin' + + assert_success + assert_output "'/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' -> '/home/vedv/ydf/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh'" + + assert [ -L '/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' ] + assert [ -f '/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' ] + + run grep "YZSH_PLUGINS+=(10ydfplugin)" "$YDF_YZSH_GEN_CONFIG_FILE" + + assert_success + assert_output "YZSH_PLUGINS+=(10ydfplugin)" + + run ydf::package_service::__instruction_plugin_zsh '10ydfplugin' + + assert_success + assert_output "'/home/vedv/.yzsh/plugins/local/10ydfplugin.plugin.zsh' -> '/home/vedv/ydf/tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh' +Plugin '10ydfplugin' already added to /home/vedv/.yzsh-gen.env" +} diff --git a/tools/install-run-manjaro b/tools/install-run-manjaro index 3a968d0..da785d2 100755 --- a/tools/install-run-manjaro +++ b/tools/install-run-manjaro @@ -6,6 +6,8 @@ set -eu # shellcheck source=../packages.env . ./packages.env +# shellcheck source=../.ydf.env +. ./.ydf.env sudo pacman -Syu --noconfirm --needed yay @@ -29,3 +31,11 @@ sudo pacman -S --noconfirm --needed docker docker-compose sudo systemctl enable docker --now sudo usermod -aG docker "$USER" + +# Install YZSH +## DATA +git clone "$YDF_YZSH_DATA_REPOSITORY_URL" "$YDF_YZSH_DATA_DIR" +## +cd "$(mktemp -d)" +git clone https://github.com/yunielrc/yzsh.git && cd yzsh +make install