intellijDependencies.kt 9.18 KB
Newer Older
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
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
/*
 * Copyright 2010-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@file:Suppress("unused") // usages in build scripts are not tracked properly

import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.JavaExec
import java.io.File

28
private fun Project.intellijRepoDir() = File("${project.rootDir.absoluteFile}/buildSrc/prepare-deps/intellij-sdk/repo")
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
29

30
fun intellijSdkRepo(repositoryHandler:RepositoryHandler,project: Project): IvyArtifactRepository = repositoryHandler.ivy {
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    val baseDir = project.intellijRepoDir()
    val intellijEnforceCommunitySdk = project.getBooleanProperty("intellijEnforceCommunitySdk") == true

    setUrl(baseDir)

    if (!intellijEnforceCommunitySdk) {
        ivyPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module]Ultimate.ivy.xml")
        ivyPattern("${baseDir.canonicalPath}/[organisation]/[revision]/intellijUltimate.plugin.[module].ivy.xml")
        artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module]Ultimate/lib/[artifact](-[classifier]).jar")
        artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/intellijUltimate/plugins/[module]/lib/[artifact](-[classifier]).jar")
    }

    ivyPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module].ivy.xml")
    ivyPattern("${baseDir.canonicalPath}/[organisation]/[revision]/intellij.plugin.[module].ivy.xml")
    ivyPattern("${baseDir.canonicalPath}/[organisation]/[revision]/plugins-[module].ivy.xml")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module]/lib/[artifact](-[classifier]).jar")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/intellij/plugins/[module]/lib/[artifact](-[classifier]).jar")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/plugins-[module]/[module]/lib/[artifact](-[classifier]).jar")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module]/[artifact].jar")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/[module]/[artifact](-[revision])(-[classifier]).jar")
    artifactPattern("${baseDir.canonicalPath}/[organisation]/[revision]/sources/[artifact]-[revision]-[classifier].[ext]")

53
    /*metadataSources {
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
54
        ivyDescriptor()
55
    }*/
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
56 57
}

58
fun Project.intellijDep(module: String = "intellij") = "kotlin.build.custom.deps:$module:${rootProject.getExtensions().getExtraProperties().get("versions.intellijSdk")}"
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
59

60 61 62 63
fun getIntellijDep(project: Project, module: String = "intellij"): String {
	return project.intellijDep(module)
}

Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
64
fun Project.intellijCoreDep() = intellijDep("intellij-core")
65
fun customIntellijCoreDep(project:Project) = project.intellijDep("intellij-core")
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
66 67
fun Project.intellijPluginDep(plugin: String) = intellijDep(plugin)

68 69
fun customIntellijPluginDep(project:Project,plugin: String):String{return project.intellijPluginDep(plugin)} 

Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
70 71 72 73 74 75 76 77 78 79
fun Project.intellijUltimateDep() = intellijDep("intellij")

fun Project.intellijUltimatePluginDep(plugin: String) = intellijDep(plugin)

fun ModuleDependency.includeJars(vararg names: String, rootProject: Project? = null) {
    names.forEach {
        var baseName = it.removeSuffix(".jar")
        if (baseName == "annotations") {
            error("Don't use anntations.jar from intellij. Kotlin stdlib already has this annotations.")
        }
80
        if (rootProject != null && rootProject.getExtensions().getExtraProperties().has("ignore.jar.$baseName")) {
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
81 82
            return@forEach
        }
83 84
        if (rootProject != null && rootProject.getExtensions().getExtraProperties().has("versions.jar.$baseName")) {
            baseName += "-${rootProject.getExtensions().getExtraProperties().get("versions.jar.$baseName")}"
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
85 86 87 88 89 90 91 92 93
        }
        artifact {
            name = baseName
            type = "jar"
            extension = "jar"
        }
    }
}

94 95
fun customIncludeJars(moduleDependency:ModuleDependency,names: List<String>, rootProject: Project? = null) {moduleDependency.includeJars(*names.toTypedArray(),rootProject = rootProject)}

Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
96 97 98
// Workaround. Top-level Kotlin function in a default package can't be called from a non-default package
object IntellijRootUtils {
    fun getRepositoryRootDir(project: Project): File = with (project.rootProject) {
99
        return File(intellijRepoDir(), "kotlin.build.custom.deps/${getExtensions().getExtraProperties().get("versions.intellijSdk")}")
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
100 101 102 103 104 105 106 107
    }

    fun getIntellijRootDir(project: Project): File = with (project.rootProject) {
        return File(getRepositoryRootDir(this), "intellij${if (isIntellijCommunityAvailable()) "" else "Ultimate"}")
    }
}

