在Sublime Text构建系统中使用自定义变量 [英] Use custom variables in Sublime Text build system

查看:156
本文介绍了在Sublime Text构建系统中使用自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在macOS上配置Sublime Text(ST)构建系统。
我想创建一个构建系统变体,将构建产品输出到用户库中的文件夹中,但是我找不到解决此目录的方法。

I am configuring a Sublime Text (ST) build system on macOS. 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.

ST Build System文档提到了Build System Variables的可用性,但是没有一个允许我寻址主目录。

The ST Build System documentation mentions the availability of Build System Variables, but none of them allow me to address my home directory.

是否可以在ST构建中使用自定义变量系统,以便我可以以某种方式推断主目录路径?

Is there a way to use custom variables in a ST build system so I can somehow infer the home directory path?

还是可以使用$ HOME环境变量?

Or is there a way to use the $HOME environment variable?

我不想在构建系统中对主目录进行硬编码,因为我希望它可由其他用户使用。

I don't want to hard-code the home directory in the build system, since I want it to be usable by different users.

推荐答案

确实可以在构建中引入自定义变量,但这是一个相当复杂的任务,因为您必须编写一个Sublime插件来执行

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-build 的文件中包含一个密钥。 c $ c> target 告诉Sublime应该执行什么内部命令以执行构建。如果不存在此密钥(通常是大多数时间),则默认为 exec 内部命令,该命令负责大多数构建过程,包括捕获和显示

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. 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.

通过创建自己的自定义 WindowCommand 实例并在 sublime-build 文件中包含 target ,Sublime将执行您的命令,而不是默认命令,这将允许您可以根据自己的需要解释变量。

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.

这种构建系统的示例是在此处可用,如果您想走这条路线。简而言之,这是一个自定义命令,该命令将变量扩展出来,然后调用内部的 exec 命令来实际完成该工作。

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.

说了这么多,您就可以直接在命令中使用环境变量,因此,如果您的目标是在已知位置基于某个文件夹

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.

这里的问题是在MacOS和Linux上, $ 字符表示变量,但Sublime也使用 $ 表示其自身变量。因此,您需要执行一些引用魔术操作才能使事情正常工作。

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. Thus you need to perform a bit of "quoting magic" to get things to work.

如果尝试使用例如 $ HOME 扩展到主目录,它将扩展为空字符串。这是因为当Sublime准备执行构建时,它会扩展所有变量。不知道其值的变量将替换为空字符串。

If you try to use for example $HOME to expand to the home directory, it will expand out to be an empty string. This is because when Sublime is getting ready to execute the build it expands all variables; 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.

然后的解决方案是引用美元符号;这不是一个受支持的构建变量。 \ $ 告诉Sublime不要特别对待;允许它将 $ 传递给最终运行shell的命令。

The solution then is to "quote" the dollar sign; \$ tells Sublime not to treat it specially; that allows it to pass the $ through to the command that is ultimately running the shell.

但是,如果尝试,您会在状态行中看到可怕的没有构建系统错误,什么也没有发生。原因是 sublime-build 文件是JSON,在JSON中 \ $ 是无效的转义序列;

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; thus the build fails to load and Sublime does nothing.

解决方案是使用 \\ $ ; JSON加载程序将其解释为带引号的斜杠,该斜杠在内部转换为 \ $ ,这使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.

以下是一个简单的构建系统示例,该系统在构建面板中显示您的主目录:

An example of this is the following simple build system that displays your home directory in the build panel:

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

这篇关于在Sublime Text构建系统中使用自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