diff --git a/lib/commands/command-communize-all.bash b/lib/commands/command-communize-all.bash new file mode 100644 index 0000000..9da3fa9 --- /dev/null +++ b/lib/commands/command-communize-all.bash @@ -0,0 +1,12 @@ +# shellcheck shell=bash + +# shellcheck source=lib/utils.sh +source "$(dirname "${BASH_SOURCE[0]}")/../utils.sh" + +install_dir=$(asdf where ruby) +install_dir="${install_dir%/*}" + +for version_dir in "$install_dir"/* +do + communize_gems "$version_dir" +done diff --git a/lib/commands/command-communize.bash b/lib/commands/command-communize.bash new file mode 100644 index 0000000..0c903ff --- /dev/null +++ b/lib/commands/command-communize.bash @@ -0,0 +1,15 @@ +# shellcheck shell=bash + +version="$1" + +# shellcheck source=lib/utils.sh +source "$(dirname "${BASH_SOURCE[0]}")/../communize.bash" + +install_dir=$(asdf where ruby) + +if [[ -n "$version" ]] +then + install_dir="${install_dir%/*}/$version" +fi + +communize_gems "$install_dir" diff --git a/lib/communize.bash b/lib/communize.bash new file mode 100644 index 0000000..e7c62f4 --- /dev/null +++ b/lib/communize.bash @@ -0,0 +1,43 @@ +#shellcheck shell=bash + +communize_gems(){ + + local install_path="$1" + + local version gem_dir etc_dir + version=$(basename "$install_path") + + etc_dir="${install_path}/etc" + communal_dir="${ASDF_DIR}/installs/rubygems" + gem_dir="${communal_dir}/$( communize_version "$version")" + individual_gem_dir="${install_path}/lib/ruby/gems" + + if [[ ! -d "$gem_dir" ]] + then + mkdir "$gem_dir" + fi + + if [[ -L "$individual_gem_dir" ]] + then + echo "Gems for ruby ${version} already communized" + elif [[ -d "$individual_gem_dir" ]] + then + rm -rf "$individual_gem_dir" + fi + if [[ ! -f "$individual_gem_dir" ]] + then + ln -s "$communal_dir" "$individual_gem_dir" + fi + + if [[ ! -f "$etc_dir/gemrc" ]] + then + mkdir -p "$etc_dir" + printf 'gemhome: %s\n' "$gem_dir" > "$etc_dir/gemrc" + fi + +} + +communize_version(){ + local version="$1" + echo "${version%.*}.0" +}