Skip to content

Instantly share code, notes, and snippets.

@xymopen
Last active April 23, 2022 01:03
Show Gist options
  • Save xymopen/6fcb1e6ecfbf9db91b5809a9f61ad9b7 to your computer and use it in GitHub Desktop.
Save xymopen/6fcb1e6ecfbf9db91b5809a9f61ad9b7 to your computer and use it in GitHub Desktop.
IntelliJ IDEA Java decompiler wrapper script for PowerShell
############################################################################
#
# IntelliJ IDEA Java Decompiler wrapper script for Windows
#
# @see https://stackoverflow.com/questions/28389006/how-to-decompile-to-java-files-intellij-idea/30106981
# @see https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine
#
############################################################################
$IDEA_PATH = "$Env:ProgramFiles\JetBrains\IntelliJ IDEA 2020.1.1"
# Add default JVM options here. You can also use JAVA_OPTS to pass JVM options to this script.
$DEFAULT_JVM_OPTS = '-Xms128M', '-Xmx4g', '-XX:+UseG1GC'
$FERNFLOWER_OPTS = '-hdc=0', '-dgs=1', '-rsy=1', '-rbr=1', '-lit=1', '-nls=1', '-mpm=60'
$CLASSPATH = Resolve-Path "$IDEA_PATH\plugins\java-decompiler\lib\java-decompiler.jar"
$CLASS = 'org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler'
# Find java.exe
function Find-Java {
[OutputType([String])]
param ()
process {
if ($null -ne $Env:JAVA_HOME) {
try {
$JAVA_EXE = Join-Path $Env:JAVA_HOME (Join-Path 'bin' 'java.exe')
Test-Path -PathType Leaf $JAVA_EXE -ErrorAction Stop > $null
return $JAVA_EXE
} catch {
throw @"
JAVA_HOME is set to an invalid directory: $Env:JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
"@
}
} else {
try {
$JAVA_EXE = 'java.exe'
Get-Command -Type Application $JAVA_EXE -ErrorAction Stop > $null
return $JAVA_EXE
} catch {
throw @"
JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
"@
}
}
}
}
function Invoke-WhenInput {
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNull()]
[ScriptBlock]$ExpectingInput,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNull()]
[ScriptBlock]$NotExpectingInput,
[Parameter(ValueFromRemainingArguments, ValueFromPipelineByPropertyName)]
[AllowEmptyCollection()]
[Object[]]$ArgumentList
)
process {
foreach ($arg in $ArgumentList) {
if ($arg -NotLike '-*') {
return & $ExpectingInput @ArgumentList
break
}
}
return & $NotExpectingInput @ArgumentList
}
}
function Invoke-WithInput {
$Dest = $null
$IsTemp = $false
if (
(Test-Path -PathType Container -Path $args[-1]) -and
($null -ne $args[-2]) -and ($args[-2] -NotLike '-*')
) {
$args = $args[0..($args.Length - 1)]
$Dest = $args[-1]
} else {
$Dest = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path $Dest > $null
$IsTemp = $true
}
& (Find-Java) @DEFAULT_JVM_OPTS @JAVA_OPTS -classpath $CLASSPATH $CLASS @($FERNFLOWER_OPTS + $args) $Dest
$ExitCode = $LASTEXICODE
foreach ($Archive in Get-ChildItem -Path $Dest -Recurse -Attributes !Directory) {
$NewArchiveName = "$($Archive.BaseName).zip"
$NewArchivePath = Join-Path -Path $Dest -ChildPath $NewArchiveName
$NewArchiveDestionation = Join-Path -Path $Dest -ChildPath $Archive.BaseName
Rename-Item -Path (Join-Path -Path $Dest -ChildPath $Archive) -NewName $NewArchiveName
try {
Expand-Archive -Path $NewArchivePath -DestinationPath $NewArchiveDestionation -ErrorAction Stop
Remove-Item -Path $NewArchivePath
} catch {
Rename-Item -Path $NewArchivePath -NewName $Archive.Name
}
}
$LASTEXICODE = $ExitCode
if ($IsTemp) {
Move-Item -Path $Dest\* -Destination .
Remove-Item -Path $Dest
}
}
function Invoke-WithoutInput {
& (Find-Java) @DEFAULT_JVM_OPTS @JAVA_OPTS -classpath $CLASSPATH $CLASS @args
}
Invoke-WhenInput -ExpectingInput ${Function:Invoke-WithInput} -NotExpectingInput ${Function:Invoke-WithoutInput} -ArgumentList $args
exit $LASTEXICODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment