简体   繁体   English

在Sublime Text构建系统中使用自定义变量

[英]Use custom variables in Sublime Text build system

I am configuring a Sublime Text (ST) build system on macOS. 我正在macOS上配置Sublime Text(ST)构建系统。 I would like to create a build system variant that outputs the build products to a folder in my user library, but I can't find a way to address this directory. 我想创建一个将构建产品输出到我的用户库中的文件夹的构建系统变体,但是我找不到解决该目录的方法。

The ST Build System documentation mentions the availability of Build System Variables, but none of them allow me to address my home directory. ST Build System文档提到了Build System Variables,但是没有一个允许我寻址主目录。

Is there a way to use custom variables in a ST build system so I can somehow infer the home directory path? 有没有一种方法可以在ST构建系统中使用自定义变量,以便以某种方式推断主目录路径?

Or is there a way to use the $HOME environment variable? 还是有一种使用$ HOME环境变量的方法?

I don't want to hard-code the home directory in the build system, since I want it to be usable by different users. 我不想在构建系统中对主目录进行硬编码,因为我希望它可由其他用户使用。

It is indeed possible to introduce custom variables into a build, but it's a somewhat complicated task in that you have to write a Sublime Plugin to do the job. 确实可以在构建中引入自定义变量,但这是一个有些复杂的任务,因为您必须编写Sublime插件才能完成这项工作。

In particular, there is a key you can include in your sublime-build file named target that tells Sublime what internal command it should execute in order to perform the build. 特别是,您可以在名为sublime-build文件中包含一个名为target的密钥,该密钥告诉Sublime应该执行什么内部命令以执行构建。 When this key is not present (which is most of the time), it defaults to the exec internal command, which is responsible for most of the build process, including capturing and showing the output in the panel, injecting error phantoms as appropriate, and so on. 当此键不存在时(通常是大多数时间),它默认为exec内部命令,该命令负责大多数构建过程,包括捕获和显示面板中的输出,适当地注入错误幻像,以及以此类推。

By creating your own custom WindowCommand instance and including a target in the sublime-build file, Sublime will execute your command instead of the default, which allows you to interpret variables as you see fit. 通过创建您自己的自定义WindowCommand实例并在sublime-build文件中包含一个target ,Sublime将执行您的命令而不是默认命令,从而使您可以按自己的意愿解释变量。

An example of such a build system is available here if you'd like to go this route. 如果您想走这条路线, 可以在这里找到这种构建系统的示例。 In a nutshell it is a custom command that expands variables out and then invokes the internal exec command to actually do the job. 简而言之,它是一个自定义命令,该命令将变量扩展出来,然后调用内部exec命令来实际完成该工作。

With all of this said, you can use environment variables in commands directly if you want, so if your goal is to come up with a folder in a known location that's based on an environment variable, you can do that with no changes at all. 综上所述,您可以根据需要直接在命令中使用环境变量,因此,如果您的目标是在基于环境变量的已知位置创建一个文件夹,则无需做任何更改。

The catch here is that on MacOS and Linux, the $ character is used by the shell to denote a variable, but Sublime uses $ to denote it's own variables as well. 这里的问题是,在MacOS和Linux上,外壳程序使用$字符表示变量,但是Sublime也使用$表示其自己的变量。 Thus you need to perform a bit of "quoting magic" to get things to work. 因此,您需要执行一些“引用魔术”操作。

If you try to use for example $HOME to expand to the home directory, it will expand out to be an empty string. 例如,如果尝试使用$HOME扩展到主目录,它将扩展为空字符串。 This is because when Sublime is getting ready to execute the build it expands all variables; 这是因为,当Sublime准备执行构建时,它会扩展所有变量。 variables whose values it does not know get replaced with an empty string instead. 不知道其值的变量将替换为空字符串。 This isn't a supported build variable, so it's considered empty. 这不是受支持的构建变量,因此被认为是空的。

The solution then is to "quote" the dollar sign; 解决的办法是“引用”美元符号。 \\$ tells Sublime not to treat it specially; \\$告诉Sublime不要特别对待它; that allows it to pass the $ through to the command that is ultimately running the shell. 这样就可以将$传递给最终运行shell的命令。

However if you try that , you get the dreaded "No Build System" error in the status line and nothing happens. 但是,如果尝试这样做 ,则会在状态行中出现可怕的“没有构建系统”错误,并且什么也没有发生。 The reason for this is that the sublime-build file is JSON, and in JSON \\$ is an invalid escape sequence; 这是因为sublime-build文件是JSON,在JSON \\$是无效的转义序列。 thus the build fails to load and Sublime does nothing. 因此构建无法加载,Sublime不执行任何操作。

The solution there is to use \\\\$ ; 解决方案是使用\\\\$ ; that gets interpreted by the JSON loader as being a quoted slash, which converts internally to \\$ , which makes Sublime convert it into $ and pass it through to the command. JSON加载程序将其解释为带引号的斜杠,该斜杠在内部转换为\\$ ,这使得Sublime将其转换为$并将其传递给命令。

An example of this is the following simple build system that displays your home directory in the build panel: 以下是一个简单的构建系统的示例,该系统在构建面板中显示您的主目录:

{
    "shell_cmd": "echo \\$HOME/",
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM