Question

I've got a custom gem that has been working just fine with regards to bundling, building, distributing, & implementing. The gem is the core of a framework from which other gems are derived. Since most derived gems will have the same basic structure, I want to include a Ruby script in the bin path of the gem that can be used to basically copy files from a template folder into a new folder where the user will develop their own gem.

The problem I'm having is that the template folder has a gemspec file named $name$.gemspec with similarly named classes/modules in the file (e.g.: module $Name$), where the $name$ gets replaced with a name provided by the user.

Unfortunately, when I run bundle install from my gem's top-most path, I get an error:

There was a SyntaxError while evaluating $name$.gemspec:
C:/my_gem/template/$name$.gemspec:8: syntax error, unexpected tGVAR
  gem.version = MyGem::$Name$::VERSION

It looks like Bundler is using the wrong Gemfile, even if I explicitly pass the Gemfile or path via one of the following:

bundle install --gemfile=Gemfile
bundle install --path=C:\my_gem

I also tried updating the gemspec line of my Gemfile to no avail:

gemspec name: 'my_gem'

Lastly, I've ensured that the template folder isn't even included in my_gem.gemspec, but that doesn't seem to matter:

gem.files = Dir.glob("lib/**/*") + %w(LICENSE.txt README.md)

Does anyone know why Bundler is trying to read the ./template/$name$.gemspec instead of ./my_gem.gemspec?

Was it helpful?

Solution

Inspecting the Bundler source, I may have spotted the culprit in lib/bundler/source/path.rb. There's GLOB used to find gemspecs in load_spec_files. The default glob is "{,*,*/*}.gemspec". This will find *.gemspec in the root directory of your gem or any directory one descendant from root (which will include your template dir).

If this is indeed the culprit, you could work around this by placing your template directory deeper in your gem's dir hierarchy or changing the name of the template file so it doesn't end in .gemspec. The Bundler::Source::Pathobject looks like it can take a different glob at initilization but I haven't dug deep enough to see if there's a viable way to specify this alternative glob in bundle execution via config or cmdline options.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top