Sublime Text Build System

Sublime Text 提供 build system 机制,允许开发者执行外部命令。常见的 build system 应用场景包括:编译、转换、优化代码和可用性测试。

Sublime Text 中的快捷键、package、插件的配置项均是通过JSON文件的形式保存在硬盘上。build system 也不例外,它通过创建JSON格式的配置文件设置 build system,规定此类文件的文件病以 .sublime-build 结尾。创建新的 build system 的方法有两种,其一是通过菜单栏 Tools > Build System > New Build System 菜单项,其二是通过命令 Build: New Build System

Build system 包括以下功能:

  • 基于文件类型,自动化选择合适的目标构建系统。
  • 记忆上一次使用的目标构建系统。
  • 构建系统构建结果(错误结果)导航。
  • 在构建过程中取消构建。

开始执行目标构建系统:

STB

配置文件使用JSON格式表示,后缀名为 .sublime-build ,配置项包含如下:

selector

The base scope name of the syntax that this build system should be enabled for.

selector : "source.python"

file_patterns

A list of file name patterns the build system should be enabled for.

file_patterns : ["*.py"]

keyfiles

A list of file names, if present in one of the opened folders, that will cause the build system to be enabled.

keyfiles : ["Makefile"]

variants

A list of subsidiary build systems that will inherit the options from the top-level build system. Each //variant needs to specify a name key, and may override or add options to the top-level build system.

variants : [
  {
        "name": "Debug Symbols",
        "cmd": ["my_command", "-D", "$file"]
    }
]

cancel

A string command name, or a list of string options. If a string is specified, the command specified will be used to cancel the build. If a list of strings, the primary target will be called, with these options added on. This only needs to be specified when using a custom target.

cancel : {
  "kill" : true
}

target

The command to run when the build system is invoked. The default value of exec allows use of the additional options specified in exec Target Options. If a value other than exec is specified, none of the options in exec Target Options will do anything. See the Advanced Example for a complete example.

target : "my_build"

windows

An object of options to use when the build system is being executed on a Windows machine.

windows : {
    "cmd": ["my_command.exe", "/D", "$file"]
}

osx

An object of options to use when the build system is being executed on a Mac machine.

osx : {
    "cmd": ["/Applications/MyProgram.app/Contents/MacOS/my_command", "-d", "$file"]
}

linux

An object of options to use when the build system is being executed on a Linux machine.

linux : {
    "cmd": ["/usr/local/bin/my_command", "-d", "$file"]
}

cmd

A list of strings specifying the executable to run, plus any arguments to pass to it. Shell constructs such as piping and redirection are not supported – see shell_cmd. May use variables.

cmd : ["my_command", "-d", "$file"]

shell_cmd

A string specifying a shell command to execute. Unlike the cmd option, this does allow piping and redirection. Will use bash on Mac and Linux machine, and cmd.exe on Windows. May use variables.

shell_cmd : "my_command \"$file\" | other_command"

working_dir

A string specifying the directory to execute the cmd or shell_cmd within. May use variables.

working_dir : "$file_path"

file_regex

A string containing a regular expression to run on the build output to match file information. The matched file information is used to enable result navigation. The regex should capture 2, 3 or 4 groups.

file_regex : "^\s*(\\S[^:]*)\\((\\d+):(\\d+)\\): ([^\\n]+)"

line_regex

A string containing a regular expression to run on the build output to match line information. The matched file information is used to enable result navigation. The regex should capture 1, 2 or 3 groups.

line_regex : "^\s*line (\\d+) col (\\d+): ([^\\n]+)"

encoding

A string specifying the encoding of the build system output. Uses Python codec names. Defaults to “utf-8”.

encoding : "iso-8859-1"

env

An object containing environment variable values to use when running the cmd or shell_cmd.

env : {
    "PYTHONIOENCODING": "utf-8"
}

quiet

A boolean that reduces the amount of output about the build system invocation.

quiet : true

word_wrap

A boolean that turns on word wrapping in the build system output panel.

word_wrap : true

syntax

A string specifying the syntax file to use to highlight the build system output panel.

syntax : "Packages/JavaScript/JSON.sublime-syntax"

以下的变量值可以在 cmd shell_cmdworking_dir 中使用。

$packages

The path to the Packages/ folder

$platform

A string containing the platform Sublime Text is running on: windows, osx or linux.

$file

The full path, including folder, to the file in the active view.

$file_path

The path to the folder that contains the file in the active view.

$file_name

The file name (sans folder path) of the file in the active view.

$file_base_name

The file name, exluding the extension, of the file in the active view.

$file_extension

The extension of the file name of the file in the active view.

$folder

The full path to the first folder open in the side bar.

$project

The full path to the current project file.

$project_path

The path to the folder containing the current project file.

$project_name

The file name (sans folder path) of the current project file.

$project_base_name

The file name, excluding the extension, of the current project file.

$project_extension

The extension of the current project file.

下一篇《npx : execute npm package binaries》

上一篇《Design in 2018 设计趋势》

永久链接 http://www.shuaihua.cc/article/sublimetext-build-system

快速跳转 心头好文 - efficiency - 《Sublime Text Build System》

发布日期 2018年2月10日 星期六

版权声明 自由转载-非商用-非衍生-保持署名(创意共享3.0许可证