How to call rake target twice - ruby

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.

Related

Order of Rake Test Task

I have a rake file with three tasks, which I need to execute in order.
require 'rake/testtask'
file 'some_binary_file.elf' do
puts 'fetching file from server ...'
# this task connects to a server and downloads some binaries
# it takes a few seconds to run
end
task flash_application: 'some_binary_file.elf' do
puts 'flashing the file to the hardware ...'
# this task copies a binary file to the flash memory
# of some external hardware, also takes a few seconds
end
Rake::TestTask(:hardware) do |t|
puts 'running tests ...'
f.test_files = FileList['test/**/*_test.rb']
end
rake default: [:flash_application, :hardware]
when I run $ rake in a terminal, it produces the following output.
running tests ... < ---- (not actually running)
fetching file from server ...
flashing the file to the hardware ...
I would expect rake to run the tasks in the order I specified, but It seems to always execute the test task first. It is remarkable that the tests do not run - but the output of the task creation is produced anyway.
When you want to run the tasks in a specific order, you must depend them on each other. In your case, :flash_application should then depend on :hardware
Found the Bug - This problem was not ruby / rake specific. The flash_application task changes the working directory. Because of that, there is no Rakefile with a task 'hardware' in the current working directory. But researching for this bug yielded some interesting insights.
Ruby arrays are ordered, if one want to execute task in an order it is sufficient to define them in execution order in an array i.e.
task some_task: [:first, :second, :third]
Rake::TestTask.new defines a plain old rake task when called. That means, when rake is called, ruby creates an instance of a Rake::TestTask. All code passed into the constructor is executed / yielded during this phase. This yields the described behavior from the original question.

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

Testing multiple hosts with the same test using serverspec

The Advanced Tips section of the Serverspec site shows an example of testing multiple hosts with the same test set. I've built an example of my own (https://gist.github.com/neilhwatson/81249ad393800a76a8ad), but there are problems.
The first problem is that the tests stop at the first failure rather than proceeding through the lot and keeping a tally. The second is that the failure output does not indicate on which host the failure occurred. What can I do to fix these problems and produce a final report for all hosts?
For the first issue, ServerSpec by default will run all your tests. However, since you have a loop that executes a Rake task for each environment, the first environment to have a failure causes the task to fails and so an exception is raised and the rest of your tasks don't run.
I've forked your gist and updated the Rake task to surround it with a begin/rescue.
...
begin
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/base,cfengine3/*_spec.rb"
end
rescue
end
...
For the second problem, it doesn't look like ServerSpec will output which environment the tests are running in. But since the updated Gist shows that the host gets set in the spec_helper.rb we can use that to add an RSpec configuration that sets up an after(:each) and only output the host on errors. The relevant code changes are in a fork of the gist, but basically you'll just need the below snippet in your spec_helper.rb:
RSpec.configure do |c|
c.after(:each) do |example|
if example.exception
puts "Failed on #{host_run_on}"
end
end
end

How can I run features within a rake task in parallel?

Normally I run my tests with parallel_cucumber which runs different features in parallel using parallel_test gem. I want to setup a rake task that will run using different profiles and run the features within each task in parallel.
I have setup my Rakefile this way:
namespace :features do
Cucumber::Rake::Task.new(:basket) do |t|
t.profile = "basket"
end
Cucumber::Rake::Task.new(:fruits) do |t|
t.profile = "fruits"
end
Cucumber::Rake::Task.new(:veggies) do |t|
t.profile = "veggies"
end
task :all => [:basket, :fruits, :veggies]
end
When I run "rake features:all" it will run each task in sequence(as expected/desired) but will run the features within each task one at a time(not desired). I would like to keep each task running in sequence but would like the features within each task to run in parallel. Is this possible? If not is there a way this can be done?
As always your help is much appreciated.
Rake offers multitask. You could either
Changing task to multitask in the rakefile:
multitask :all => [:basket, :fruits, :veggies]
use -m option in the command line:
rake -m features:all
Have a look at the parallel_tests gem. It allows you to run your features in parallel. While the typical use case is to run all of your features, you are able to run specific rake tasks in parallel. See the documentation for more details.

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.

Resources