188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar/*
288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Copyright (C) 2017 The Android Open Source Project
388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * you may not use this file except in compliance with the License.
688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * You may obtain a copy of the License at
788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
1088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Unless required by applicable law or agreed to in writing, software
1188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
1288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * See the License for the specific language governing permissions and
1488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * limitations under the License.
1588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar */
1676542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikas// upload anchor for subprojects to upload their artifacts to the local repo.
1788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyartask(mainUpload)
1888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
1976542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikastask createArchive(type : Zip) {
2078e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    description "Creates a maven repository that includes just the libraries compiled in this" +
2178e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar            " project, without any history from prebuilts."
22279780d41883518f007c5b9669677a8c6dc7cbfeAurimas Liutikas    from rootProject.ext.supportRepoOut
2378e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    destinationDir rootProject.ext.distDir
2478e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    into 'm2repository'
2578e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    baseName = String.format("top-of-tree-m2repository-%s", project.ext.buildNumber)
2678e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    dependsOn mainUpload
2788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
2888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
29a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyartask createDiffArchive(type : Zip) {
30a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    description "Creates a maven repository that includes just the libraries compiled in this" +
31a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            " project without any libraries that are already on maven.google.com. If you need " +
32a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            " a full repo, use createArchive task."
33a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    dependsOn mainUpload
34a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    /**
35a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar     * building filters in a doFirst block so that we can query the output of other tasks and also
36a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar     * not query maven.google unless task runs.
37a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar     */
38a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    doFirst {
39a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar        def includeFilters = subprojects.collect { it.tasks.withType(Upload) }.findResults {
40a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            def group = it.project.group[0]
41a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            def archiveName = it.project.name[0]
42a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            def version = it.project.version[0]
43a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            if (group == null || archiveName == null || version == null) {
44a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                logger.info "null artifact info for ${it.project.path}"
45a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                return null
46a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            }
47a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            def subFolder = group.replace('.', '/') + "/" + archiveName + "/" + version
48a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            def localFolder = new File(rootProject.ext.supportRepoOut, subFolder)
49a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            if (!localFolder.exists()) {
50a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                // no reason to check, not even built
51a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                logger.info "skipping $subFolder because it does not exist"
52a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                return null
53a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            }
54a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            // query maven.google to check if it is released.
55a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            if (rootProject.ext.versionChecker.isReleased(group, archiveName, version)) {
56a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                logger.info "looks like $subFolder is released, skipping"
57a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                return null
58a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            } else {
59a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                logger.info "adding $subFolder to partial maven zip because it cannot be found on" +
60a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar                        " maven.google.com"
61a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            }
62a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            return subFolder + "/**"
63a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar        }
64a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar        logger.info "include filters for diff maven zip ${includeFilters}"
65a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar        includeFilters.forEach {
66a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar            include it
67a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar        }
68a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    }
69a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    from rootProject.ext.supportRepoOut
70a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    destinationDir rootProject.ext.distDir
71a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    into 'm2repository'
72a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    baseName = String.format("gmaven-diff-top-of-tree-m2repository-%s", project.ext.buildNumber)
73a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar    dependsOn mainUpload
74a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar}
75a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyar
7676542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikas// anchor for prepare repo. This is post unzip.
7776542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikastask prepareRepo() {
7878e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    description "This task clears the repo folder to ensure that we run a fresh build every" +
7978e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar            " time we create arhives. Otherwise, snapshots will accumulate in the builds folder."
8078e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    doFirst {
8178e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar        rootProject.ext.supportRepoOut.deleteDir()
8278e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar        rootProject.ext.supportRepoOut.mkdirs()
8378e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar    }
8478e026c72c28f4256c1d6ebdca89da4946f3ba19Yigit Boyar}
85