1buildscript {
2    ext.addRepos(repositories)
3    dependencies {
4        classpath 'com.google.guava:guava:18.0'
5    }
6}
7import com.google.common.io.Files
8import com.google.common.base.Charsets
9import com.google.common.hash.HashCode
10import com.google.common.hash.HashFunction
11import com.google.common.hash.Hashing
12import java.nio.charset.Charset
13
14
15ext.extraVersion = 22
16ext.supportRepoOut = ''
17ext.buildToolsVersion = '22.1.0'
18ext.buildNumber = Integer.toString(ext.extraVersion)
19
20/*
21 * With the build server you are given two env variables.
22 * The OUT_DIR is a temporary directory you can use to put things during the build.
23 * The DIST_DIR is where you want to save things from the build.
24 *
25 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
26 */
27if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
28    buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
29    project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
30
31    // the build server does not pass the build number so we infer it from the last folder of the dist path.
32    ext.buildNumber = project.ext.distDir.getName()
33} else {
34    buildDir = file('../../out/host/gradle/frameworks/support/build')
35    project.ext.distDir = file('../../out/dist')
36}
37project.ext.distDir.mkdirs()
38ext.supportRepoOut = new File(buildDir, 'support_repo')
39
40// Main task called by the build server.
41task(createArchive) << {
42}
43createArchive.doLast {
44    println "support repo out ${project.ext.supportRepoOut}"
45    println "dist dir ${project.ext.distDir}"
46
47}
48
49// upload anchor for subprojects to upload their artifacts
50// to the local repo.
51task(mainUpload) << {
52}
53
54// repository creation task
55task createRepository(type: Zip, dependsOn: mainUpload) {
56    from project.ext.supportRepoOut
57    destinationDir project.ext.distDir
58    into 'm2repository'
59    baseName = String.format("sdk-repo-linux-m2repository-%s", project.ext.buildNumber)
60}
61createArchive.dependsOn createRepository
62// prepare repository with older versions
63task unzipRepo(type: Copy) {
64    from "${dataBindingConfig.externalPrebuiltsBase}/maven_repo/android"
65    into project.ext.supportRepoOut
66}
67
68unzipRepo.doFirst {
69    project.ext.supportRepoOut.deleteDir()
70    project.ext.supportRepoOut.mkdirs()
71}
72
73// anchor for prepare repo. This is post unzip + sourceProp.
74task(prepareRepo) << {
75}
76
77task(createXml) << {
78    def repoArchive = createRepository.archivePath
79    def repoArchiveName = createRepository.archiveName
80    def size = repoArchive.length()
81    def sha1 = getSha1(repoArchive)
82
83    def xml =
84            "<sdk:sdk-addon xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sdk=\"http://schemas.android.com/sdk/android/addon/6\">\n\
85  <sdk:extra>\n\
86    <sdk:revision>\n\
87      <sdk:major>${project.ext.extraVersion}</sdk:major>\n\
88    </sdk:revision>\n\
89    <sdk:vendor-display>Android</sdk:vendor-display>\n\
90    <sdk:vendor-id>android</sdk:vendor-id>\n\
91    <sdk:name-display>Local Maven repository for Support Libraries</sdk:name-display>\n\
92    <sdk:path>m2repository</sdk:path>\n\
93    <sdk:archives>\n\
94      <sdk:archive>\n\
95       <sdk:size>${size}</sdk:size>\n\
96       <sdk:checksum type=\"sha1\">${sha1}</sdk:checksum>\n\
97       <sdk:url>${repoArchiveName}</sdk:url>\n\
98      </sdk:archive>\n\
99    </sdk:archives>\n\
100  </sdk:extra>\n\
101</sdk:sdk-addon>"
102
103    Files.write(xml, new File(project.ext.distDir, 'repo-extras.xml'), Charsets.UTF_8)
104}
105createArchive.dependsOn createXml
106
107task(createSourceProp) << {
108    def sourceProp =
109            "Extra.VendorDisplay=Android\n\
110Extra.Path=m2repository\n\
111Archive.Arch=ANY\n\
112Extra.NameDisplay=Android Support Repository\n\
113Archive.Os=ANY\n\
114Pkg.Revision=${project.ext.extraVersion}.0.0\n\
115Extra.VendorId=android"
116    Files.write(sourceProp, new File(project.ext.supportRepoOut, 'source.properties'), Charsets.UTF_8)
117}
118createSourceProp.dependsOn unzipRepo
119prepareRepo.dependsOn createSourceProp
120
121
122def getSha1(File inputFile) {
123    HashFunction hashFunction = Hashing.sha1()
124    HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charset.forName("UTF-8"))
125    return hashCode.toString()
126}
127
128task(bundleSupportLib) << {
129
130}
131createRepository.dependsOn prepareRepo
132createRepository.dependsOn unzipRepo
133createRepository.dependsOn bundleSupportLib
134