execute task in .rb file - ruby

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

Related

How to call rake target twice

I generate two different sets of DLL files from my .sln by modifying the .csproj files to include an extra compilation symbol. I'm using rake to build the solution, and have the following Build task:
#==========================================================
desc "Builds the DPSF.sln in Release mode."
msbuild :Build do |msb|
puts 'Building the DPSF solution...'
msb.properties :configuration => :Release
msb.targets [:Clean, :Rebuild]
msb.solution = DPSF_SOLUTION_FILE_PATH
msb.parameters "/nologo", "/maxcpucount", "/fileLogger", "/noconsolelogger"
msb.verbosity = "quiet" # Use "diagnostic" instead of "quiet" for troubleshooting build problems.
# Delete the build log file if the build was successful (otherwise the script will puke before this point).
File.delete('msbuild.log')
end
I then try to generate both sets of DLL files using:
desc "Builds new regular and AsDrawableGameComponent DLLs."
task :BuildNewDLLs => [:DeleteExistingDLLs, :Build, :UpdateCsprojFilesToBuildAsDrawableGameComponentDLLs, :Build, :RevertCsprojFilesToBuildRegularDLLs]
You can see that I call :Build twice here. The problem is that only the first one runs. If I copy/paste my :Build target and call it :Build2 and change :BuildNewDLLs to call :Build2 the second time, then everything works fine. So how can I make it so that I can call the :Build target multiple times from within the :BuildNewDLLs target?
Thanks in advance.
I know this is an old question, but I just spent 15 minutes figuring this out, so for the sake of documentation, here goes:
You can call reenable from within the same task that you wish to reenable. And since the task block yields the current task as first argument, you can do:
task :thing do |t|
puts "hello"
t.reenable
end
And now this works:
rake thing thing
Rake will, by default, ensure that each rake task is executed once and only once per session. You can re-enable your build task with the following code.
::Rake.application['Build'].reenable
That will allow it to be re-executed in the same session.

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.

calling a ruby file from a RakeFile

I'm creating an application which requires migrations without rails. For that I have created a rake file to execute commands.
My problem is how can I call a ruby class function from a rake file. I want something like this. consider both are in the same directory
class A
def b
puts 'calling method B from class A'
end
end
in the RakeFile
task :create do
A.new.b
end
I want to execute it as
rake create
But currently I'm getting this error
rake aborted!
no such file to load -- a
I'm using ruby 1.9.1, rake (0.8.7)
thanks in advance
cheers
sameera
Have you required the file containing the class? Meaning, have you used any statement like
require "path/to/a.rb" #where a.rb contains the class A
It seems like ruby converter unable to find where to look for class A.

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

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

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.

Resources