2024-03-16 13:23:52 +08:00
|
|
|
|
import java.security.MessageDigest
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义函数
|
|
|
|
|
|
ext {
|
|
|
|
|
|
gitVersionCode = {
|
|
|
|
|
|
def cmd = 'git rev-list HEAD --count'
|
|
|
|
|
|
return cmd.execute().text.trim().toInteger()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gitVersionTag = {
|
|
|
|
|
|
def cmd = 'git describe --tags'
|
|
|
|
|
|
def version = cmd.execute().text.trim()
|
|
|
|
|
|
|
|
|
|
|
|
def pattern = "-(\\d+)-g"
|
|
|
|
|
|
def matcher = version =~ pattern
|
|
|
|
|
|
|
|
|
|
|
|
// 若最近的 commit 没有 tag,则增加第4位
|
|
|
|
|
|
if (matcher) {
|
|
|
|
|
|
version = version.substring(0, matcher.start()) + "." + matcher[0][1]
|
|
|
|
|
|
}
|
|
|
|
|
|
return version
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
releaseTime = {
|
|
|
|
|
|
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 14:23:33 +08:00
|
|
|
|
genUpdateJson = { downloadUrl, releaseDir, apkFileName, logfile, defaultConfig, variant ->
|
2024-03-16 13:23:52 +08:00
|
|
|
|
// 生成 update.json 文件,用于自动更新
|
|
|
|
|
|
def releaseTime = releaseTime()
|
|
|
|
|
|
def txtFile = new File(releaseDir, "update.json")
|
|
|
|
|
|
def changeLog = ""
|
|
|
|
|
|
def fileMd5 = ""
|
|
|
|
|
|
|
|
|
|
|
|
// 读取 changelog,并提取最新版本的日志
|
2025-09-12 14:23:33 +08:00
|
|
|
|
def changeLogFile = new File(project.getProjectDir().path, logfile)
|
2024-03-16 13:23:52 +08:00
|
|
|
|
def text = changeLogFile.text
|
2025-09-12 14:23:33 +08:00
|
|
|
|
def marker = "#### 更新记录"
|
2024-03-16 13:23:52 +08:00
|
|
|
|
def startIndex = text.indexOf(marker)
|
|
|
|
|
|
|
|
|
|
|
|
if (startIndex != -1) {
|
|
|
|
|
|
// 找到标记后,找到下一个空行
|
2025-09-12 14:23:33 +08:00
|
|
|
|
def endIndex = text.indexOf('### [', startIndex)
|
|
|
|
|
|
|
2024-03-16 13:23:52 +08:00
|
|
|
|
if(endIndex == -1)
|
|
|
|
|
|
endIndex = text.length() - 1
|
|
|
|
|
|
|
|
|
|
|
|
if (endIndex != -1) {
|
|
|
|
|
|
// 截取内容并存储到变量B
|
|
|
|
|
|
def content = text.substring(startIndex + marker.length(), endIndex)
|
|
|
|
|
|
changeLog = content.trim()
|
2025-09-12 14:23:33 +08:00
|
|
|
|
changeLog = changeLog
|
|
|
|
|
|
.replaceAll("\r\n", "\\\\n") // 处理 Windows 换行
|
|
|
|
|
|
.replaceAll("\n", "\\\\n") // 处理 Linux/macOS 换行
|
|
|
|
|
|
.replaceAll("\r", "\\\\n") // 处理旧版 macOS 换行
|
2024-03-16 13:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def apkFile = new File(releaseDir, apkFileName)
|
|
|
|
|
|
// 计算 md5 值
|
|
|
|
|
|
// 使用 Java 的 MessageDigest 计算 MD5
|
|
|
|
|
|
MessageDigest md5 = MessageDigest.getInstance("MD5")
|
|
|
|
|
|
apkFile.eachByte(4096) { bytes, bytesRead ->
|
|
|
|
|
|
md5.update(bytes, 0, bytesRead)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 将 MD5 转换为十六进制字符串
|
|
|
|
|
|
fileMd5 = md5.digest().collect { String.format("%02X", it) }.join()
|
|
|
|
|
|
|
|
|
|
|
|
// 写入APK信息到文本文件
|
|
|
|
|
|
txtFile.text = "{\n" +
|
|
|
|
|
|
" \"applicationId\": \"${defaultConfig.applicationId}\",\n" +
|
|
|
|
|
|
" \"variantName\": \"${variant.buildType.name}\",\n" +
|
|
|
|
|
|
" \"versionName\": \"${defaultConfig.versionName}\",\n" +
|
|
|
|
|
|
" \"versionCode\": ${defaultConfig.versionCode},\n" +
|
|
|
|
|
|
" \"releaseDate\": \"${releaseTime}\",\n" +
|
|
|
|
|
|
" \"changeLog\": \"${changeLog}\",\n" +
|
|
|
|
|
|
" \"fileMd5\": \"${fileMd5}\",\n" +
|
|
|
|
|
|
" \"downloadUrl\": \"${downloadUrl}\"\n" +
|
|
|
|
|
|
"}"
|
|
|
|
|
|
}
|
2025-09-12 14:23:33 +08:00
|
|
|
|
|
|
|
|
|
|
replaceText = {File file, String key, String value ->
|
|
|
|
|
|
def fileText = file.text
|
|
|
|
|
|
def regex = '\\$\\{' + key + '\\}'
|
|
|
|
|
|
fileText = (fileText =~ /${regex}/).replaceAll(value)
|
|
|
|
|
|
file.write(fileText)
|
|
|
|
|
|
}
|
2024-03-16 13:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义任务
|
|
|
|
|
|
task customTask {
|
|
|
|
|
|
doLast {
|
|
|
|
|
|
println "This is a custom task."
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|