Sunday, 10 March 2013

A better Drush php-eval tool

I use Drush php-eval quite some time. It's part of a quick evaluation process, to see if your function is working, find everything it needs and preferably provides you the perfect result. But I don't like entering php code in the terminal.

I was wondering a lot what would be the perfect way to use this tool. First I need color coding. I have to see the proper syntax highlight. What can provide that? Not too much, unfortunately. A proper editor and JavaScript plugins. If you go on that line you have some options - you can build plugins. In PHPStorm or in Sublime maybe. In the latter one you have a nice Python interface that you can extend - but the problem in Sublime is the result. Probably in PHPStorm you could create something really advanced - like imagine you could select a code or a function and just say: run on maximum bootstrap level. And you have the console there. Well, even better, you could use a nice output where you could open the arrays or object definitions.

You have a similar thing provided by Devel module. You can evaluate on a page callback in a textarea. But ehm doesn't feel ok. Only advantage is that you can use convenient line breaks. Probably there is a library that adds syntax highlight to textareas - or something. Not sure you can do that actually. Maybe that has to be an iframe using document.designmode = 'on' directive on it.

I was thinking on a native cocoa app, but I'm looking for an easier solution. So I turned to Python. After a little search it seemed that the WX library is one of the best way to make UIs. (However it's surprising how many GUI libraries are there.) I quickly installed the library and created a simple project:

import wx
from subprocess import call

class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, -1, "Drush proxy")

        id = wx.NewId()
        self.textCtrl = wx.TextCtrl(frame, id)

        id = wx.NewId()
        self.button = wx.Button(frame, id, "Run")

        self.Bind(wx.EVT_BUTTON, self.onClick)


        return True

    def onClick(self, event):
        call(['drush', '-r ~/WEBROOT', 'php-eval', '"' + self.textCtrl.GetValue() + '"'])

app = MyApp(0)

It's not ready - just saying. I'm using the subprocess library to reach the shell layer but for some reason when calling Drush it returns with some strange JSON objects. Native commands seems to work fine.

My guess is that IO streams are not functioning the same way when you originate your process from Python - maybe I'm wrong. That's a little homework for me.



1 comment:

  1. phpMyAdmin uses CodeMirror: – what about adding it to Drupal's devel.module? Er, only support for that, since CodeMirror's license is MIT, so we can't commit it to git@d.o…