Simplify interface of latest_github_release
helper function
This commit is contained in:
parent
9d5111e86a
commit
209ff619ef
3 changed files with 29 additions and 27 deletions
|
@ -19,10 +19,17 @@ impl super::LspAdapter for CLspAdapter {
|
||||||
http: Arc<dyn HttpClient>,
|
http: Arc<dyn HttpClient>,
|
||||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||||
async move {
|
async move {
|
||||||
let version = latest_github_release("clangd/clangd", http, |release_name| {
|
let release = latest_github_release("clangd/clangd", http).await?;
|
||||||
format!("clangd-mac-{release_name}.zip")
|
let asset_name = format!("clangd-mac-{}.zip", release.name);
|
||||||
})
|
let asset = release
|
||||||
.await?;
|
.assets
|
||||||
|
.iter()
|
||||||
|
.find(|asset| asset.name == asset_name)
|
||||||
|
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||||
|
let version = GitHubLspBinaryVersion {
|
||||||
|
name: release.name,
|
||||||
|
url: asset.browser_download_url.clone(),
|
||||||
|
};
|
||||||
Ok(Box::new(version) as Box<_>)
|
Ok(Box::new(version) as Box<_>)
|
||||||
}
|
}
|
||||||
.boxed()
|
.boxed()
|
||||||
|
|
|
@ -25,14 +25,14 @@ struct NpmInfoDistTags {
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub(crate) struct GithubRelease {
|
pub(crate) struct GithubRelease {
|
||||||
name: String,
|
pub name: String,
|
||||||
assets: Vec<GithubReleaseAsset>,
|
pub assets: Vec<GithubReleaseAsset>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub(crate) struct GithubReleaseAsset {
|
pub(crate) struct GithubReleaseAsset {
|
||||||
name: String,
|
pub name: String,
|
||||||
browser_download_url: String,
|
pub browser_download_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn npm_package_latest_version(name: &str) -> Result<String> {
|
pub async fn npm_package_latest_version(name: &str) -> Result<String> {
|
||||||
|
@ -78,11 +78,10 @@ pub async fn npm_install_packages(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn latest_github_release(
|
pub(crate) async fn latest_github_release(
|
||||||
repo_name_with_owner: &str,
|
repo_name_with_owner: &str,
|
||||||
http: Arc<dyn HttpClient>,
|
http: Arc<dyn HttpClient>,
|
||||||
asset_name: impl Fn(&str) -> String,
|
) -> Result<GithubRelease, anyhow::Error> {
|
||||||
) -> Result<GitHubLspBinaryVersion> {
|
|
||||||
let mut response = http
|
let mut response = http
|
||||||
.get(
|
.get(
|
||||||
&format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),
|
&format!("https://api.github.com/repos/{repo_name_with_owner}/releases/latest"),
|
||||||
|
@ -91,24 +90,13 @@ pub async fn latest_github_release(
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.context("error fetching latest release")?;
|
.context("error fetching latest release")?;
|
||||||
|
|
||||||
let mut body = Vec::new();
|
let mut body = Vec::new();
|
||||||
response
|
response
|
||||||
.body_mut()
|
.body_mut()
|
||||||
.read_to_end(&mut body)
|
.read_to_end(&mut body)
|
||||||
.await
|
.await
|
||||||
.context("error reading latest release")?;
|
.context("error reading latest release")?;
|
||||||
|
|
||||||
let release: GithubRelease =
|
let release: GithubRelease =
|
||||||
serde_json::from_slice(body.as_slice()).context("error deserializing latest release")?;
|
serde_json::from_slice(body.as_slice()).context("error deserializing latest release")?;
|
||||||
let asset_name = asset_name(&release.name);
|
Ok(release)
|
||||||
let asset = release
|
|
||||||
.assets
|
|
||||||
.iter()
|
|
||||||
.find(|asset| asset.name == asset_name)
|
|
||||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
|
||||||
Ok(GitHubLspBinaryVersion {
|
|
||||||
name: release.name,
|
|
||||||
url: asset.browser_download_url.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,17 @@ impl LspAdapter for RustLspAdapter {
|
||||||
http: Arc<dyn HttpClient>,
|
http: Arc<dyn HttpClient>,
|
||||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||||
async move {
|
async move {
|
||||||
let version = latest_github_release("rust-analyzer/rust-analyzer", http, |_| {
|
let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?;
|
||||||
format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH)
|
let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
|
||||||
})
|
let asset = release
|
||||||
.await?;
|
.assets
|
||||||
|
.iter()
|
||||||
|
.find(|asset| asset.name == asset_name)
|
||||||
|
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||||
|
let version = GitHubLspBinaryVersion {
|
||||||
|
name: release.name,
|
||||||
|
url: asset.browser_download_url.clone(),
|
||||||
|
};
|
||||||
Ok(Box::new(version) as Box<_>)
|
Ok(Box::new(version) as Box<_>)
|
||||||
}
|
}
|
||||||
.boxed()
|
.boxed()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue