Module: Rsg::Generators::Actions

Included in:
Rsg::Generators::App::AppGenerator, Base
Defined in:
lib/rsg/generators/actions.rb

Instance Method Summary collapse

Instance Method Details

#api_mode?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/rsg/generators/actions.rb', line 60

def api_mode?
  return @api_mode if defined?(@api_mode)
  @api_mode = !!(File.read("config/application.rb") =~ /^[^#]*config\.api_only = true[^\n]*$/)
end

#append_gem(gem_name, install: true, within_group: [], after: ["bootsnap", "rails"], **gem_line_params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rsg/generators/actions.rb', line 83

def append_gem(gem_name, install: true, within_group: [], after: ["bootsnap", "rails"], **gem_line_params)
  within_group = Array(within_group)
  if within_group.any?
    group_part = within_group.map { |g| "[:'\"]#{g}['\"]?" }.join(", ")
    regex = /^ *group #{group_part} do\n(\s*(gem|#)[^\n]+\n)+ *end\n/
  else
    regex = /.+/m
  end

  gsub_file "Gemfile", regex do |match|
    gem_line = build_gem_line(gem_name, gem_line_params)
    append_gem_line(match, gem_line, after)
  end

  run("bundle install") if install
end

#confirm?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/rsg/generators/actions.rb', line 65

def confirm?(prompt)
  opts = { limited_to: %w(y n) }
  ask(prompt, opts).downcase == 'y'
end

#enable_railtie(name) ⇒ Object



79
80
81
# File 'lib/rsg/generators/actions.rb', line 79

def enable_railtie(name)
  uncomment_lines "config/application.rb", /require ['"]#{name}\/railtie['"]/
end

#git_add_commit(commit_msg) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/rsg/generators/actions.rb', line 70

def git_add_commit(commit_msg)
  truncated_msg = commit_msg.strip.gsub("\n", ' ')[0..59].strip
  truncated_msg.gsub!(/.{3}$/, '...') if commit_msg.length > 60
  say_status :run, "git add . && git commit -m \"#{truncated_msg}\""

  run "git add .", verbose: false, capture: true
  run "git commit -m #{Shellwords.escape(commit_msg)}", verbose: false, capture: true
end

#rsg_apply(template) ⇒ Object



56
57
58
# File 'lib/rsg/generators/actions.rb', line 56

def rsg_apply(template)
  apply Rsg.lookup_app_template(template)
end

#rsg_apply_default!(rsg_options = {}) ⇒ Object



51
52
53
54
# File 'lib/rsg/generators/actions.rb', line 51

def rsg_apply_default!(rsg_options = {})
  @rsg_options = rsg_options
  rsg_apply "rsg-default"
end

#rsg_generate(name, generator_opts = {}, quiet: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rsg/generators/actions.rb', line 33

def rsg_generate(name, generator_opts = {}, quiet: false)
  cmd = "generate #{name}"
  cmd << " -q" if quiet

  if generator_opts.is_a?(Hash)
    generator_opts.each do |opt, value|
      cmd <<  " --#{opt.to_s.dasherize}=#{Shellwords.escape(value)}"
    end
  end

  rsg_state[:generators] << name

  # FIXME: abort_on_failure is not working as expected, because `rails generate <invalid-generator>`
  # does not return an exit code != 0
  run("rails #{cmd}", abort_on_failure: true)
  git_add_commit "RSG Generator executed: #{name}"
end

#rsg_installObject



16
17
18
19
20
21
# File 'lib/rsg/generators/actions.rb', line 16

def rsg_install
  append_gem "rsg",
             within_group: %i(development test),
             install: false,
             path: options["path"]
end

#rsg_optionsObject



8
9
10
# File 'lib/rsg/generators/actions.rb', line 8

def rsg_options
  @rsg_options ||= {}
end

#rsg_skip_or_confirm(option_key, prompt, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rsg/generators/actions.rb', line 23

def rsg_skip_or_confirm(option_key, prompt, &block)
  if rsg_options.key?(option_key)
    return if [false, :skip].include?(rsg_options[option_key])
  else
    return unless confirm?(prompt)
  end

  self.instance_eval(&block)
end

#rsg_stateObject



12
13
14
# File 'lib/rsg/generators/actions.rb', line 12

def rsg_state
  @rsg_state ||= {generators: []}
end