IncludeRepoTask.groovy 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2004-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package io.spring.gradle
  17. import groovy.transform.CompileStatic
  18. import groovy.transform.TypeChecked
  19. import groovy.transform.TypeCheckingMode
  20. import org.gradle.api.DefaultTask
  21. import org.gradle.api.Task
  22. import org.gradle.api.provider.Property
  23. import org.gradle.api.tasks.Input
  24. import org.gradle.api.tasks.OutputDirectory
  25. import org.gradle.api.tasks.TaskAction
  26. /**
  27. * Checkout a project template from a git repository.
  28. *
  29. * @author Marcus Da Coregio
  30. */
  31. @CompileStatic
  32. abstract class IncludeRepoTask extends DefaultTask {
  33. private static final String DEFAULT_URI_PREFIX = 'https://github.com/'
  34. /**
  35. * Git repository to use. Will be prefixed with {@link #DEFAULT_URI_PREFIX} if it isn't already
  36. * @return
  37. */
  38. @Input
  39. abstract Property<String> getRepository();
  40. /**
  41. * Git reference to use.
  42. */
  43. @Input
  44. abstract Property<String> getRef()
  45. /**
  46. * Directory where the project template should be copied.
  47. */
  48. @OutputDirectory
  49. File outputDirectory = project.file("$project.buildDir/$name")
  50. @TaskAction
  51. void checkoutAndCopy() {
  52. outputDirectory.deleteDir()
  53. File checkoutDir = checkout(this, getRemoteUri(), ref.get())
  54. moveToOutputDir(checkoutDir, outputDirectory)
  55. }
  56. private static File cleanTemporaryDir(Task task, File tmpDir) {
  57. if (tmpDir.exists()) {
  58. task.project.delete(tmpDir)
  59. }
  60. return tmpDir
  61. }
  62. static File checkout(Task task, String remoteUri, String ref) {
  63. checkout(task, remoteUri, ref, task.getTemporaryDir())
  64. }
  65. @TypeChecked(TypeCheckingMode.SKIP)
  66. static File checkout(Task task, String remoteUri, String ref, File checkoutDir) {
  67. cleanTemporaryDir(task, checkoutDir)
  68. task.project.exec {
  69. commandLine = ["git", "clone", "--no-checkout", remoteUri, checkoutDir.absolutePath]
  70. errorOutput = System.err
  71. }
  72. task.project.exec {
  73. commandLine = ["git", "checkout", ref]
  74. workingDir = checkoutDir
  75. errorOutput = System.err
  76. }
  77. return checkoutDir
  78. }
  79. private static void moveToOutputDir(File tmpDir, File outputDirectory) {
  80. File baseDir = tmpDir
  81. baseDir.renameTo(outputDirectory)
  82. }
  83. private String getRemoteUri() {
  84. String remoteUri = this.repository.get()
  85. if (remoteUri.startsWith(DEFAULT_URI_PREFIX)) {
  86. return remoteUri
  87. }
  88. return DEFAULT_URI_PREFIX + remoteUri
  89. }
  90. }