mirror of
https://github.com/HighCapable/SweetDependency.git
synced 2025-09-05 18:25:48 +08:00
fix: checking update function
This commit is contained in:
@@ -26,40 +26,53 @@ import com.highcapable.sweetdependency.generated.SweetDependencyProperties
|
|||||||
import com.highcapable.sweetdependency.gradle.helper.GradleHelper
|
import com.highcapable.sweetdependency.gradle.helper.GradleHelper
|
||||||
import com.highcapable.sweetdependency.utils.debug.SLog
|
import com.highcapable.sweetdependency.utils.debug.SLog
|
||||||
import com.highcapable.sweetdependency.utils.executeUrlBody
|
import com.highcapable.sweetdependency.utils.executeUrlBody
|
||||||
|
import org.xml.sax.InputSource
|
||||||
|
import java.io.StringReader
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插件自身检查更新工具类
|
* 插件自身检查更新工具类
|
||||||
*/
|
*/
|
||||||
internal object PluginUpdateHelper {
|
internal object PluginUpdateHelper {
|
||||||
|
|
||||||
private const val REPO_NAME = SweetDependencyProperties.PROJECT_NAME
|
/** OSS Release URL 地址 */
|
||||||
private const val AUTHOR_NAME = SweetDependencyProperties.PROJECT_DEVELOPER_NAME
|
private const val SONATYPE_OSS_RELEASES_URL = "https://s01.oss.sonatype.org/content/repositories/releases"
|
||||||
|
|
||||||
|
/** 依赖配置文件名 */
|
||||||
|
private const val METADATA_FILE_NAME = "maven-metadata.xml"
|
||||||
|
|
||||||
|
/** 插件自身依赖 URL 名称 */
|
||||||
|
private val groupUrlNotation =
|
||||||
|
"${SweetDependencyProperties.PROJECT_GROUP_NAME.replace(".","/")}/${SweetDependencyProperties.GRADLE_PLUGIN_MODULE_NAME}"
|
||||||
|
|
||||||
/** 检查更新 URL 地址 */
|
/** 检查更新 URL 地址 */
|
||||||
private const val RELEASE_URL = "https://github.com/$AUTHOR_NAME/$REPO_NAME"
|
private val releaseUrl = "$SONATYPE_OSS_RELEASES_URL/$groupUrlNotation/$METADATA_FILE_NAME"
|
||||||
|
|
||||||
/** 检查更新 */
|
/** 检查更新 */
|
||||||
internal fun checkingForUpdate() {
|
internal fun checkingForUpdate() {
|
||||||
if (GradleHelper.isOfflineMode) return
|
if (GradleHelper.isOfflineMode) return
|
||||||
val latestVersion = RELEASE_URL.executeUrlBody(isShowFailure = false).findVersionName()
|
val latestVersion = releaseUrl.executeUrlBody(isShowFailure = false).trim().findLatest()
|
||||||
if (latestVersion.isNotBlank() && latestVersion != SweetDependency.VERSION)
|
if (latestVersion.isNotBlank() && latestVersion != SweetDependency.VERSION) SLog.note(
|
||||||
SLog.note(
|
"""
|
||||||
"""
|
Plugin update is available, the current version is ${SweetDependency.VERSION}, please update to $latestVersion
|
||||||
Plugin update is available, the current version is ${SweetDependency.VERSION}, please update to $latestVersion
|
You can modify your plugin version in your project's settings.gradle / settings.gradle.kts
|
||||||
You can modify your plugin version in your project's settings.gradle / settings.gradle.kts
|
plugins {
|
||||||
plugins {
|
id("${SweetDependencyProperties.PROJECT_GROUP_NAME}") version "$latestVersion"
|
||||||
id("${SweetDependencyProperties.PROJECT_GROUP_NAME}") version "$latestVersion"
|
...
|
||||||
...
|
}
|
||||||
}
|
For more information, you can visit ${SweetDependency.PROJECT_URL}
|
||||||
For more information, you can visit ${SweetDependency.PROJECT_URL}
|
""".trimIndent(), SLog.UP
|
||||||
""".trimIndent(), SLog.UP
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析 JSON 并查找字符串版本 "name"
|
* 解析 [METADATA_FILE_NAME] 内容并获取 "latest"
|
||||||
* @return [String]
|
* @return [String]
|
||||||
*/
|
*/
|
||||||
private fun String.findVersionName() =
|
private fun String.findLatest() = runCatching {
|
||||||
runCatching { trim().split("href=\"/$AUTHOR_NAME/$REPO_NAME/releases/tag/")[1].split("\"")[0] }.getOrNull() ?: ""
|
if ((contains("<metadata ") || contains("<metadata>")).not() || endsWith("</metadata>").not()) return@runCatching ""
|
||||||
|
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(InputSource(StringReader(this))).let { document ->
|
||||||
|
document.getElementsByTagName("latest")?.let { if (it.length > 0) it.item(0)?.textContent ?: "" else "" }
|
||||||
|
}
|
||||||
|
}.getOrNull() ?: ""
|
||||||
}
|
}
|
Reference in New Issue
Block a user