Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/downloadable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,25 @@ pub trait Downloadable {
return Ok(path);
}

let version = self.fetch_latest_version()?;
self.download(&version, language_server_id)
let downloaded = self
.fetch_latest_version()
.and_then(|version| self.download(&version, language_server_id));

match downloaded {
Ok(path) => Ok(path),
// The version check or download failed (e.g. GitHub API rate
// limiting) — an existing local installation is better than none.
Err(err) => match self.find_local() {
Some(path) => {
println!(
"Failed to update {}, falling back to local installation: {err}",
Self::INSTALL_PATH
);
Ok(path)
}
None => Err(err),
},
}
}

fn user_configured_path(
Expand Down
47 changes: 23 additions & 24 deletions src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{fs::metadata, path::PathBuf};

use zed_extension_api::{
self as zed, DownloadedFileType, GithubReleaseOptions, LanguageServerId,
LanguageServerInstallationStatus, Worktree, serde_json::Value,
set_language_server_installation_status,
self as zed, DownloadedFileType, LanguageServerId, LanguageServerInstallationStatus, Worktree,
serde_json::Value, set_language_server_installation_status,
};

use crate::{
Expand Down Expand Up @@ -58,15 +57,9 @@ impl Downloadable for Proxy {
}

fn fetch_latest_version(&self) -> zed::Result<String> {
Ok(zed::latest_github_release(
GITHUB_REPO,
GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)
.map_err(|err| format!("Failed to fetch latest proxy release from {GITHUB_REPO}: {err}"))?
.version)
// The proxy is built and released together with the extension, so the
// matching release is the one tagged with the extension's own version.
Ok(format!("v{}", env!("CARGO_PKG_VERSION")))
}

fn download(
Expand All @@ -82,14 +75,8 @@ impl Downloadable for Proxy {
return Ok(PathBuf::from(bin_path));
}

let release = zed::latest_github_release(
GITHUB_REPO,
GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)
.map_err(|err| format!("Failed to fetch proxy release: {err}"))?;
let release = zed::github_release_by_tag_name(GITHUB_REPO, version)
.map_err(|err| format!("Failed to fetch proxy release {version}: {err}"))?;

let asset = release
.assets
Expand Down Expand Up @@ -139,17 +126,29 @@ impl Downloadable for Proxy {
return Ok(path);
}

if let Ok(version) = self.fetch_latest_version()
&& let Ok(path) = self.download(&version, language_server_id)
{
let downloaded = self
.fetch_latest_version()
.and_then(|version| self.download(&version, language_server_id));

let download_err = match downloaded {
Ok(path) => return Ok(path),
Err(err) => err,
};

// The version check or download failed (e.g. GitHub API rate
// limiting) — an existing local installation is better than none.
if let Some(path) = self.find_local() {
println!("Failed to update proxy, falling back to local installation: {download_err}");
let s = path.to_string_lossy().to_string();
self.cached_path = Some(s);
return Ok(path);
}

if let Some(path) = worktree.which(proxy_exec().as_str()) {
return Ok(PathBuf::from(path));
}

Err(format!("'{}' not found", proxy_exec()))
Err(format!("'{}' not found: {download_err}", proxy_exec()))
}

fn user_configured_path(
Expand Down
54 changes: 28 additions & 26 deletions src/task.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{fs::metadata, path::PathBuf};

use zed_extension_api::{
self as zed, DownloadedFileType, GithubReleaseOptions, LanguageServerId,
LanguageServerInstallationStatus, Worktree, serde_json::Value,
set_language_server_installation_status,
self as zed, DownloadedFileType, LanguageServerId, LanguageServerInstallationStatus, Worktree,
serde_json::Value, set_language_server_installation_status,
};

use crate::{
Expand Down Expand Up @@ -47,17 +46,9 @@ impl Downloadable for TaskHelper {
}

fn fetch_latest_version(&self) -> zed::Result<String> {
Ok(zed::latest_github_release(
GITHUB_REPO,
GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)
.map_err(|err| {
format!("Failed to fetch latest task helper release from {GITHUB_REPO}: {err}")
})?
.version)
// The task helper is built and released together with the extension, so
// the matching release is the one tagged with the extension's own version.
Ok(format!("v{}", env!("CARGO_PKG_VERSION")))
}

fn download(
Expand All @@ -76,14 +67,8 @@ impl Downloadable for TaskHelper {
return Ok(PathBuf::from(bin_path));
}

let release = zed::latest_github_release(
GITHUB_REPO,
GithubReleaseOptions {
require_assets: true,
pre_release: false,
},
)
.map_err(|err| format!("Failed to fetch task helper release: {err}"))?;
let release = zed::github_release_by_tag_name(GITHUB_REPO, version)
.map_err(|err| format!("Failed to fetch task helper release {version}: {err}"))?;

let asset = release
.assets
Expand Down Expand Up @@ -133,17 +118,34 @@ impl Downloadable for TaskHelper {
return Ok(path);
}

if let Ok(version) = self.fetch_latest_version()
&& let Ok(path) = self.download(&version, language_server_id)
{
let downloaded = self
.fetch_latest_version()
.and_then(|version| self.download(&version, language_server_id));

let download_err = match downloaded {
Ok(path) => return Ok(path),
Err(err) => err,
};

// The version check or download failed (e.g. GitHub API rate
// limiting) — an existing local installation is better than none.
if let Some(path) = self.find_local() {
println!(
"Failed to update task helper, falling back to local installation: {download_err}"
);
let s = path.to_string_lossy().to_string();
self.cached_path = Some(s);
return Ok(path);
}

if let Some(path) = worktree.which(task_helper_exec().as_str()) {
return Ok(PathBuf::from(path));
}

Err(format!("'{}' not found", task_helper_exec()))
Err(format!(
"'{}' not found: {download_err}",
task_helper_exec()
))
}
}

Expand Down