fun ModuleDependency.includeIntellijCoreJarDependencies(project: Project) =
108
        includeJars(*(project.rootProject.getExtensions().getExtraProperties().get("IntellijCoreDependencies") as List<String>).toTypedArray(), rootProject = project.rootProject)
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
109

110 111
fun customIncludeIntellijCoreJarDependencies(moduleDependency:ModuleDependency,project:Project){return moduleDependency.includeIntellijCoreJarDependencies(project)}

Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
112
fun ModuleDependency.includeIntellijCoreJarDependencies(project: Project, jarsFilterPredicate: (String) -> Boolean) =
113
        includeJars(*(project.rootProject.getExtensions().getExtraProperties().get("IntellijCoreDependencies") as List<String>).filter { jarsFilterPredicate(it) }.toTypedArray(), rootProject = project.rootProject)
114 115
        
fun customIncludeIntellijCoreJarDependencies(moduleDependency:ModuleDependency,project:Project,jarsFilterPredicate: (String) -> Boolean){return moduleDependency.includeIntellijCoreJarDependencies(project,jarsFilterPredicate)}
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
116

117
fun Project.isIntellijCommunityAvailable() = !(rootProject.getExtensions().getExtraProperties().get("intellijUltimateEnabled") as Boolean) || rootProject.getExtensions().getExtraProperties().get("intellijSeparateSdks") as Boolean
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
118

119
fun Project.isIntellijUltimateSdkAvailable() = (rootProject.getExtensions().getExtraProperties().get("intellijUltimateEnabled") as Boolean)
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
120 121 122 123 124

fun Project.intellijRootDir() = IntellijRootUtils.getIntellijRootDir(project)

fun Project.intellijUltimateRootDir() =
        if (isIntellijUltimateSdkAvailable())
125
            File(intellijRepoDir(), "kotlin.build.custom.deps/${rootProject.getExtensions().getExtraProperties().get("versions.intellijSdk")}/intellijUltimate")
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
126 127 128
        else
            throw GradleException("intellij ultimate SDK is not available")

129 130
/*fun DependencyHandlerScope.excludeInAndroidStudio(rootProject: Project, block: DependencyHandlerScope.() -> Unit) {
    if (!rootProject.getExtensions().getExtraProperties().has("versions.androidStudioRelease")) {
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
131 132
        block()
    }
133
}*/
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
134

135

Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
136 137
fun Project.runIdeTask(name: String, ideaPluginDir: File, ideaSandboxDir: File, body: JavaExec.() -> Unit): JavaExec {

138
    return getTasks().create(name,JavaExec::class.java) {
Saif Abdul Cassim's avatar
Saif Abdul Cassim committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        val ideaSandboxConfigDir = File(ideaSandboxDir, "config")

        classpath = mainSourceSet.runtimeClasspath

        main = "com.intellij.idea.Main"

        workingDir = File(intellijRootDir(), "bin")

        jvmArgs(
            "-Xmx1250m",
            "-XX:ReservedCodeCacheSize=240m",
            "-XX:+HeapDumpOnOutOfMemoryError",
            "-ea",
            "-Didea.debug.mode=true",
            "-Didea.system.path=$ideaSandboxDir",
            "-Didea.config.path=$ideaSandboxConfigDir",
            "-Dapple.laf.useScreenMenuBar=true",
            "-Dapple.awt.graphics.UseQuartz=true",
            "-Dsun.io.useCanonCaches=false",
            "-Dplugin.path=${ideaPluginDir.absolutePath}"
        )

        if (rootProject.findProperty("versions.androidStudioRelease") != null) {
            jvmArgs("-Didea.platform.prefix=AndroidStudio")
        }

        if (project.hasProperty("noPCE")) {
            jvmArgs("-Didea.ProcessCanceledException=disabled")
        }

        jvmArgs("-Didea.is.internal=${project.findProperty("idea.is.internal") ?: true}")

        project.findProperty("idea.args")?.let { arguments ->
            jvmArgs(arguments.toString().split(" "))
        }

        args()

        doFirst {
            val disabledPluginsFile = File(ideaSandboxConfigDir, "disabled_plugins.txt")
            val disabledPluginsContents = disabledPluginsFile.takeIf { it.isFile }?.readLines()
            val filteredContents = disabledPluginsContents?.filterNot { it.contains("org.jetbrains.kotlin") }
            if (filteredContents != null && filteredContents.size != disabledPluginsContents.size) {
                with(disabledPluginsFile.printWriter()) {
                    filteredContents.forEach(this::println)
                }
            }
        }

        body()
    }
}