我可以从 VSC 中的 tasks.json 访问其他 json 文件中定义的设置吗?

Can I access settings defined in other json files from tasks.json in VSC?

我想在 VSC 中创建 tasks.json 文件。 我知道如何访问预定义变量、环境变量、配置变量……这些都在 variables reference 页面中得到了很好的解释。 但是我在项目的另一个 json 文件中有一些配置 - 有没有办法访问它们? 目前我只是将我需要的设置复制到 settings.json - 所以我可以像访问任何其他配置变量一样访问它们 - 但这看起来不正确。 这就是我的意思: 我将以下内容添加到 settings.json

"tq.appName" : "App Name",
"tq.appPublisher" : "Pub Name",
"tq.appVersion" : "1111111111",
"tq.targetDockerContainerName": "docker"

然后我可以在我的任务中访问它们:

        "label": "Build APP and replace/publish in docker image",
        "type": "shell",
        "command": "${workspaceFolder}\.vscode\DevelopingDaily.ps1",
        "args": [
            {
                "value": "${config:tq.appName}",
                "quoting": "strong"
            },
            {
                "value": "${config:tq.targetDockerContainerName}",
                "quoting": "strong"
            },
            {
                "value": "${workspaceFolder}\${config:tq.appPublisher}_${config:tq.appName}_${config:tq.appVersion}.app",
                "quoting": "strong"
            }
        ]

但这些值存储在项目的 app.json 文件中 - 而不是 settings.json。我更愿意直接从 app.json 获取值 - 无需将它们复制到设置中...... 可能吗?

您可以使用扩展 Command Variable 命令 extension.commandvariable.file.content

使用 config.json 文件

{
  "log": "foobar.log",
  "server1": {
    "port": 5011
  },
  "server2": {
    "port": 5023
  }
}

在您的 tasks.json 中,您想使用 server1 端口值。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo Server1Port",
      "type": "shell",
      "command": "echo",
      "args": [
        "${input:configServer1Port}"
      ],
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "configServer1Port",
      "type": "command",
      "command": "extension.commandvariable.file.content",
      "args": {
        "fileName": "${workspaceFolder}/config.json",
        "json": "content.server1.port",
        "default": "4321"
      }
    }
  ]
}