Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy a file from a gem to my rails application using rake

Tags:

rake

rakefile

I have a gem with a default configuration YAML file, some_config.yml. I want to create a rake task to copy that file into the config/ directory of my rails application. How can I achieve this?

like image 599
neojin Avatar asked Aug 29 '12 16:08

neojin


People also ask

What does rake file do?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.

What is rake file in Rails?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

How do I create a rake file?

Simply create a new file and name it as task. rake. Normally we put the rake task in directory lib/tasks. But you can put it anywhere you like.

How do you call a rake in Rails?

To get the full backtrace for running rake task you can pass the option --trace to command line, for example rake db:create --trace . You can also use rake -T to get the list of tasks.


1 Answers

If we assume the target gem is in your Gemfile and you want to include the Rake task in your Rails Rakefile, then you could try something like:

namespace :config do
  # desc "Copy the config"
  task :copy do
    source = File.join(Gem.loaded_specs["myGem"].full_gem_path, "config", "config.yml")
    target = File.join(Rails.root, "config", "myGemConfig.yml")
    FileUtils.cp_r source, target
  end
end
like image 194
Martin Harrigan Avatar answered Nov 08 '22 20:11

Martin Harrigan