Sublime Forum

Use custom command as build tool?

#1

Hey All,

I’ve written a WindowCommand (text below) that I would like to call for a build command instead of the normal shell-out-to-external-exe. (Basically my command does the shelling, but with some extra functionality.)

Looking at the docs for build systems, I’m not quite sure how to do this. I have tried:

"cmd": "run_sas_program"],

Which earns me:

[quote][Error 2] The system cannot find the file specified
[cmd: [u’run_sas_program’]]
[dir: C:\deleteme]
[path: yadda yadda yadda]
[Finished]
[/quote]

And also:

  "run_sas_program": ],

Which gets me (in the console):

Traceback (most recent call last): File ".\sublime_plugin.py", line 337, in run_ File ".\exec.py", line 154, in run TypeError: __init__() got an unexpected keyword argument 'run_sas_program'

Can anyone tell me how to get this done?

Many thanks!

-Roy

P.S. In case it’s useful, here’s the command. It seems to work when I bind it to a shortcut key:

import sublime, sublime_plugin, subprocess, os
class RunSasProgramCommand(sublime_plugin.WindowCommand):
  """Prototype custom SAS build command"""
  def run(self):
    prg_filename = self.window.active_view().file_name()
    extension = os.path.splitext(prg_filename)-1].lower()
    if extension == '.sas':
      log_filename = prg_filename:-3] + 'log'
      lst_filename = prg_filename:-3] + 'lst'
      lrn_filename = lst_filename + '.last.run'
      if os.path.exists(lrn_filename):
        os.rm(lrn_filename)
      s = sublime.load_settings('sas.sublime-settings')
      sas_path = s.get('sas-path', "C:\\Program Files\\SAS\\SASFoundation\\9.2\\sas.exe")
      sas_args = s.get('sas-args', '-nologo', '-noovp'])
      err_regx = s.get('err-regx', "($(error|warning:)|uninitialized|^l]remerge|Invalid data for)(?! (the .{4,15} product with which|your system is scheduled|will be expiring soon, and|this upcoming expiration.|information on your warning period.))")
      s.set('sas-path', sas_path)
      s.set('sas-args', sas_args)
      sublime.save_settings('sas.sublime-settings')
      call_args = [sas_path, '-sysin', prg_filename, '-log', log_filename, '-print', lst_filename] + sas_args
      print subprocess.list2cmdline(call_args)
      subprocess.call(call_args)
      sublime.status_message("Finished running " + prg_filename)
      if os.path.exists(lst_filename):
        self.window.open_file(lst_filename)
      if os.path.exists(log_filename):
        self.window.open_file(log_filename)
        sublime.run_command('show_next_error')
      else:
        sublime.message_dialog("Problem!  Did not find the expected log file (" + log_filename + ").")
      # print sas_path + " exists?: " + str(os.path.exists(sas_path))
      # sublime.message_dialog("Pretend I ran " + sas_path)
      # self.window.open_file(r'C:\Users\Roy\AppData\Roaming\Sublime Text 3\Packages\SAS\notes.txt')
    else:
      sublime.message_dialog('Sorry--this only works with .sas files.')
0 Likes

#2

I’m disappointed not to have a response to this by now. Is it just not possible?

FWIW-here’s the problem I’m having that’s led me to write the command. The type of programming I’m doing in sublime is SAS–a big-bore data manipulation & analytics platform. The SAS executable takes a script of commands and runs it, producing a .log file and an output listing file (.lst). My existing build tool sends my script to sas.exe, waits for it to finish, and then shell executes the .log and .lst files, which causes them to open in whatever program is registered as preferred for those file extensions (to wit–sublime). So I can edit my programs, submit them, and immediately see the output when they finish.

This works wonderfully–so long as you only have a single project open. If you have more than one, then the log file will open in whichever instance of sublime was last active. So I wind up with the log file from project 1 opened up in project 2’s window. This is messy and annoying. I figured if I shelled out to sas.exe from a sublime command, and followed it right up with a window.open_file(), it’d always wind up in the proper instance. Happily, I was right & it seems to work pretty well. So now I just need to know how to get sublime to treat this command as the build tool for SAS programs.

Can anybody help?

Thanks!

-Roy

0 Likes

#3

Nevermind–here’s the fix.

{
  "target": "run_sas_program",
  "selector": "source.sas"
}
0 Likes