如何在VSC IDE(版本2.0.0)中配置任务以启动.ps1脚本?

4

我想使用Visual Studio Code IDE("VSC")来开发MQL(而不是本地的MetaEditor IDE),如此描述:在Visual Studio中编写和编译MQL5的方法

我的问题涉及编译过程,其中包括一个调用PowerShell脚本的VSC任务,该脚本反过来会调用MetaEditor.exe,并动态地将当前的.mq5文件传递给它进行编译(这就是为什么需要任务的原因)。

当我直接运行PowerShell脚本时(选择其代码并按F8),一切正常,但是当我尝试通过指定的VSC任务运行它时,我会收到以下错误信息...

终端进程以退出码1终止

...即使我已经选择了PowerShell作为VSC要使用的默认shell(而不是cmd,这是我的相应设置:"terminal.integrated.shell.windows": "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")。

这是我所说的版本2.0.0的.json格式的VSC任务:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile-MQL",
            "type": "shell",
            "command": "C:\\Users\\Username\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\MQL5\\Compile-MQL.ps1 ${file}",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

请有人将我上述的VSC任务适应,以使其能够立即使用。@postanote:请不要再次复制粘贴类似问题的答案,因为我很遗憾不能将版本0.1.0转换为2.0.0(或任何其他偏差),我相信有人可以快速调整我的几行代码,以便它们立即正常工作... 非常感谢! PS:这是上述PowerShell脚本(可使用F8):
#gets the File To Compile as an external parameter... Defaults to a Test file...
Param($FileToCompile = "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Experts\Advisors\ExpertMACD.mq5")

#cleans the terminal screen and sets the log file name...
Clear-Host
$LogFile = $FileToCompile + ".log"
& "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\compile.bat" "C:\Program Files\MetaTrader 5\metaeditor64.exe" "$FileToCompile" "$LogFile" "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5"

#before continue check if the Compile File has any spaces in it...
if ($FileToCompile.Contains(" ")) {
    "";"";
    Write-Host "ERROR!  Impossible to Compile! Your Filename or Path contains SPACES!" -ForegroundColor Red;
    "";
    Write-Host $FileToCompile -ForegroundColor Red;
    "";"";
    return;
}

#first of all, kill MT Terminal (if running)... otherwise it will not see the new compiled version of the code...
Get-Process -Name terminal64 -ErrorAction SilentlyContinue |
    Where-Object {$_.Id -gt 0} |
    Stop-Process

#fires up the Metaeditor compiler...
& "C:\Program Files\MetaTrader 5\metaeditor64.exe" /compile:"$FileToCompile" /log:"$LogFile" /inc:"C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5" | Out-Null

#get some clean real state and tells the user what is being compiled (just the file name, no path)...
"";"";"";"";""
$JustTheFileName = Split-Path $FileToCompile -Leaf
Write-Host "Compiling........: $JustTheFileName"
""

#reads the log file. Eliminates the blank lines. Skip the first line because it is useless.
$Log = Get-Content -Path $LogFile |
       Where-Object {$_ -ne ""} |
       Select-Object -Skip 1

#Green color for successful Compilation. Otherwise (error/warning), Red!
$WhichColor = "Red"
$Log | ForEach-Object {
    if ($_.Contains("0 error(s), 0 warning(s)")) {
        $WhichColor="Green"
    }
}

#runs through all the log lines...
$Log | ForEach-Object {
     #ignores the ": information: error generating code" line when ME was successful
     if (-not $_.Contains("information:")) {
          #common log line... just print it...
          Write-Host $_ -ForegroundColor $WhichColor
     }
}

#get the MT Terminal back if all went well...
if ($WhichColor -eq "Green") {
    & "c:\program files\metatrader 5\terminal64.exe"
}

附注:MetaEditor IDE可以与MetaTrader 5一起免费安装

1个回答

2

同时,我已经自己找到了原因:我的默认Windows Powershell安全设置阻止了我的.ps1脚本的执行。解决办法是以管理员身份运行PowerShell(Win+x→Windows PowerShell(管理员)),并运行命令。

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

→ 确认。

对于有兴趣的读者,以下是一些背景信息:

关于执行策略

设置执行策略


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接