Skip to content

Commit

Permalink
DEV2-2461: Create a thin plugin for IJ for publishing (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
Assaf Sapir authored Apr 24, 2023
1 parent f208eda commit 9c6a00a
Show file tree
Hide file tree
Showing 26 changed files with 657 additions and 209 deletions.
2 changes: 1 addition & 1 deletion Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id 'java'
id "org.jetbrains.kotlin.jvm" version "1.4.32"
id 'org.jetbrains.intellij' version '1.2.0'
id 'org.jetbrains.intellij' version '1.13.3'
id 'org.jlleitschuh.gradle.ktlint' version "10.0.0"
id 'org.jlleitschuh.gradle.ktlint-idea' version "10.0.0"
id "com.github.sherter.google-java-format" version "0.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public static CapabilitiesService getInstance() {
}

public void init() {
if (!Config.IS_SELF_HOSTED) {
scheduleFetchCapabilitiesTask();
}
scheduleFetchCapabilitiesTask();
}

public boolean isCapabilityEnabled(Capability capability) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public class StaticConfig {
// Must be identical to what is written under <id>com.tabnine.TabNine</id> in plugin.xml !!!
public static final String TABNINE_PLUGIN_ID_RAW = "com.tabnine.TabNine";
public static final String TABNINE_ENTERPRISE_ID_RAW = "com.tabnine.TabNine-Enterprise";
public static final PluginId TABNINE_PLUGIN_ID = PluginId.getId(TABNINE_PLUGIN_ID_RAW);
public static final int MAX_COMPLETIONS = 5;
public static final String BINARY_PROTOCOL_VERSION = "4.4.223";
Expand Down
23 changes: 0 additions & 23 deletions Common/src/main/java/com/tabnineCommon/general/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -166,25 +164,4 @@ public static void replaceCustomRepository(String oldUrl, String newUrl) {
}
ContainerUtil.removeDuplicates(pluginHosts);
}

public static Optional<String> getTabnineCustomRepository(String host) {
List<String> sources = UpdateSettings.getInstance().getStoredPluginHosts();
if (sources.isEmpty()) {
return Optional.empty();
}

return sources.stream().filter(s -> s.contains(host)).findFirst();
}

