What is a Rakefile? - ruby

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

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

Get build configuration details using Jenkins API

I am looking for a way to get the configuration details of a Jenkins job using Jenkins API. Something which is displayed in the command block in the below image.
Has anybody tried getting configuration details using Jenkins API?
You can get the raw XML configuration of a job from the URL: http://jenkins:8080/job/my-job/config.xml
This URL returns the persistent job configuration in XML. The build steps are listed under the builders element, different types of build steps are identified by different elements:
<builders>
<hudson.tasks.Shell>
<command>
# Run my shell command...
</command>
</hudson.tasks.Shell>
</builders>
There's no direct way of doing this that I know of, however you can collect the shell execution with the console output API and a little regex magic.
The API endpoint looks like this:
"http://#{server}:#{port}/job/#{job_name}/{build_numer}/logText/progressiveText?start=0"
For this example, let's say your shell command looks like:
bundle install
bundle exec rspec spec/
The console puts a + before every execution command, so the following script would work:
# using rest-client gem for ease of use
# but you could use net:http and open/uri in the standard library
require 'rest-client'
console_output = RestClient.get 'http://jenkins_server:80/job/my_job/100/logtext/progressiveText?start=0'
console_output.scan(/^\+.+/).each_with_object([]) { |match, array| array << match.gsub('+ ', '') }
#=> ["bundle install", "bundle exec rspec spec/"]

Creating Ruby Main (command line utility) program with multiple files

I am trying to use the main gem for making command line utilities. This was presented in a recent Ruby Rogues podcast.
If I put all the code in one file and require that file, then rspec gives me an error, as the main dsl regards rpsec as a command line invocation of the main utility.
I can break out a method into a new file and have rspec require that file. Suppose you have this program, but want to put the do_something method in a separate file to test with rspec:
require 'main'
def do_something(foo)
puts "foo is #{foo}"
end
Main {
argument('foo'){
required # this is the default
cast :int # value cast to Fixnum
validate{|foo| foo == 42} # raises error in failure case
description 'the foo param' # shown in --help
}
do_something(arguments['foo'].value)
}
What is the convenient way to distribute/deploy a ruby command line program with multiple files? Maybe create a gem?
You are on the right track for testing - basically you want your "logic" in separate files so you can unit test them. You can then use something like Aruba to do an integration test.
With multiple files, your best bet is to distribute it as a RubyGem. There's lots of resources out there, but the gist of it is:
Put your executable in bin
Put your files in lib/YOUR_APP/whatever.rb where "YOUR_APP" is the name of your app. I'd also recommend namespacing your classes with modules named for your app
In your executable, require the files in lib as if lib were in the load path
In your gemspec, make sure to indicate what your bin files are and what your lib files are (if you generate it with bundle gem and are using git, you should be good to go)
This way, your app will have access to the files in lib at runtime, when installed with RubyGems. In development, you will need to either do bundle exec bin/my_app or RUBYLIB=lib bin/my_app. Point is, RubyGems takes care of the load path at runtime, but not at development time.

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

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