【问题标题】:How to configure a task to start a .ps1-script in VSC IDE (version 2.0.0)?如何配置任务以在 VSC IDE(版本 2.0.0)中启动 .ps1 脚本?
【发布时间】:2019-05-07 15:45:28
【问题描述】:

我想使用 Visual Studio Code IDE(“VSC”)在 MQL(而不是本机 MetaEditor IDE)中进行开发,如下所述:How to code & compile MQL5 in Visual Studio

我的问题是关于编译过程,它由一个 VSC 任务组成,该任务调用一个 PowerShell 脚本,该脚本又调用 MetaEditor.exe 并且 动态 将当前的 .mq5 文件传递​​给它已编译(这就是使用任务的原因)。

当我直接运行 PowerShell 脚本时一切正常(通过选择其代码并点击 F8),但是当我尝试通过指定的 VSC 任务运行它时,我得到了错误...

终端进程以退出代码终止:1

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

这是 .json 格式的 VSC 任务,版本 2.0.0,我正在谈论:

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

PS2:MetaEditor IDE 可以是installed for free together with MetaTrader 5 here

【问题讨论】:

    标签: json powershell visual-studio-code mql


    【解决方案1】:

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

    Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
    

    →确认。

    这里有一些背景信息供感兴趣的读者参考:

    About Execution Policies

    Set Execution Policy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多