File: versioning.gradle

package info (click to toggle)
gradle 4.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 85,104 kB
  • sloc: java: 319,724; xml: 14,356; cpp: 4,200; ansic: 1,025; sh: 300; makefile: 60; asm: 17; jsp: 17; objc: 10
file content (153 lines) | stat: -rw-r--r-- 5,630 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import org.gradle.build.BuildReceipt
import org.gradle.internal.os.OperatingSystem

if (buildTypes.promotionBuild.active) {
    logger.lifecycle "Invocation tasks: $gradle.startParameter.taskNames\nInvocation properties: $gradle.startParameter.projectProperties"
}

ext.milestoneNumber = project.hasProperty("milestoneNumber") ? project.milestoneNumber.toInteger() : null
ext.rcNumber = project.hasProperty("rcNumber") ? project.rcNumber.toInteger() : null
ext.finalRelease = project.hasProperty("finalRelease")
if ((milestoneNumber != null && rcNumber != null) ||
    (rcNumber != null && finalRelease) ||
    (milestoneNumber != null && finalRelease)) {
    throw new InvalidUserDataException("Cannot set any combination of milestoneNumber, rcNumber and finalRelease at the same time")
}

ext.baseVersion = rootProject.file("version.txt").text.trim()

File incomingBuildReceiptDir = file("incoming-distributions")
if (!project.hasProperty("ignoreIncomingBuildReceipt") && new File(incomingBuildReceiptDir, BuildReceipt.BUILD_RECEIPT_FILE_NAME).exists()) {
    def incomingDistributionsBuildReceipt = BuildReceipt.readBuildReceipt(incomingBuildReceiptDir)
    ext.buildTimestamp = incomingDistributionsBuildReceipt.buildTimestamp
    println "Using timestamp from incoming build receipt: $buildTimestamp"
} else {
    def timestampFormat = new java.text.SimpleDateFormat('yyyyMMddHHmmssZ')
    timestampFormat.timeZone = TimeZone.getTimeZone("UTC")
    Date buildTime
    if (project.hasProperty("buildTimestamp")) {
        buildTime = timestampFormat.parse(buildTimestamp)
    } else {
        if (isCiServer || ['install', 'installAll'].collectMany { [':' + it, it]}.any { gradle.startParameter.taskNames.contains(it) }) {
            buildTime = new Date()
        } else {
            def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date dateWithoutTime = sdf.parse(sdf.format(new Date()))
            buildTime = dateWithoutTime
        }
    }
    ext.buildTimestamp = timestampFormat.format(buildTime)
}

task determineCommitId {
    ext.commitId = null

    doLast {
        def strategies = []

        def env = System.getenv()

        // For promotion builds use the commitId passed in as a project property
        strategies << {
            project.hasProperty('promotionCommitId') ? project.property('promotionCommitId') : null
        }

        // Builds of Gradle happening on the CI server
        strategies << {
            env["BUILD_VCS_NUMBER"]
        }

        // For the discovery builds, this points to the Gradle revision
        strategies << {
            env.find { it.key.startsWith("BUILD_VCS_NUMBER_Gradle_Master") }?.value
        }

        // For the discovery release builds, this points to the Gradle revision
        strategies << {
            env.find { it.key.startsWith("BUILD_VCS_NUMBER_Gradle_release_branch") }?.value
        }

        // If it's a checkout, ask Git for it
        strategies << {
            def gitDir = file("${rootDir}/.git")
            if (gitDir.exists()) {
                def baos = new ByteArrayOutputStream()
                def execResult = exec {
                    workingDir = rootDir
                    ignoreExitValue = true
                    commandLine = ["git", "log", "-1", "--format=%H"]
                    if (OperatingSystem.current().windows) {
                        commandLine = ["cmd", "/c"] + commandLine
                    }

                    standardOutput = baos
                }
                if (execResult.exitValue == 0) {
                    new String(baos.toByteArray(), "utf8").trim()
                } else if (file("${gitDir}/HEAD").exists()) {
                    // Read commit id directly from filesystem
                    def headRef = file("${gitDir}/HEAD").text
                    headRef = headRef.replaceAll('ref: ', '').trim()
                    file("${gitDir}/$headRef").text.trim()
                }
            } else {
                null
            }
        }

        // It's a source distribution, we don't know.
        strategies << {
            if (!file("design-docs").directory) {
                "<unknown>"
            }
        }

        for (strategy in strategies) {
            commitId = strategy()
            if (commitId) {
                break
            }
        }
        if (!commitId) {
            throw new InvalidUserDataException("Could not determine commit id")
        }
    }
}

task configureBuildReceipt {
    doLast {
        createBuildReceipt.versionNumber = rootProject.version
        createBuildReceipt.baseVersion = rootProject.baseVersion
        createBuildReceipt.snapshot = rootProject.isSnapshot
        createBuildReceipt.buildTimestamp = '20121221000000+0000'
        createBuildReceipt.commitId = 'none'
    }
}

task createBuildReceipt(type: org.gradle.build.BuildReceipt) {
    dependsOn configureBuildReceipt
    destinationDir = file("${rootProject.buildDir}")
}

ext.buildReceiptPackage = '/org/gradle/'

version = baseVersion

ext.isSnapshot = false
if (finalRelease) {
    // use version base
} else if (rcNumber != null) {
    version += "-rc-$rcNumber"
} else if (milestoneNumber != null) {
    version += "-milestone-$milestoneNumber"
} else {
    isSnapshot = true
    version += "-$buildTimestamp"
}

if (buildTypes.promotionBuild.active) {
    logger.lifecycle "Version: $version (base version: $baseVersion, timestamp: $buildTimestamp, snapshot: $isSnapshot)"
    if (isCiServer) {
        logger.lifecycle "##teamcity[buildStatus text='{build.status.text}, Promoted version ${version}']"
    }
}