我试图自定义默认的Visual Studio Studio Gradle插件任务,以在vcxproj文件中为可执行的解决方案中的每个项目生成vcxproj.user文件(我想跳过库)。

这个想法是要找到所有类型为GenerateProjectFileTask的任务,以某种方式仅过滤那些代表可执行二进制文件的任务(这部分我还不知道如何执行)和finalizedBy子句以使它们运行我的任务,该任务还将生成.user文件。

我需要这个,因为我的项目有一个自定义的Working Directory路径,并且每次添加新的源文件等时都必须重新生成该项目。显然,这样做(清理后)后,我必须为每个可执行文件再次设置这些路径。这很烦人。

到目前为止我一直想做的是

subprojects {
    // every proejct with plugin cpp also uses visual-studio plugin
    plugins.withId('cpp') {
        afterEvaluate {
            tasks.withType(GenerateProjectFileTask) { task ->
                def project = task.getVisualStudioProject()
                if(project != null) {
                    println project.projectFile.location
               }
               else {
                   println "Project is null"
               }
               //println task.getOutputFile().getPath() <- this didnt work either
               task.finalizedBy "someGeneratedTaskForEachProjectToCreateUserProperties"
            }
        }
    }
}

但是它总是打印Project is null,我不明白为什么。我需要知道在哪里生成文件的路径。

我也不喜欢finalizedBy子句,因为无论终结任务是否失败,它总是运行。有没有更好的解决方案来解决这个问题?

简而言之,我要实现的任务是:例如运行一个任务:gradlew demoAppVisualStudio,它不仅应生成解决方案和所有必要的项目,而且还应为代表可执行文件的项目运行我生成的任务,以创建包含我将准备的内容的附加vcxproj.user文件。 (主要是<LocalDebuggerWorkingDirectory>部分)

最佳答案

好的,因为没有答案,我将回答我自己的问题,因为尽管我对问题不是很满意,但我对它的回答不是很满意,因为我认为它不是很漂亮:

subprojects { subproj ->
  plugins.withId('cpp') {
    model {
      components {
        withType(NativeExecutableSpec) { c ->
          subproj.tasks.whenTaskAdded {
            if(it.name == "${c.name}VisualStudio") {

              it.dependsOn task("${c.name}_${c.name}VisualStudioUserProperties", type:Task) {

                def projectTask = tasks["${c.name}_${c.name}ExeVisualStudioProject"]
                def path = projectTask.outputs.files.singleFile.parentFile.absolutePath
                def outputPath = "${path}/${c.name}_${c.name}Exe.vcxproj.user"

                inputs.file(file("data/vsDebuggerWorkingDirectory.xml"))
                outputs.file(outputPath)

                doLast {
                  new File(outputPath).write(("${inputs.files.singleFile.text}"))
                }
                mustRunAfter "${c.name}_${c.name}ExeVisualStudioProject"
              }
            }
          }
        }
      }
    }
  }
}

10-07 20:08
查看更多