Class: Jets::Generators

Inherits:
Object
  • Object
show all
Includes:
Command::Behavior
Defined in:
lib/jets/generators.rb,
lib/jets/generators/job/job_generator.rb

Overview

:nodoc:

Defined Under Namespace

Classes: JobGenerator

Constant Summary collapse

AppGenerator =

Keeps zeitwerk happy

Jets::Generators::Overrides::App::AppGenerator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, args = ARGV, config = {}) ⇒ Generators

Returns a new instance of Generators.



10
11
12
13
# File 'lib/jets/generators.rb', line 10

def initialize(namespace, args = ARGV, config = {})
  @namespace, @args, @config = namespace, args, config
  @args << '--pretend' if noop?
end

Class Method Details

.configure!(config) ⇒ Object



98
99
100
101
# File 'lib/jets/generators.rb', line 98

def configure!(config)
  require "rails/generators"
  Rails::Generators.configure!(config)
end

.invoke(generator, args = ARGV, config = {}) ⇒ Object



103
104
105
# File 'lib/jets/generators.rb', line 103

def invoke(generator, args = ARGV, config = {})
  new(generator, args, config).run(:invoke)
end

.revoke(generator, args = ARGV, config = {}) ⇒ Object



107
108
109
# File 'lib/jets/generators.rb', line 107

def revoke(generator, args = ARGV, config = {})
  new(generator, args, config).run(:revoke)
end

Instance Method Details

#configObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jets/generators.rb', line 72

def config
  config = Jets.application.config.generators
  config.orm :active_record, migration: true, timestamps: true
  config.templates = [jets_templates_path] # add jets_templates_path for customizations
  if Jets.application.config.mode == 'api'
    config.api_only = true
    config.template_engine nil
  else
    config.template_engine :erb
  end
  config
end

#jets_templates_pathObject

Rails and Thor allows overriding the provided templates with source root. The structure is slightly different with source_paths.

source_root  railties-7.0.8/lib/rails/generators/erb/scaffold/templates
source_paths lib/jets/generators/overrides/templates/erb/scaffold

See how templates is prefix instead of a suffix.

erb/scaffold/templates
templates/erb/scaffold

This is nice because everything is together in the overrides/template folder.



93
94
95
# File 'lib/jets/generators.rb', line 93

def jets_templates_path
  File.expand_path("generators/overrides/templates", __dir__)
end

#noop?Boolean

Used to delegate noop option to Rails generator pretend option. Both work:

jets generate scaffold user title:string --noop
jets generate scaffold user title:string --pretend

Grabbing directly from the ARGV because think its cleaner than passing options from Thor all the way down.

Returns:

  • (Boolean)


22
23
24
# File 'lib/jets/generators.rb', line 22

def noop?
  ARGV.include?('--noop')
end

#run(behavior = :invoke) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jets/generators.rb', line 26

def run(behavior=:invoke)
  if @namespace == "job"
    run_job_generator
  else
    # Required by:
    #   jets generate migration create_articles user:references
    #   jets generate model article user:references
    # Makes use of Rails.application
    if %w[model migration].include?(@namespace)
      require "jets/overrides/dummy/rails"
    end
    run_rails_generator
  end
end

#run_job_generatorObject

For job generators, use a more custom generator instead of Rails generators. We this for more control over the jobs and can add more features. IE: Different event based jobs.



44
45
46
47
# File 'lib/jets/generators.rb', line 44

def run_job_generator
  require "jets/generators/job/job_generator"
  JobGenerator.start(@args, @config)
end

#run_rails_generatorObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/generators.rb', line 49

def run_rails_generator
  # We lazy require so Rails const is only defined when using generators
  # Using require at the top and configuring do_not_eager_load is not enough.
  # This is because we call require "jets/generators" throughout the codebase.
  require "rails/generators"
  require "rails/configuration"

  # => Jets::Generators.configure! => Rails::Generators.configure!(config)
  self.class.configure!(config)

  # Ultimately, Rails::Generator.invoke is called.
  # Here are some examples to show how args are passed to Rails::Generator.invoke.
  #
  # jets generate kingsman:controllers users -c=sessions
  #     @namespace kingsman:controllers
  #     @args ["users", "-c=sessions"]
  #
  # jets generate scaffold post title:string body:text published:boolean --force
  #     @args ["post", "title:string", "body:text", "published:boolean", "--force"]

  Rails::Generators.invoke(@namespace, @args, @config)
end