Sublime Text is currently the text editor of choice for a number of developers in the open-source community. It’s sophisticated, has powerful text selection and customization support and also includes a feature not used by many – its build system. In this post, I’d like to take you through the Sublime build system and share build scripts for working with many of the languages and tools we use today.

These will include scripts for GruntCoffeeScriptSASS and others.

Introduction

Sublime Text build systems can be considered simplistic, but highly customizable. The basic idea is that each type of Build profile is powered by a “.sublime-build” file – a JSON representations of the commands, paths and configuration needed to build a project using a specific tool or set of tools.

Builds can be executed using a keyboard shortcut (Command+B on Mac is the default on Mac or F7 on Windows), via the Tools menu or when a file is saved. If a project is currently open, the build system we last selected (e.g grunt) will be remembered.

When Sublime is passed references to external tools/binaries via a “.sublime-build” files, it can execute these applications with any arguments or flags that may be necessary. It is also able to pipe back the output of calling any of these apps using the built-in console in Sublime. Effectively this allows us to easily build projects without the need to leave our editor.

Adding a custom Build System

Sublime populates its Tools/Build System menu based on the “.sublime-build” files stored in the Sublime “Packages” directory. Should one need to locate this, it can be found in “~/Library/Application Support/Sublime Text 2/Packages/User” (if using OS X) or the corresponding Packages/User directory on other platforms.

A basic “.sublime-build” file could be represented in key/value form as follows:

{
"cmd": ["command", "argument", "--flag"],
"selector": ["source.js"],
"path": "/usr/local/bin",
"working_dir": "/projects/"
}

Keys supported include:

  • cmd - An array containing a command to run and its desired arguments and flags. Note that Sublime will search your PATH for any tools listed unless an absolute path has been used to point to them.
  • selector – An optional string used to locate the best builder to use for the current file scope. This is only relevant if Tools/Build System/Automatic is true.
  • path – An optional string that replaces your current process’s PATH before calling the commands listed.
  • working_dir – An optional string defining a directory to switch the current directory to prior to calling any commands.
  • shell - An optional boolean that defines whether commands should be run through the shell (e.g bash).
  • file_regex – An optional regular expression used to capture error output from commands.

For a comprehensive list of keys supported in Sublime build scripts, see theunofficial docs.

Build Variables:

In addition, Sublime supports variable substitutions in build files such as$file_path (for the path to the current file) and more. These include:

  • $file_path – the directory of the current file being viewed
  • $file_name - only the name portion of the current file (extension included)
  • $file_base_name - the name portion of the current file (extension excluded)
  • $project_path - the directory path to the current project
  • $project_name – the name portion of the current project

A complete list of substitutions supported is also available.

Grouping build tasks

Some developers also like to group together tasks within an external bash script (or equivalent). For example, here’s a simple git-ftp deploy script you can use with Sublime to commit and push your latest changes with git and then upload your latest files to FTP.

Example: Commit, Push And Upload To FTP

deployment.sh:

#!/bin/bash
git add . && git commit -m 'deployment' && git push && git ftp init -u username -p password - ftp://host.example.com/public_html

deployment.sublime-build:

{
"cmd": ["deployment"],
"working_dir": "${project_path:${folder}}"
}

If you haven’t used git-ftp before, Alex Fluger has a solid article about using it that may be of interest.

Targeting Platforms:

Sublime build files also support specifying configuration data for specific platforms (namely, OS X, Windows and Linux). Targeting a platform can easily be done by specifying another element in our config with the name of the platform. e.g

{
"cmd": ...
...
"windows":
{
"cmd": ...
},
"osx":
{
"cmd": ...
},
"linux":
{
"cmd": ...
}
}

Build files for popular front-end tools

To help you get started, I’ve written a collection of “.sublime-build” files for some of the front-end tools I’m aware web developers are using these days below.

Most of these will function fine without the need to specify path, but if you run into an issue with paths, try including it to your config (e.g "path": "/usr/local/bin").

grunt:

{
"cmd": ["grunt", "--no-color"],
"selector": ["source.js", "source.less", "source.json"]
}

Node Build Script:

{
"cmd": ["h5bp", "--no-color"],
"selector": ["source.js", "source.less", "source.json"]
}

CoffeeScript:

{
"cmd": ["coffee","-c", "$file"],
"selector" : "source.coffee"
}

SASS:

{
"cmd": ["sass", "--watch", ".:."],
"working_dir": "$file_path",
"selector": ["source.scss", "source.sass"]
}

Whilst a more verbose version with automatic minification and watch config could be written:

{
"cmd": ["sass", "--watch", "sass:stylesheets", "--style", "compressed"],
"working_dir": "$project_path",
"selector": ["source.scss", "source.sass"]
}

LESS:

{
"cmd": ["lessc", "-x", "$file", "$file_path/$file_base_name.css", "--verbose"],
"shell" : true,
"selector": "source.css.less"
}

Stylus:

{
"cmd": ["stylus", "$file"],
"file_regex": ".",
"selector": "source.stylus"
}

(a more comprehensive version of this can be found in the LESS-build-sublimeproject.)

Jade:

{
"cmd": ["cmd", "/c", "jade", "$file"],
"selector": "source.jade"
}

r.js (RequireJS Optimizer):

{
"cmd": ["node", "r.js", "-o", "app.build.js"],
"working_dir": "$project_path",
"selector": "source.js"
}

UglifyJS:

{
"cmd": [ "node", "uglifyjs", "-o", "${file_path}/${file_base_name}.min.js", "$file"],
"selector": "source.js"
}

Node (just passing in directly):

{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js"
}

Pandoc (Markdown to HTML):

{
"cmd": ["pandoc", "-S", "-s", "-f", "markdown", "-t", "html", "-o", "$file_base_name.html", "$file"],
"selector": "text.html.markdown"
}

(and when it’s released, Yeoman):

{
"cmd": ["yeoman", "build", "--no-color"],
"selector": ["source.js", "source.scss", "source.sass", "source.html"]
}

JSHint:

I imagine most web developers would want to run JSHint from within a broader build process, but if you’d also like to run it standalone via a Sublime build file, thesublime-jshint package has a build file that will work fine on both OS X and Windows.

Build files for specific programming languages

I also thought that while we were looking at build files, it would be useful to demonstrate how these can be used to build/compile with some popular programming languages. These may differ to those included with Sublime by default, but are useful for reference:

Ruby (using RVM):

