在 rbenv 中从以前版本的 Ruby 复制 gems [英] Copying gems from previous version of Ruby in rbenv

查看:74
本文介绍了在 rbenv 中从以前版本的 Ruby 复制 gems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 rbenv 安装了 Ruby 1.9.3-p286.现在,在安装新版本 (p327) 后,很明显,它对之前版本安装的 GEM 一无所知.

I installed Ruby 1.9.3-p286 with rbenv. Now, after installing a newer version (p327), obviously, it doesn't know anything about the GEMs installed with the previous version.

是否可以将 Gems 从那个版本复制到新版本,这样就不需要再次下载它们?

Is it possible to copy Gems from that version to the newer one, so that it won't be needed to download them all again?

推荐答案

我一直专门从升级和重新安装而不下载的角度来看待这个问题.这并非微不足道,我建议您对 gem 进行一些清理,以最大程度地减少需要完成的处理/安装量(例如,我安装了五个版本的 ZenTest;在此之前我做了gem 清理 ZenTest").但是,请小心使用gem cleanup",因为它会删除除最后一个版本之外的所有内容:如果您需要支持旧版本的 Rails,请手动清理您不需要的版本.

I've been looking at this specifically from the perspective of upgrading and reinstalling without downloading. It's not trivial, and I recommend you do some cleanup of your gems to minimize the amount of processing/installation that needs to be done (e.g., I had five versions of ZenTest installed; I did 'gem cleanup ZenTest' before doing this). Be careful with 'gem cleanup', though, as it removes all but the LAST version: if you need to support an older version of Rails, manually clean up the versions you don't need.

我称这个脚本为migrate-gems.sh":

I called this script 'migrate-gems.sh':

#! /bin/bash

if [ ${#} -ne 2 ]; then
  echo >&2 Usage: $(basename ${0}) old-version new-version
  exit 1
fi

home_path=$(cd ~; pwd -P)
old_version=${1}
new_version=${2}

rbenv shell ${old_version}

declare -a old_gem_paths old_gems
old_gem_paths=($(gem env gempath | sed -e 's/:/ /'))

rbenv shell ${new_version}

for ogp in "${old_gem_paths[@]}"; do
  case "${ogp}" in
    ${home_path}/.gem/ruby*|*/.gem/ruby*)
      # Skip ~/.gem/ruby.
      continue
      ;;
  esac

  for old_gem in $(ls -1 ${ogp}/cache/*.gem); do
    gem install --local --ignore-dependencies ${ogp}/cache/${old_gem}
  done
done

完成这项工作的三个部分:

There are three pieces that make this work:

  1. gem env gempath 包含安装 gems 的路径(:-separated).因为gems 在 ~/.gem/ruby 中共享,我跳过这个.
  2. gem install 接受 --local,这会强制没有网络连接.
  3. gem install 接受 --ignore-dependencies,这会禁用依赖项检查.
  1. gem env gempath contains the paths (:-separated) where gems are installed. Because gems are shared in ~/.gem/ruby, I skip this one.
  2. gem install accepts --local, which forces no network connections.
  3. gem install accepts --ignore-dependencies, which disables dependency checking.

我今天有一个相当大的 gem 列表要移动,我不想从 ruby​​gems.org 下载(另外,我需要旧版本),所以我很快就完成了.

I had a fairly large list of gems to move over today and I didn't want to download from rubygems.org (plus, I needed older versions), so I whipped this up fairly quickly.

这篇关于在 rbenv 中从以前版本的 Ruby 复制 gems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