How can I modify/extend a rake file from another rake file? - ruby

I'm trying to find a way to modify/extend a RakeFile from another RakeFile without actually changing it.
When I run my rake task I retrieve a solution from SVN which contains a rakefile. I want to:
Change a variable in this rakefile.
Add a new task to this rakefile
which makes use of existing tasks.
Execute the new task.
I want to do this preferably without actually modifying the original RakeFile on disc.

Here's a way to run arbitrary code prior to executing the task.
your_task = Rake::Task['task:name']
your_task.enhance { this_runs_before_the_task_executes }
You can execute rake tasks similarly.
your_task.invoke
Full docs here.

This is the code which I ended up with to solve the particular problem I was having.
Dir.chdir File.dirname(__FILE__) + '/their_app'
load 'RakeFile'
# Modify stuff from original RakeFile
COMPILE_TARGET = "release"
# Add my task
task :my_task =>[:my_pre_task, :their_task]
I don't know if this is the right way to do it and would appreciate comments/edits if anyone knows a better way.
Thanks to leethal for submitting a answer which helped me on the way and was very useful for another problem I was having.

Related

execute task in .rb file

I'm working on a project that's been in development for quite a while now. I'm trying to figure out how everything works and my next step was how to update/fill the 'library' as it's called in the project.
I found several .rake files hidden away somewhere, but they are used in another .rb file. In said .rb file the entire logic behind the several tasks is set up, so everything happens in the right order.
Now, here's my problem: I need to use the tasks in the .rb file in order to generate the library. They're nested in namespaces and I can't figure out how to reach the tasks.
Here's a shortened version of the structure in the library.rb file:
namespace :library do
task :something do
...
end
...
namespace :local do
...
namespace :generate do
task :default do
...
end
end
end
end
I want to reach the task :default.
Thanks in advance for any help or advice.
EDIT:
The command rake library:local:generate:default gives an error:
rake aborted!
Don't know how to build task 'library:local:generate:default'
EDIT: I can't change the file's names, extentions or locations. Library.rb is currently located in config/deploy/
I'm assuming you would like to run the rake task from the command line, in which case you would enter rake library:local:generate:default.
You also need to require your library.rb file to make the task available. You can do that with the -f flag on the rake command. So, for example: rake -f path/to/library.rb library:local:generate:default.
A fast solution could be rename the file with .rake extension and put the file under lib/task so you can call it with rake namespace:task
namespace :library do
task :something do
...
end
...
namespace :local do
...
namespace :generate do
task default: :environment do
...
end
end
end
end
You forgot to add environment to it. Try the same rake task command again:
rake library:local:generate:default
This might help you

How to change the default 'test' folder in Autotest with Ruby

My test folder is inside lib/foo/test instead of /test. How can I tell Autotest to look at lib/foo/test for the tests to run? I tried to add some hooks with mappings in the .autotest file but got no success. Thanks in advance.
I'm sorry for not providing a direct answer, but I've stopped using Autotest myself and switched to Guard. Guard is a plain Ruby process that sits outside test::unit or rspec and as such it's much more configurable.
https://github.com/guard/guard
What you want is easy to accomplish in Guard, just write a Guardfile in the project root and mention the directory to watch:
watch("lib/foo/test") { |m| "test/lib/foo/test/#{m[1]}_test.rb" }
obviously tailoring it further to your need.

Require a file for all db:migrate?

I'm not sure the "right" way to do this, so I wanted to ask the community. Probably a simple question.
I have a file "dbutils.rb" that I want to automatically include to have available whenever I run a "rake db:migrate", without putting it in application.rb and without putting it in every single db migration.
Where would I put my require to make this happen?
Rails defines $rails_rake_task = true in the :environment task.
The :environment task again is always loaded when you run :migrate (it is loaded for other Rake tasks also). You could use this to add require "dbutils" to your environment.rb when $rails_rake_task is true. And skip loading otherwise.
The other option is a custom Rake task like fl00r suggested.

RubyMine debugger with Rake?

Using RubyMine 3.0, I set up a Rake configuration to run a Unit Test. Then I set some breakpoints, then ran the Rake task. No breakpoints were hit, the test just executed like normal and then exited.
Does the RubyMine debugger not work through Rake?
Try this:
Go to Run -> Edit Configurations
Expand the Rake node and add new rake configuration for your rake task (if not already done)
Go to Run -> Debug...
Select your configured rake task.
The Edit/Debug Configurations tab can be a little confusing when setting up rake tasks. I will assume you followed this approach:
Run > Edit Configurations
Select Rake from the List and select the + button (Add New Configuration)
You are greeted with a Configuration tab:
Name
The name attribute just assigns a unique name for this task. You can call it whatever you want.
Task Name
This one is important for rake tasks. This specifies the name of the rake task to be executed. So let's say you wanted to run "rake db:migrate" in debug mode, then for the task name here, you would put "db:migrate" without the quotes.
Turn on invoke/execute tracing, enable full backtrace (--trace)
This option is useful to turn on the standard rake --trace option.
Ruby Arguments
The other useful option is to specify the arguments to be passed to the Ruby interpreter.
Those are the main options. Now you can use Run > Debug and it will stop at breakpoints in the rake task itself.
The above answer is correct. I just want to elaborate on it a little bit, when using a mountable engine. In that case, I had to do the following:
Run > Edit Configuration > Rake
Enter task name e.g. scan_spreadsheet
Change the working directory to your main application or dummy application, not the engine root directory.
If you are using RVM with multiple gemsets, select the second option for Ruby SDK and select the correct gemset

What is a Rakefile?

I have started learning Ruby and just tried out my first hello world program in NetBeans IDE. I have one doubt, I can see that the new project wizard created set of package structure. It had one "Rakefile" in it. What does that mean and what is the use of it?
It is an alternative to Makefile with Ruby syntax.
I've been using a rake file to manually kick off a call in the code to import various config files.
My rake file "import_contracts.rake" has the following code:
require 'yaml'
task :import_choice_contracts => :environment do
desc 'Import Choice Contracts for Contract Service'
path = "/Users/ernst.r/Desktop/import_contract.yml"
PhiDao::Contract::Service.import_contract_from_file(path)
end
This rake task calls the "import_contract_from_file()" method and passes it the path to a file to import. Once the server is running I use the command "rake import_choice_contracts". Its great for testing my code while I still don't have a GUI frontend to call the completed code in the backend.
Fissh

Resources