|
| 1 | +use anyhow::{Context, Result, bail}; |
| 2 | +use std::{ |
| 3 | + fs::{self}, |
| 4 | + path::{Path, PathBuf}, |
| 5 | + time::Duration, |
| 6 | +}; |
| 7 | +use version_compare::Version; |
| 8 | + |
| 9 | +use crate::github; |
| 10 | + |
| 11 | +#[cfg(target_os = "windows")] |
| 12 | +const EXE_SUFFIX: &'static str = ".exe"; |
| 13 | + |
| 14 | +#[cfg(not(target_os = "windows"))] |
| 15 | +const EXE_SUFFIX: &str = ""; |
| 16 | + |
| 17 | +#[cfg(target_os = "linux")] |
| 18 | +const NAME: &str = "linux-x86-defold-nvim-bridge"; |
| 19 | + |
| 20 | +#[cfg(all(target_os = "macos", target_arch = "x86_64"))] |
| 21 | +const NAME: &str = "macos-x86-defold-nvim-bridge"; |
| 22 | + |
| 23 | +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] |
| 24 | +const NAME: &str = "macos-arm-defold-nvim-bridge"; |
| 25 | + |
| 26 | +#[cfg(target_os = "windows")] |
| 27 | +const NAME: &str = "windows-x86-defold-nvim-bridge"; |
| 28 | + |
| 29 | +const OWNER: &str = "atomicptr"; |
| 30 | +const REPOSITORY: &str = "defold.nvim"; |
| 31 | + |
| 32 | +pub fn path(plugin_root: &Path) -> Result<PathBuf> { |
| 33 | + let exe = exe_name(); |
| 34 | + |
| 35 | + if plugin_root.exists() { |
| 36 | + let candidates = [ |
| 37 | + plugin_root.join(&exe), |
| 38 | + plugin_root.join("target").join("debug").join(&exe), |
| 39 | + plugin_root.join("target").join("release").join(&exe), |
| 40 | + ]; |
| 41 | + |
| 42 | + if let Some(bridge_path) = candidates.into_iter().find(|p| p.exists()) { |
| 43 | + return Ok(bridge_path); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + install() |
| 48 | +} |
| 49 | + |
| 50 | +fn exe_name() -> String { |
| 51 | + format!("defold-nvim-bridge{EXE_SUFFIX}") |
| 52 | +} |
| 53 | + |
| 54 | +fn local_path() -> Result<PathBuf> { |
| 55 | + let dir = dirs::data_dir() |
| 56 | + .context("could not get state dir")? |
| 57 | + .join("defold.nvim") |
| 58 | + .join("bin"); |
| 59 | + |
| 60 | + fs::create_dir_all(&dir)?; |
| 61 | + |
| 62 | + Ok(dir.join(exe_name())) |
| 63 | +} |
| 64 | + |
| 65 | +fn version_path() -> Result<PathBuf> { |
| 66 | + let dir = dirs::data_dir() |
| 67 | + .context("could not get state dir")? |
| 68 | + .join("defold.nvim") |
| 69 | + .join("meta"); |
| 70 | + |
| 71 | + fs::create_dir_all(&dir)?; |
| 72 | + |
| 73 | + Ok(dir.join("bridge_version")) |
| 74 | +} |
| 75 | + |
| 76 | +fn version() -> Result<String> { |
| 77 | + let file = version_path()?; |
| 78 | + |
| 79 | + if !file.exists() { |
| 80 | + bail!("Version not found"); |
| 81 | + } |
| 82 | + |
| 83 | + Ok(fs::read_to_string(file)?) |
| 84 | +} |
| 85 | + |
| 86 | +fn is_update_available() -> Result<bool> { |
| 87 | + if version_path()?.exists() { |
| 88 | + // if the version file is younger than a week dont bother |
| 89 | + let last_modified = version_path()?.metadata()?.modified()?; |
| 90 | + if last_modified.elapsed()? < Duration::from_hours(24 * 7) { |
| 91 | + return Ok(false); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + let Ok(v) = version() else { |
| 96 | + return Ok(true); |
| 97 | + }; |
| 98 | + |
| 99 | + // re-write the file again so that we only check once a week |
| 100 | + fs::write(version_path()?, &v)?; |
| 101 | + |
| 102 | + tracing::debug!("Bridge Version {v} installed"); |
| 103 | + |
| 104 | + let Some(installed) = Version::from(&v) else { |
| 105 | + return Ok(true); |
| 106 | + }; |
| 107 | + |
| 108 | + let release = github::fetch_release(OWNER, REPOSITORY)?; |
| 109 | + |
| 110 | + tracing::debug!("Bridge Version {} is newest", release.tag_name); |
| 111 | + |
| 112 | + let Some(current) = Version::from(&release.tag_name) else { |
| 113 | + return Ok(false); |
| 114 | + }; |
| 115 | + |
| 116 | + Ok(current > installed) |
| 117 | +} |
| 118 | + |
| 119 | +fn install() -> Result<PathBuf> { |
| 120 | + let path = local_path()?; |
| 121 | + |
| 122 | + if path.exists() && !is_update_available()? { |
| 123 | + return local_path(); |
| 124 | + } |
| 125 | + |
| 126 | + let (downloaded_file, release) = github::download_release(OWNER, REPOSITORY, NAME)?; |
| 127 | + |
| 128 | + tracing::debug!("New Bridge version found {}", release.tag_name); |
| 129 | + |
| 130 | + fs::rename(downloaded_file, &path)?; |
| 131 | + fs::write(version_path()?, release.tag_name)?; |
| 132 | + |
| 133 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 134 | + { |
| 135 | + use std::{fs::Permissions, os::unix::fs::PermissionsExt}; |
| 136 | + fs::set_permissions(&path, Permissions::from_mode(0o700))?; |
| 137 | + } |
| 138 | + |
| 139 | + github::clear_downloads(OWNER, REPOSITORY)?; |
| 140 | + |
| 141 | + Ok(path) |
| 142 | +} |
0 commit comments