version.gradle (1711B)
1 // This file is in the public domain. 2 3 // Applied by the root project, independently of the Kotlin build plugins. 4 def git = { List<String> arguments -> 5 def result = providers.exec { 6 workingDir rootDir 7 commandLine(['git'] + arguments) 8 environment = System.getenv().findAll { name, value -> 9 !(name in ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_COMMON_DIR']) 10 } 11 ignoreExitValue = true 12 } 13 if (result.result.get().exitValue != 0) { 14 throw new GradleException("git ${arguments.join(' ')}: ${result.standardError.asText.get().trim()}") 15 } 16 result.standardOutput.asText.get().trim() 17 } 18 19 String resolvedVersion 20 try { 21 // Do not describe an enclosing repository when building a source archive. 22 if (!new File(rootDir, '.git').exists()) { 23 throw new GradleException('source tree has no Git metadata') 24 } 25 if (new File(git(['rev-parse', '--show-toplevel'])).canonicalFile != rootDir.canonicalFile) { 26 throw new GradleException('Git metadata belongs to another source tree') 27 } 28 resolvedVersion = git(['describe', '--tags', '--always', '--abbrev=8']) 29 } catch (Exception gitError) { 30 def stamp = new File(rootDir, '.version') 31 if (!stamp.isFile()) { 32 throw new GradleException( 33 "Cannot determine program version: ${gitError.message}. Source archives must contain ${stamp}", 34 gitError 35 ) 36 } 37 resolvedVersion = stamp.getText('UTF-8').trim() 38 } 39 if (!resolvedVersion || resolvedVersion.contains('\n') || resolvedVersion.contains('\r') || resolvedVersion.contains('\u0000')) { 40 throw new GradleException('Program version must be a nonempty single line') 41 } 42 version = resolvedVersion