public static <T> T criticalSection(ReentrantLock lock, Function0<T> block) {
try {
lock.lock();
T returnVal = block.invoke();
lock.unlock();
return returnVal;
} catch (Throwable e) {
lock.unlock();
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fun hadSuffix(currentCompletion: TabNineCompletion): Boolean {
}

fun isSingleLine(currentCompletion: TabNineCompletion): Boolean {
return !currentCompletion.suffix.trim().contains("\n") ?: true
return !currentCompletion.suffix.trim().contains("\n")
}
fun shouldRemoveSuffix(currentCompletion: TabNineCompletion): Boolean {
return hadSuffix(currentCompletion) && isSingleLine(currentCompletion)
Expand Down
2 changes: 1 addition & 1 deletion Tabnine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.stream.Collectors
plugins {
id 'java'
id "org.jetbrains.kotlin.jvm" version "1.4.32"
id 'org.jetbrains.intellij' version '1.2.0'
id 'org.jetbrains.intellij' version '1.13.3'
id 'org.jlleitschuh.gradle.ktlint' version "10.0.0"
id 'org.jlleitschuh.gradle.ktlint-idea' version "10.0.0"
id "com.github.sherter.google-java-format" version "0.9"
Expand Down
13 changes: 10 additions & 3 deletions TabnineSelfHosted/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.stream.Collectors
plugins {
id 'java'
id "org.jetbrains.kotlin.jvm" version "1.4.32"
id 'org.jetbrains.intellij' version '1.2.0'
id 'org.jetbrains.intellij' version '1.13.3'
id 'org.jlleitschuh.gradle.ktlint' version "10.0.0"
id 'org.jlleitschuh.gradle.ktlint-idea' version "10.0.0"
id "com.github.sherter.google-java-format" version "0.9"
Expand Down Expand Up @@ -34,6 +34,7 @@ apply plugin: "org.jlleitschuh.gradle.ktlint-idea"

dependencies {
implementation(project(":Common"))
/*implementation(project(":TabnineSelfHostedForMarketplace"))*/
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32")
}

Expand All @@ -47,13 +48,13 @@ publishPlugin {
enabled = false
}

tasks.create("currentVersion") {
tasks.register("currentVersion") {
doLast {
println version
}
}

tasks.getByName("runIde").configure {
tasks.named("runIde").configure {
if (project.hasProperty('logFilePath')) {
systemProperty("TABNINE_LOG_FILE_PATH", logFilePath)
}
Expand All @@ -77,4 +78,10 @@ runPluginVerifier {
if (project.hasProperty("localPaths")) {
localPaths = Arrays.stream(project.localPaths.split(',')).map(File::new).collect(Collectors.toList())
}
}

sourceSets {
main {
java.srcDirs += "../TabnineSelfHostedForMarketplace/src/main/java"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package com.tabnineSelfHosted
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.PreloadingActivity
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.tabnineCommon.config.Config
import com.tabnineCommon.dialogs.Dialogs.showRestartDialog
import com.tabnineCommon.general.StaticConfig
import com.tabnineCommon.general.Utils
import com.tabnineCommon.lifecycle.BinaryStateService
import com.tabnineCommon.logging.initTabnineLogger
import com.tabnineCommon.notifications.ConnectionLostNotificationHandler
import com.tabnineCommon.userSettings.AppSettingsState
import com.tabnineSelfHosted.dialogs.TabnineEnterpriseUrlDialogWrapper
import java.util.concurrent.atomic.AtomicBoolean

class Initializer : PreloadingActivity(), StartupActivity {
Expand All @@ -28,46 +22,14 @@ class Initializer : PreloadingActivity(), StartupActivity {
}

private fun initialize() {
val shouldInitialize = !(initialized.getAndSet(true) || ApplicationManager.getApplication().isUnitTestMode)
if (!shouldInitialize) {
if (initialized.getAndSet(true) || ApplicationManager.getApplication().isUnitTestMode) {
return
}

Logger.getInstance(javaClass)
.info(
"Initializing for ${Config.CHANNEL}, plugin id = ${StaticConfig.TABNINE_PLUGIN_ID_RAW}"
)
initTabnineLogger()
connectionLostNotificationHandler.startConnectionLostListener()
ServiceManager.getService(BinaryStateService::class.java).startUpdateLoop()
initTabnineLogger()
requireSelfHostedUrl()
}

private fun requireSelfHostedUrl() {
val cloud2Url = StaticConfig.getTabnineEnterpriseHost()
if (cloud2Url.isPresent) {
Logger.getInstance(javaClass)
.info(String.format("Tabnine Enterprise host is configured: %s", cloud2Url.get()))
// This is for users that already configured the cloud url, but didn't set the repository.
// Duplication is handle inside
Utils.setCustomRepository(cloud2Url.get())
} else {
Logger.getInstance(javaClass)
.warn(
"Tabnine Enterprise host is not configured, showing some nice dialog"
)
ApplicationManager.getApplication().invokeLater(
Runnable {
val dialog = TabnineEnterpriseUrlDialogWrapper(null)
if (dialog.showAndGet()) {
val url = dialog.inputData
AppSettingsState.instance.cloud2Url = url
showRestartDialog("Self hosted URL configured successfully - Restart your IDE for the change to take effect.")
}
}
)
}
TabnineEnterprisePluginInstaller().installTabnineEnterprisePlugin()
val host = AppSettingsState.instance.cloud2Url
SelfHostedInitializer().initialize(host)
}

companion object {
Expand Down
1 change: 0 additions & 1 deletion TabnineSelfHosted/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<vendor email="[email protected]" url="https://tabnine.com">Tabnine</vendor>

<extensions defaultExtensionNs="com.intellij">
<preloadingActivity implementation="com.tabnineSelfHosted.Initializer"/>
<postStartupActivity implementation="com.tabnineSelfHosted.Initializer"/>
</extensions>
<description><![CDATA[
Expand Down
75 changes: 75 additions & 0 deletions TabnineSelfHostedForMarketplace/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.stream.Collectors

plugins {
id 'java'
id "org.jetbrains.kotlin.jvm" version "1.4.32"
id 'org.jetbrains.intellij' version '1.13.3'
id 'org.jlleitschuh.gradle.ktlint' version "10.0.0"
id 'org.jlleitschuh.gradle.ktlint-idea' version "10.0.0"
id "com.github.sherter.google-java-format" version "0.9"
}

repositories {
mavenCentral()
}

group 'com.tabnineSelfHosted'
version '0.0.1'

sourceCompatibility = 9
targetCompatibility = 9

tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "9"
}
}

repositories {
mavenCentral()
}

apply plugin: "org.jlleitschuh.gradle.ktlint-idea"

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32")
}

intellij {
version = '2019.3'
type = 'IC'
updateSinceUntilBuild = false
}

tasks.create("currentVersion") {
doLast {
println version
}
}

tasks.getByName("runIde").configure {
if (project.hasProperty('logFilePath')) {
systemProperty("TABNINE_LOG_FILE_PATH", logFilePath)
}
}

tasks {
compileKotlin {
dependsOn(ktlintFormat)
}
}

static def getIdeVersions(Project project) {
def ideVersions = project.hasProperty('ideVersions') ?
Arrays.asList(project.ideVersions.split(',')) :
['IC-2019.3.5']
return ideVersions
}

runPluginVerifier {
ideVersions = getIdeVersions(project)
if (project.hasProperty("localPaths")) {
localPaths = Arrays.stream(project.localPaths.split(',')).map(File::new).collect(Collectors.toList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import com.intellij.util.text.SemVer
import com.intellij.util.xmlb.XmlSerializer
import com.intellij.util.xmlb.annotations.Attribute
import com.intellij.util.xmlb.annotations.Tag
import com.tabnineCommon.dialogs.Dialogs
import com.tabnineCommon.general.StaticConfig
import com.tabnineCommon.general.StaticConfig.TABNINE_ENTERPRISE_ID_RAW
import com.tabnineCommon.general.Utils
import com.tabnineSelfHosted.dialogs.Dialogs
import com.tabnineSelfHosted.general.StaticConfig.TABNINE_ENTERPRISE_ID_RAW
import java.net.URL
import java.util.concurrent.locks.ReentrantLock

Expand All @@ -32,19 +30,21 @@ data class TabninePluginDescriptor(

class TabnineEnterprisePluginInstaller {
private val downloadLock = ReentrantLock()
fun installTabnineEnterprisePlugin() {
val host = StaticConfig.getTabnineEnterpriseHost()
if (!host.isPresent) {
fun installTabnineEnterprisePlugin(host: String?) {
if (host.isNullOrBlank()) {
Logger.getInstance(javaClass).info("Can't install Tabnine custom repository, I don't know what is the host url /shrug")
return
}
val pluginDescriptor = getTabninePluginDescriptor(host.get()) ?: return
val pluginDescriptor = getTabninePluginDescriptor(host) ?: return

ProgressManager.getInstance().run(object : Task.Backgroundable(null, "Downloading Tabnine Enterprise Plugin", true) {
override fun run(indicator: ProgressIndicator) {
try {
Utils.criticalSection(downloadLock) {
downloadAndInstall(indicator, pluginDescriptor)
val wasUpdated = Utils.criticalSection(downloadLock) {
return@criticalSection downloadAndInstall(indicator, pluginDescriptor)
}
if (wasUpdated) {
Dialogs.showRestartDialog("Tabnine was installed successfully - Restart your IDE for the change to take effect.")
}
} catch (e: Throwable) {
Logger.getInstance(javaClass).warn("Failed to install Tabnine Enterprise plugin", e)
Expand All @@ -55,23 +55,23 @@ class TabnineEnterprisePluginInstaller {

private fun getTabninePluginDescriptor(host: String): TabninePluginDescriptor? {
val url = Utils.getTabnineCustomRepository(host) ?: return null
val element = JDOMUtil.load(URL(url.get()))
val element = JDOMUtil.load(URL(url))
return XmlSerializer.deserialize(element.getChild("plugin"), TabninePluginDescriptor::class.java)
}

private fun downloadAndInstall(
indicator: ProgressIndicator,
plugin: TabninePluginDescriptor,
) {
): Boolean {
val newVersion = plugin.parsedVersion
if (newVersion == null) {
Logger.getInstance(javaClass).warn("Now downloading new version because was unable to find one. This shouldn't happen!")
return
return false
}
val downloadUrl = plugin.url
if (downloadUrl == null) {
Logger.getInstance(javaClass).warn("Now downloading new version because no url was supplied. This shouldn't happen!")
return
return false
}

val existingVersion =
Expand All @@ -82,19 +82,19 @@ class TabnineEnterprisePluginInstaller {
if (existingVersion?.let { it >= newVersion } == true) {
Logger.getInstance(javaClass)
.info("$TABNINE_ENTERPRISE_ID_RAW is already installed with version ${existingVersion.rawVersion}, which is >= the requested version $newVersion - skipping installation.")
return
return false
}

val downloader = createPluginDownloader(downloadUrl)

if (!downloader.prepareToInstall(indicator)) {
// the reason should appear in the logs because `prepareToInstall` have logged it - it's not available here.
Logger.getInstance(javaClass).warn("Failed to prepare installation for $TABNINE_ENTERPRISE_ID_RAW")
return
return false
}

downloader.install()
Dialogs.showRestartDialog("Tabnine enterprise plugin updated successfully - Restart is required")
return true
}

private fun createPluginDownloader(downloadUrl: String): PluginDownloader {
Expand Down
Loading

0 comments on commit 9c6a00a

Please sign in to comment.