首页 > 解决方案 > 选择Git时如何有条件地生成文件?

问题描述

为了使用 Qt Creator 加快我的工作流程,我创建了一个 JSON 格式的自定义项目向导,作为其中的一部分,我添加了一个摘要页面

{
    "trDisplayName": "Project Management",
    "trShortTitle": "Summary",
    "typeId": "Summary"
}

文件生成器中,我将两个文件(即.gitattributes.gitignore)复制到项目目录中:

{
    "source": ".gitattributes",
    "target": "%{TargetPath}/.gitattributes"
},
{
    "source": ".gitignore",
    "target": "%{TargetPath}/.gitignore"
}

像这样写的,文件总是被复制的,即使没有选择 Git 作为版本控制系统。我希望仅在需要时才复制文件。

摘要页面的文档说:

它将 VersionControl 设置为正在使用的版本控制系统的 ID

但没有添加有关VersionControl变量及其值的更多信息。

如何达到预期的效果?

标签: qt-creatorwizard

解决方案


我会建议你以下解决方案:

  1. Wizard.Inspect通过转到为命令分配键盘快捷键

    工具->选项->环境->键盘->快捷键

  2. 启动自定义向导并导航到​​摘要页面

  3. 按选定的快捷方式

    这将打开一个窗口,其中列出了触发操作时向导中所有已定义的字段和变量。在那里你可以找到:

    Key            | Type    | Value | Eval
    VersionControl | QString | G.Git | G.Git
    
  4. 有了这些信息,像这样更改文件生成器代码:

    {
        "source": ".gitattributes",
        "target": "%{TargetPath}/.gitattributes",
        "condition": "%{JS: '%{VersionControl}' === 'G.Git'}"
    },
    {
        "source": ".gitignore",
        "target": "%{TargetPath}/.gitignore",
        "condition": "%{JS: '%{VersionControl}' === 'G.Git'}"
    }
    

推荐阅读