Skip to content

Commit

Permalink
fix grammars fetching, allow zstd archives
Browse files Browse the repository at this point in the history
  • Loading branch information
panekj committed Jul 13, 2024
1 parent a53a3e4 commit ae6e654
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ thiserror = { version = "1.0" }
toml = { version = "*" }
toml_edit = { version = "0.20.2", features = ["serde"] }
url = { version = "2.5.0" }
zstd = { version = "0.11.2" } # follow same version wasmtime-cache in lockfile

lsp-types = { version = "0.95.1", features = ["proposed"] } # not following semver, so should be locked to patch version updates only
psp-types = { git = "https://github.com/lapce/psp-types", rev = "f7fea28f59e7b2d6faa1034a21679ad49b3524ad" }
Expand Down
1 change: 1 addition & 0 deletions lapce-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tracing-log = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-appender = { workspace = true }
url = { workspace = true }
zstd = { workspace = true }
floem = { workspace = true }

pulldown-cmark = { version = "0.11.0" }
Expand Down
6 changes: 3 additions & 3 deletions lapce-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3764,16 +3764,16 @@ pub fn launch() {
});
std::thread::spawn(move || {
use self::grammars::*;
match find_release().map(|r| describe_release(&r.tag_name)) {
Ok(Ok(release)) => {
match find_release() {
Ok(release) => {
if let Err(e) = fetch_grammars(&release) {
trace!(TraceLevel::ERROR, "failed to fetch grammars: {e}");
}
if let Err(e) = fetch_queries(&release) {
trace!(TraceLevel::ERROR, "failed to fetch grammars: {e}");
}
}
Err(e) | Ok(Err(e)) => {
Err(e) => {
trace!(TraceLevel::ERROR, "failed to obtain release info: {e}");
}
}
Expand Down
48 changes: 22 additions & 26 deletions lapce-app/src/app/grammars.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{
env,
fs::{self, File},
fs::{self},
io::{Seek, Write},
path::PathBuf,
};

use anyhow::{anyhow, Context, Result};
use lapce_core::directory::Directory;

use crate::{tracing::*, update::ReleaseInfo};
use crate::update::ReleaseInfo;

fn get_github_api(url: &str) -> Result<String> {
let resp = reqwest::blocking::ClientBuilder::new()
Expand Down Expand Up @@ -61,24 +62,11 @@ pub fn find_release() -> Result<ReleaseInfo> {
Ok(release.to_owned())
}

pub fn describe_release(id: &str) -> Result<ReleaseInfo> {
let url = format!(
"https://api.github.com/repos/lapce/tree-sitter-grammars/releases/{id}"
);

let resp =
get_github_api(&url).context(format!("failed to find release {id}"))?;
let release: ReleaseInfo = serde_json::from_str(&resp)?;

Ok(release)
}

pub fn fetch_grammars(release: &ReleaseInfo) -> Result<()> {
let dir =
Directory::grammars_directory().context("can't get grammars directory")?;

let file_name =
format!("grammars-{}-{}.zip", env::consts::OS, env::consts::ARCH);
let file_name = format!("grammars-{}-{}", env::consts::OS, env::consts::ARCH);

download_release(dir, release, &file_name)?;

Expand All @@ -89,7 +77,7 @@ pub fn fetch_queries(release: &ReleaseInfo) -> Result<()> {
let dir =
Directory::queries_directory().context("can't get queries directory")?;

let file_name = "queries.zip";
let file_name = "queries";

download_release(dir, release, file_name)?;

Expand All @@ -113,22 +101,30 @@ fn download_release(
}

for asset in &release.assets {
if asset.name == file_name {
if asset.name.starts_with(file_name) {
let mut resp = reqwest::blocking::get(&asset.browser_download_url)?;
if !resp.status().is_success() {
return Err(anyhow!("download file error {}", resp.text()?));
}

let file = tempfile::tempfile()?;

{
let mut out = File::create(dir.join(file_name))?;
resp.copy_to(&mut out)?;
let file = &mut &file;
resp.copy_to(file)?;
file.flush()?;
file.rewind()?;
}

if asset.name.ends_with(".zip") {
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(&dir)?;
} else if asset.name.ends_with(".tar.zst") {
let mut archive =
tar::Archive::new(zstd::stream::read::Decoder::new(file)?);
archive.unpack(&dir)?;
}

let mut archive =
zip::ZipArchive::new(File::open(dir.join(file_name))?)?;
archive.extract(&dir)?;
if let Err(err) = fs::remove_file(dir.join(file_name)) {
trace!(TraceLevel::ERROR, "Failed to remove file: {err}");
};
fs::write(dir.join("version"), release.tag_name.clone())?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lapce-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ toml = { workspace = true }
tracing = { workspace = true }
tracing-log = { workspace = true }
url = { workspace = true }
zstd = { workspace = true }

lsp-types = { workspace = true }
psp-types = { workspace = true }
Expand All @@ -39,7 +40,6 @@ floem-editor-core = { workspace = true }

# proxy specific dependencies

zstd = "0.11.2" # follow same version wasmtime-cache in lockfile
dyn-clone = "1.0.16"
walkdir = "2.4.0"
locale_config = "0.3.0"
Expand Down

0 comments on commit ae6e654

Please sign in to comment.