如何创建读取shebang的Sublime Text 3构建系统 [英] How to create Sublime Text 3 build system which reads shebang

查看:39
本文介绍了如何创建读取shebang的Sublime Text 3构建系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Sublime Text 3 中创建一个构建系统,其中 "cmd" 被替换为存在的shebang?

How can I create a build system in Sublime Text 3 where "cmd" is replaced with a shebang if it exists?

更具体地说,有没有办法改变 Python 构建系统以使用 shebang 中指定的 Python 版本,并在不存在 shebang 时使用默认值?

More specifically, is there a way to alter the Python build system to use the version of Python specified in the shebang, and use a default if no shebang is present?

推荐答案

Sublime 构建系统有一个名为 target 的选项,它指定了要调用以执行的 WindowCommand构建.默认情况下,这是内部 exec 命令.您可以创建自己的命令来检查文件中是否存在 shebang 并使用该解释器或其他一些默认解释器.

Sublime build systems have an option named target which specifies a WindowCommand that is to be invoked to perform the build. By default this is the internal exec command. You can create your own command that would examine the file for a shebang and use that interpreter or some default otherwise.

例如(警告:我不是非常精通 Python,所以这可能很丑陋):

For example (caveat: I'm not super proficient in Python so this is probably quite ugly):

import sublime, sublime_plugin

class ShebangerCommand(sublime_plugin.WindowCommand):
    def parseShebang (self, filename):
        with open(filename, 'r') as handle:
            shebang = handle.readline ().strip ().split (' ', 1)[0]
        if shebang.startswith ("#!"):
            return shebang[2:]
        return None

    def createExecDict(self, sourceDict):
        current_file = self.window.active_view ().file_name()
        args = dict (sourceDict)

        interpreter = args.pop ("interpreter_default", "python")
        exec_args = args.pop ("interpreter_args", ["-u"])
        shebang = self.parseShebang (current_file)

        args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter,
                                                   " ".join (exec_args),
                                                   current_file)

        return args

    def run(self, **kwargs):
        self.window.run_command ("exec", self.createExecDict (kwargs))

您可以将其保存在 Packages/User 中作为 python 文件(例如 shebanger.py).

You would save this in Packages/User as a python file (e.g. shebanger.py).

这将创建一个名为 shebanger 的新命令,该命令收集给定的参数,检查触发构建的窗口的当前活动视图中的文件,以查看第一行是否为 shebang,然后合成 exec 命令所需的参数并运行它.

This creates a new command named shebanger that collects the arguments it's been given, examines the file in the currently active view of the window the build is triggered in to see if the first line is a shebang, and then synthesizes the arguments needed for the exec command and runs it.

由于默认的 python 构建系统假定它正在构建当前文件并将 -u 作为参数传递,这也是该命令复制的内容.但是请注意,此代码并非 100% 正确,因为将忽略 shebang 行中的任何参数,但您已大致了解.

Since the default python build system assumes it is building the current file and passing -u as an argument, that's what this command replicates as well. Note however that this code is not 100% correct because any arguments in the shebang line will be ignored, but you get the general idea.

在使用中,您将修改默认的 Python.sublime-build 文件,使其看起来像这样:

In use, you would modify the default Python.sublime-build file to look like this:

{
    // WindowCommand to execute for this build
    "target": "shebanger",

    // Use this when there is no shebang
    "interpreter_default": "python",

    // Args to pass to the interpreter
    "interpreter_args": ["-u"],

    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

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

    "variants":
    [
        {
            "name": "Syntax Check",
            "interpreter_args": ["-m py_compile"],
        }
    ]
}

请注意,在变体中,我们覆盖了解释器参数;如果需要,您也可以覆盖那里的默认解释器.

Notice that in the variant we override what the interpreter arguments are; you could also override the default interpreter there as well if desired.

这篇关于如何创建读取shebang的Sublime Text 3构建系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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