Skip to content

Commit

Permalink
self update improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
UwUDev committed Aug 19, 2024
1 parent 17ad1c1 commit d44a455
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mlua::prelude::*;
use mlua::Value;

use crate::mods::*;
use crate::updater::self_update_balamod;
use crate::updater::{get_latest_cli_version, self_update};

mod core;
mod mods;
Expand Down Expand Up @@ -49,8 +49,8 @@ fn balalib(lua: &Lua) -> LuaResult<LuaTable> {
lua.create_function(|lua, mod_info: ModInfo| is_mod_present(lua, mod_info))?,
)?;
exports.set(
"self_update_balamod",
lua.create_function(|_, ()| self_update_balamod("v0.1.11"))?,
"self_update",
lua.create_function(|_, ()| self_update(get_latest_cli_version().as_str()))?,
)?;
exports.set("restart", lua.create_function(|_, ()| restart())?)?;
exports.set(
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/update.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
taskkill /f /im balatro.exe
balamod.exe -u -a
start balatro.exe
pause
6 changes: 6 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod tests {
use crate::utils::minify_lua;
use std::fs;
use crate::updater::get_latest_cli_version;

#[test]
fn test_update() {
Expand All @@ -26,4 +27,9 @@ mod tests {
r#"function test() print("Hello World!") a = function() print("Hello World!") end a() end test()"#
);
}

#[test]
fn test_get_last_cli_version() {
println!("Latest CLI version: {}", get_latest_cli_version());
}
}
53 changes: 45 additions & 8 deletions src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn need_update(current_version: String) -> LuaResult<bool> {
}

#[cfg(target_os = "windows")]
pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> {
pub fn self_update(cli_ver: &str) -> LuaResult<()> {
let url = format!(
"https://github.com/balamod/balamod/releases/download/{}/balamod-{}-windows.exe",
cli_ver, cli_ver
Expand All @@ -49,18 +49,24 @@ pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> {
std::fs::rename("balamod.tmp", "balamod.exe")?;
drop(file);

let output = std::process::Command::new("balamod.exe")
.arg("-u")
.arg("-a")
.output()?;
let script = include_bytes!("scripts/update.cmd");

println!("{:?}", output);
let mut file = std::fs::File::create("update.cmd").unwrap();
file.write_all(script).unwrap();
drop(file);

restart()
// opens it in a new cmd window
std::process::Command::new("cmd")
.arg("/C")
.arg("start")
.arg("update.cmd")
.spawn()?;

std::process::exit(0);
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> {
pub fn self_update(cli_ver: &str) -> LuaResult<()> {
use std::os::unix::fs::PermissionsExt;

let mut filename = format!("balamod-{}-", cli_ver);
Expand Down Expand Up @@ -114,3 +120,34 @@ pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> {

restart()
}

pub fn get_latest_cli_version() -> String {
let client = reqwest::blocking::Client::builder()
.user_agent("balalib")
.build()
.unwrap();

match client
.get("https://api.github.com/repos/balamod/balamod/releases")
.send()
{
Ok(response) => match response.text() {
Ok(text) => {
let releases: Vec<serde_json::Value> = serde_json::from_str(&text)
.expect(format!("Failed to parse json: {}", text).as_str());
let latest_version = releases
.iter()
.find(|release| {
!release["prerelease"].as_bool().unwrap()
&& !release["draft"].as_bool().unwrap()
})
.unwrap()["tag_name"]
.as_str()
.unwrap();
latest_version.to_string()
}
Err(_) => "0.0.0".to_string(),
},
Err(_) => "0.0.0".to_string(),
}
}

0 comments on commit d44a455

Please sign in to comment.