Reformat: KotlinPluginUpdater.kt

This commit is contained in:
Nikolay Krasko
2018-06-06 21:03:43 +03:00
parent f797592365
commit 19eaf9289b
@@ -39,17 +39,30 @@ sealed class PluginUpdateStatus {
object LatestVersionInstalled : PluginUpdateStatus() object LatestVersionInstalled : PluginUpdateStatus()
class Update(val pluginDescriptor: IdeaPluginDescriptor, class Update(
val hostToInstallFrom: String?) : PluginUpdateStatus() val pluginDescriptor: IdeaPluginDescriptor,
val hostToInstallFrom: String?
) : PluginUpdateStatus()
class CheckFailed(val message: String, val detail: String? = null) : PluginUpdateStatus() class CheckFailed(val message: String, val detail: String? = null) : PluginUpdateStatus()
fun mergeWith(other: PluginUpdateStatus): PluginUpdateStatus { fun mergeWith(other: PluginUpdateStatus): PluginUpdateStatus {
if (other is Update && (this is LatestVersionInstalled || if (other is Update) {
(this is Update && VersionComparatorUtil.compare(other.pluginDescriptor.version, when (this) {
pluginDescriptor.version) > 0))) { is LatestVersionInstalled -> {
return other return other
}
is Update -> {
if (VersionComparatorUtil.compare(other.pluginDescriptor.version, pluginDescriptor.version) > 0) {
return other
}
}
is PluginUpdateStatus.CheckFailed -> {
// proceed to return this
}
}
} }
return this return this
} }
@@ -63,15 +76,14 @@ sealed class PluginUpdateStatus {
} }
class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Disposable { class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Disposable {
private val INITIAL_UPDATE_DELAY = 2000L
private val CACHED_REQUEST_DELAY = TimeUnit.DAYS.toMillis(1)
private var updateDelay = INITIAL_UPDATE_DELAY private var updateDelay = INITIAL_UPDATE_DELAY
private val alarm = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this) private val alarm = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this)
private val notificationGroup = NotificationGroup("Kotlin plugin updates", NotificationDisplayType.STICKY_BALLOON, true) private val notificationGroup = NotificationGroup("Kotlin plugin updates", NotificationDisplayType.STICKY_BALLOON, true)
@Volatile private var checkQueued = false @Volatile
@Volatile private var lastUpdateStatus: PluginUpdateStatus? = null private var checkQueued = false
@Volatile
private var lastUpdateStatus: PluginUpdateStatus? = null
fun kotlinFileEdited(file: VirtualFile) { fun kotlinFileEdited(file: VirtualFile) {
if (!file.isInLocalFileSystem) return if (!file.isInLocalFileSystem) return
@@ -122,8 +134,7 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
var updateStatus: PluginUpdateStatus var updateStatus: PluginUpdateStatus
if (KotlinPluginUtil.isSnapshotVersion()) { if (KotlinPluginUtil.isSnapshotVersion()) {
updateStatus = PluginUpdateStatus.LatestVersionInstalled updateStatus = PluginUpdateStatus.LatestVersionInstalled
} } else {
else {
try { try {
updateStatus = checkUpdatesInMainRepository() updateStatus = checkUpdatesInMainRepository()
@@ -131,8 +142,7 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
val customUpdateStatus = checkUpdatesInCustomRepository(host) val customUpdateStatus = checkUpdatesInCustomRepository(host)
updateStatus = updateStatus.mergeWith(customUpdateStatus) updateStatus = updateStatus.mergeWith(customUpdateStatus)
} }
} } catch (e: Exception) {
catch(e: Exception) {
updateStatus = PluginUpdateStatus.fromException("Kotlin plugin update check failed", e) updateStatus = PluginUpdateStatus.fromException("Kotlin plugin update check failed", e)
} }
} }
@@ -144,8 +154,8 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
recordSuccessfulUpdateCheck() recordSuccessfulUpdateCheck()
} }
ApplicationManager.getApplication().invokeLater({ ApplicationManager.getApplication().invokeLater({
callback(updateStatus) callback(updateStatus)
}, ModalityState.any()) }, ModalityState.any())
} }
private fun initPluginDescriptor(newVersion: String): IdeaPluginDescriptor { private fun initPluginDescriptor(newVersion: String): IdeaPluginDescriptor {
@@ -163,7 +173,8 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
val os = URLEncoder.encode(SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION, CharsetToolkit.UTF8) val os = URLEncoder.encode(SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION, CharsetToolkit.UTF8)
val uid = PermanentInstallationID.get() val uid = PermanentInstallationID.get()
val pluginId = KotlinPluginUtil.KOTLIN_PLUGIN_ID.idString val pluginId = KotlinPluginUtil.KOTLIN_PLUGIN_ID.idString
val url = "https://plugins.jetbrains.com/plugins/list?pluginId=$pluginId&build=$buildNumber&pluginVersion=$currentVersion&os=$os&uuid=$uid" val url =
"https://plugins.jetbrains.com/plugins/list?pluginId=$pluginId&build=$buildNumber&pluginVersion=$currentVersion&os=$os&uuid=$uid"
val responseDoc = HttpRequests.request(url).connect { val responseDoc = HttpRequests.request(url).connect {
JDOMUtil.load(it.inputStream) JDOMUtil.load(it.inputStream)
} }
@@ -176,21 +187,24 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
} }
val newVersion = responseDoc.getChild("category")?.getChild("idea-plugin")?.getChild("version")?.text val newVersion = responseDoc.getChild("category")?.getChild("idea-plugin")?.getChild("version")?.text
if (newVersion == null) { if (newVersion == null) {
return PluginUpdateStatus.CheckFailed("Couldn't find plugin version in repository response", JDOMUtil.writeElement(responseDoc, "\n")) return PluginUpdateStatus.CheckFailed(
"Couldn't find plugin version in repository response",
JDOMUtil.writeElement(responseDoc, "\n")
)
} }
val pluginDescriptor = initPluginDescriptor(newVersion) val pluginDescriptor = initPluginDescriptor(newVersion)
return updateIfNotLatest(pluginDescriptor, null) return updateIfNotLatest(pluginDescriptor, null)
} }
private fun checkUpdatesInCustomRepository(host: String): PluginUpdateStatus { private fun checkUpdatesInCustomRepository(host: String): PluginUpdateStatus {
val plugins = try { val plugins = try {
RepositoryHelper.loadPlugins(host, null) RepositoryHelper.loadPlugins(host, null)
} } catch (e: Exception) {
catch(e: Exception) {
return PluginUpdateStatus.fromException("Checking custom plugin repository $host failed", e) return PluginUpdateStatus.fromException("Checking custom plugin repository $host failed", e)
} }
val kotlinPlugin = plugins.find { it.pluginId == KotlinPluginUtil.KOTLIN_PLUGIN_ID } ?: return PluginUpdateStatus.LatestVersionInstalled val kotlinPlugin =
plugins.find { it.pluginId == KotlinPluginUtil.KOTLIN_PLUGIN_ID } ?: return PluginUpdateStatus.LatestVersionInstalled
return updateIfNotLatest(kotlinPlugin, host) return updateIfNotLatest(kotlinPlugin, host)
} }
@@ -209,9 +223,10 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
private fun notifyPluginUpdateAvailable(update: PluginUpdateStatus.Update) { private fun notifyPluginUpdateAvailable(update: PluginUpdateStatus.Update) {
val notification = notificationGroup.createNotification( val notification = notificationGroup.createNotification(
"Kotlin", "Kotlin",
"A new version ${update.pluginDescriptor.version} of the Kotlin plugin is available. <b><a href=\"#\">Install</a></b>", "A new version ${update.pluginDescriptor.version} of the Kotlin plugin is available. <b><a href=\"#\">Install</a></b>",
NotificationType.INFORMATION) { notification, event -> NotificationType.INFORMATION
) { notification, _ ->
notification.expire() notification.expire()
installPluginUpdate(update) { installPluginUpdate(update) {
notifyPluginUpdateAvailable(update) notifyPluginUpdateAvailable(update)
@@ -289,7 +304,10 @@ class KotlinPluginUpdater(val propertiesComponent: PropertiesComponent) : Dispos
} }
companion object { companion object {
private val PROPERTY_NAME = "kotlin.lastUpdateCheck" private const val INITIAL_UPDATE_DELAY = 2000L
private val CACHED_REQUEST_DELAY = TimeUnit.DAYS.toMillis(1)
private const val PROPERTY_NAME = "kotlin.lastUpdateCheck"
private val LOG = Logger.getInstance(KotlinPluginUpdater::class.java) private val LOG = Logger.getInstance(KotlinPluginUpdater::class.java)
fun getInstance(): KotlinPluginUpdater = ServiceManager.getService(KotlinPluginUpdater::class.java) fun getInstance(): KotlinPluginUpdater = ServiceManager.getService(KotlinPluginUpdater::class.java)