How to access namespace from within a rake task? - ruby

My rake task (rake v 0.9.2.2) includes a bit to print out the name of the task. I'd like it to also print its namespace. Kind of like the following:
namespace :yelp do
desc "..."
task :scrape => :environment do
puts "rake #{task.namespace}:#{task.name}"
end
end
The problem is that namespace is a private method.
rake aborted!
private method `namespace' called for <Rake::Task => []>:Rake::Task
Anyone have a solution?

task.name includes the namespace. Use this tip to get task.name to print under rake 0.9.2.2.

Related

Rake in Ruby: undefined method `namespace' for main:Object (NoMethodError)

I'm currently trying to run a Rake task in a Ruby project (No Rails). What I am trying to accomplish is to run a method from a file within my Ruby project. However I get the following error:
undefined method `namespace' for main:Object (NoMethodError)
I created a folder task that holds a test.rb file. Before I had it as test.rake but I think this was incorrect. I also created a Rakefile pointing to task/test.rb
For redeability, I'm using namespace: although I'll be honest I'm not sure if I even need it.
#Rakefile
task :default => [:test]
task :test do
ruby 'task/test.rb'
end
Here is the task.test.rb
require './src/lambda_function.rb'
class KMS
def initialize
end
def decrypt(key)
return "some password"
end
end
class SNS
def initialize
end
end
TEST_FORM_ID=123
namespace :test do
namespace :lambda do
desc 'Run the Lambda process function'
task :process do
LambdaFunctions::LambdaHandler.process(box_api: BoxApi.new,
form: TEST_FORM_ID,
sns: SNS.new,
kms: KMS.new)
end
end
end
What I'm I doing wrong?

How do I programmatically extract a rake task's description?

I'm attempting to capture the equivalent of rake -D programmatically. I can load the Rakefile I'm targeting and see a list of tasks, but I can not figure out how to get the descriptions.
This will let me see the tasks that I am interested in:
Dir.chdir #myTarget
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
tasks = Rake.application.tasks
puts tasks.inspect
The above outputs something similar to:
[<Rake::Task default => [test]>, <Rake::Task foodcritic => []>, <Rake::Task integration => [kitchen:all]>]
My question is how to access the desc comments that are visible if I run rake -D. Here's what I'm after:
rake foodcritic
Run Foodcritic lint checks
rake integration
Alias for kitchen:all
rake kitchen:all
Run all test instances
Here's the final solution. The key was that I was missing metadata from the taskmanager:
Dir.chdir #myTarget
rake = Rake::Application.new
Rake::TaskManager.record_task_metadata = true
Rake.application = rake
rake.init
rake.load_rakefile
Rake.application.tasks.each do |t, n|
puts t
puts t.full_comment
puts "\n"
end
Use the methods comment or full_comment for that. More docs on the Rake::Task class here.

Can't pass args through to rake task - has the syntax changed?

I'm using a rails 3.1.3 project which has rake (0.9.2.2). I want to do this in a rake task: call it like
rake tale:import_kml /path/to/file.txt
and then inside the rake task, get access to "/path/to/file.txt" as args.filename
I thought i would be able to do this like so (the puts is there for bit of debugging):
namespace :tale do
desc "Expects to get a file or folder name as the first argument, and passes that to Tale.import_kml"
task(:import_kml, [:filename] => :environment) do |t, args|
puts "args = #{args.inspect}"
if File.exists?(args.filename)
Tale.import_kml(filename)
end
end
end
But, i get this:
** Invoke tale:import_kml (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute tale:import_kml
args = {}
rake aborted!
can't convert nil into String
so, filename isn't making it into args. I can't work out what i'm doing wrong here...
Try such snippet:
namespace :tale do
desc "Expects to get a file or folder ..."
task(:import_kml, [:filename]) do |t, args|
args.with_default(:filename => :environment)
puts "args = #{args.inspect}"
end
end
rake tale:import_kml[foo] # => args = {:filename => "foo"}
WarHog helped me work it out: i had to change the 'task' line to
task :import_kml, [:filename] => [:environment] do |t, args|
and call it like
rake tale:import_kml[/path/to/file.txt]

How do I log queries in a rake task using datamapper and padrino?

I have a padrino install that is using datamapper and logging queries to a file. This is working fine when browsing my application. But queries are not logged if executed inside a rake file. Why?
This is how the task is defined:
# lib/tasks/example.rake
task :example => :environment do
players = Player.all #Player is a datamapper object
puts players.first.to_s
end
I also added this line to /config/boot.rb
Padrino::Logger::Config[:development] = { :log_level => :devel, :stream => :to_file }
And this line is called in /config/database.rb
DataMapper.logger = logger
And this is how I'm executing the script
$ padrino rake example
Invoke rake with:
PADRINO_LOG_LEVEL=development padrino rake my:task

Alias of task name in Rake

When I need to alias some task's name, how should I do it?
For example, how do I turn the task name:
rake db:table
rake db:create
rake db:schema
rake db:migration
to:
rake db:t
rake db:c
rake db:s
rake db:m
Editing after getting the answer:
def alias_task(tasks)
tasks.each do |new_name, old_name|
task new_name, [*Rake.application[old_name].arg_names] => [old_name]
end
end
alias_task [
[:ds, :db_schema],
[:dc, :db_create],
[:dr, :db_remove]
]
Why do you need an alias? You may introduce a new task without any code, but with a prerequisite to the original task.
namespace :db do
task :table do
puts "table"
end
#kind of alias
task :t => :table
end
This can be combined with parameters:
require 'rake'
desc 'My original task'
task :original_task, [:par1, :par2] do |t, args|
puts "#{t}: #{args.inspect}"
end
#Alias task.
#Parameters are send to prerequisites, if the keys are identic.
task :alias_task, [:par1, :par2] => :original_task
To avoid to search for the parameters names you may read the parameters with arg_names:
#You can get the parameters of the original
task :alias_task2, *Rake.application[:original_task].arg_names, :needs => :original_task
Combine it to a define_alias_task-method:
def define_alias_task(alias_task, original)
desc "Alias #{original}"
task alias_task, *Rake.application[original].arg_names, :needs => original
end
define_alias_task(:alias_task3, :original_task)
Tested with ruby 1.9.1 and rake-0.8.7.
Hmmm, well, I see that's more or less exactly the same solution RyanTM already posted some hours ago.
Here is some code someone wrote to do it: https://gist.github.com/232966
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end

Resources