2016-08-19 99 views
1

我想自定義默認Visual-Studio Gradle插件任務,以生成一個vcxproj.user文件沿着vcxproj文件中的每個項目的解決方案是一個可執行文件(我想跳過庫)。自定義Gradle Visual Studio插件來生成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>部分)

回答

1

好的,因爲沒有答案,我會回答我自己的問題,因爲我解決了問題,雖然我不是最開心的,因爲它不是很漂亮猜:

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" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
相關問題