Class: Jets::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/generator.rb

Overview

Piggy back off of Rails Generators.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, *args) ⇒ Generator

Returns a new instance of Generator.



63
64
65
66
# File 'lib/jets/generator.rb', line 63

def initialize(generator, *args)
  @generator, @args = generator, args
  @args << '--pretend' if noop?
end

Class Method Details

.capture_stdoutObject



35
36
37
38
39
40
41
42
# File 'lib/jets/generator.rb', line 35

def capture_stdout
  stdout_old = $stdout
  io = StringIO.new
  $stdout = io
  yield
  $stdout = stdout_old
  io.string
end

.help(args = ARGV) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jets/generator.rb', line 12

def help(args=ARGV)
  # `jets generate -h` results in:
  #
  #     args = ["generate", "-h"]
  #
  args = args[1..-1] || []
  help_flags = Thor::HELP_MAPPINGS + ["help"]
  args.pop if help_flags.include?(args.last)
  subcommand = args[0]

  out = capture_stdout do
    if subcommand
      # Using invoke because it ensure the generator is configured properly
      invoke(subcommand) # sub-level: jets generate scaffold -h
    else
      puts Jets::Commands::Help.text(:generate) # to trigger the regular Thor help
      # Note: How to call the original top-level help menu from Rails. Keeping around in case its useful later:
      # Rails::Generators.help # top-level: jets generate -h
    end
  end
  out.gsub('rails','jets').gsub('Rails','Jets')
end

.invoke(generator, *args) ⇒ Object



4
5
6
# File 'lib/jets/generator.rb', line 4

def invoke(generator, *args)
  new(generator, *args).run(:invoke)
end

.require_active_job_generatorObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/jets/generator.rb', line 51

def require_active_job_generator
  require "active_job"
  require "rails/generators/job/job_generator"
  # Override the source_root
  Rails::Generators::JobGenerator.class_eval do
    def self.source_root
      File.expand_path("../generator/templates/active_job/job/templates", __FILE__)
    end
  end
end

.require_generatorsObject



44
45
46
47
48
49
# File 'lib/jets/generator.rb', line 44

def require_generators
  # lazy require so Rails const is only defined when using generators
  require "rails/generators"
  require "rails/configuration"
  require_active_job_generator
end

.revoke(generator, *args) ⇒ Object



8
9
10
# File 'lib/jets/generator.rb', line 8

def revoke(generator, *args)
  new(generator, *args).run(:revoke)
end

Instance Method Details

#configObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jets/generator.rb', line 88

def config
  g = Rails::Configuration::Generators.new
  g.orm             :active_record, migration: true, timestamps: true
  # TODO: support g.orm :dynamodb
  g.test_framework  nil #:test_unit, fixture: false
  # g.test_framework :rspec # TODO: load rspec configuration to use rspec
  g.stylesheets     false
  g.javascripts     false
  g.assets          false
  if Jets.config.mode == 'api'
    g.api_only = true
    g.template_engine nil
  else
    g.template_engine :erb
  end
  g.resource_route  true
  g.templates.unshift(template_paths)
  g
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)


75
76
77
# File 'lib/jets/generator.rb', line 75

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

#run(behavior = :invoke) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/jets/generator.rb', line 79

def run(behavior=:invoke)
  self.class.require_generators

  Jets::Commands::Db::Tasks.load!

  Rails::Generators.configure!(config)
  Rails::Generators.invoke(@generator, @args, behavior: behavior, destination_root: Jets.root)
end

#template_pathsObject



108
109
110
111
# File 'lib/jets/generator.rb', line 108

def template_paths
  templates_path = File.expand_path("../generator/templates", __FILE__)
  [templates_path]
end