ruby - Rails Gem: Running All Generators for given Namespace -
i'm developing gem core multiple sub modules, each it's own gem. developer, you'll able install core , other of gems. how can create rake task or generator run generators of installed gems generators under main gem namespace.
example, if gem called admin:
module admin module generators class installgenerator < rails::generators::base end end end and have generator 1 of sub-gems:
module admin module generators class postsgenerator < rails::generators::base end end end and one:
module admin module generators class tagslgenerator < rails::generators::base end end end and there might 10 more gems can installed. rather rail g admin:... installing each one, create rake task or generator runs of tasks.
thanks in advance!
keep "allgenerator" class under admin module. generator have following :
- for each class under namespace generator class,
- get namespace classname.
- call
invokemethod namespace.
something :
module admin module generators class allgenerator < rails::generators::base def generator rails::generators.lookup! admin::generators.constants.each |const| generator_class = admin::generators.const_get(const) next if self.class == generator_class if generator_class < rails::generators::base namespace = generator_klass_to_namespace(generator_class) invoke(namespace) end end end private def generator_klass_to_namespace(klass) namespace = thor::util.namespace_from_thor_class(klass) return namespace.sub(/_generator$/, '').sub(/:generators:/, ':') end end end end here's link gist complete tested code
this way, running rails g admin:all run every other generator directly under admin::generators .
Comments
Post a Comment