-
Notifications
You must be signed in to change notification settings - Fork 4
impl: support for latest_eap, latest_release and latest_installed placeholders
#237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fioan89
wants to merge
7
commits into
main
Choose a base branch
from
support-for-ide-version-placeholders-in-uri
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8c7703
impl: model for the IDE product
fioan89 01bcb66
impl: retrofit service to fetch IDEs
fioan89 a871581
impl: load and cache IDEs
fioan89 d5bf957
impl: initialize and pass the feed manager to URI handler
fioan89 ce3f67b
impl: support for `latest_eap`, `latest_release` and `latest_installe…
fioan89 98dc126
impl: UTs for URI handling and a couple of other optimizations
fioan89 1be2413
fix: UTs and add more scenarios
fioan89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.coder.toolbox.feed | ||
|
|
||
| import com.squareup.moshi.FromJson | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
| import com.squareup.moshi.ToJson | ||
|
|
||
| /** | ||
| * Represents a JetBrains IDE product from the feed API. | ||
| * | ||
| * The API returns an array of products, each with a code and a list of releases. | ||
| */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class IdeProduct( | ||
| @Json(name = "code") val code: String, | ||
| @Json(name = "intellijProductCode") val intellijProductCode: String, | ||
| @Json(name = "name") val name: String, | ||
| @Json(name = "releases") val releases: List<IdeRelease> = emptyList() | ||
| ) | ||
|
|
||
| /** | ||
| * Represents an individual release of a JetBrains IDE product. | ||
| */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class IdeRelease( | ||
| @Json(name = "build") val build: String, | ||
| @Json(name = "version") val version: String, | ||
| @Json(name = "type") val type: IdeType, | ||
| @Json(name = "date") val date: String | ||
| ) | ||
|
|
||
| /** | ||
| * Type of IDE release: release or EAP (Early Access Program) | ||
| */ | ||
| enum class IdeType { | ||
| RELEASE, | ||
| EAP, | ||
| UNSUPPORTED; | ||
|
|
||
| val value: String | ||
| get() = when (this) { | ||
| RELEASE -> "release" | ||
| EAP -> "eap" | ||
| UNSUPPORTED -> "unsupported" | ||
| } | ||
| } | ||
|
|
||
| class IdeTypeAdapter { | ||
| @FromJson | ||
| fun fromJson(type: String): IdeType { | ||
| return when (type.lowercase()) { | ||
| "release" -> IdeType.RELEASE | ||
| "eap" -> IdeType.EAP | ||
| else -> IdeType.UNSUPPORTED | ||
| } | ||
| } | ||
|
|
||
| @ToJson | ||
| fun toJson(type: IdeType): String = type.value | ||
| } | ||
|
|
||
| /** | ||
| * Simplified representation of an IDE for use in the plugin. | ||
| * | ||
| * Contains the essential information: product code, build number, version, and type. | ||
| */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class Ide( | ||
| val code: String, | ||
| val build: String, | ||
| val version: String, | ||
| val type: IdeType | ||
| ) { | ||
| companion object { | ||
| /** | ||
| * Create an Ide from an IdeProduct and IdeRelease. | ||
| */ | ||
| fun from(product: IdeProduct, release: IdeRelease): Ide { | ||
| return Ide( | ||
| code = product.intellijProductCode, | ||
| build = release.build, | ||
| version = release.version, | ||
| type = release.type | ||
| ) | ||
| } | ||
| } | ||
| } |
235 changes: 235 additions & 0 deletions
235
src/main/kotlin/com/coder/toolbox/feed/IdeFeedManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| package com.coder.toolbox.feed | ||
|
|
||
| import com.coder.toolbox.CoderToolboxContext | ||
| import com.coder.toolbox.plugin.PluginManager | ||
| import com.coder.toolbox.sdk.CoderHttpClientBuilder | ||
| import com.coder.toolbox.sdk.interceptors.Interceptors | ||
| import com.coder.toolbox.util.OS | ||
| import com.coder.toolbox.util.ReloadableTlsContext | ||
| import com.coder.toolbox.util.getOS | ||
| import com.squareup.moshi.Moshi | ||
| import com.squareup.moshi.Types | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import retrofit2.Retrofit | ||
| import retrofit2.converter.moshi.MoshiConverterFactory | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.exists | ||
| import kotlin.io.path.readText | ||
|
|
||
| /** | ||
| * Manages the caching and loading of JetBrains IDE product feeds. | ||
| * | ||
| * This manager handles fetching IDE information from JetBrains data services, | ||
| * caching the results locally, and supporting offline mode. | ||
| * | ||
| * Cache files are stored in platform-specific locations: | ||
| * - macOS: ~/Library/Application Support/JetBrains/Toolbox/plugins/com.coder.toolbox/ | ||
| * - Linux: ~/.local/share/JetBrains/Toolbox/plugins/com.coder.toolbox/ | ||
| * - Windows: %LOCALAPPDATA%/JetBrains/Toolbox/plugins/com.coder.toolbox/ | ||
| */ | ||
| class IdeFeedManager( | ||
| private val context: CoderToolboxContext, | ||
| feedService: JetBrainsFeedService? = null | ||
| ) { | ||
| private val moshi = Moshi.Builder() | ||
| .add(IdeTypeAdapter()) | ||
| .build() | ||
|
|
||
| // Lazy initialization of the feed service | ||
| private val feedService: JetBrainsFeedService by lazy { | ||
| if (feedService != null) return@lazy feedService | ||
|
|
||
| val interceptors = buildList { | ||
| add((Interceptors.userAgent(PluginManager.pluginInfo.version))) | ||
| add(Interceptors.logging(context)) | ||
| } | ||
| val okHttpClient = CoderHttpClientBuilder.build( | ||
| context, | ||
| interceptors, | ||
| ReloadableTlsContext(context.settingsStore.readOnly().tls) | ||
| ) | ||
|
|
||
| val retrofit = Retrofit.Builder() | ||
| .baseUrl("https://data.services.jetbrains.com/") | ||
| .client(okHttpClient) | ||
| .addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
| .build() | ||
|
|
||
| val feedApi = retrofit.create(JetBrainsFeedApi::class.java) | ||
| JetBrainsFeedService(context, feedApi) | ||
| } | ||
|
|
||
| private var cachedIdes: List<Ide>? = null | ||
|
|
||
| /** | ||
| * Lazily load the IDE list. | ||
| * | ||
| * This method will only execute once. Subsequent calls will return the cached result. | ||
| * | ||
| * If offline mode is enabled (via -Doffline=true), this will load from local cache files. | ||
| * Otherwise, it will fetch from the remote feeds and save to local cache. | ||
| * | ||
| * @return List of IDE objects from both release and EAP feeds | ||
| */ | ||
| suspend fun loadIdes(): List<Ide> { | ||
| // Return cached value if already loaded | ||
| cachedIdes?.let { return it } | ||
|
|
||
| val isOffline = isOfflineMode() | ||
| context.logger.info("Loading IDEs in ${if (isOffline) "offline" else "online"} mode") | ||
|
|
||
| val ides = if (isOffline) { | ||
| loadIdesOffline() | ||
| } else { | ||
| loadIdesOnline() | ||
| } | ||
|
|
||
| cachedIdes = ides | ||
| return ides | ||
| } | ||
|
|
||
| /** | ||
| * Load IDEs from local cache files in offline mode. | ||
| */ | ||
| private suspend fun loadIdesOffline(): List<Ide> = withContext(Dispatchers.IO) { | ||
| context.logger.info("Loading IDEs from local cache files") | ||
|
|
||
| val releaseIdes = loadFeedFromFile(getReleaseCachePath()) | ||
| val eapIdes = loadFeedFromFile(getEapCachePath()) | ||
|
|
||
| val allIdes = releaseIdes + eapIdes | ||
| context.logger.info("Loaded ${allIdes.size} IDEs from cache (${releaseIdes.size} release, ${eapIdes.size} EAP)") | ||
|
|
||
| allIdes | ||
| } | ||
|
|
||
| /** | ||
| * Fetch IDEs from remote feeds and cache them locally. | ||
| */ | ||
| private suspend fun loadIdesOnline(): List<Ide> { | ||
| context.logger.info("Fetching IDEs from remote feeds") | ||
|
|
||
| // Fetch from both feeds | ||
| val releaseIdes = try { | ||
| feedService.fetchReleaseFeed() | ||
| } catch (e: Exception) { | ||
| context.logger.warn(e, "Failed to fetch release feed") | ||
| emptyList() | ||
| } | ||
|
|
||
| val eapIdes = try { | ||
| feedService.fetchEapFeed() | ||
| } catch (e: Exception) { | ||
| context.logger.warn(e, "Failed to fetch EAP feed") | ||
| emptyList() | ||
| } | ||
|
|
||
| val allIdes = releaseIdes + eapIdes | ||
| context.logger.info("Fetched ${allIdes.size} IDEs from remote (${releaseIdes.size} release, ${eapIdes.size} EAP)") | ||
|
|
||
| return allIdes | ||
| } | ||
|
|
||
| /** | ||
| * Get the platform-specific cache directory path. | ||
| */ | ||
| private fun getCacheDirectory(): Path { | ||
| val os = getOS() | ||
| val userHome = System.getProperty("user.home") | ||
|
|
||
| val basePath = when (os) { | ||
| OS.MAC -> Path.of(userHome, "Library", "Application Support") | ||
| OS.LINUX -> Path.of(userHome, ".local", "share") | ||
| OS.WINDOWS -> { | ||
| val localAppData = System.getenv("LOCALAPPDATA") | ||
| ?: Path.of(userHome, "AppData", "Local").toString() | ||
| Path.of(localAppData) | ||
| } | ||
|
|
||
| null -> { | ||
| context.logger.warn("Unable to determine OS, using home directory for cache") | ||
| Path.of(userHome, ".cache") | ||
| } | ||
| } | ||
|
|
||
| return basePath.resolve("JetBrains/Toolbox/plugins/com.coder.toolbox") | ||
| } | ||
|
|
||
| /** | ||
| * Get the path for the release feed cache file. | ||
| */ | ||
| private fun getReleaseCachePath(): Path { | ||
| return getCacheDirectory().resolve(RELEASE_CACHE_FILE) | ||
| } | ||
|
|
||
| /** | ||
| * Get the path for the EAP feed cache file. | ||
| */ | ||
| private fun getEapCachePath(): Path { | ||
| return getCacheDirectory().resolve(EAP_CACHE_FILE) | ||
| } | ||
|
|
||
| /** | ||
| * Load a list of IDEs from a JSON file. | ||
| * | ||
| * @return List of IDEs, or empty list if the file doesn't exist or can't be read | ||
| */ | ||
| private suspend fun loadFeedFromFile(path: Path): List<Ide> = withContext(Dispatchers.IO) { | ||
| try { | ||
| if (!path.exists()) { | ||
| context.logger.info("Cache file does not exist: $path") | ||
| return@withContext emptyList() | ||
| } | ||
|
|
||
| val json = path.readText() | ||
| val listType = Types.newParameterizedType(List::class.java, Ide::class.java) | ||
| val adapter = moshi.adapter<List<Ide>>(listType) | ||
| val ides = adapter.fromJson(json) ?: emptyList() | ||
|
|
||
| context.logger.info("Loaded ${ides.size} IDEs from $path") | ||
| ides | ||
| } catch (e: Exception) { | ||
| context.logger.warn(e, "Failed to load feed from $path") | ||
| emptyList() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if offline mode is enabled via the -Doffline=true system property. | ||
| */ | ||
| private fun isOfflineMode(): Boolean { | ||
| return System.getProperty(OFFLINE_PROPERTY)?.toBoolean() == true | ||
| } | ||
|
|
||
| /** | ||
| * Find the best matching IDE based on the provided query criteria. | ||
| * | ||
| * This method filters the loaded IDEs by product code and type, optionally | ||
| * filtering by available builds, then returns the IDE with the highest build. | ||
| * | ||
| * @param productCode The IntelliJ product code (e.g., "RR" for RustRover) | ||
| * @param type The type of IDE release (RELEASE or EAP) | ||
| * @param availableBuilds List of acceptable builds to filter by | ||
| * @return The IDE with the highest build matching the criteria, or null if no match found | ||
| */ | ||
| suspend fun findBestMatch( | ||
| productCode: String, | ||
| type: IdeType, | ||
| availableBuilds: List<String> | ||
| ): Ide? { | ||
| val ides = loadIdes() | ||
|
|
||
| return ides | ||
| .filter { it.code == productCode } | ||
| .filter { it.type == type } | ||
| .filter { it.build in availableBuilds } | ||
| .maxByOrNull { it.build } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val RELEASE_CACHE_FILE = "release.json" | ||
| private const val EAP_CACHE_FILE = "eap.json" | ||
| private const val OFFLINE_PROPERTY = "offline" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.coder.toolbox.feed | ||
|
|
||
| /** | ||
| * Query object for finding the best matching IDE from loaded feeds. | ||
| * | ||
| * This encapsulates the filtering criteria for IDE selection, including | ||
| * product code, type (release/eap), and optionally available versions. | ||
| */ | ||
| data class IdeQuery( | ||
| /** | ||
| * The IntelliJ product code (e.g., "RR" for RustRover, "IU" for IntelliJ IDEA Ultimate) | ||
| */ | ||
| val productCode: String, | ||
|
|
||
| /** | ||
| * The type of IDE release to filter for | ||
| */ | ||
| val type: IdeType, | ||
|
|
||
| /** | ||
| * List of available builds to install. | ||
| */ | ||
| val availableBuilds: List<String> | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Linux we should first try
XDG_DATA_HOMEif set.I think we have some overlap with getting the Coder data dir in the settings store, I wonder if we can share some code here.
Actually, I wonder if
XDG_CACHE_HOME(defaults to~/.cache) may be more appropriate, based on the function name. On macOS I think the equivalent is~/Library/Caches?