【发布时间】:2017-02-02 07:42:51
【问题描述】:
在 VS 代码中,您可以使用 php 可执行文件来验证 php。但是有没有办法使用安装在 WSL 上的 php 代替?
【问题讨论】:
标签: visual-studio-code windows-subsystem-for-linux
在 VS 代码中,您可以使用 php 可执行文件来验证 php。但是有没有办法使用安装在 WSL 上的 php 代替?
【问题讨论】:
标签: visual-studio-code windows-subsystem-for-linux
什么对我有用:
创建 php.bat 文件:
@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
@bash -c "php %v_params%"
把它放在C:\WSL-Tools\php.bat
然后,在 VScode 设置中(Ctrl+逗号):
"php.validate.executablePath": "C:\\WSL-Tools\\php.bat"
它有效。
【讨论】:
另一个答案对我也不起作用:经过一些工作,我想出了这两个脚本:
这个叫php.bat,我放在C:\wsl-tools\:
@echo OFF
setlocal ENABLEDELAYEDEXPANSION
rem Collect the arguments and replace:
rem '\' with '/'
rem 'c:' with 'mnt/c'
rem '"' with '\"'
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:C:=/mnt/c%
set v_params=%v_params%
set v_params=%v_params:"=\"%
rem Call the windows-php inside WSL.
rem windows-php is just a script which passes the arguments onto
rem the original php executable and converts its output from UNIX
rem syntax to Windows syntax.
C:\Windows\sysnative\bash.exe -l -c "windows-php %v_params%"
这个叫做 windows-php,放在 WSL 路径的某个地方(我选择了/usr/local/bin)。
# Pass all the arguments to PHP.
output=$(php "$@")
# Perform UNIX->WINDOWS syntax replacements.
output="${output//$'\n'/$'\r'$'\n'}"
output="${output//\/mnt\/c/C:}"
output="${output//\//\\}"
# Echo corrected output.
echo $output
设置 "php.validate.executablePath": "c:\\wsl-tools\\php.bat" 对我有用。
注意:
您可能想要关注this issue 和this pull request,因为看起来这个问题将在下一个版本中得到正式解决。
【讨论】:
C:\Windows\system32\wsl.exe "windows-php %v_params%" 因为 bash.exe 已被弃用,这是新的方法
使用老式批处理文件可以做到这一点。
php.bat
@echo off
c:\windows\sysnative\bash.exe -c "php %*"`
setting.json
"php.validate.executablePath": "c:\\PATH_TO\\php.bat"
【讨论】:
bash.exe -c "php %*" 而不是:c:\windows\sysnative\bash.exe -c "php %*"`
如果我还不算太晚,这就是使用 ubuntu 20.04 WSL 2 的方法
@echo OFF
setlocal ENABLEDELAYEDEXPANSION
rem Collect the arguments and replace:
rem '\' with '/'
rem 'c:' with 'mnt/c'
rem '"' with '\"'
set v_params=%*
IF DEFINED v_params (
set v_params=%v_params:\=/%
set v_params=%v_params:C:=/mnt/c%
set v_params=%v_params%
set v_params=%v_params:"=\"%
)
rem Call the windows-php inside WSL.
rem windows-php is just a script which passes the arguments onto
rem the original php executable and converts its output from UNIX
rem syntax to Windows syntax.
@wsl.exe -d Ubuntu-20.04 -- php %v_params%
通过将这个 php.bat 放入
c:/wsl-plugins/
你不需要做 WSL 位。
【讨论】: