Author Topic: Calling Ruby Script  (Read 5994 times)

afflictedd2

  • Guest
Calling Ruby Script
« on: May 10, 2010, 04:57:44 am »
Hi everyone,

I have a ruby script that beautifies code for me
in Bash. I was wondering, is it possible to make a
command in slicked it that calls upon this script to
beautify the Shell script file I'm currently editing.

could anyone post what the macro.. would look like?

Ted.

jimlangrunner

  • Senior Community Member
  • Posts: 360
  • Hero Points: 31
  • Jim Lang - always a student.
Re: Calling Ruby Script
« Reply #1 on: May 10, 2010, 12:47:55 pm »
Let's start off with this - I'm guessing.  I haven't done this before, but I am intrigued.

Have you looked in the Macro board?  There are a ton of user-written macros in there.

That said, I recorded a macro to run dir and get this:
Code: [Select]
_command OS_Command() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
    _macro('R',1);
    _deselect();
    save();
    execute('dir c:\fred','a');
}

Which you could probably modify to do what you need.  "dir c:\fred" is pretty self-explanatory.  the 'a' argument is, I believe, the arguments to pass to the program.  There is an environment variable that points to the current buffer, which you would need to pass to your Ruby script.  (I'm also not a Ruby guy.)

Hope that gets you started, at least.

Jim

Graeme

  • Senior Community Member
  • Posts: 2345
  • Hero Points: 307
Re: Calling Ruby Script
« Reply #2 on: May 12, 2010, 12:22:03 pm »
Look up the shell command in the help.

I don't know anything about running ruby scripts but assuming your environment knows how to, you could try

shell('your_script.rb ' :+ p_buf_name, 'Q' );
_ReloadFiles();

I dunno about the Q option  - you might need to experiment.  p_buf_name gets the full pathname of the current buffer.
Assuming that shell() doesn't return until the ruby script completes, _ReloadFiles should prompt you to reload the current buffer if your script has updated the file.

If your script takes its input from stdin and outputs on stdout, you might be able to use filter_command.  Type fp filter_command on the slick command line and you'll get taken to the code for filter_command.  So then you could do
select_all();
filter_command('your_script.rb');

Graeme

MartyL

  • Senior Community Member
  • Posts: 161
  • Hero Points: 26
  • Synergex Software Engineer
Re: Calling Ruby Script
« Reply #3 on: May 12, 2010, 02:41:38 pm »
This should get you most of the way there. Make sure your ruby.exe is in your PATH and replace BeautifierScriptPath with the path to your ruby script. The concur_command() line will run the beautifier from your Build window, so if you have debug output you'll be able to see it. This will also prompt you with the SlickEdit dialog to make the file writable if it's read only.

Best regards

Code: [Select]
#include "slick.sh"

#define BeautifierScriptPath "C:\\myfilepath\\myrubybeautifier.rb"
_command void BeatifyBash() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
    _str bashfile = p_buf_name;

    if (bashfile == "")
    {
sticky_message("Current file required");
return;
    }

    if (_QReadOnly())
    {
int status = _on_readonly_error(0, true, false);
if (status == COMMAND_CANCELLED_RC)
{
    sticky_message("Current file is read only");
    return;
}
    }

    if (!file_exists(BeautifierScriptPath))
    {
sticky_message("Could not find beautifier script");
return;
    }

    _str rubypath = slick_path_search("ruby.exe", "M");
    if (rubypath == "")
    {
rubypath = path_search("ruby.exe", "", "P");
if (rubypath == "")
{
    sticky_message("Could not locate ruby.exe in the PATH");
    return;
}
    }

    save();
    concur_command("ruby " :+ BeautifierScriptPath :+ " " :+ bashfile);
    _ReloadFiles();
}

jimlangrunner

  • Senior Community Member
  • Posts: 360
  • Hero Points: 31
  • Jim Lang - always a student.
Re: Calling Ruby Script
« Reply #4 on: May 12, 2010, 02:54:53 pm »
Now that's beautiful.  I learned something new today - woot! 8)

afflictedd2

  • Guest
Re: Calling Ruby Script
« Reply #5 on: October 02, 2010, 10:55:45 pm »
Works great! but it does not show the change immediately because the file must
be refreshed in the editor.. any command to do this?

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Calling Ruby Script
« Reply #6 on: October 03, 2010, 12:19:06 am »
'revert_or_refresh' could be useful (SEv15)
HS2

afflictedd2

  • Guest
Re: Calling Ruby Script
« Reply #7 on: October 03, 2010, 05:40:44 am »
hmm that still doesn't work :\

Graeme

  • Senior Community Member
  • Posts: 2345
  • Hero Points: 307
Re: Calling Ruby Script
« Reply #8 on: October 03, 2010, 06:13:52 am »
You probably need to wait for concur_command to complete before calling _reloadfiles.  Try replacing the call to concur_command with a call to shell as I wrote in my earlier post in this thread.  Maybe there's a way to tell when concur_command command completes but I don't know what it is.

Graeme

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: Calling Ruby Script
« Reply #9 on: October 03, 2010, 12:44:48 pm »
Of course the modifying command must complete before try to re-read the file.
Quote
a way to tell when concur_command command completes
AFAIK there is no officially supported method except the built-in build system using 'vsbuild'.
I'm not sure if this is still restricted to project_build/compile commands in conjunction with error parsing.
Maybe a simple 'shell' call is sufficient in this case which can be run synchronously.
Should be fine if you don't need to parse error output.
Good luck, HS2

afflictedd2

  • Guest
Re: Calling Ruby Script
« Reply #10 on: October 10, 2010, 06:48:43 pm »
I've tried this with concur shell, and it is not successful apparently. I have tried getting the
error as an integer.

I also tried executing the command manually, and it actually completes with a cygwin warning.

I get back as an error code: -2042

Code: [Select]
cygwin warning:
  MS-DOS style path detected: C:/Users/Viper/Documents/Ruby/beautify_bash.rb
  Preferred POSIX equivalent is: /cygdrive/c/Users/Viper/Documents/Ruby/beautify
_bash.rb
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames

Code: [Select]
_str command = "ruby " :+ BeautifierScriptPath :+ " " :+ bashfile;

   int i = concur_shell(command);
   insert_line(command :+ i);
   save();
   revert_or_refresh();
   _ReloadFiles(true);