5

I have various shell build scripts for a project and want to create one centralized build system with options that will allow which shell script to run. For example, a user presses Cmd + B then the user is given the option:

1) shellscript1.sh
2) shellscript2.sh
3) shellscript3.sh

The user presses 3 and Sublime Text runs 'sh shellscript3.sh'.

I've been reading http://sublimetext.info/docs/en/reference/build_systems.html, but am unsure how to integrate this option in the JSON code for the Sublime Text build system.

How do you accomplish this in a build system in Sublime Text 2?

Thanks!

wwwuser
  • 6,282
  • 8
  • 52
  • 64

3 Answers3

2

Actually, you do not need your own plugin. All you need are build variants. Here's simple example using your example commands:

{
  "name Script 1",
  "cmd": ["shellscript1.sh", "$file"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${project_path:${folder:${file_path}}}",

  "variants":
  [
    {
      "name": "Script 2",
      "cmd": ["shellscript2.sh", "$file"]
    },
    {
      "name": "Script 3",
      "cmd": ["shellscript3.sh", "$file"]
    }
  ]
}

Save this in your User preferences folder as MyScript.sublime-build. You will then be able to select it from the build menu, turning off the automatic target.

Now, when you press Command+B (on Mac, Control+B on Windows and Linux), the default target executes Script 1, on your file, but you can also select either of the variants.

See this answer, also, for a build file that I personally use providing variants for different Make targets.

Community
  • 1
  • 1
dbn
  • 13,144
  • 3
  • 60
  • 86
1

I don't know enough python to give you the specific code, but it looks like you need to write your own exec.py to handle an array of the commands and provide the control logic. Then in the JSON file, you would just need to write the value of the "cmd" key as [["first cmd"], ["second cmd"],..., ["last cmd"]],.

I'm following this question; I really like your idea.

Colin R
  • 17,711
  • 2
  • 20
  • 28
  • I believe you're right in that it needs to be python. I'm unable to get Sublime Text to recognize input from a shell script. Maybe a plugin instead of a build system? – wwwuser Nov 14 '11 at 06:31
  • this wouldn't work, as sublime won't allow multiple commands in "cmd" as it is passed to popen internally. – Andrey Dec 18 '11 at 10:17
  • Sublime passes the commands to Popen in the exec.py script. If that script was rewritten to handle an array of commands and determine which command to pass to Popen (which is what my answer says), then why would this not work? – Colin R Dec 19 '11 at 01:27
1

I ended up making my own plugin and placed the following in run():

self.view.window().run_command('exec', {'cmd': ['sh', 'script.sh'], 'quiet': False})        

I based it off of the code of this Git Support plugin: https://github.com/notanumber/gitst2

wwwuser
  • 6,282
  • 8
  • 52
  • 64
  • 2
    Why not just having it as your `custom build system` (i.e. "Sublime > Build System > New build System...")? You don't seem to do anything in this example that requires a plugin – Andrey Dec 18 '11 at 10:20