Skip to content

Instantly share code, notes, and snippets.

@GhostTypes
Last active August 7, 2022 18:49
Show Gist options
  • Select an option

  • Save GhostTypes/5e7425aa36868aef5c6f416f74ccab9f to your computer and use it in GitHub Desktop.

Select an option

Save GhostTypes/5e7425aa36868aef5c6f416f74ccab9f to your computer and use it in GitHub Desktop.
Powershell tool to help with Meteor Addon development
$root = Get-Location
$gradle_build = Get-ChildItem $root\build.gradle
$gradle_props = Get-ChildItem $root\gradle.properties
$gradle_wrapper = Get-ChildItem $root\gradlew.bat
$addon_url = "https://github.com/MeteorDevelopment/meteor-addon-template/archive/refs/heads/main.zip"
$project_mode = $true
if (-not $gradle_build.Exists -or -not $gradle_props.Exists -or -not $gradle_wrapper.Exists) {
Write-Host -ForegroundColor Yellow "One or more project files are not in the current directory."
Write-Host -ForegroundColor Yellow "Project mode is now disabled."
$project_mode = $false
}
function MainMenu {
$choice = 1
do {
ShowMenu
do {
$inputValid = [int]::TryParse((Read-Host), [ref]$choice)
if (-not $inputValid) {
BadChoice
} else {
if ($choice -eq 1) { #sync gradle props
SyncGradleProps
} elseif ($choice -eq 2) { #build latest
DownloadLatestDevBuild
PatchGradle -mode "to_file"
BuildAddon
PatchGradle -mode "to_default"
} elseif ($choice -eq 3) { #build custom
$custom_ver = Read-Host "Dev build version?"
DownloadCustomDevBuild -version $custom_ver
PatchGradle -mode "to_file"
BuildAddon
PatchGradle -mode "to_default"
} elseif ($choice -eq 4) { #build offline
BuildAddon -offline $true
} elseif ($choice -eq 5) { #fix deps
cmd.exe /c $gradle_wrapper --refresh-dependencies *> $null
} elseif ($choice -eq 6) { #download template
DownloadTemplate
} elseif ($choice -eq 0) { #exit
Clear-Host
Write-Host -ForegroundColor Blue "AddonToolkit by GhostTypes"
Write-Host -ForegroundColor Cyan "https://github.com/StonedGiraffe"
}
}
} while (-not $inputValid)
} while (-not $choice -eq 0)
}
function BadChoice {
Write-Host -ForegroundColor Red "Invalid input..."
Start-Sleep -Seconds 1
ShowMenu
}
function ShowMenu {
Clear-Host
if ($project_mode -eq $false) {
Write-Host -ForegroundColor Blue "[Addon Toolkit by GhostTypes]"
Write-Host -ForegroundColor Cyan "[X] Sync gradle props with template"
Write-Host -ForegroundColor Cyan "[X] Build for latest dev build"
Write-Host -ForegroundColor Cyan "[X] Build for custom dev build"
Write-Host -ForegroundColor Cyan "[X] Build offline"
Write-Host -ForegroundColor Cyan "[X] Fix dependencies"
Write-Host -ForegroundColor Cyan "[6] Setup new addon project"
Write-Host -ForegroundColor Cyan "[0] Exit"
} else {
Write-Host -ForegroundColor Blue "[Addon Toolkit by GhostTypes]"
Write-Host -ForegroundColor Cyan "[1] Sync gradle props with template"
Write-Host -ForegroundColor Cyan "[2] Build for latest dev build"
Write-Host -ForegroundColor Cyan "[3] Build for custom dev build"
Write-Host -ForegroundColor Cyan "[4] Build offline"
Write-Host -ForegroundColor Cyan "[5] Fix dependencies"
Write-Host -ForegroundColor Cyan "[6] Setup new addon project"
Write-Host -ForegroundColor Cyan "[0] Exit"
}
}
function DownloadLatestDevBuild {
Clear-Host
try {
Write-Host "Downloading latest dev build..."
Invoke-WebRequest -Uri "https://meteorclient.com/download?devBuild=latest" -OutFile $root\meteor_client.jar
}
catch {
Write-Host -ForegroundColor Red "Download failed!"
Pause
Exit
}
}
function DownloadCustomDevBuild {
param ([Parameter()] [string] $version)
Clear-Host
try {
Write-Host "Downloading dev build" $version
Invoke-WebRequest -Uri "https://meteorclient.com/download?devBuild=${version}" -OutFile $root\meteor_client.jar
}
catch {
Write-Host -ForegroundColor Red "Download failed!"
Pause
Exit
}
}
function DownloadTemplate {
Clear-Host
try {
Write-Host "Downloading latest addon template src..."
Invoke-WebRequest -Uri $addon_url -OutFile $root\addon_template.zip
Write-Host "Extracting source..."
Expand-Archive $root\addon_template.zip -DestinationPath $root\addon_template
Remove-Item -Path $root\addon_template.zip
Write-Host -ForegroundColor Green "Source download success!"
Start-Sleep -Seconds 5
}
catch {
Write-Host -ForegroundColor Red "Download failed!"
Pause
Exit
}
}
function SyncGradleProps {
if (-not $project_mode -eq $true) { return }
Clear-Host
$prop_url = "https://raw.githubusercontent.com/MeteorDevelopment/meteor-addon-template/main/gradle.properties"
try {
Write-Host "Getting gradle properties from addon template..." #grab properties from addon template
Invoke-WebRequest -Uri $prop_url -OutFile $root\temp_props.txt
$prop_data = Get-Content -Path $root\temp_props.txt
Remove-Item -Path $root\temp_props.txt
#Write-Host $prop_data
$mc_ver = $prop_data | Select-String -Pattern "minecraft_version=*" #read properties
$yarn_ver = $prop_data | Select-String -Pattern "yarn_mappings=*"
$loader_ver = $prop_data | Select-String -Pattern "loader_version=*"
$mc_ver_disp = $mc_ver -replace "minecraft_version="
$yarn_disp_ver = $yarn_ver -replace "yarn_mappings="
$loader_disp_ver = $loader_ver -replace "loader_version=*"
Write-Host "Latest Addon Properties"
Write-Host "MC Version:" $mc_ver_disp
Write-Host "Yarn Version:" $yarn_disp_ver
Write-Host "Loader Version:" $loader_disp_ver
$local_prop = Get-Content -Path $gradle_props #read properties from current addon
$local_mc_ver = $local_prop | Select-String -Pattern "minecraft_version=*"
$local_yarn_ver = $local_prop | Select-String -Pattern "yarn_mappings=*"
$local_loader_ver = $local_prop | Select-String -Pattern "loader_version=*"
$local_mc_ver_disp = $local_mc_ver -replace "minecraft_version="
$local_yarn_disp_ver = $local_yarn_ver -replace "yarn_mappings="
$local_loader_disp_ver = $local_loader_ver -replace "loader_version="
Write-Host ""
Write-Host ""
Write-Host "Local Properties"
Write-Host "MC Version:" $local_mc_ver_disp
Write-Host "Yarn Version:" $local_yarn_disp_ver
Write-Host "Loader Version:" $local_loader_disp_ver
Write-Host ""
$do_sync = Read-Host -Prompt "Sync properties? (y/n)"
if ($do_sync -contains "Y" -or $do_sync -contains "y") {
$local_prop = $local_prop.Replace($local_mc_ver, $mc_ver) #replace
$local_prop = $local_prop.Replace($local_yarn_ver, $yarn_ver)
$local_prop = $local_prop.Replace($local_loader_ver, $loader_ver)
Set-Content -Path $gradle_props -Value $local_prop #save changes
Write-Host -ForegroundColor Green "Updated gradle.properties!"
Start-Sleep -Seconds 5
} else {
Write-Host "Didn't sync properties.."
Start-Sleep -Seconds 3
}
} catch {
Write-Host -ForegroundColor Red "Error while sycning gradle properties."
Pause
Exit
}
}
function PatchGradle {
param (
[Parameter()]
[string]
$mode
)
if (-not $project_mode -eq $true) { return }
$find, $replace
if ($mode -eq "to_file") { #build from local file
$find = 'modImplementation "meteordevelopment:meteor-client:${project.meteor_version}"'
$replace = "implementation files('meteor_client.jar')"
} elseif ($mode -eq "to_default") { #default build
$find = "implementation files('meteor_client.jar')"
$replace = 'modImplementation "meteordevelopment:meteor-client:${project.meteor_version}"'
} else { #do nothing
$find = ""
$replace = ""
}
try { #update build.gradle
(Get-Content $gradle_build).replace($find, $replace) | Set-Content $gradle_build
} catch {
Write-Host -ForegroundColor Red "Error while modifying build.gradle..."
Pause
}
}
function BuildAddon {
param (
[Parameter()]
[bool]
$offline
)
if (-not $project_mode -eq $true) { return }
Clear-Host
Write-Host "Building addon..."
$log
if ($offline -eq $true) {
Write-Host -ForegroundColor Yellow "(Using offline mode)"
cmd.exe /c $gradle_wrapper --offline build *> $log
} else {
cmd.exe /c $gradle_wrapper build *> $log
}
if (-not $log -contains "Success" -or $log -contains "BUILD FAILED") {
Write-Host -ForegroundColor Red "Build failed!"
Pause
} else {
Write-Host -ForegroundColor Green "Build success!"
Start-Sleep -Seconds 5
}
}
Clear-History
$Host.UI.RawUI.WindowTitle = "AddonToolkit 1.0 | GhostTypes"
$global:progressPreference = 'silentlyContinue' #hides the download bar from console
MainMenu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment