Custom Sublime Text Build Systems For Popular Tools And Languages

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:

?
1
2
3
4
5
6
{
     "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:

?
1
2
#!/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:

?
1
2
3
4
{
   "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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
     "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:

?
1
2
3
4
{
     "cmd" : [ "grunt" , "--no-color" ],
     "selector" : [ "source.js" , "source.less" , "source.json" ]
}

Node Build Script:

?
1
2
3
4
{
     "cmd" : [ "h5bp" , "--no-color" ],
     "selector" : [ "source.js" , "source.less" , "source.json" ]
}

CoffeeScript:

?
1
2
3
4
{
     "cmd" : [ "coffee" , "-c" , "$file" ],
     "selector" : "source.coffee"
}

SASS:

?
1
2
3
4
5
{
     "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:

?
1
2
3
4
5
{
     "cmd" : [ "sass" , "--watch" , "sass:stylesheets" , "--style" , "compressed" ],
     "working_dir" : "$project_path" ,
     "selector" : [ "source.scss" , "source.sass" ]
}

LESS:

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

Stylus:

?
1
2
3
4
5
{
     "cmd" : [ "stylus" , "$file" ],
     "file_regex" : "." ,
     "selector" : "source.stylus"
}

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

Jade:

?
1
2
3
4
{
    "cmd" : [ "cmd" , "/c" , "jade" , "$file" ],
    "selector" : "source.jade"
}

r.js (RequireJS Optimizer):

?
1
2
3
4
5
{
     "cmd" : [ "node" , "r.js" , "-o" , "app.build.js" ],
     "working_dir" : "$project_path" ,
     "selector" : "source.js"
}

UglifyJS:

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

Node (just passing in directly):

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

Pandoc (Markdown to HTML):

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

(and when it’s released, Yeoman):

?
1
2
3
4
{
      "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):

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

Python:

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

PHP:

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

Java:

?
1
2
3
4
5
6
{
     "cmd" : [ "javac" , "$file_name" , "&&" , "java" , "$file_base_name" ],
     "working_dir" : "${project_path:${folder}}" ,
     "selector" : "source.java" ,
     "shell" : true
}

.Net (Windows):

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

C:

?
1
2
3
4
5
{
     "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):

?
1
2
3
4
5
6
7
{
     "cmd" : [ "g++" , "$file" , "-o" , "$file_base_name" , "-I/usr/local/include" ],
     "selector" : "source.c++" ,
     "windows" : {
        "cmd" : [ "cl" , "/Fo${file_path}" , "/O2" , "$file" ]
     }
}

Haskell:

?
1
2
3
4
5
{
     "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.

在当今科技日新月异的时代,智慧社区的概念正悄然改变着我们的生活方式。它不仅仅是一个居住的空间,更是一个集成了先进科技、便捷服务与人文关怀的综合性生态系统。以下是对智慧社区整体解决方案的精炼融合,旨在展现其知识性、趣味性与吸引力。 一、智慧社区的科技魅力 智慧社区以智能化设备为核心,通过综合运用物联网、大数据、云计算等技术,实现了社区管理的智能化与高效化。门禁系统采用面部识别技术,让居民无需手动操作即可轻松进出;停车管理智能化,不仅提高了停车效率,还大大减少了找车位的烦恼。同时,安防报警系统能够实时监测家中安全状况,一旦有异常情况,立即联动物业进行处理。此外,智能家居系统更是将便捷性发挥到了极致,通过手机APP即可远程控制家中的灯光、窗帘、空调等设备,让居民随时随地享受舒适生活。 视频监控与可视对讲系统的结合,不仅提升了社区的安全系数,还让居民能够实时查看家中情况,与访客进行视频通话,大大增强了居住的安心感。而电子巡更、公共广播等系统的运用,则进一步保障了社区的治安稳定与信息传递的及时性。这些智能化设备的集成运用,不仅提高了社区的管理效率,更让居民感受到了科技带来的便捷与舒适。 二、智慧社区的增值服务与人文关怀 智慧社区不仅仅关注科技的运用,更注重为居民提供多元化的增值服务与人文关怀。社区内设有互动LED像素灯、顶层花园控制喷泉等创意设施,不仅美化了社区环境,还增强了居民的归属感与幸福感。同时,社区还提供了智能家居的可选追加项,如空气净化器、远程监控摄像机等,让居民能够根据自己的需求进行个性化选择。 智慧社区还充分利用大数据技术,对居民的行为数据进行收集与分析,为居民提供精准化的营销服务。无论是周边的商业信息推送,还是个性化的生活建议,都能让居民感受到社区的智慧与贴心。此外,社区还注重培养居民的环保意识与节能意识,通过智能照明、智能温控等系统的运用,鼓励居民节约资源、保护环境。 三、智慧社区的未来发展与无限可能 智慧社区的未来发展充满了无限可能。随着技术的不断进步与创新,智慧社区将朝着更加智能化、融合化的方向发展。比如,利用人工智能技术进行社区管理与服务,将能够进一步提升社区的智能化水平;而5G、物联网等新技术的运用,则将让智慧社区的连接更加紧密、服务更加高效。 同时,智慧社区还将更加注重居民的体验与需求,通过不断优化智能化设备的功能与服务,让居民享受到更加便捷、舒适的生活。未来,智慧社区将成为人们追求高品质生活的重要选择之一,它不仅是一个居住的空间,更是一个融合了科技、服务、人文关怀的综合性生态系统,让人们的生活更加美好、更加精彩。 综上所述,智慧社区整体解决方案以其科技魅力、增值服务与人文关怀以及未来发展潜力,正吸引着越来越多的关注与认可。它不仅能够提升社区的管理效率与居民的生活品质,更能够为社区的可持续发展注入新的活力与动力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值