【问题标题】:How to create Sublime Text 3 build system which reads shebang如何创建读取 shebang 的 Sublime Text 3 构建系统
【发布时间】:2016-09-21 01:38:47
【问题描述】:

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

更具体地说,有没有办法改变 Python 构建系统以使用 shebang 中指定的 Python 版本,如果没有 shebang,则使用默认版本?

【问题讨论】:

    标签: sublimetext3 shebang build-system


    【解决方案1】:

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

    例如(警告:我对 Python 不是很精通,所以这可能很难看):

    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)。

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

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

    在使用中,您可以将默认的Python.sublime-build 文件修改为如下所示:

    {
        // 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"],
            }
        ]
    }
    

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

    【讨论】:

    • 这看起来很有希望——我会告诉你进展如何。谢谢!
    【解决方案2】:

    如果认为使用标准 .sublime-build 文件执行此操作的唯一方法是将文件传递给另一个脚本,然后该脚本会解析 shebang 并将其传递给正确的 Python 版本。

    或者,您可以指定build variants,但您必须手动选择所需的构建变体。

    【讨论】:

      【解决方案3】:

      我的 python.sublime-build

      {
          "cmd": ["py", "-u", "$file"],
          "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
          "selector": "source.python",
          "shell":true
      }
      

      在windows中我使用py启动器根据shebang检测版本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-21
        • 2015-03-01
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        • 2018-04-27
        相关资源
        最近更新 更多