module Rsg::Generators::Actions

Public Instance Methods

api_mode?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rsg/generators/actions.rb, line 56
def rsg_apply(template)
  apply Rsg.lookup_app_template(template)
end
rsg_apply_default!(rsg_options = {}) click to toggle source
# 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) click to toggle source
# 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_install() click to toggle source
# 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_options() click to toggle source
# File lib/rsg/generators/actions.rb, line 8
def rsg_options
  @rsg_options ||= {}
end
rsg_skip_or_confirm(option_key, prompt, &block) click to toggle source
# 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_state() click to toggle source
# File lib/rsg/generators/actions.rb, line 12
def rsg_state
  @rsg_state ||= {generators: []}
end

Private Instance Methods

append_gem_line(code, gem_line, after) click to toggle source
# File lib/rsg/generators/actions.rb, line 106
def append_gem_line(code, gem_line, after)
  code_lines = code.split("\n")
  index = code_lines.length
  indentation = ""

  # Goes to the line above the `end` line by default
  if code_lines[0] =~ /^([^#]*)group/
    index -= 1
    indentation = "#{$1}  "
  end

  Array(after).each do |candidate|
    idx = code_lines.index { |l| l =~ /^([^#]*)gem ['"]#{candidate}['"].*$/ }
    next unless idx

    indentation = $1
    index = idx + 1
    break
  end

  code_lines.insert(index, "#{indentation}#{gem_line}").join("\n") << "\n"
end
build_gem_line(gem_name, params = {}) click to toggle source
# File lib/rsg/generators/actions.rb, line 129
def build_gem_line(gem_name, params = {})
  line = "gem '#{gem_name}'"
  if params.key?(:version)
    version_args = Array(params[:version]).map { |v| "'#{v}'" }.join(", ")
    line << ", #{version_args}"
  end
  line << ", require: '#{params[:require]}'" if params.key?(:require)
  line << ", group: #{params[:group]}" if params.key?(:group)
  line << ", path: '#{params[:path]}'" if params[:path].present?
  line
end
run(*) click to toggle source
Calls superclass method
# File lib/rsg/generators/actions.rb, line 102
def run(*)
  Bundler.with_unbundled_env { super }
end