summaryrefslogtreecommitdiff
path: root/plugins/kotlin/project-wizard/core/src/org/jetbrains/kotlin/tools/projectWizard/ir/buildsystem/gradle/AndroidIR.kt
blob: 26d0141499cb723af6ce492966a1568ead3d9131 (plain)
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
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.gradle

import org.jetbrains.kotlin.tools.projectWizard.core.asStringWithUnixSlashes
import org.jetbrains.kotlin.tools.projectWizard.ir.buildsystem.FreeIR
import org.jetbrains.kotlin.tools.projectWizard.plugins.printer.GradlePrinter
import org.jetbrains.kotlin.tools.projectWizard.settings.JavaPackage
import java.nio.file.Path

interface AndroidIR : GradleIR

//TODO parameterize
data class AndroidConfigIR(
    val javaPackage: JavaPackage?,
    val newManifestPath: Path?,
    val printVersionCode: Boolean,
    val printBuildTypes: Boolean,
    var androidSdkVersion: String = "31"
) : AndroidIR, FreeIR {
    override fun GradlePrinter.renderGradle() {
        sectionCall("android", needIndent = true) {
            call("compileSdkVersion") { +androidSdkVersion }; nlIndented()
            if (newManifestPath != null) {
                when (dsl) {
                    GradlePrinter.GradleDsl.KOTLIN -> {
                        +"""sourceSets["main"].manifest.srcFile("${newManifestPath.asStringWithUnixSlashes()}")"""
                    }
                    GradlePrinter.GradleDsl.GROOVY -> {
                        +"""sourceSets.main.manifest.srcFile('${newManifestPath.asStringWithUnixSlashes()}')"""
                    }
                }
                nlIndented()
            }
            sectionCall("defaultConfig", needIndent = true) {
                if (javaPackage != null) {
                    assignmentOrCall("applicationId") { +javaPackage.asCodePackage().quotified }; nlIndented()
                }
                call("minSdkVersion") { +"24" }; nlIndented()  // TODO dehardcode
                call("targetSdkVersion") { +androidSdkVersion };
                if (printVersionCode) {
                    nlIndented()
                    assignmentOrCall("versionCode") { +"1" }; nlIndented()
                    assignmentOrCall("versionName") { +"1.0".quotified }
                }
            }
            nlIndented()
            sectionCall("compileOptions", needIndent = true) {
                assignmentOrCall("sourceCompatibility") { +"JavaVersion.VERSION_1_8" }; nlIndented()
                assignmentOrCall("targetCompatibility") { +"JavaVersion.VERSION_1_8" }
            }
            if (printBuildTypes) {
                nlIndented()
                sectionCall("buildTypes", needIndent = true) {
                    val sectionIdentifier = when (dsl) {
                        GradlePrinter.GradleDsl.KOTLIN -> """getByName("release")"""
                        GradlePrinter.GradleDsl.GROOVY -> "release".quotified
                    }
                    sectionCall(sectionIdentifier, needIndent = true) {
                        val minifyCallName = when (dsl) {
                            GradlePrinter.GradleDsl.KOTLIN -> "isMinifyEnabled"
                            GradlePrinter.GradleDsl.GROOVY -> "minifyEnabled"
                        }

                        assignmentOrCall(minifyCallName) { +"false" }
                    }
                }
            }
        }
    }
}