12 篇文章 0 订阅

# Build Systems

Build systems let you run your files through external programs and see the output they generate within Sublime Text.

Build systems consist of two –or optionally three– parts:

• configuration data in JSON format (the .sublime-build file contents)
• a Sublime Text command driving the build process
• optionally, an external executable file (script, binary file)

• 使用JSON格式保存配置文件 (.sublime-build 内容)
• 使用Sublime Text命令来驱动构建过程
• 还包括一个外部的可执行程序(脚本或者二进制)

Essentially, .sublime-build files are configuration data for an external program as well as for the Sublime Text command just mentioned. In them, you specify the switches, options and environment information you want forwarded.

The Sublime Text command then receives the data stored in the .sublime-build file. At this point, it can do whatever it needs to build the files. By default, build systems will use the exec command, implemented in Packages/Default/exec.py. As we’ll explain below, you can override this command.

Sublime Text命令从 .sublime-build 中读取配置数据，然后根据需要*构建*这些文件。 构建系统缺省会使用exec 命令，该命令在 Packages/Default/exec.py 中实现。 在后续的讲解中，我们会重写这个命令。

Lastly, the external program may be a shell script you’ve created to process your files, or a well-known utility like make or tidy. Usually, these executable files will receive paths to files or directories, along with switches and options to be run with.

Note that build systems need not call any external program at all if there isn’t any reason to; you could implement a build system entirely in a Sublime Text command.

## 文件格式

.构建系统 文件使用JSON. 以下是一个例子:

{
"cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }  ## 选项 cmd Array containing the command to run and its desired arguments. If you don’t specify an absolute path, the external program will be searched in your PATH, one of your system’s environmental variables. cmd 包括命令及其参数数组。如果不指定绝对路径，外部程序会在你系统的:const:PATH 环境变量中搜索。 On Windows, GUIs are supressed. 在Windows 系统中，*TBT* file_regex Optional. Regular expression (Perl-style) to capture error output of cmd. See the next section for details. file_regex 可选。 Perl格式的正则表达式可以获取cmd的错误输出，详情参考下一节 line_regex Optional. If file_regex doesn’t match on the current line, but line_regex exists, and it does match on the current line, then walk backwards through the buffer until a line matching file regex is found, and use these two matches to determine the file and line to go to. line_regex 可选。当file_regex与该行不匹配，如果line_regex存在，并且确实与当前行匹配， 则遍历整个缓冲区，直到与file regex匹配的行出现，并用这两个匹配决定最终要跳转的文件 或行。 selector Optional. Used when Tools | Build System | Automatic is set to true. Sublime Text uses this scope selector to find the appropriate build system for the active view. selector 可选。在选定 Tools | Build System | Automatic 时使用。Sublime Text使用这个 选择器自动为活动试图选择构建系统。 working_dir Optional. Directory to change the current directory to before running cmd. The original current directory is restored afterwards. working_dir 可选。在运行cmd前会切换到该目录。运行结束后会切换到原来的目录。 encoding Optional. Output encoding of cmd. Must be a valid python encoding. Defaults to UTF-8. encoding 可选。输出cmd的编码。必须是合法的Python编码，缺省为UTF-8。 target Optional. Sublime Text command to run. Defaults to exec (Packages/Default/exec.py). This command receives the configuration data specified in the .build-system file. Used to override the default build system command. Note that if you choose to override the default command for build systems, you can add arbitrary variables in the .sublime-build file. target 可选。运行的Sublime Text命令，缺省为exec (Packages/Default/exec.py)。该命令从 *.build-system*中获取配置数据。 用来替代缺省的构建系统命令。注意，如果你希望替代构建系统的缺省命令，请在*.sublime-build* 文件中专门设置。 env Optional. Dictionary of environment variables to be merged with the current process’ before passing them to cmd. Use this element, for example, to add or modify environment variables without modifying your system’s settings. env 可选。在环境变量被传递给cmd前，将他们封装成词典。 shell Optional. If truecmd will be run through the shell ( cmd.exebash…). shell 可选。如果该选项为true ， cmd则可以通过shell运行。 path Optional. This string will replace the current process’ PATH before calling cmd. The old PATHvalue will be restored after that. Use this option to add directories to PATH without having to modify your system’s settings. path 可选。该选项可以在调用cmd前替换当前进程的’ PATH 。原来的’ PATH 将在运行后恢复。 使用这个选项可以在不修改系统设置的前提下将目录添加到’ PATH 中。 variants Optional. A list of dictionaries of options to override the main build system’s options. Variant names will appear in the Command Palette for easy access if the build system’s selector matches for the active file. variants 可选。用来替代主构建系统的备选。如果构建系统的选择器与激活的文件匹配，变量的名称则 会出现在 Command Palette 中。 name Only valid inside a variant (see variants). Identifies variant build systems. If name is Run, the variant will show up under the Tools | Build System menu and be bound to Ctrl + Shift + B. name 仅在variant中是合法的 (详见 variants)。用来标识系统中不同的构建系统。如果 name是*Run* ,则会显示在**Tools | Build System** 下，并且可以使用 *Ctrl + Shift + B*调用。 ### 使用 file_regex获取错误输出 The file_regex option uses a Perl-style regular expression to capture up to four fields of error information from the build program’s output, namely: file nameline numbercolumn number and error message. Use groups in the pattern to capture this information. The file name field and the line number field are required. file_regex选项用Perl的正则表达式来捕获构建系统的错误输出，主要包括四部分内容，分别是 file name*, line numbercolumn number and error message. Sublime Text 在匹配模式中使用分组的方式捕获信息。file name 和 *line number*域是必须的。 When error information is captured, you can navigate to error instances in your project’s files with F4 and Shift+F4. If available, the captured error message will be displayed in the status bar. 当错误信息被捕获时，你可以使用F4 和 Shift+F4在你的项目文件中跳转。被捕获的*错误 信息*会显示在状态栏。 ### 平台相关选项 The windowsosx and linux elements let you provide platform-specific data in the build system. Here’s an example: windows, osx 以及 linux元素可以帮助你在构建系统中设定平台相关  的选项，举例如下： { “cmd”: [“ant”], “file_regex”: “^ $javac$ (.+):([0-9]+):() (.)$”, “working_dir”: “${project_path:${folder}}”, “selector”: “source.java”,

“windows”: {

“cmd”: [“ant.bat”]

}

}

