Rails 4, custom gem with generator 'Could not find generator'
Asked Answered
E

3

11

I'm building a gem simply to go through the processes, and I'm trying to add a generator to it. When I run $ rails g, my generator shows up:

Mygem:
  mygem:install

but rails doesn't recognize it

rails g mygem:install
Could not find generator mygem:install.

I have my gem pointed to the latest version in my gemfile

#mygem/lib/rails/generators/mygem_generator.rb
require 'rails/generators'
require 'rails/generators/base'

module Mygem
  class InstallGenerator < Rails::Generators::Base
    def test
        puts 'hi'
    end
  end
end

.

-mygem
  - lib
    - generators
      - mygem
        mygem_generator.rb
    - mygem
      version.rb
    mygem.rb
  - pkg
    mygem-0.0.1.gem
.gitignore
Gemfile
LICENSE.txt
README.md
Rakefile
mygem.gemspec

.

#mygem.gemspec
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'

Gem::Specification.new do |spec|
  spec.name          = "mygem"
  spec.version       = Mygem::VERSION
  spec.authors       = ["me"]
  spec.email         = ["[email protected]"]
  spec.summary       = %q{lalaala}
  spec.description   = %q{lalalalal}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = `git ls-files -z`.split("\x0")
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.5"
  spec.add_development_dependency "rake"
end
Edyth answered 1/6, 2014 at 8:4 Comment(1)
It looks like the problem is the /rails/ in the path to the generator file ... try it in mygem/lib/generators/mygem/install_generator.rb ... also renaming mygem_generator.rb as install_generator.rbHale
C
13

We've just made a gem, and have a generator which allows you to call rails generate exception_handler:install, like this:

#lib/generators/my_gem/install_generator.rb
module MyGem
    class InstallGenerator < Rails::Generators::Base
         def test
             puts "hi"
         end
     end
end

This should help you - it works for us

Cuenca answered 1/6, 2014 at 9:9 Comment(13)
No luck :( I'm 100% out of ideas.Edyth
Errors? What's happening when you hit "Enter"?Cuenca
Same problem, 'Could not find generator ...'Edyth
Okay - can you detail your gem's file structure? Our gem works (although we're still developing the installers)Cuenca
Added in edit, I also added the updated generator fileEdyth
first structure was incorrect, also added my gemspec fileEdyth
You should move it to the following directory: lib/generators/my_gem/install_generator.rbCuenca
You should also call it after the class - I.E install_generator.rbCuenca
No problem! Do you want me to explain why it worked?Cuenca
I was having a same problem and your answer helped me a great deal. Thank you. Could you explain why it worked?Bryna
@RichardPeck thanks for your help. Any objection to providing more detail to your comment; "you should call it after the class?"Attica
@Attica the name of the file needs to match the name of the class in the file; which also needs to be the name of the generator itself. Does that help?Hale
This worked for me also. Pay close attention to the file path and the naming of the files and classes, according to the names of your gem and generator. Also capitalization can be an issue, e.g., my_gem translates to MyGem, mygem to Mygem, etc.Hale
R
1

I faced this issue after publishing my first public gem, it's because the bundler, or let's say the Rails app don't have access to the actual repository with the generator (I believe so).

I kept including gem 'spree_custom_checkout'

no luck to see the gem's generator when typing rails g.

Then I did this:

gem 'spree_custom_checkout', github: '0bserver07/Spree-Custom-Checkout'

and bam the generator shows up!

steps:

  1. I did `gem build spree_custom_checkout.gemspec.
  2. add it to the github repo, and push to the repostiory.
  3. link to the repository.
Ruel answered 29/7, 2016 at 21:18 Comment(0)
U
0

I was having similar trouble because I had used capital letters in my gem name. There's some great documentation about how to name gems here, but the most relevant bit:

Don't use uppercase letters - OS X and Windows have case-insensitive filesystems by default. Users may mistakenly require files from a gem using uppercase letters which will be non-portable if they move it to a non-windows or OS X system. While this will mostly be a newbie mistake we don’t need to be confusing them more than necessary.

I recreated the gem with lowercase and the problem went away.

Unsuccess answered 4/3, 2023 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.