Ruby-on-rails 我是否必须手动卸载所有依赖的 gem?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/952836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I have to manually uninstall all dependent gems?
提问by Hola
I tried to uninstall datamapper using the command gem uninstall dm-core.
我尝试使用命令卸载数据映射器gem uninstall dm-core。
But it seems that a whole bunch of dependent gems also need to be uninstalled.
不过好像还需要卸载一大堆依赖的gem。
C:\>gem uninstall dm-core
You have requested to uninstall the gem:
dm-core-0.9.11
dm-migrations-0.9.11 depends on [dm-core (= 0.9.11)]
dm-cli-0.9.11 depends on [dm-core (= 0.9.11)]
dm-serializer-0.9.11 depends on [dm-core (= 0.9.11)]
dm-timestamps-0.9.11 depends on [dm-core (= 0.9.11)]
dm-aggregates-0.9.11 depends on [dm-core (= 0.9.11)]
dm-types-0.9.11 depends on [dm-core (= 0.9.11)]
dm-is-tree-0.9.11 depends on [dm-core (= 0.9.11)]
dm-observer-0.9.11 depends on [dm-core (= 0.9.11)]
dm-validations-0.9.11 depends on [dm-core (= 0.9.11)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn] n
ERROR: While executing gem ... (Gem::DependencyRemovalException)
Uninstallation aborted due to dependent gem(s)
I tried finding documentation on "gem uninstall" but there doesn't seem to be a way to uninstall the dependencies automatically:
我尝试查找有关“gem uninstall”的文档,但似乎没有办法自动卸载依赖项:
C:\>gem help uninstall
Usage: gem uninstall GEMNAME [GEMNAME ...] [options]
Options:
-a, --[no-]all Uninstall all matching versions
-I, --[no-]ignore-dependencies Ignore dependency requirements while
uninstalling
-x, --[no-]executables Uninstall applicable executables with
out
confirmation
-i, --install-dir DIR Directory to uninstall gem from
-n, --bindir DIR Directory to remove binaries from
--[no-]user-install Uninstall from user's home directory
in addition to GEM_HOME.
-v, --version VERSION Specify version of gem to uninstall
--platform PLATFORM Specify the platform of gem to uninst
all
Common Options:
-h, --help Get help on this command
-V, --[no-]verbose Set the verbose level of output
-q, --quiet Silence commands
--config-file FILE Use this config file instead of defau
lt
--backtrace Show stack backtrace on errors
--debug Turn on Ruby debugging
Arguments:
GEMNAME name of gem to uninstall
Summary:
Uninstall gems from the local repository
Defaults:
--version '>= 0' --no-force --install-dir C:/Ruby18/lib/ruby/gems/1.8
--user-install
C:\>
Am I missing something?
我错过了什么吗?
采纳答案by Jonathan R. Wallace
As far as I know you're correct, there is not an easy way built-in to the gem command to do this.
据我所知,您是对的, gem 命令中没有内置的简单方法来执行此操作。
However, you can check out gem-prune which can help clean up your gem repository after you've removed dm-core.
但是,您可以查看 gem-prune,它可以在您删除 dm-core 后帮助清理您的 gem 存储库。
回答by Bruno Tavares
gem list | cut -d" " -f1 | xargs gem uninstall -aIxdeletes all installed ruby gems!
gem list | cut -d" " -f1 | xargs gem uninstall -aIx删除所有已安装的 ruby gem!
回答by ma11hew28
I ended up making a simple command line tool to gem uninstall dependencies recursively.
我最终制作了一个简单的命令行工具来递归地 gem 卸载依赖项。
I also filed a rubygems issue to gem uninstall dependencies recursively.
我还提交了一个 rubygems 问题以递归地gem 卸载依赖项。
That rubygems issue was closed and will not be considered until somebody provides a patch including tests.
该 rubygems 问题已关闭,在有人提供包括测试在内的补丁之前不会考虑。
回答by RobertDeRose
for gem in `gem list --no-version`; do
gem uninstall -aIx $gem
done
Works the best for me, not sure why but
对我来说效果最好,不知道为什么,但是
gem list | cut -d" " -f1 | xargs gem uninstall -aIx
doesn't work on my system as it still complains...
在我的系统上不起作用,因为它仍然抱怨...
ERROR: While executing gem ... (Gem::InstallError)
cannot uninstall, check `gem list -d some-gem-here`
回答by J. Morgan Lieberthal
回答by Michael Cabral Poubel Bastos
The problem when running these sort of uninstalls is that they go down the list of gems in order so if a gum is uninstallable then you end up getting stuck. Run the below a few times and it should remove all the gems it is allowed to.
运行此类卸载时的问题在于它们会按顺序排列在 gem 列表中,因此如果可卸载口香糖,那么您最终会卡住。运行下面几次,它应该删除它允许的所有宝石。
gem list | cut -d" " -f1 | sort -R | xargs -n1 gem uninstall -aIx
回答by dnshio
If you would like to use some wild cards to remove some gems (e.g. to remove some gems from a specific vendor) then you can pipe the output from gem list to grep as shown below
如果您想使用一些通配符来删除一些 gem(例如从特定供应商那里删除一些 gem),那么您可以将输出从 gem 列表传递到 grep,如下所示
gem list --no-version | grep "opener-" | cut -d " " -f1 | xargs gem uninstall -aIx
The above command removes all the gems whose name begins with "opener-"
上面的命令删除所有名称以“opener-”开头的 gem
回答by cldwalker
This snippet of code does it for me:
这段代码为我做了:
def gem_deps(name)
if `gem dependency #{name}` =~ /(Gem #{name}-.*?)(Gem|\z)/m
.split("\n").grep(/runtime\s*\)/).map do |line|
line[/[\w-]+/]
end.compact
else
[]
end
end
def gem_recursive_uninstall(name)
deps = gem_deps(name)
if deps.empty?
system('sudo','gem','uninstall',name)
else
puts("Uninstall #{name} with dependencies: #{deps.join(', ')}? [y/n]")
if gets.chomp[/y/]
system(*(%w{sudo gem uninstall} + [name] + deps))
end
end
end
Taken from http://github.com/cldwalker/irbfiles/blob/master/.irb/libraries/gem.rb
取自http://github.com/cldwalker/irbfiles/blob/master/.irb/libraries/gem.rb
回答by Peter Baker
Just list all the gems you want to uninstall e.g. gem uninstall dm-migrations dm-cli dm-observer. And try to manage your gems with Bundler whenever possible.
只需列出您要卸载的所有 gem,例如gem uninstall dm-migrations dm-cli dm-observer. 并尽可能尝试使用 Bundler 管理您的宝石。

