Skip to content

Commit

Permalink
feat(package): add an instruction to install a ydof plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yunielrc committed Sep 16, 2023
1 parent a93a2ba commit e2baaa0
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Vedvfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion src/usr/bin/ydf
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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
Expand Down
46 changes: 40 additions & 6 deletions src/usr/lib/ydf/components/package/ydf-package-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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"
}

#
Expand Down Expand Up @@ -176,7 +180,7 @@ ydf::package_service::__instruction_@snap() {
}

#
# Execute docker-compose.yml instruction
# Execute docker_compose instruction
#
# Arguments:
# pkg_name string package name
Expand All @@ -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
#
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/usr/lib/ydf/errors.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/packages/10ydfplugin/10ydfplugin.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Aliases
alias hello='Hello from zsh plugin'

# Functions
hello_func() {
echo 'Hello from zsh plugin'
}
Empty file.
2 changes: 0 additions & 2 deletions tests/fixtures/packages/bandwhich/bandwhich.plugin.zsh

This file was deleted.

6 changes: 0 additions & 6 deletions tests/fixtures/packages/bandwhich/postsetup

This file was deleted.

34 changes: 30 additions & 4 deletions tests/usr/lib/ydf/components/package/ydf-package-command.f.bats
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)"
}
60 changes: 57 additions & 3 deletions tests/usr/lib/ydf/components/package/ydf-package-service.i.bats
Original file line number Diff line number Diff line change
@@ -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() {
#

# }


Expand Down Expand Up @@ -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"
}
10 changes: 10 additions & 0 deletions tools/install-run-manjaro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
set -eu
# shellcheck source=../packages.env
. ./packages.env
# shellcheck source=../.ydf.env
. ./.ydf.env

sudo pacman -Syu --noconfirm --needed yay

Expand All @@ -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

0 comments on commit e2baaa0

Please sign in to comment.