I am trying to create a rails template that will add code to files at particular line numbers. For example I need to add a route to the config/routes.rb
I have tried sed, gsed(only cause I am on a mac and they say that sed has problems with insert and append), anyway, I was not able to achieve the result I want.
Any help on this will be greatly appreciated.
I have tried several permutations of this command, but none work, here is an example
run "gsed '3 a/This is it' config/routes.rb"
perhaps even another suggestion
EDIT::::::
ok I took a break and when I came back, after reading up on sed, I realized that I needed to write the stream back to the file, but I was doing this before with,
run "gsed '2 a\
Add this line after 2nd line
' config/routes.rb > config/routes.rb"
but the routes file would be blank, so I tried using a different filename(new.routes.rb),
run "gsed '2 a\
Add this line after 2nd line
' config/routes.rb > config/new.routes.rb"
and this worked, so I know what to do now.
Since line numbers change as your file evolves, it's better to insert your new code before or after existing string expressions. Inside your template generator, you can do something like this:
inject_into_file 'config/application.rb', :before => " end" do
"\n config.logger = Logger.new(config.paths.log.first, 50, 1048576)\n\n"
end
Note, you can use :after if :before doesn't meet your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With