Last active
January 17, 2025 22:59
-
-
Save instance-id/4c22359bb87859ed1574c1bd0900310b to your computer and use it in GitHub Desktop.
Update JetBrains github copilot plugin to allow current Rider EAP version
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
#!/usr/bin/env -S pwsh -noProfile -nologo | |
<# | |
.NOTES | |
============================================================ | |
Last Edit: 10/20/2024 | |
Created by: instance.id (https://github.com/instance-id) | |
Platform: Linux/Windows | |
Filename: github_copilot_eap.ps1 | |
PSVersion: Last tested with 7.4.x | |
============================================================ | |
.DESCRIPTION | |
Edit the github copilot Jetbrains plugin to allow it to work in the current Rider EAP | |
If the current version is 243.* but the plugin only allows 242.*, setting the parameter `-eapVersion '3'` will update the plugin to 243.* | |
10/20/24 - If no source path is provided, downloads the latest nightly into the current directory and updates it | |
.EXAMPLE | |
./github_copilot_eap.ps1 -sourcePath 'github-copilot-intellij-1.5.25.21-nightly.zip' -eapVersion '3' | |
#> | |
param( | |
[string]$sourcePath = '', | |
[string]$eapVersion = '3' | |
) | |
$currentPath = $PSScriptRoot | |
function DownloadNightly() { | |
$latestNightly = $(curl -s "https://plugins.jetbrains.com/api/plugins/17718/updates?channel=nightly&page=1&size=8") | ConvertFrom-Json | |
$latestPlugin = "https://downloads.marketplace.jetbrains.com/files/$($latestNightly[0].file)" | |
$pluginBaseName = $latestNightly[0].file -replace '17718/\d*/', '' | |
write-host "Downloading ${pluginBaseName} from $latestPlugin" | |
# Download the latest nightly version | |
Invoke-WebRequest -Uri $latestPlugin -OutFile $pluginBaseName | |
return $pluginBaseName | |
} | |
if (-not (Test-Path $sourcePath)) { | |
write-host "Downloading the latest nightly version of the github copilot plugin" | |
$downloadedPath = [System.IO.Path]::Combine($currentPath, $(DownloadNightly)) | |
if (-not (Test-Path $downloadedPath)) { | |
throw "Could not download the latest nightly version of the github copilot plugin" | |
} else { | |
$sourcePath = $downloadedPath | |
} | |
} | |
$sourceFile = [System.IO.Path]::GetFileName($sourcePath) | |
if (($sourceFile -notmatch 'github-copilot-intellij') -or ($sourceFile.EndsWith('.zip') -eq $false)) { | |
throw "Source file must be a github-copilot-intellij zip file" | |
} | |
$fileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile) | |
$fileNameJar = "${fileBaseName}.jar" | |
$pluginJar = "github-copilot-intellij/lib/${fileNameJar}" | |
$innerFilePathInJar = "META-INF/plugin.xml" | |
$sourceZip = $null | |
$jarFile = $null | |
$jarStream = $null | |
$jarArchive = $null | |
$pluginXmlStream = $null | |
$pluginXmlWriter = $null | |
try { | |
$sourceZip = [System.IO.Compression.ZipFile]::Open($sourcePath, 'Update') | |
$jarFile = $sourceZip.GetEntry($pluginJar) | |
if ($jarFile -eq $null) { | |
throw "Could not find ${pluginJar} in ${sourceFile}" | |
} | |
$jarStream = $jarFile.Open() | |
if ($jarStream -eq $null) { | |
throw "Could not open ${pluginJar} in ${sourceFile}" | |
} | |
$jarArchive = [System.IO.Compression.ZipArchive]::new($jarStream, [System.IO.Compression.ZipArchiveMode]::Update) | |
if ($jarArchive -eq $null) { | |
throw "Could not open ${pluginJar} in ${sourceFile}" | |
} | |
$pluginXml = $jarArchive.GetEntry($innerFilePathInJar) | |
if ($pluginXml -eq $null) { | |
throw "Could not find ${innerFilePathInJar} in ${pluginJar}" | |
} | |
$pluginXmlStream = $pluginXml.Open() | |
$pluginXmlReader = [System.IO.StreamReader]::new($pluginXmlStream) | |
$pluginXmlContent = $pluginXmlReader.ReadToEnd() | |
$pluginXmlReader.Close() | |
$pluginXmlStream.Close() | |
[xml]$xmlDoc = $pluginXmlContent | |
$ideaVersionNode = $xmlDoc.'idea-plugin'.'idea-version' | |
if ($null -eq $ideaVersionNode) { | |
throw "Could not find 'idea-version' node in ${innerFilePathInJar}" | |
} | |
$untilBuildAttr = $ideaVersionNode.'until-build' | |
if ($null -eq $untilBuildAttr) { | |
throw "Could not find 'until-build' attribute in 'idea-version' node" | |
} | |
$untilBuildValue = $ideaVersionNode.'until-build' | |
if ($untilBuildValue -match '(\d)(\d)(\d)(\.\*)') { | |
$firstPart = [int]$matches[1] | |
$secondPart = [int]$matches[2] | |
$fourthPart = $matches[4] | |
$newUntilBuildValue = "${firstPart}$(($secondPart+1))$eapVersion$($fourthPart)" | |
if ($newUntilBuildValue -eq $untilBuildValue) { | |
write-host "The 'until-build' attribute value is already set to the expected value" | |
exit 0 | |
} | |
$ideaVersionNode.'until-build' = $newUntilBuildValue | |
} else { | |
throw "The 'until-build' attribute value does not match the expected pattern" | |
} | |
$pluginXmlStream = $pluginXml.Open() | |
$pluginXmlWriter = [System.IO.StreamWriter]::new($pluginXmlStream) | |
$xmlDoc.Save($pluginXmlWriter) | |
$pluginXmlWriter.Close() | |
$pluginXmlStream.Close() | |
$jarArchive.Dispose() | |
$jarStream.Close() | |
$sourceZip.Dispose() | |
write-host "Updated the 'until-build' attribute value in ${innerFilePathInJar} to ${newUntilBuildValue}" | |
} catch { $_ } | |
finally { | |
if ($null -ne $jarArchive) { $jarArchive.Dispose() } | |
if ($null -ne $jarStream) { $jarStream.Close() } | |
if ($null -ne $pluginXmlStream) { $pluginXmlStream.Close() } | |
if ($null -ne $pluginXmlWriter) { $pluginXmlWriter.Close() } | |
if ($null -ne $sourceZip) { $sourceZip.Dispose() } | |
} |
Updated to support going from Ex: 243.* to 251.*
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mac version