Skip to content

Instantly share code, notes, and snippets.

@mattconnolly
Last active December 25, 2022 03:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattconnolly/5875987 to your computer and use it in GitHub Desktop.
Save mattconnolly/5875987 to your computer and use it in GitHub Desktop.
Gem Spec support for git submodules inside a gem.
Gem::Specification.new do |s|
# normal spec stuff above
s.files = `git ls-files`.split("\n")
# get an array of submodule dirs by executing 'pwd' inside each submodule
gem_dir = File.expand_path(File.dirname(__FILE__)) + "/"
`git submodule --quiet foreach pwd`.split($\).each do |submodule_path|
Dir.chdir(submodule_path) do
submodule_relative_path = submodule_path.sub gem_dir, ""
# issue git ls-files in submodule's directory and
# prepend the submodule path to create absolute file paths
`git ls-files`.split($\).each do |filename|
s.files << "#{submodule_relative_path}/#{filename}"
end
end
end
end
@msievers
Copy link

Hey, thanks for your optimizations. I will update the post with a link to this gist.

@msievers
Copy link

msievers commented Jul 9, 2013

I updated the post.

@emiltin
Copy link

emiltin commented Mar 25, 2021

Using Pathname:

`git submodule --quiet foreach pwd`.split($\).each do |submodule_path|
  Dir.chdir(submodule_path) do
    relative = Pathname.new(submodule_path).relative_path_from(__dir__)
    `git ls-files`.split($\).each do |filename|
      spec.files << relative.join(filename).to_s
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment