|
1 | | -use anyhow::{Context, Result, bail}; |
| 1 | +use anyhow::{Context, Result}; |
2 | 2 | use std::{ |
3 | 3 | fs::{self}, |
4 | 4 | path::{Path, PathBuf}, |
5 | | - time::Duration, |
6 | 5 | }; |
7 | 6 | use version_compare::Version; |
8 | 7 |
|
9 | | -use crate::{github, utils}; |
10 | | - |
11 | | -const MIN_VERSION: &str = "0.0.0"; |
12 | | - |
13 | | -#[cfg(target_os = "windows")] |
14 | | -const EXE_SUFFIX: &'static str = ".exe"; |
| 8 | +use crate::{release_downloader, utils}; |
15 | 9 |
|
| 10 | +const EXECUTABLE_NAME: &str = "defold-nvim-bridge"; |
| 11 | +const OWNER: &str = "atomicptr"; |
| 12 | +const REPOSITORY: &str = "defold.nvim"; |
16 | 13 | #[cfg(not(target_os = "windows"))] |
17 | 14 | const EXE_SUFFIX: &str = ""; |
18 | 15 |
|
19 | 16 | #[cfg(target_os = "linux")] |
20 | | -const NAME: &str = "linux-x86-defold-nvim-bridge"; |
| 17 | +const ASSET_NAME: &str = "linux-x86-defold-nvim-bridge"; |
21 | 18 |
|
22 | 19 | #[cfg(all(target_os = "macos", target_arch = "x86_64"))] |
23 | | -const NAME: &str = "macos-x86-defold-nvim-bridge"; |
| 20 | +const ASSET_NAME: &str = "macos-x86-defold-nvim-bridge"; |
24 | 21 |
|
25 | 22 | #[cfg(all(target_os = "macos", target_arch = "aarch64"))] |
26 | | -const NAME: &str = "macos-arm-defold-nvim-bridge"; |
| 23 | +const ASSET_NAME: &str = "macos-arm-defold-nvim-bridge"; |
27 | 24 |
|
28 | 25 | #[cfg(target_os = "windows")] |
29 | | -const NAME: &str = "windows-x86-defold-nvim-bridge"; |
| 26 | +const ASSET_NAME: &str = "windows-x86-defold-nvim-bridge"; |
30 | 27 |
|
31 | | -const OWNER: &str = "atomicptr"; |
32 | | -const REPOSITORY: &str = "defold.nvim"; |
33 | | - |
34 | | -pub fn path(plugin_root: &Path) -> Result<PathBuf> { |
| 28 | +pub fn path(plugin_root: Option<&Path>) -> Result<PathBuf> { |
35 | 29 | let exe = exe_name(); |
36 | 30 |
|
37 | | - if plugin_root.exists() { |
| 31 | + if let Some(plugin_root) = plugin_root |
| 32 | + && plugin_root.exists() |
| 33 | + { |
38 | 34 | let candidates = [ |
39 | | - plugin_root.join(&exe), |
40 | 35 | plugin_root.join("target").join("debug").join(&exe), |
41 | 36 | plugin_root.join("target").join("release").join(&exe), |
42 | 37 | ]; |
@@ -64,96 +59,40 @@ fn local_path() -> Result<PathBuf> { |
64 | 59 | Ok(dir.join(exe_name())) |
65 | 60 | } |
66 | 61 |
|
67 | | -fn version_path() -> Result<PathBuf> { |
68 | | - let dir = dirs::data_dir() |
69 | | - .context("could not get data dir")? |
70 | | - .join("defold.nvim") |
71 | | - .join("meta"); |
72 | | - |
73 | | - fs::create_dir_all(&dir)?; |
74 | | - |
75 | | - Ok(dir.join("bridge_version")) |
76 | | -} |
77 | | - |
78 | | -fn version() -> Result<String> { |
79 | | - let file = version_path()?; |
80 | | - |
81 | | - if !file.exists() { |
82 | | - bail!("Version not found"); |
83 | | - } |
84 | | - |
85 | | - Ok(fs::read_to_string(file)?) |
86 | | -} |
87 | | - |
88 | | -fn is_update_available() -> Result<bool> { |
89 | | - if !local_path()?.exists() { |
90 | | - return Ok(true); |
91 | | - } |
92 | | - |
93 | | - if !version_path()?.exists() { |
94 | | - return Ok(true); |
95 | | - } |
96 | | - |
97 | | - let Ok(v) = version() else { |
98 | | - return Ok(true); |
99 | | - }; |
100 | | - |
101 | | - tracing::debug!("Bridge Version {v} installed"); |
102 | | - |
103 | | - let Some(installed) = Version::from(&v) else { |
104 | | - tracing::debug!("Couldnt parse version"); |
105 | | - return Ok(true); |
106 | | - }; |
107 | | - |
108 | | - // if min version is higher, force update |
109 | | - let min_version = Version::from(MIN_VERSION).expect("cant parse min version"); |
110 | | - if installed < min_version { |
111 | | - tracing::debug!("Bridge Min Version {MIN_VERSION} exceeded (current {v})"); |
112 | | - return Ok(true); |
113 | | - } |
114 | | - |
115 | | - // if the version file is younger than a week dont bother |
116 | | - let last_modified = version_path()?.metadata()?.modified()?; |
117 | | - if last_modified.elapsed()? < Duration::from_hours(24 * 7) { |
118 | | - return Ok(false); |
119 | | - } |
120 | | - |
121 | | - // re-write the file again so that we only check once a week |
122 | | - fs::write(version_path()?, &v)?; |
123 | | - |
124 | | - let release = github::fetch_release(OWNER, REPOSITORY)?; |
125 | | - |
126 | | - tracing::debug!("Bridge Version {} is newest", release.tag_name); |
127 | | - |
128 | | - let Some(current) = Version::from(&release.tag_name) else { |
129 | | - return Ok(false); |
130 | | - }; |
131 | | - |
132 | | - Ok(current > installed) |
133 | | -} |
134 | | - |
135 | 62 | fn install() -> Result<PathBuf> { |
136 | | - let path = local_path()?; |
137 | | - |
138 | | - if path.exists() && !is_update_available()? { |
139 | | - tracing::debug!("No update available for {}", path.display()); |
140 | | - return Ok(path); |
141 | | - } |
142 | | - |
143 | | - let (downloaded_file, release) = github::download_release(OWNER, REPOSITORY, NAME)?; |
144 | | - |
145 | | - tracing::debug!("New Bridge version found {}", release.tag_name); |
146 | | - |
147 | | - utils::move_file(&downloaded_file, &path)?; |
148 | | - fs::write(version_path()?, release.tag_name)?; |
149 | | - |
150 | | - #[cfg(any(target_os = "linux", target_os = "macos"))] |
151 | | - { |
152 | | - use std::{fs::Permissions, os::unix::fs::PermissionsExt}; |
153 | | - fs::set_permissions(&path, Permissions::from_mode(0o700))?; |
154 | | - } |
155 | | - |
156 | | - github::clear_downloads(OWNER, REPOSITORY)?; |
157 | | - |
158 | | - Ok(path) |
| 63 | + let min_version = utils::version(); |
| 64 | + let min_version = Version::from(&min_version); |
| 65 | + let curr_version = release_downloader::version(EXECUTABLE_NAME); |
| 66 | + let curr_version = curr_version |
| 67 | + .as_ref() |
| 68 | + .map(|v| Version::from(v)) |
| 69 | + .ok() |
| 70 | + .flatten(); |
| 71 | + |
| 72 | + release_downloader::install_with( |
| 73 | + OWNER, |
| 74 | + REPOSITORY, |
| 75 | + ASSET_NAME, |
| 76 | + EXECUTABLE_NAME, |
| 77 | + |downloaded_file| { |
| 78 | + let path = local_path()?; |
| 79 | + |
| 80 | + utils::move_file(downloaded_file, &path)?; |
| 81 | + |
| 82 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 83 | + { |
| 84 | + use std::{fs::Permissions, os::unix::fs::PermissionsExt}; |
| 85 | + fs::set_permissions(&path, Permissions::from_mode(0o700))?; |
| 86 | + } |
| 87 | + |
| 88 | + Ok(()) |
| 89 | + }, |
| 90 | + local_path, |
| 91 | + match (min_version, curr_version) { |
| 92 | + (Some(mv), Some(cv)) => mv > cv, |
| 93 | + _ => false, |
| 94 | + }, |
| 95 | + )?; |
| 96 | + |
| 97 | + local_path() |
159 | 98 | } |
0 commit comments