lib/rsg/generators/actions.rb in rsg-0.0.1 vs lib/rsg/generators/actions.rb in rsg-0.1.0
- old
+ new
@@ -1,27 +1,57 @@
+require "bundler"
require "json"
require "shellwords"
module Rsg
module Generators
module Actions
+ def rsg_options
+ @rsg_options ||= {}
+ end
+
+ def rsg_state
+ @rsg_state ||= {generators: []}
+ end
+
def rsg_install
append_gem "rsg",
within_group: %i(development test),
install: false,
- path: options[:path]
+ path: options["path"]
end
- def rsg_generate(name, quiet: false)
+ 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
+
+ def rsg_generate(name, generator_opts = {}, quiet: false)
cmd = "generate #{name}"
cmd << " -q" if quiet
- rails_command cmd
+ 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
- def rsg_apply_default!
+ def rsg_apply_default!(rsg_options = {})
+ @rsg_options = rsg_options
rsg_apply "rsg-default"
end
def rsg_apply(template)
apply Rsg.lookup_app_template(template)
@@ -62,13 +92,17 @@
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", capture: true) if install
+ run("bundle install") if install
end
private
+
+ def run(*)
+ Bundler.with_unbundled_env { super }
+ end
def append_gem_line(code, gem_line, after)
code_lines = code.split("\n")
index = code_lines.length
indentation = ""