Skip to content

Commit

Permalink
Use Git for downloading modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fkorotkov committed May 30, 2024
1 parent 8f78960 commit bc4ac28
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
intellij {
pluginName.set("Starlark")
version.set("2022.1.1")
plugins.set(listOf("PythonCore:221.5591.52", "com.intellij.java"))
plugins.set(listOf("PythonCore:221.5591.52", "com.intellij.java", "Git4Idea"))
downloadSources.set(true)
updateSinceUntilBuild.set(false) // let's leave until open ended
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class CirrusModuleManager : IndexableSetContributor() {
internal val globalCacheLocation: Path
get() = Paths.get(PathManager.getSystemPath(), "extStarlarkModules")

fun modulePath(module: ModuleLocator): Path = globalCacheLocation.resolve(module.org).resolve(module.repo)
fun modulePath(module: ModuleLocator): Path =
globalCacheLocation.resolve(module.org)
.resolve(module.repo)
.resolve(module.revision ?: "main")
}

override fun getAdditionalRootsToIndex(): Set<VirtualFile> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,54 @@ import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.platform.templates.github.DownloadUtil
import com.intellij.platform.templates.github.ZipUtil
import git4idea.checkout.GitCheckoutProvider
import git4idea.commands.Git
import git4idea.commands.GitCommand
import git4idea.commands.GitLineHandler
import org.cirruslabs.intellij.starlark.StarlarkBundle
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.util.zip.ZipInputStream
import kotlin.io.path.name

class DownloadGitHubModuleQuickFix(val module: ModuleLocator) : LocalQuickFix {
class DownloadGitHubModuleQuickFix(private val module: ModuleLocator) : LocalQuickFix {
constructor(module: String) : this(ModuleLocator.parse(module))

override fun getFamilyName(): String =
"Download ${module.org}/${module.repo} module"

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val containingFile = descriptor.psiElement.containingFile

ProgressManager.getInstance().run(object : Backgroundable(project, StarlarkBundle.getMessage("starlark.intention.fetch.progress.fetching.module")) {
override fun run(indicator: ProgressIndicator) {
val moduleURL = "https://github.com/${module.org}/${module.repo}/archive/${module.revision ?: "main"}.zip"
val modulePath = CirrusModuleManager.modulePath(module)
val git = Git.getInstance()

val moduleParentDir = VfsUtil.createDirectories(modulePath.parent.toString())
moduleParentDir.findChild(modulePath.name)?.also {
ApplicationManager.getApplication().runWriteAction { it.delete(this@DownloadGitHubModuleQuickFix) }
}

val tmpFile = File.createTempFile("download", "module")
tmpFile.deleteOnExit()
try {
DownloadUtil.downloadAtomically(indicator, moduleURL, tmpFile)
FileUtil.delete(modulePath)
ZipUtil.unzip(indicator, modulePath, ZipInputStream(BufferedInputStream(FileInputStream(tmpFile))), null, null, true)
} finally {
tmpFile.delete()
val success = GitCheckoutProvider.doClone(
project,
git,
modulePath.name, modulePath.parent.toString(),
"git@github.com:${module.org}/${module.repo}.git"
)
if (!success) {
return
}

val moduleDir = VirtualFileManager.getInstance().refreshAndFindFileByNioPath(modulePath) ?: return

if (module.revision != null && module.revision != "main") {
val handler = GitLineHandler(project, moduleDir, GitCommand.CHECKOUT)
handler.setSilent(false)
handler.setStdoutSuppressed(false)
handler.addParameters("-b", module.revision ?: "main")
handler.endOptions()
git.runCommand(handler).throwOnError()
}
// refresh VFS so the files are picked up before requesting a code analyzer to restart
VirtualFileManager.getInstance().refreshAndFindFileByNioPath(modulePath.resolve(module.path ?: "lib.star"))
ApplicationManager.getApplication().invokeLater {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.cirruslabs.intellij.starlark.sdk

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.vfs.VirtualFile
import org.cirruslabs.intellij.starlark.StarlarkBundle
import org.cirruslabs.intellij.starlark.modules.CirrusModuleManager

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<description>Code assistance for Cirrus specific Starlark configuration scripts and hooks</description>
<vendor>@fedor</vendor>

<idea-version since-build="221.*"/>
<idea-version since-build="221"/>

<resource-bundle>StarlarkBundle</resource-bundle>

<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.python</depends>
<depends>Git4Idea</depends>

<extensions defaultExtensionNs="com.intellij">
<fileType name="Starlark" extensions="star" language="Starlark"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/StarlarkBundle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
starlark.name=Starlark
starlark.description=Starlark script
starlark.unresolved.module.reference=External starlark module is not yet cached locally
starlark.unresolved.module.reference=External Starlark module is not yet cached locally
starlark.intention.fetch.progress.fetching.module=Fetching external Starlark module

0 comments on commit bc4ac28

Please sign in to comment.