{
"cmd": ["~/.rvm/bin/rvm-auto-ruby", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}

Python:

{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

PHP:

{
"cmd": ["/usr/bin/php", "-l", "$file"], <- Couldn't just use "php" ?
"file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)",
"selector": "source.php"
}

Java:

{
"cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector": "source.java",
"shell": true
}

.Net (Windows):

{
"cmd": ["%WINDIR%\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild", "${project_base_name}.sln"],
"shell": true,
"working_dir": "${project_path:${folder}}"
}

C:

{
"cmd": ["make && ./a.out"],
"path": "/usr/bin:/usr/local/bin:...",
"shell": true
}

C++ (via g++):

(Note that we’re also able to specify OS-specific configurations too, as in the below):

{
"cmd": ["g++", "$file", "-o", "$file_base_name", "-I/usr/local/include"],
"selector": "source.c++",
"windows": {
"cmd": ["cl", "/Fo${file_path}", "/O2", "$file"]
}
}

Haskell:

{
"cmd": ["runhaskell", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.haskell"
}

Conclusions

Sublime build systems are awesome and can help you avoid the need to manually switch between your editor and external build tools regularly. As you’ve hopefully now learned, putting together your own custom build systems is a straight-forward process and I’d recommend trying it out if Sublime happens to be your editor of choice.

Custom Sublime Text Build Systems For Popular Tools And Languages的更多相关文章

  1. Sublime Text Build 3065 License key

      Sublime Text Build 3065 License key 复制如下三个任意一个正版注册码即可 —– BEGIN LICENSE —– Andrew Weber Single User ...

  2. 最新版Sublime Text Build 3156 x64 的下载 + 注册码 + Install Package Control + 汉化教程

    一.Sublime Text  下载 神器 Sublime Text 最近开始更新到开发版本 Build 3156,本身英语不是太6,汉化党自然各种百度汉化教程,网上不是一堆绿色汉化包,就是让你下载汉 ...

  3. Sublime Text Build System——编译运行Java

    今天Google如何在ST中编译运行Java的时候,无意中发现了一个更好的方法. 其实,在ST中是可以编译Java的,但是运行不了,因为没有配置运行命令.那么一般的配置方法都是如下的: http:// ...

  4. sublime text build system automatic ctrl/cmd+B自动选择 python2 或 python3

    背景 我同时安装了 python2 和 python3 时,python 指向 python2,python3 才是 python3 默认情况下,在 Sublime 内 Ctrl/Cmd + B 运行 ...

  5. Sublime Text Build 3207 x64 无法安装Package Control和插件

    两个问题的解决方法: 以下都是问题的解决,在本人电脑成功解决,还有就是在虚拟机上也成功解决,可以自行尝试下 . 测试电脑为win7-64位 问题1 : 安装Package Control失败 解决问题 ...

  6. 杂谈:用 Sublime Text 2 写 ActionScript3

    Sublime Text这是程序员最喜爱的编辑器,说说在win7下使用Sublime Text来编写as文件以及编译与运行swf. 准备工作 1.Sublime Text 2 2.Java 的JDK( ...

  7. Sublime Text使用配置介绍

    这篇文章很多内容都是来源自网络,发布这里当作自己留个底,以后不用到处去找 对于文本编辑器,我用过notepad2.notepad++.Editplus.UltraEdit.Vim.TextPad,都没 ...

  8. 配置 Sublime Text 用 Node.js 执行 JavaScript 程序

    1. 首先到 nodejs.org 下载 Node.js 安装包并安装. 2. 打开 Sublime Text 2 编辑器.选择菜单 Tools --> Build System --> ...

  9. 【Nodejs】【node.js 安装 和 配置Sublime Text的Node.js】

    [一] [安装nodejs] 第一步:下载安装文件: https://nodejs.org/en/download/ 第二步:安装nodejs 下载完成之后,双击"node-v6.10.1- ...

随机推荐

  1. 微信小程序,创业新选择

    微信小程序,创业新选择 创业者们 总是站在时代的风口浪尖,他们踌躇满志无所畏惧,这大概就是梦想的力量.但是,如果没有把梦想拆解成没有可预期的目标和可执行的实现路径那么一切都只能叫做梦想. 小程序 张小 ...

  2. Pinpoint - 应用性能管理(APM)平台实践之部署篇

    0.0 前言 国内的APM行业这两年刚刚起步,但是在国外却比较成熟了,并且由于这两年人力成本的快速提高,国内外涌现了几家非常不错的APM企业,例如APPdynamic,Dynamic,NewRelic ...

  3. Git 经常使用命令合集

    ====== Git 经常使用命令合集 ====== === 1.Git 文档 ===     Git 中文文档观看地址:http://git.oschina.net/progit/      === ...

  4. django日志使用TimeRotateFileHandler

    如果使用django的settings.py设置日志会产生一些问题. 问题描述 报错信息如下: Traceback (most recent call last): File "C:\Pyt ...

  5. Librec的AoBPR算法实现

    Librec的AoBPR算法实现:(基于1.3版本) 要用AoBPR,但是没有找到相应的配置文件,应该怎么办呢?       ——因为用的是1.3版本,所以没有,2.0版本有的.[跟BPR参数一样,就 ...

  6. [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'beginTime';

    依照https://stackoverflow.com/questions/23702041/failed-to-convert-property-value-of-type-java-lang-st ...

  7. 为Magento1.5新增会员注册字段(转)

    第一步.新建一个模块,在app/etc/modules/目录下新建文件Shuishui_Customer.xml <config> <modules> <Shuishui ...

  8. apk 静默安装

    老大要我弄个自动更新,要用到静默安装,网上找到了些大拿的代码,我拿去改吧改吧,先贴出来: /** * 软件静默安装 * @param apkAbsolutePath apk文件所在路径 * @retu ...

  9. 根据友盟统计错误分析线上的崩溃-b

    登陆友盟官网找到友盟统计,找到你iOS平台下你所属的APP(图1) 图1 点击进去会出现当日错误列表,选择你发生错误的日期(图2) 图2 我们可以看到,这一天中出现了两个错误,每个错误出现在不同的时间 ...

  10. MySQL -- 外键创建失败

    使用show engine innodb status\G 查看数据库状态的时候,发现以下报错信息: ------------------------ LATEST FOREIGN KEY ERROR ...