Run Rake from Controller/View?!

Hi - I have a simple problem, maybe you guys can help me come up with a
solution:

I'm looking for a way to let a user of my rails app execute a rake-task
by clicking on a link in a view.

Szenario:
I'm writing a rails-App that takes a given directory and analyzes any
ruby files in that directory. Since there already are quite a few tools
that gather information from ruby-sourcecode i don't feel like
re-inventing the wheel and i'd like to integrate some of those tools
into my app.

The "Metric-Fu"-Suite for example comes with all kinds of
Metric-Calculation tools that can be started with different rake-tasks
I.e. "rake metrics:reek" will start collecting info about "bad smells"
using the roodi-Toolset. It's also possible to pass parameters (or
define them in the rakefile) to tell reek which directories to parse for
sourcefiles.

For my project I would like to create a view where a user can add local
directories where sourcefiles are located, and select the metric tools
he wants to use to check his code (like reek, roodi, flog etc).

Is there any way to implement this?
I'd basically need to create a rake task with the given parameters and
than run it from my controller...

Any thoughts?
Thanks in advance

Jim

Just a guess, since I haven't ever had this situation but, rake files
are ruby files, you should be able to include them from any other ruby
file and execute the methods it has defined inside.
As I said, I haven't tried this, is just a guess.

Otherwise, you can always use a system call to execute system commands
(such as rake)

Hope it helps.

Marnen Laibow-Koser wrote:

, where
Rake::Task#execute is documented. Next time, you may want to take those
20 seconds before posting...

Well, thanks for the reply.

After feeling incredibly stupid for a couple of minutes I tried using
the methods given in the docs you linked to.

Unfortunately I still can't get my task to run:

my code in the controller:

def test_rake
t = Rake::Task["doc:app"]
puts Rake::Task.task_defined?("doc:app") --> true
puts t.inspect() --> <Rake::Task doc:app =>
[doc/app/index.html]>
puts t.name() --> doc:app
t.execute() --> returns nothing
t.invoke() --> returns false

#I found a line like this in another forum:
Rake::Task["rake:doc:app"].execute() --> also returns nothing
end

When I run the "test_rake" method, I don't get any errors, but the task
still doesn't get executed...

Where am I going wrong?

Ingmar Hamer wrote:

Marnen Laibow-Koser wrote:

, where
Rake::Task#execute is documented. Next time, you may want to take those
20 seconds before posting...

Well, thanks for the reply.

After feeling incredibly stupid for a couple of minutes I tried using
the methods given in the docs you linked to.

Unfortunately I still can't get my task to run:

my code in the controller:

def test_rake
t = Rake::Task["doc:app"]
puts Rake::Task.task_defined?("doc:app") --> true
puts t.inspect() --> <Rake::Task doc:app =>
[doc/app/index.html]>
puts t.name() --> doc:app
t.execute() --> returns nothing
t.invoke() --> returns false

It's not clear to me from looking at the source whether execute is
supposed to return anything. Does the task in fact get run?

#I found a line like this in another forum:
Rake::Task["rake:doc:app"].execute() --> also returns nothing
end

This is of course equivalent to your other execute() call. (And you
don't need parentheses if there are no arguments.)

When I run the "test_rake" method, I don't get any errors, but the task
still doesn't get executed...

Where am I going wrong?

I don't know if you are. Are you sure the task isn't being run? Does
it work from the Rails console?

Best,

Finally got it to run using the "system call"-approach:

The trick to get it to run on my windows machine was using the "start"
command (that usually spawns a procell in a new shell). Why windows
executes the "dir" command without "start" but not the rake command is
beyond me, but at least now it is working:

Is rake a batch file in windows? That could explain this, I believe a
batch file cannot just be run, it needs a command shell to do it.
Whether it might explain other problems I do not know.

Colin

Is rake a batch file in windows? That could explain this, I believe a
batch file cannot just be run, it needs a command shell to do it.
Whether it might explain other problems I do not know.

Colin

That sounds reasonable. As a matter of fact there are two rake-files in
my ruby/bin directory, one of which is a batch file (the other one is
just called 'rake' with no suffix).
It seems like the rake.bat file starts a ruby command executing the
'rake'-File (which, when opened in notepad is just a regular ruby file
but has no extension)...

At least that makes sense now, thanks Colin :wink:

Dear Ingmar Hamer

Thank you for your support to run rake task with

system('rake ultrasphinx:index:delta')

I have to run rake ultrasphinx:index:delta after every entry in database
so I use this command

It help me

so thx you.