Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thor - inject into file at end

I am working on a rails engine and I am trying to write a generator that will put this line

do_stuff (foo)

as the last statement in config/routes.rb, without breaking the file syntax.

Specifically, if my config/routes.rb looks like this currently

Rails.application.routes.draw do
    blah
    more blah
end

After running the generator I would like the config/routes.rb to look like this

Rails.application.routes.draw do
    blah
    more blah
    do_stuff (foo) # injected line
end

I looked at what ActiveAdmin does, but am unable to create a blanket last line rule. Any help is greatly appreciated.

like image 591
Srikanth Venugopalan Avatar asked Nov 07 '13 12:11

Srikanth Venugopalan


1 Answers

i did not test that out, but i think from what you linked to in the ActiveAdmin generator it might work like this:

inject_into_file "config/routes.rb", "  do_stuff(foo)\n", :before => /^end/

this should insert your code right before an end token that starts at the beginning of a line. this only works for properly formatted routes files though....

like image 86
phoet Avatar answered Nov 09 '22 15:11

phoet