我正在尝试在gemspec文件中包含所有git子模块中的文件。 gemspec读取如下
require File.expand_path(File.dirname(__FILE__)) + '/lib/nameofgem.rb'
# Gets all files, from all submodules, and returns them
# as an array
def getSubmoduleFiles()
files = []
# get an array of submodule dirs by executing 'pwd' inside each submodule
`git submodule --quiet foreach pwd`.split("\n").each do |submodule_path|
gemRootDir = File.dirname(File.expand_path(__FILE__))
# for each submodule, change working directory to that submodule
Dir.chdir(submodule_path) do
submodule_files = getSubmoduleFiles()
# issue git ls-files in submodule's directory
submodule_files + `git ls-files`.split("\n")
puts "found:"
puts submodule_files.to_s
puts
# prepend the submodule path to create absolute file paths
submodule_files_fullpaths = submodule_files.map do |filename|
"#{submodule_path}/#{filename}"
end
# remove leading path parts to get paths relative to the gem's root dir
# (this assumes, that the gemspec resides in the gem's root dir)
submodule_files_paths = submodule_files_fullpaths.map do |filename|
filename.gsub "#{gemRootDir}/", ""
end
# add relative paths to gem.files
files += submodule_files_paths
end
end
return files
end
Gem::Specification.new do |s|
s.name = 'name'
s.version = "1.1.1"
s.executables << 'exec'
s.licenses = ['LICENCE']
s.summary = "Does stuff"
s.description = "Longer description of stuff"
s.authors = ["author"]
s.email = 'my@email'
s.files = `git ls-files -- lib/*`.split("\n")
s.homepage = 'https://example.com/'
s.required_ruby_version = '>= 2.0.0'
s.files += getSubmoduleFiles()
end
但是在do块中,我得到了
Invalid gemspec in [nameofgem.gemspec]: undefined method `getSubmoduleFiles' for Gem::Specification:::Module
我做错了什么?为什么不能在do块中调用函数?
答案 0 :(得分:0)
gemspec文件似乎在作用域和命名空间上做了一些奇怪的事情(在整个文件中,不仅在gemspec块本身内),而且在将我的头撞到办公桌上15分钟并放下另一把啤酒使之变钝之后由于无法调用gemspec中的函数而导致的生存折磨,我想出了一个合理的解决方案:在定义和调用中都将self.
放在函数名称的前面,就像这样:
def self.foo
puts 'It works :)'
end
Gem::Specification.new do |s|
# ...
self.foo # should print 'It works :)' instead of erroring out
# ...
end
为什么这是必要的,以及为什么似乎没有人在万维网上的任何地方都提出或回答过这个问题,我没有最愚蠢的想法,但是我们开始吧。