Kristian Mandrup
2010-Feb-07 14:44 UTC
New gem ''thor-ext'' with addons and patches for updating the Gemfile
I know the code could be much improved, fx the add_gem has so far no
support for an options hash.
Perhaps this could be integrated into the original
Rails::Generators::Actions in a new fork?
I will also work on a ''txt-mutator'' gem with a DSL for
general-purpose
code (text file) refactorings.
''thor-ext'' updates and extends Rails::Generators::Actions with
the
following:
module ThorExtensions
def append_line_to_file(path, *args, &block)
if block_given?
data = block
else
data = args.shift
end
append_file path, "#{data}\n"
end
# add newline between each gem statement in Gemfile
# Use Rails.root ?
def cleanup_gemfile
gsub_file ''Gemfile'', /(''|")gem/,
"\1\ngem"
end
def has_gem?(text, gem_name)
if
/\n[^#]*gem\s*(''|")\s*#{Regexp.escape(gem_name)}\s*(''|")/
i.match(text)
true
else
false
end
end
def has_plugin?(plugin_name)
File.directory?(File.join(Rails.root, "vendor/plugins/
#{plugin_name}"))
end
def add_gem(gem_name, gem_version = nil)
if !has_gem?(gemfile_txt, gem_name)
gem_version_str = gem_version ? ",
''#{gem_version}''" : ''''
append_line_to_file ''Gemfile'', "gem
''#{gem_name}''#{gem_version_str}"
end
end
def add_gems(gem_names)
gem_names.each{|gem_name| add_gem(gem_name) }
end
# Use Rails.root here?
def gemfile_txt
@gemfile_txt ||= File.open(''Gemfile'').read
end
end
# patch to add newline after gem ''..'' so we avoid a Gemfile
like this:
# gem ''abc'' gem ''abd''... but instead have
newlines after each.
# Also only adds gem statement if gem statement not already present
def gem(*args)
...
in_root do
if !has_gem?(gemfile_txt, gem_name)
append_line_file "Gemfile", "gem #{parts.join(",
")}", :verbose => false
end
end
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.