Sublime Forum

Custom build system for rails

#1

I’d like to create a custome build system for Rails 5
I’ve tried the following:

Rails.sublime-build:

{
  "shell_cmd": "/Users/max/.rbenv/shims/rails test $file"
}

Which works.

I just bring up the command palette and can quickly find the Build With: Rails option by typing in “rails”

However, I’d like it to “just work” with super+b.

Also, it would be nice to use the bin/rails and not require the hard coded path to rails.

I’m not sure if this is possible if it isn’t saved as a project because I think I need $project.

Also, I’m not sure how to distinguish between a rails projects and a regular ruby project.

Thanks for your help :smile:

0 Likes

#2

Sublime selects build systems automatically (part of it “just working”) by using a set selector in the build (while you have Tools > Build System > Automatic checked). Basically it compares the syntax scope of file you’re currently editing in order to select the appropriate build.

So for example, this would automatically select your build for any file that was a plain ruby file:

{
    "shell_cmd": "/Users/max/.rbenv/shims/rails test $file",
    "selector": "source.ruby"
}

Sublime ships with a Ruby on Rails syntax, and based on that it thinks that any file with the extension .rxml or .builder is a Ruby on Rails file. Such files have a scope of source.ruby.rails, so you could sub that in for source.ruby and things should work, assuming that sublime knows that it’s a rails file.

Ruby is not one of my languages (looking at it makes me die a little inside ;)) so I have no idea if one of those two extensions is the norm for all files in a rails project or not.

With all that said, as a C developer I know that Sublime is capable of automatically selecting make as the build system automatically regardless of the type of file that I’m currently editing. The Makefile.sublime-build file has an entry that looks like:

    "keyfiles": ["Makefile", "makefile"],

This isn’t documented, but it would appear that it is also able to determine what build system to use based on specifically named files. Since it’s not documented I don’t know if it applies to only the directory of the current file, the entire project, or what (all of my C projects contain a Makefile in every subdirectory).

So, in theory if there is a some file in a Ruby on Rails project that is always named the same, you could possible also get your build to be selected automatically by adding that as well. If there is no such standard file you could (hackilly) create a file like rails.project in your project and have Sublime look for that, although this requires useless files to be present, which the OCD in me “Rails” at. :wink:

For the first case (selecting based on scope) no explicit project is needed; it will work regardless. For the keyfiles operation, it’s my belief that it would still work regardless although I can’t swear that I have ever actually tried it. It’s simple enough to check, although I don’t have the ability to do that at the moment.

As long as the executable is in your path you can just specify it by name without the absolute path and it will do what you want.

The “trick” (such as it is) is that MacOS X sets the inherited $PATH differently depending on how you started the program; Applications started via e.g. the dock are started using launchctl and get a default path, while applications spawned from the terminal use the path as set in your .bashrc file.

So a lot of people run into the problem of setting up their path, verifying it works by manually running things, and then trying to use the same commands in Sublime only to find that it doesn’t work for unknown reasons.

You can get around this by installing the Fix Mac Path plugin, which makes sure that the path is set to the path you expect regardless of how the application was started. I understand that some future build of Sublime is planned to have something like this already rolled in to stop the problem from happening, although the current build (3126) does not do this by default yet.

2 Likes

#3

Thanks @OdatNurd, helping me out like always :smile:, you rock!

0 Likes