获取无安装的红宝石宝石版本

发布于 2025-02-04 19:08:20 字数 537 浏览 2 评论 0原文

如果我有一个本地的宝石(gemfile.gem),如何在不安装宝石的情况下从代码或命令行中获取名称和版本信息。

原因:我正在安装用户宝石来验证它,并希望将其卸载以清理。用户宝石不是我控制的东西,因此我不能依靠命名约定。

CLI解决方案:

gem spec gemfile.gem name
gem spec gemfile.gem version

Ruby解决方案:

name = Psych.safe_load(`gem spec gemfile.gem name`).to_s
version = Psych.safe_load(`gem spec gemfile.gem version`, permitted_classes: [Gem::Version]).to_s
# Now you can uninstall the gem with
Gem::Uninstaller.new(name, {:version => version, :force => true}).uninstall

If I have a local gem (gemfile.gem) how can I get the name and version information from code or the command line without installing the gem.

Reason: I'm installing a user gem to validate it and want to uninstall it to clean up. User gem is not something I control so I can't depend on naming conventions.

CLI Solution:

gem spec gemfile.gem name
gem spec gemfile.gem version

Ruby Solution:

name = Psych.safe_load(`gem spec gemfile.gem name`).to_s
version = Psych.safe_load(`gem spec gemfile.gem version`, permitted_classes: [Gem::Version]).to_s
# Now you can uninstall the gem with
Gem::Uninstaller.new(name, {:version => version, :force => true}).uninstall

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2025-02-11 19:08:20

您可以在gemfile.lock其他选项中看到锁定版本

cat Gemfile.lock | grep gem-name

,但需要bundle install首先

bundle exec gem dependency | grep gem-name

更新:

如果您需要检查本地GEM版本,例如SOONE GEM 。来自二进制的信息

gem specification some-gem.gem

,或者

gem spec some-gem.gem

您也可以使用Ruby格式来查看它,

gem spec some-gem.gem --ruby

当然您可以使用grep版本 Word过滤行,

但是最好以这样的参数将其传递给

gem spec some-gem.gem version

You can see locked version in Gemfile.lock

cat Gemfile.lock | grep gem-name

Other option, but need bundle install first

bundle exec gem dependency | grep gem-name

Update:

If you need to check local gem version, for example some-gem.gem, you can use such command to parse all information from binary

gem specification some-gem.gem

or just

gem spec some-gem.gem

You can also look it with Ruby format

gem spec some-gem.gem --ruby

Of course you can use grep to filter lines with version word

But it's better to pass it as argument like this

gem spec some-gem.gem version
梦巷 2025-02-11 19:08:20

您的问题模棱两可。如果您的意思是“我该如何阅读Gemspec的宝石名称和版本?”然后,您可以使用 gem ::规格#加载。例如,假设您有一个具有标准布局的宝石,并且foo_bar.gemspec在宝石项目目录的根部中,您可以使用git来查找项目的顶级级别,并在Gemspec中阅读:

$ cd "$(git rev-parse --show-toplevel) 
$ ruby -e 'puts Gem::Specification.load "#{File.basename Dir.pwd}.gemspec"'
#<Gem::Specification name=foo_bar version=0.1.0>

然后,您可以用SED,尴尬或切割来解析输出。

Your question is ambiguous. If you mean "How can I read in the gem name and version from the gemspec?" then you can use the output of Gem::Specification#load. For example, assuming you have a gem with a standard layout and foo_bar.gemspec in the root of your gem's project directory, you can use Git to find the top-level of your project and read in the gemspec:

$ cd "$(git rev-parse --show-toplevel) 
$ ruby -e 'puts Gem::Specification.load "#{File.basename Dir.pwd}.gemspec"'
#<Gem::Specification name=foo_bar version=0.1.0>

You can then parse the output with sed, awk, or cut.

人疚 2025-02-11 19:08:20

gemfile没有(一定)指定依赖关系的确切版本。它可能没有指定(即“任何版本不错”)或〜&gt; 1.0(即&gt; = 1.0 and &lt; 2.0)或其他。同样,依赖性约束可能会进一步限制版本的有效范围。

假设这不是您的问题的意思。相反,您想知道依赖关系的确切版本将通过运行bundle install给定gemfile.lock.lock来安装。

通过解析来解析gemfile.lock.lock::

require 'bundler'

lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))

puts lockfile.specs.find { |spec| spec.name == 'the-gem-you-want-to-check' }.version

A Gemfile does not (necessarily) specify an exact version of a dependency. It might specify nothing (i.e. "any version is fine"), or ~> 1.0 (i.e. >= 1.0 and < 2.0), or whatever. Also, dependency constraints might further restrict the valid range of versions.

I'm assuming that this isn't what you meant by your question. Instead, you'd like to know what exact versions of dependencies will be installed by running bundle install, given a Gemfile.lock.

One way to achieve this reliably (i.e. rather than using grep and eyeballing which line(s) are most relevant) is by parsing the Gemfile.lock:

require 'bundler'

lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))

puts lockfile.specs.find { |spec| spec.name == 'the-gem-you-want-to-check' }.version
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文