【问题标题】:How to use a relative executable to build a file on windows?如何使用相对可执行文件在 Windows 上构建文件?
【发布时间】:2013-09-11 15:42:30
【问题描述】:

我有一个随身碟:

  • Sublime Text 2 可移植到 Windows(32 位和 64 位)
  • 便携式 Python 安装
  • Android ADT 等更多内容

目前我有一个批处理文件,它为 cmd 会话“设置”环境变量,然后我从 cmd 运行这些文件。现在显然这不是很理想,也不是很有效。

如何使用同一笔式驱动器上的 python 可执行文件在 Sublime Text 2 中构建/运行文件,比如 .py 文件?


更新:
尝试失败
F:\foo\SublimeText2_x64\Data\Packages\Build Systems\My Python.sublime-build

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

这就是我最终得到的结果:

{
    "working_dir": "${packages}\\Build Systems",
    "cmd": ["build.bat", "python", "$file_path", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

build.bat(我打算为此添加更多语言,使其成为多语言构建文件)

@echo off

if "%1"=="" call :usage 0 & goto end
if "%2"=="" call :usage 1 & goto end
if "%3"=="" call :usage 2 & goto end

set my_dir=%~dp0\..\..\..\..

::------------------------- Setting Up the Environment -------------------------
:: Python
SET PATH=^
%my_dir%\PortablePython2.7.5.1\App;^
%my_dir%\PortablePython2.7.5.1\App\Scripts;%PATH%

:: Git
SET PATH=%my_dir%\Git\cmd;%PATH%
::------------------------------------------------------------------------------

pushd %2 :: Goto to the directory with the file
python -u %3 & goto end


:: ----------------------------------- Usage -----------------------------------
:usage
echo Error: Need 3 arguments (got %1)
echo Usage:
echo    build.bat language file_path file
echo.
echo Arguments:
echo   language    : The language to build the file with
echo                 Supported languages - python
echo   file_path   : Path to the file
echo   file        : Name of the file (preferably with path)
goto end
::------------------------------------------------------------------------------

:end
popd

【问题讨论】:

    标签: python sublimetext2 build-automation


    【解决方案1】:

    语法错误,你应该创建一个从sublime接收两个参数的.bat文件并在.bat文件中进行所有处理。

    F:\foo\SublimeText2_x64\Data\Packages\Build Systems\My Python.sublime-build 的源代码

    {
    "working_dir": "path\\to\\your\\batch\\file",
    "cmd": ["build-py.bat", "$file_path", "$file"]
    }
    

    批处理文件的来源

    @echo off
    setupenv
    cd %1
    python -u %2
    

    【讨论】:

    • 很抱歉,我真的无法理解问题所在。在我在这个答案中绘制的构建文件的 cmd 属性中,您可以将任意数量的参数传递给批处理文件。
    • 我还有一个问题:语言参数从何而来?我认为您需要为不同的语言创建 5 个不同的构建文件,并对每个文件中的特定语言参数进行硬编码。例如,您将有一个 cmd 属性设置为 ["build.bat", "python", "$file_path", "$file"] 的文件和另一个 cmd 属性设置为 ["build .bat", "gcc", "$file_path", "$file"].
    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2012-01-04
    • 2014-04-21
    相关资源
    最近更新 更多