我目前有一个安装程序,可以安装一组ruby脚本(这是一个从主程序进行的可选安装)。我已经写了一个脚本,在安装之后,它扫描包含的ruby文件中的所有require语句,然后取出所有的“必需”文件。例如,它将查找
require 'curb'
require 'rest-client'
# require etc...这是在每个文件中,然后将它们包含在一个列表中(删除重复项),所以我有一个如下所示的数组:
"curb", "rest-client", etc...对于大多数gem来说,这是非常好的,因为它们的名字是匹配的,并且我只做了一个
gem install GEMNAME但是,在名称不匹配的情况下,我正在尝试通过查询gem服务器,从请求行中找出gem名称的方法。例如:
xml-simple有一个请求语句xmlsimple,但是gem是xml-simple。经过多次搜索,我能找到的唯一“解决方案”是安装每个gem,并检查文件是否包含在
gem specification GEMNAME这远不是最优的,实际上是一个非常糟糕的想法,我想知道是否有一种方法可以查询rubygems来查看gem中包含哪些文件。
发布于 2011-09-24 07:00:06
Rubygems有一个带有搜索端点的API:
/api/v1/search.(json|xml|yaml)?query=[YOUR QUERY]搜索终结点在Gem Methods上列出。我确信这个搜索可以用来做您想做的事情,可能是通过提供部分名称并使用正则表达式来过滤关闭的匹配项。
编辑:如果我没记错的话,也可以看看Bundler gem本身,因为当你输入错误时,他们有时会推荐gems。
更新:
我会遵循这样的工作流程:
尝试安装gem,因为它们是必需的。
如果出现错误,请选择宝石名称的几个片段,比如gem_name_str[0..5]、gem_name_str[0..4]和gem_name_str[0..3]。
使用这些字符串查询API。
删除重复项
使用动态生成的正则表达式测试返回值。类似于:
#given @param name = string from 'require' statement
values_returned_from_api.each do |value|
split_name = name.split(//)
split_value = value.split(//)
high_mark = 0
#this will find the index of the last character which is the same in both strings
for (i in 0..split_name.length) do
if split_name[i] == split_value[i]
high_mark = i
else
break
end
end
#this regex will tell you if the names differ by only one character
#at the point where they stopped matching. In my experience, this has
#been the only way gem names differ from their require statement.
#such as 'active_record'/'activerecord', 'xml-simple'/'xmlsimple' etc...
#get the shorter name of the two
regex_str = split_name.length < split_value.length ? split_name : split_value
#get the longer name of the two
comparison_str = split_name.length < split_value.length ? value : name
#build the regex
regex_str.insert((high_mark + 1), '.') #insert a dot where the two strings differ
regex = /#{regex_str.join('')}/
return name if comparison_str =~ regex
end注意:此代码未经过测试。这只是为了说明这一点。它也可能被优化和压缩。
我假设API返回部分匹配。我还没有真正尝试过。
https://stackoverflow.com/questions/7532979
复制相似问题