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
On Mon, Sep 28, 2009 at 4:29 PM, Iggy Whydoucare
[email protected] wrote:
I’m writing a rails-App that takes a given directory and analyzes any
sourcefiles.
Thanks in advance
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.
–
Leonardo M…
There’s no place like ~
Leonardo M. wrote:
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)
Hi Leonardo
I think you’re spot on with your answers. It’s exactly what I tried to
do (in that order, too
The problem is that the task simply doesnt get executed when I try
running it from the controller:
If I do s.th. like this in my controller:
require ‘rake’
Rake::Task[“doc:app”]
–> the app runs without error but the rake task doesn’t get executed
however if I use a random ‘task’ name:
require ‘rake’
Rake::Task[“foo:bar”]
–>Error: Don’t know how to build task ‘foo:bar’
So it does seem like my code gets evaluated but the rake task is just
not executed. Am I missing something like an explicit “run task”
command?!
The “system call” approach produces similar behavior:
If I add this line in my controller:
system(‘dir > test.txt’)
–> works fine and places the test.txt file in my rails base directory
however if I add:
system(‘rake doc:app’)
–> my app executes but nothing happens (no docs are generated)
If I run “rake doc:app” from the command line it works as expected …
What is my problem here?!
I’m confused…
Ingmar H. wrote:
Leonardo M. wrote:
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)
Hi Leonardo
I think you’re spot on with your answers. It’s exactly what I tried to
do (in that order, too
The problem is that the task simply doesnt get executed when I try
running it from the controller:
If I do s.th. like this in my controller:
require ‘rake’
Rake::Task[“doc:app”]
→ the app runs without error but the rake task doesn’t get executed
however if I use a random ‘task’ name:
require ‘rake’
Rake::Task[“foo:bar”]
–>Error: Don’t know how to build task ‘foo:bar’
So it does seem like my code gets evaluated but the rake task is just
not executed. Am I missing something like an explicit “run task”
command?!
It seems that you are. Although I’ve never done anything like this, 20
seconds of reading docs led me to ActionController::Base , where
Rake::Task#execute is documented. Next time, you may want to take those
20 seconds before posting…
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Marnen Laibow-Koser wrote:
[…]
It seems that you are. Although I’ve never done anything like this, 20
seconds of reading docs led me to ActionController::Base
Sorry, I meant http://bit.ly/2VBEj .
, where
Rake::Task#execute is documented. Next time, you may want to take those
20 seconds before posting…
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
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 H. 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,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
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:
Windows:
system(‘start rake doc:app’)
Linux:
system(‘rake doc:app’)
I’d still be interested why the “test_rake”-Funktion in my previous post
doesn’t start the rails task, just in case anybody sees my error…
Thanks for your help!
@marnen
I SWEAR I WILL USE GOOGLE AND CHECK ANY DOCS BEFORE POSTING
I don’t know if you are. Are you sure the task isn’t being run? Does
it work from the Rails console?
Yes, the task runs fine from the rails console
and No, it doesn’t get executed when I run the “test_rake”-function (the
view renders in like half a second, I don’t have any output other than
what i’ve posted before and I don’t have any documentation in my ‘docs’
folder)
If I understand the docs correctly I don’t think “execute” should return
anything… - still it should “execute” something, right?
PS: I tried running this in my windows-environment (Ruby 1.8.6 Gem
1.3.3, Rake 0.8.5, Rails 2.3.4) and on my unix-server (Ruby 1.8.6, Gem
1.3.1, Rake 0.8.4, Rails 2.3.2)
2009/9/29 Ingmar H. [email protected]:
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
Dear Ingmar H.
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.