ruby 查看 Gem 的源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10453249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:05:48  来源:igfitidea点击:

Viewing a Gem's Source Code

rubyrubygems

提问by Alan Storm

Ruby dabbler/newbie here who's not familiar with the ecosystem, so apologies if this is one of those super duh questions.

Ruby 涉猎者/新手在这里不熟悉生态系统,所以如果这是那些超级愚蠢的问题之一,请道歉。

Is there a way to view allthe files and/or source code installed by a gem? That is, I just ran

有没有办法查看gem 安装的所有文件和/或源代码?也就是说,我刚刚跑了

$ gem install sass

And the sass gem is now a part of my local system

sass gem 现在是我本地系统的一部分

$ gem list --local
...
sass (3.1.16, 3.1.2)
...

I want to know what the gem installcommand put on my system. Is there a command I can run to see allthe files installed by the gem?

我想知道gem install命令在我的系统上放置了什么。有没有我可以运行的命令来查看gem 安装的所有文件?

After some googling, man gemand gem help commands, I discovered the contentscommand.

经过一番谷歌搜索man gemgem help commands,我发现了该contents命令。

$ gem contents sass

$ gem 内容 sass

However, when I run this command with the aforementioned sass gem, I get the following results

但是,当我使用上述 sass gem 运行此命令时,我得到以下结果

.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/engine_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/functions_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/extend_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/logger_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/css2sass_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/subset_map_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/multibyte_string_scanner_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/callbacks_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/importer_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/css_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/scss_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/rx_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/less_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/cache_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/plugin_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass-convert
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/scss

However, this list seems incomplete as I know there are files in

但是,这个列表似乎不完整,因为我知道里面有文件

.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.2/lib/

Why does contentsnot show the files from lib?

为什么不contents显示来自 的文件lib

Is it possible for a geminstaller to install files outside of the gems folder?

gem安装程序是否可以在 gems 文件夹之外安装文件?

Is there a command that can show everythinginstalled by a gem?

是否有可以显示gem 安装的所有内容的命令?

回答by Erik Petersen

gem has an unpack command: http://guides.rubygems.org/command-reference/#gem-unpack

gem 有一个解包命令:http: //guides.rubygems.org/command-reference/#gem-unpack

gem unpack rake
ls rake-0.4.3/

回答by earlonrails

There are two really good ways to do this. There is another gem which allows you to open the gem and edit. This gem is call gem-open

有两种非常好的方法可以做到这一点。还有另一个 gem 允许您打开 gem 并进行编辑。这个 gem 叫做gem-open

gem install gem-open 

then

然后

gem open sass

Another way is to generate your own rdocs.

另一种方法是生成您自己的 rdoc。

gem rdoc sass

You can then look at your rdocs by

然后您可以通过以下方式查看您的 rdocs

gem server

Also if you are using rvm, you can type rvm infoand it will show GEM_HOMElocation. This will be where all your gems source code is.

此外,如果您使用的是 rvm,您可以输入rvm info并显示GEM_HOME位置。这将是您所有 gems 源代码的位置。

cd $GEM_HOME
cd gems/sass-3.1.2/

Update:

更新:

This is the way I mostly do this now, when using bundler.

这是我现在主要使用捆绑程序的方式。

cd $(bundle show sass) 

This will be the version of sass in your Gemfile.

这将是您 Gemfile 中的 sass 版本。

回答by luis.madrigal

I usually open a gem by running this command from the console

我通常通过从控​​制台运行这个命令来打开一个 gem

EDITOR=<your editor> bundle open <name of gem>

回答by echristopherson

The lib/ directory you mentioned is for version 3.1.2 of the gem; gem contentswithout --versionwill just list one version (it appears to pick the newest version, but I'm unable to verify this is always true). What output do you get for gem contents --version 3.1.2 sass?

您提到的 lib/ 目录适用于 gem 3.1.2 版;gem contentswithout--version将只列出一个版本(它似乎选择了最新版本,但我无法验证这总是正确的)。你得到什么输出gem contents --version 3.1.2 sass

回答by codelion

You can also use just rename the .gem file to .tar and extract as a posix archive. The source code is inside it in the libfolder. See https://blog.srcclr.com/extracting-ruby-source-code-from-gem-packages/for more details.

您也可以使用将 .gem 文件重命名为 .tar 并提取为 posix 存档。源代码在它里面的lib文件夹中。有关更多详细信息,请参阅https://blog.srcclr.com/extracting-ruby-source-code-from-gem-packages/

回答by emery

In addition to gem contents, another command you might find useful is gem environment. If you have multiple paths for your gem installations, they will all be listed under the "GEM PATHS" label.

此外gem contents,您可能会发现另一个有用的命令是gem environment. 如果您的 gem 安装有多个路径,它们都将列在“GEM PATHS”标签下。