Skip to content

Commit 7d51e49

Browse files
committed
refactor neovide, mobdap and bridge download handler into single releases manager and improve error handling (in case of e.g. no internet)
1 parent c7b5e20 commit 7d51e49

File tree

14 files changed

+363
-360
lines changed

14 files changed

+363
-360
lines changed

.github/workflows/release.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
config:
1818
- name: Linux x86
1919
os: ubuntu-latest
20+
cargo-tools: cargo-edit,cargo-cross
2021
artifact:
2122
sidecar: target/x86_64-unknown-linux-gnu/release/libdefold_nvim_sidecar.so
2223
bridge: target/x86_64-unknown-linux-gnu/release/defold-nvim-bridge
@@ -25,6 +26,7 @@ jobs:
2526
bridge: target/release/linux-x86-defold-nvim-bridge
2627
- name: MacOS x86
2728
os: macos-15-intel
29+
cargo-tools: cargo-edit
2830
artifact:
2931
sidecar: target/release/libdefold_nvim_sidecar.dylib
3032
bridge: target/release/defold-nvim-bridge
@@ -33,6 +35,7 @@ jobs:
3335
bridge: target/release/macos-x86-defold-nvim-bridge
3436
- name: MacOS ARM
3537
os: macos-latest
38+
cargo-tools: cargo-edit
3639
artifact:
3740
sidecar: target/release/libdefold_nvim_sidecar.dylib
3841
bridge: target/release/defold-nvim-bridge
@@ -41,6 +44,7 @@ jobs:
4144
bridge: target/release/macos-arm-defold-nvim-bridge
4245
- name: Windows x86
4346
os: windows-latest
47+
cargo-tools: cargo-edit
4448
artifact:
4549
sidecar: target/release/defold_nvim_sidecar.dll
4650
bridge: target/release/defold-nvim-bridge.exe
@@ -53,11 +57,20 @@ jobs:
5357
- uses: actions-rust-lang/setup-rust-toolchain@v1
5458
with:
5559
rustflags: ""
60+
- uses: taiki-e/install-action@v2
61+
with:
62+
tool: ${{ matrix.config.cargo-tools }}
63+
64+
- name: set version ${{ matrix.config.name }}
65+
shell: bash
66+
run: |
67+
TAG_NAME=${GITHUB_REF#refs/tags/}
68+
VERSION=${TAG_NAME#v}
69+
cargo set-version $VERSION
5670
5771
- name: build ${{ matrix.config.name }}
5872
if: contains(matrix.config.os, 'ubuntu')
5973
run: |
60-
cargo install cargo-cross
6174
cargo cross build --release --target x86_64-unknown-linux-gnu
6275
6376
- name: build ${{ matrix.config.name }}

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bridge/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "defold-nvim-bridge"
3-
version = "0.1.0"
3+
version = "0.1.4"
44
edition = "2024"
55

66
[dependencies]

crates/core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "defold-nvim-core"
3-
version = "0.1.0"
3+
version = "0.1.4"
44
edition = "2024"
55

66
[dependencies]

crates/core/src/bridge.rs

Lines changed: 53 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1-
use anyhow::{Context, Result, bail};
1+
use anyhow::{Context, Result};
22
use std::{
33
fs::{self},
44
path::{Path, PathBuf},
5-
time::Duration,
65
};
76
use version_compare::Version;
87

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};
159

10+
const EXECUTABLE_NAME: &str = "defold-nvim-bridge";
11+
const OWNER: &str = "atomicptr";
12+
const REPOSITORY: &str = "defold.nvim";
1613
#[cfg(not(target_os = "windows"))]
1714
const EXE_SUFFIX: &str = "";
1815

1916
#[cfg(target_os = "linux")]
20-
const NAME: &str = "linux-x86-defold-nvim-bridge";
17+
const ASSET_NAME: &str = "linux-x86-defold-nvim-bridge";
2118

2219
#[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";
2421

2522
#[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";
2724

2825
#[cfg(target_os = "windows")]
29-
const NAME: &str = "windows-x86-defold-nvim-bridge";
26+
const ASSET_NAME: &str = "windows-x86-defold-nvim-bridge";
3027

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> {
3529
let exe = exe_name();
3630

37-
if plugin_root.exists() {
31+
if let Some(plugin_root) = plugin_root
32+
&& plugin_root.exists()
33+
{
3834
let candidates = [
39-
plugin_root.join(&exe),
4035
plugin_root.join("target").join("debug").join(&exe),
4136
plugin_root.join("target").join("release").join(&exe),
4237
];
@@ -50,7 +45,11 @@ pub fn path(plugin_root: &Path) -> Result<PathBuf> {
5045
}
5146

5247
fn exe_name() -> String {
53-
format!("defold-nvim-bridge{EXE_SUFFIX}")
48+
if cfg!(target_os = "windows") {
49+
"defold-nvim-bridge.exe".to_string()
50+
} else {
51+
"defold-nvim-bridge".to_string()
52+
}
5453
}
5554

5655
fn local_path() -> Result<PathBuf> {
@@ -64,96 +63,40 @@ fn local_path() -> Result<PathBuf> {
6463
Ok(dir.join(exe_name()))
6564
}
6665

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-
13566
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)
67+
let min_version = utils::version();
68+
let min_version = Version::from(&min_version);
69+
let curr_version = release_downloader::version(EXECUTABLE_NAME);
70+
let curr_version = curr_version
71+
.as_ref()
72+
.map(|v| Version::from(v))
73+
.ok()
74+
.flatten();
75+
76+
release_downloader::install_with(
77+
OWNER,
78+
REPOSITORY,
79+
ASSET_NAME,
80+
EXECUTABLE_NAME,
81+
|downloaded_file| {
82+
let path = local_path()?;
83+
84+
utils::move_file(downloaded_file, &path)?;
85+
86+
#[cfg(any(target_os = "linux", target_os = "macos"))]
87+
{
88+
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
89+
fs::set_permissions(&path, Permissions::from_mode(0o700))?;
90+
}
91+
92+
Ok(())
93+
},
94+
local_path,
95+
match (min_version, curr_version) {
96+
(Some(mv), Some(cv)) => mv > cv,
97+
_ => false,
98+
},
99+
)?;
100+
101+
local_path()
159102
}

crates/core/src/editor_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn create_runner_script(
105105
fs::create_dir_all(&dir)?;
106106

107107
let script_path = dir.join(format!("run.{SCRIPT_EXT}"));
108-
let bridge_path = bridge::path(plugin_root)?;
108+
let bridge_path = bridge::path(Some(plugin_root))?;
109109
let launch_pre_args = launcher_settings.bridge_pre_cli_args().join(" ");
110110
let launch_post_args = launcher_settings.bridge_post_cli_args().join(" ");
111111

crates/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ pub mod github;
99
pub mod mobdap;
1010
pub mod neovide;
1111
pub mod project;
12+
mod release_downloader;
1213
pub mod script_api;
1314
pub mod utils;

0 commit comments

Comments
 (0)