Skip to content

Commit

Permalink
product-info: drop BuildInfo.getActualIdeaBuild extension method, ins…
Browse files Browse the repository at this point in the history
…tead read the build number from product info #SCL-22564
  • Loading branch information
unkarjedy committed Jun 7, 2024
1 parent 0d4f01f commit 2919467
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CommunityUpdater(
def update(): Unit = {
topoSort(dependencies).foreach(update)

val actualBuildNumber = ideaBuildInfo.getActualIdeaBuild(baseDirectory)
val actualBuildNumber = context.productInfo.buildNumber
val buildNumber = ideaBuildInfo.buildNumber
if (buildNumber != actualBuildNumber) {
log.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,5 @@ package object download {
}
}

implicit class BuildInfoOps(private val buildInfo: BuildInfo) extends AnyVal {

/**
* @note build number can be obtained from two places:
* - build.txt
* - product-info.json
* In this method we use `build.txt` because has a primitive structure
*/
def getActualIdeaBuild(ideaRoot: Path): String = {
val buildTxt = ideaRoot.resolve("build.txt")
//example: `IU-241.17011.2`
val content = new String(Files.readAllBytes(buildTxt)).trim
content.substring(content.indexOf("-") + 1)
}
}

val NotFoundHttpResponseCode = 404
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class PluginRepoUtils(implicit ctx: InstallContext) extends PluginRepoApi {
case IntellijPlugin.Id(id, Some(version), channel, _) =>
MerketplaceUrls.download(id, version, channel)
case IntellijPlugin.Id(id, None, channel, _) =>
MerketplaceUrls.downloadViaPLuginManager(id, idea, channel)
MerketplaceUrls.downloadViaPluginManager(id, idea, channel)
}

private object MerketplaceUrls {
private val BaseUrl = "https://plugins.jetbrains.com"

def pluginsList(id: String, buildInfo: BuildInfo, channel: Option[String]): URL = {
val edition = buildInfo.edition.edition
val buildNumber = buildInfo.getActualIdeaBuild(ctx.baseDirectory)
val buildNumber = ctx.productInfo.buildNumber
val channelQuery = channel.fold("")(c => s"&channel=$c")
new URL(s"$BaseUrl/plugins/list?pluginId=$id$channelQuery&build=$edition-$buildNumber")
}
Expand All @@ -38,9 +38,9 @@ class PluginRepoUtils(implicit ctx: InstallContext) extends PluginRepoApi {
new URL(s"$BaseUrl/plugin/download?noStatistic=true&pluginId=$id&version=$version$channelQuery")
}

def downloadViaPLuginManager(id: String, buildInfo: BuildInfo, channel: Option[String]): URL = {
def downloadViaPluginManager(id: String, buildInfo: BuildInfo, channel: Option[String]): URL = {
val edition = buildInfo.edition.edition
val buildNumber = buildInfo.getActualIdeaBuild(ctx.baseDirectory)
val buildNumber = ctx.productInfo.buildNumber
val channelQuery = channel.fold("")(c => s"&channel=$c")
new URL(s"$BaseUrl/pluginManager?action=download&noStatistic=true&id=$id$channelQuery&build=$edition-$buildNumber")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class RepoPluginInstaller(buildInfo: BuildInfo)
private[plugin] def isPluginCompatibleWithIdea(metadata: PluginDescriptor)(implicit ctx: InstallContext): Boolean = {
val lower = metadata.sinceBuild.replaceAll("^.+-", "") // strip IC- / PC- etc. prefixes
val upper = metadata.untilBuild.replaceAll("^.+-", "")
val actualIdeaBuild = buildInfo.getActualIdeaBuild(ctx.baseDirectory)
val actualIdeaBuild = ctx.productInfo.buildNumber
val lowerValid = compareIdeaVersions(lower, actualIdeaBuild) <= 0
val upperValid = compareIdeaVersions(upper, actualIdeaBuild) >= 0
lowerValid && upperValid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.sbtidea.productInfo

import org.jetbrains.annotations.TestOnly
import org.jetbrains.sbtidea.PluginLogger as log
import spray.json.DefaultJsonProtocol.*

Expand All @@ -19,6 +20,10 @@ object ProductInfoParser {
jsonAst.convertTo[ProductInfo]
}

@TestOnly
def toJsonString(productInfo: ProductInfo): String =
productInfo.toJson(productInfoFormat).prettyPrint

private implicit def productInfoFormat: RootJsonFormat[ProductInfo] = jsonFormat8(ProductInfo)
private implicit def launchFormat: JsonFormat[Launch] = jsonFormat8(Launch)
private implicit def layoutItemFormat: RootJsonFormat[LayoutItem] = jsonFormat3(LayoutItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SearchPluginId(
private def searchPluginIdRemote(queryRaw: String): Map[String, (String, Boolean)] = {
try {
val query: String = URLEncoder.encode(queryRaw, "UTF-8")
val build: String = s"${buildInfo.edition.edition}-${buildInfo.getActualIdeaBuild(ideaRoot)}"
val build: String = s"${buildInfo.edition.edition}-${context.productInfo.buildNumber}"
val url: String = getMarketplaceSearchUrl(query, build)
val data: String = getHttpGetResponseString(url)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class IdeaInstallerTest extends AnyFunSuite with Matchers with IdeaMock with Tmp
val dist = getDistCopy
val ideaInstallRoot = installer.installDist(dist)
ideaInstallRoot.toFile.exists() shouldBe true
ideaInstallRoot.list.map(_.getFileName.toString) should contain allElementsOf Seq("lib", "bin", "plugins", "build.txt")
}

val fileNamesInRoot = ideaInstallRoot.list.map(_.getFileName.toString).toList
fileNamesInRoot should contain allElementsOf Seq(
"lib",
"bin",
"plugins",
"product-info.json",
"build.txt",
"dependencies.txt"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.sbtidea.download.plugin
import org.apache.commons.io.FileUtils
import org.jetbrains.sbtidea.download.BuildInfo
import org.jetbrains.sbtidea.download.api.InstallContext
import org.jetbrains.sbtidea.productInfo.{ProductInfo, ProductInfoParser}
import org.jetbrains.sbtidea.{IntelliJPlatform, IntellijPlugin}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.featurespec.AnyFeatureSpecLike
Expand All @@ -12,6 +13,20 @@ import java.nio.file.Files

class PluginRepoUtilsTest extends AnyFeatureSpecLike with BeforeAndAfterAll {

private def createDummyProductInfoJson(buildNumber: String): String = {
val productInfo = ProductInfo(
name = "",
version = "",
versionSuffix = "",
buildNumber = buildNumber,
productCode = "",
modules = Nil,
launch = Nil,
layout = Nil
)
ProductInfoParser.toJsonString(productInfo)
}

Feature("getPluginDownloadURL") {
val buildInfo = BuildInfo("1.2.3", IntelliJPlatform.IdeaUltimate)

Expand All @@ -20,8 +35,8 @@ class PluginRepoUtilsTest extends AnyFeatureSpecLike with BeforeAndAfterAll {
val downloadDir = Files.createTempDirectory("PluginRepoUtilsTest_downloadDir")

FileUtils.writeStringToFile(
baseDir.resolve("build.txt").toFile,
"IU-11.22.33-actual",
baseDir.resolve("product-info.json").toFile,
createDummyProductInfoJson("11.22.33-actual"),
"UTF-8"
)

Expand Down

0 comments on commit 2919467

Please sign in to comment.