genPlatform.gradle 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-FileCopyrightText: 2025 Weibo, Inc.
  2. //
  3. // SPDX-License-Identifier: Apache-2.0
  4. import java.nio.file.*
  5. import java.security.MessageDigest
  6. tasks.register('genPlatform', Zip) {
  7. doFirst {
  8. // Delete existing platform.zip right before regeneration
  9. def zipFile = new File(project.projectDir, "platform.zip")
  10. if (zipFile.exists()) {
  11. zipFile.delete()
  12. println "Deleted existing platform.zip before regeneration"
  13. }
  14. // Download platform files only when task actually executes
  15. download(project)
  16. }
  17. from(new File(project.buildDir, "genPlatform/gen"))
  18. into("")
  19. destinationDirectory = project.projectDir
  20. archiveFileName = "platform.zip"
  21. }
  22. def download(Project project){
  23. def version = project.findProperty("vscodeVersion")
  24. String windows_x64 = "https://update.code.visualstudio.com/${version}/win32-x64-archive/stable"
  25. String mac_x64 = "https://update.code.visualstudio.com/${version}/darwin/stable"
  26. String mac_arm64 = "https://update.code.visualstudio.com/${version}/darwin-arm64/stable"
  27. String linux_x64 = "https://update.code.visualstudio.com/${version}/linux-x64/stable"
  28. // To support other platforms, need to synchronously modify the initPlatfromFiles method in WecoderPlugin
  29. def list = [] // node_module directories for multiple platforms
  30. def projectBuild = new File(project.buildDir,"genPlatform")
  31. projectBuild.mkdirs();
  32. println "Downloading Windows platform files"
  33. def windowsZipFile = new File(projectBuild,"windows-x64.zip")
  34. if (!windowsZipFile.exists()) {
  35. windowsZipFile << new URL(windows_x64).openStream()
  36. } else {
  37. println "Windows platform file already exists, skipping download"
  38. }
  39. def windowsDir = new File(projectBuild, "windows-x64")
  40. copy {
  41. from(zipTree(new File(projectBuild, "windows-x64.zip")))
  42. into(windowsDir)
  43. }
  44. new File(windowsDir, "resources/app/node_modules").renameTo( new File(windowsDir, "resources/app/windows-x64"))
  45. list << new File(windowsDir, "resources/app/windows-x64")
  46. println "Downloading Mac x64 platform files"
  47. def macX64ZipFile = new File(projectBuild,"darwin-x64.zip")
  48. if (!macX64ZipFile.exists()) {
  49. macX64ZipFile << new URL(mac_x64).openStream()
  50. } else {
  51. println "Mac x64 platform file already exists, skipping download"
  52. }
  53. def macX64Dir = new File(projectBuild, "darwin-x64")
  54. copy {
  55. from(zipTree(new File(projectBuild, "darwin-x64.zip")))
  56. into(macX64Dir)
  57. }
  58. new File(macX64Dir, "Visual Studio Code.app/Contents/Resources/app/node_modules").renameTo(new File(macX64Dir, "Visual Studio Code.app/Contents/Resources/app/darwin-x64"))
  59. list << new File(macX64Dir, "Visual Studio Code.app/Contents/Resources/app/darwin-x64")
  60. println "Downloading Mac arm64 platform files"
  61. def macArm64ZipFile = new File(projectBuild,"darwin-arm64.zip")
  62. if (!macArm64ZipFile.exists()) {
  63. macArm64ZipFile << new URL(mac_arm64).openStream()
  64. } else {
  65. println "Mac arm64 platform file already exists, skipping download"
  66. }
  67. def macArm64Dir = new File(projectBuild, "darwin-arm64")
  68. copy {
  69. from(zipTree(new File(projectBuild, "darwin-arm64.zip")))
  70. into(macArm64Dir)
  71. }
  72. new File(macArm64Dir, "Visual Studio Code.app/Contents/Resources/app/node_modules").renameTo(new File(macArm64Dir, "Visual Studio Code.app/Contents/Resources/app/darwin-arm64"))
  73. list << new File(macArm64Dir, "Visual Studio Code.app/Contents/Resources/app/darwin-arm64")
  74. println "Downloading Linux x64 platform files"
  75. def linuxZipFile = new File(projectBuild,"linux-x64.zip")
  76. if (!linuxZipFile.exists()) {
  77. linuxZipFile << new URL(linux_x64).openStream()
  78. } else {
  79. println "Linux x64 platform file already exists, skipping download"
  80. }
  81. def linuxDir = new File(projectBuild, "linux-x64")
  82. copy {
  83. from(tarTree(resources.gzip(projectBuild.toPath().resolve("linux-x64.zip"))))
  84. into(linuxDir)
  85. }
  86. new File(linuxDir, "VSCode-linux-x64/resources/app/node_modules").renameTo(new File(linuxDir, "VSCode-linux-x64/resources/app/linux-x64"))
  87. list << new File(linuxDir, "VSCode-linux-x64/resources/app/linux-x64")
  88. def targetDir = new File(projectBuild, "gen/node_modules")
  89. def txtFile = new File(projectBuild, "gen/platform.txt")
  90. mergeDirectories(list, targetDir,txtFile)
  91. }
  92. def mergeDirectories(List<File> dirs, File targetDir, File outputFile = null) {
  93. def outputContent = new StringBuilder()
  94. if (!targetDir.exists()) {
  95. targetDir.mkdirs()
  96. }
  97. // Collect all file paths (relative paths), ignore .DS_Store
  98. def allFiles = []
  99. dirs.each { dir ->
  100. if (dir.exists()) {
  101. dir.eachFileRecurse { file ->
  102. if (file.isFile() && file.name != ".DS_Store") { // Ignore .DS_Store
  103. def relativePath = dir.toPath().relativize(file.toPath()).toString()
  104. allFiles << [dir: dir, file: file, relativePath: relativePath]
  105. }
  106. }
  107. }
  108. }
  109. // Group by relative path
  110. def groupedFiles = allFiles.groupBy { it.relativePath }
  111. groupedFiles.each { relativePath, entries ->
  112. def targetFile = new File(targetDir, relativePath)
  113. targetFile.parentFile.mkdirs()
  114. if (entries.size() == 1) {
  115. // Unique file, copy directly
  116. Files.copy(entries[0].file.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
  117. } else {
  118. // Check if all file contents are the same
  119. def uniqueHashes = entries.collect { entry ->
  120. def file = entry.file
  121. def digest = MessageDigest.getInstance("SHA-256")
  122. file.withInputStream { is ->
  123. byte[] buffer = new byte[8192]
  124. int read
  125. while ((read = is.read(buffer)) != -1) {
  126. digest.update(buffer, 0, read)
  127. }
  128. }
  129. def hash = digest.digest().encodeHex().toString()
  130. [hash: hash, file: file, dir: entry.dir]
  131. }.groupBy { it.hash }
  132. if (uniqueHashes.size() == 1) {
  133. // Same content, keep one copy
  134. Files.copy(entries[0].file.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
  135. } else {
  136. // Different content, add directory name as suffix
  137. if (outputFile) {
  138. outputContent.append("$relativePath\n")
  139. } else {
  140. println "$relativePath"
  141. }
  142. uniqueHashes.each { hash, files ->
  143. def sourceFile = files[0].file
  144. def dirName = files[0].dir.name
  145. def newName = targetFile.name + dirName
  146. def conflictFile = new File(targetFile.parentFile, newName)
  147. Files.copy(sourceFile.toPath(), conflictFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
  148. }
  149. }
  150. }
  151. }
  152. if (outputFile && outputContent.length() > 0) {
  153. outputFile.parentFile.mkdirs()
  154. outputFile.text = outputContent.toString()
  155. }
  156. }