1
+ package io.gitpod.jetbrains.launcher
2
+
3
+ import com.google.gson.JsonParser
4
+ import okhttp3.HttpUrl.Companion.toHttpUrl
5
+ import okhttp3.OkHttpClient
6
+ import okhttp3.Request
7
+ import java.nio.file.Files
8
+ import java.nio.file.Path
9
+ import java.nio.file.StandardOpenOption
10
+
11
+ class IdeDownloader @JvmOverloads constructor(private val httpClient : OkHttpClient = OkHttpClient ()) {
12
+
13
+ private companion object {
14
+ const val ROBOT_PLUGIN_VERSION_DEFAULT = " 0.11.9"
15
+
16
+ fun getRobotServerPluginDownloadUrl (version : String ): String =
17
+ " https://packages.jetbrains.team/maven/p/ij/intellij-dependencies/com/intellij/remoterobot/robot-server-plugin/$version /robot-server-plugin-$version .zip"
18
+
19
+ fun getFeedsOsPropertyName () = when (Os .hostOS()) {
20
+ Os .WINDOWS -> " windowsZip"
21
+ Os .LINUX -> " linux"
22
+ Os .MAC -> " mac"
23
+ }
24
+ }
25
+
26
+ fun downloadAndExtractLatestEap (ide : Ide , toDir : Path ): Path {
27
+ val idePackage = downloadIde(ide, toDir)
28
+ return extractIde(idePackage, toDir)
29
+ }
30
+
31
+ @JvmOverloads
32
+ fun downloadRobotPlugin (toDir : Path , version : String = ROBOT_PLUGIN_VERSION_DEFAULT ): Path {
33
+ return downloadFile(getRobotServerPluginDownloadUrl(version), toDir.resolve(" robot-server-plugin-$version " ))
34
+ }
35
+
36
+ private fun extractIde (idePackage : Path , toDir : Path ): Path = when (Os .hostOS()) {
37
+ Os .LINUX -> extractTar(idePackage, toDir).single()
38
+ Os .MAC -> extractDmgApp(idePackage, toDir)
39
+ Os .WINDOWS -> {
40
+ val appDir = Files .createDirectory(toDir.resolve(idePackage.fileName.toString().substringBefore(" .win.zip" )))
41
+ extractZip(idePackage, appDir)
42
+ appDir
43
+ }
44
+ }
45
+
46
+ private fun downloadIde (ide : Ide , toDir : Path ): Path {
47
+ val ideDownloadLink = getIdeDownloadUrl(ide, httpClient)
48
+ val idePackageName = ideDownloadLink.substringAfterLast(" /" ).removeSuffix(" /" )
49
+ val targetFile = toDir.resolve(idePackageName)
50
+ return downloadFile(ideDownloadLink, targetFile)
51
+ }
52
+
53
+ private fun downloadFile (url : String , toFile : Path ): Path {
54
+ return httpClient.newCall(Request .Builder ().url(url).build()).execute().use { response ->
55
+ check(response.isSuccessful) { " failed to download file from $url " }
56
+ Files .newOutputStream(toFile, StandardOpenOption .CREATE_NEW ).use {
57
+ response.body!! .byteStream().buffered().copyTo(it)
58
+ }
59
+ toFile
60
+ }
61
+ }
62
+
63
+ private fun getIdeDownloadUrl (ide : Ide , httpClient : OkHttpClient ): String {
64
+ return httpClient.newCall(
65
+ Request .Builder ().url(
66
+ " https://data.services.jetbrains.com/products/releases" .toHttpUrl()
67
+ .newBuilder()
68
+ .addQueryParameter(" code" , ide.feedsCode)
69
+ .addQueryParameter(" platform" , getFeedsOsPropertyName())
70
+ .build()
71
+ ).build()
72
+ ).execute().use { response ->
73
+ check(response.isSuccessful) { " failed to get $ide feeds" }
74
+ JsonParser .parseReader(response.body!! .charStream())
75
+ .asJsonObject[ide.feedsCode]
76
+ .asJsonArray
77
+ .firstOrNull {
78
+ it.asJsonObject[" downloads" ]?.asJsonObject?.keySet()?.isNotEmpty() ? : false
79
+ }
80
+ ?.asJsonObject?.get(" downloads" )
81
+ ?.asJsonObject?.get(getFeedsOsPropertyName())
82
+ ?.asJsonObject?.get(" link" )
83
+ ?.asString ? : error(" no suitable ide found" )
84
+ }
85
+ }
86
+ }
0 commit comments