Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/jetbrains/gateway-plugin/build.gradle.kts
2499 views
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.changelog.date
import org.jetbrains.changelog.markdownToHTML
import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

fun properties(key: String) = project.findProperty(key).toString()

plugins {
    // Java support
    id("java")
    // Kotlin support - check the latest version at https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm
    id("org.jetbrains.kotlin.jvm") version "2.0.0"
    // gradle-intellij-plugin - read more: https://github.com/JetBrains/gradle-intellij-plugin
    id("org.jetbrains.intellij.platform") version "2.0.1"
//    id("org.jetbrains.intellij.platform.migration") version "2.0.1"
    // gradle-changelog-plugin - read more: https://github.com/JetBrains/gradle-changelog-plugin
    id("org.jetbrains.changelog") version "1.1.2"
    // detekt linter - read more: https://detekt.github.io/detekt/gradle.html
    id("io.gitlab.arturbosch.detekt") version "1.23.6"
    // ktlint linter - read more: https://github.com/JLLeitschuh/ktlint-gradle
    id("org.jlleitschuh.gradle.ktlint") version "10.0.0"
    // Gradle Properties Plugin - read more: https://github.com/stevesaliman/gradle-properties-plugin
    id("net.saliman.properties") version "1.5.2"
}

group = properties("pluginGroup")
val environmentName = properties("environmentName")
var pluginVersion = properties("pluginVersion")

if (environmentName.isNotBlank()) {
    pluginVersion += "-$environmentName"
}

pluginVersion = pluginVersion.replace("{{LOCAL_VERSION}}", date("MMddhhmm") + "-local")

project(":") {
    kotlin {
        val excludedPackage = if (environmentName == "latest") "stable" else "latest"
        sourceSets["main"].kotlin.exclude("io/gitpod/jetbrains/gateway/${excludedPackage}/**")
    }

    sourceSets {
        main {
            resources.srcDirs("src/main/resources-${environmentName}")
        }
    }
}

repositories {
    mavenCentral()
    intellijPlatform {
        defaultRepositories()
    }
}

dependencies {
    implementation(project(":gitpod-protocol")) {
        artifact {
            type = "jar"
        }
    }
    compileOnly("javax.websocket:javax.websocket-api:1.1")
    compileOnly("org.eclipse.jetty.websocket:websocket-api:9.4.44.v20210927")
    testImplementation(kotlin("test"))
    detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.18.1")
    // https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/javax-websocket-client-impl
    implementation("org.eclipse.jetty.websocket:javax-websocket-client-impl:9.4.44.v20210927")
}

dependencies {
    intellijPlatform {
        // https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-dependencies-extension.html#target-platforms
        // https://www.jetbrains.com/updates/updates.xml
        create(IntelliJPlatformType.Gateway, properties("platformVersion"))
        bundledPlugins(properties("platformBundledPlugins").split(',').map{ it.trim() })
    }
}

// Configure gradle-intellij-plugin plugin.
intellijPlatform {
    pluginConfiguration {
        id = properties("pluginId")
        name = properties("latestPluginName")
        version = pluginVersion

        description = providers.fileContents(layout.projectDirectory.file("README.md")).asText.map {
            val start = "<!-- Plugin description -->"
            val end = "<!-- Plugin description end -->"

            with(it.lines()) {
                if (!containsAll(listOf(start, end))) {
                    throw GradleException("Plugin description section not found in README.md:\n$start ... $end")
                }
                subList(indexOf(start) + 1, indexOf(end)).joinToString("\n").let(::markdownToHTML)
            }
        }

        changeNotes = changelog.getLatest().toHTML()

        ideaVersion {
            sinceBuild = properties("pluginSinceBuild")
            untilBuild = properties("pluginUntilBuild")
        }
    }

    pluginVerification {
        ides {
            properties("pluginVerifierIdeVersions").split(',').map(String::trim).forEach { version ->
                ide(IntelliJPlatformType.Gateway, version)
            }
        }
    }

    publishing {
        token = providers.environmentVariable("JB_MARKETPLACE_PUBLISH_TOKEN").getOrElse("")
        var pluginChannels = providers.environmentVariable("JB_GATEWAY_GITPOD_PLUGIN_CHANNEL").getOrElse("")
        if (pluginChannels.isBlank()) {
            pluginChannels = if (pluginVersion.contains("-main-gha.")) {
                "Stable"
            } else {
                "Dev"
            }
        }
        channels = listOf(pluginChannels)
    }
    instrumentCode = false
}

changelog {
    version = pluginVersion
    groups = emptyList()
}

// Configure detekt plugin.
// Read more: https://detekt.github.io/detekt/kotlindsl.html
detekt {
    autoCorrect = true
    buildUponDefaultConfig = true
}

tasks.withType<Detekt> {
    jvmTarget = "21"
}

tasks.withType<KotlinCompile> {
    compilerOptions {
        jvmTarget.set(JvmTarget.JVM_21)
    }
}

tasks.withType<JavaCompile> {
    sourceCompatibility = "21"
    targetCompatibility = "21"
}

tasks {

    buildSearchableOptions {
        enabled = false
    }

    test {
        useJUnitPlatform()
    }

    register("buildFromLeeway") {
        if ("true" == System.getenv("DO_PUBLISH")) {
            print("publishing $pluginVersion...")
            dependsOn("publishPlugin")
        } else {
            print("building $pluginVersion...")
            dependsOn("buildPlugin")
        }
    }
}

tasks.register("installPlugin") {
    group = "gitpod"

    println("Building plugin $pluginVersion")

    dependsOn("buildPlugin")

    doLast {
        val pluginTargetPath = "distributions/jetbrains-gateway-gitpod-plugin.zip"
        val pluginFile = layout.buildDirectory.file(pluginTargetPath).orNull?.asFile ?: {
            throw GradleException("Plugin file not found at $pluginTargetPath")
        }

        // Example for macOS ~/Library/Application Support/JetBrains/JetBrainsGateway2024.3/plugins
        //
        // JB_GATEWAY_PLUGINS_DIR=/Users/hwen/Library/Application Support/JetBrains/JetBrainsGateway2024.3/plugins
        // JB_GATEWAY_IDEA_LOG_FILE=/Users/hwen/Library/Logs/JetBrains/JetBrainsGateway2024.3/idea.log
        val gatewayPluginsDir = System.getenv("JB_GATEWAY_PLUGINS_DIR")
        val gatewayIDEALogFile = System.getenv("JB_GATEWAY_IDEA_LOG_FILE")

        if (gatewayPluginsDir.isNullOrEmpty()) {
            throw GradleException("Found no JB_GATEWAY_PLUGINS_DIR environment variable")
        }
        println("Copying plugin from $pluginFile to $gatewayPluginsDir")

        copy {
            from(zipTree(pluginFile))
            into(file(gatewayPluginsDir))
        }

        println("Plugin successfully copied to $gatewayPluginsDir")

        exec {
            commandLine("sh", "-c", "pkill -f 'Gateway' || true")
        }
        if (!gatewayIDEALogFile.isNullOrEmpty()) {
            exec {
                commandLine("sh", "-c", "echo '' > $gatewayIDEALogFile")
            }
        }
    }
}