In this case, ant will be executed for every platform except Windows, where ant.bat will be used instead.

### 构建系统备选项

{
"selector": "source.python",
"cmd": ["date"],

"variants": [

{ "cmd": ["ls -l *.py"],
"name": "List Python Files",
"shell": true
},

{ "cmd": ["wc", "$file"], "name": "Word Count (current file)" }, { "cmd": ["python", "-u", "$file"],
"name": "Run"
}
]
}


Given these settings, Ctrl + B would run the date command, Crtl + Shift + B would run the Python interpreter and the remaining variants would appear in the Command Palette whenever the build system was active.

## 构建系统变量

Build systems expand the following variables in .sublime-build files:

 $file_path 当前文件所在路径, 比如 C:\Files. $file 当前文件的完整路径, 比如 C:\Files\Chapter1.txt. $file_name 当前文件的文件名, 比如 Chapter1.txt. $file_extension 当前文件的扩展名, 比如 txt. $file_base_name 当前文件仅包含文件名的部分, 比如 Document. $packages Packages 文件夹的完整路径. $project 当前项目文件的完整路径. $project_path 当前项目文件的路径. $project_name 当前项目文件的名称. $project_extension 当前项目文件的扩展部分. $project_base_name 当前项目仅包括名的部分. ### 变量用法 Features found in snippets can be used with these variables. For example: 可以在代码片段上中使用以上变量。例如: ${project_name:Default}


This will emit the name of the current project if there is one, otherwise Default.

\${file/\.php/\.txt/}


This will emit the full path of the current file, replacing .php with .txt.

## 运行构建系统

Select the desired build system from Tools | Build System, and then select Tools | Build or press F7.

## 构建系统常见问题

Build systems will look for executables in your PATH, unless you specify an absolute path to the executable. Therefore, your PATH variable must be correctly set.

On some operating systems, the value for PATH will vary from a terminal window to a graphical application. Thus, even if the command you are using in your build system works in the command line, it may not work from Sublime Text. This is due to user profiles in shells.

To solve this issue, make sure you set the desired PATH so that graphical applications such as Sublime Text can find it. See the links below for more information.

Alternatively, you can use the path element in .sublime-build files to override the PATH used to locate the executable specified in cmd. This new value for PATH will only be in effect for as long as your build system is running. After that, the old PATH will be restored.

PATH

• 6
点赞
• 0
评论
• 4
收藏
• 一键三连
• 扫一扫，分享海报

05-26 827
03-31 927
09-04
09-17 817
04-26 812
04-06 448
10-07 3164
12-15 1440