Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Use cached template when it is up-to-date #11

Merged
merged 12 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 27 additions & 25 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn get_current_sha() -> Result<String, CliError> {
let response_text = response.text().await.ok().unwrap_or("{}".to_string());
match &serde_json::from_str::<Value>(&response_text).unwrap_or_default()["sha"] {
Value::String(str) => Ok(str.clone()),
_ => unreachable!("Internal error: GitHub API broken"),
_ => Err(CliError::MalformedResponse),
}
}

Expand All @@ -52,34 +52,36 @@ async fn fetch_template() -> Result<Template, CliError> {
data: bytes.to_vec(),
sha: get_current_sha().await.ok(),
};
store_cached_template(template.clone());
store_cached_template(template.clone()).await;
Ok(template)
}

#[cfg(feature = "fetch-template")]
fn get_cached_template() -> Option<Template> {
let sha = cached_template_dir()
.and_then(|path| fs::read_to_string(path.with_file_name("cache-id.txt")).ok());
cached_template_dir()
.map(|path| path.with_file_name("vexide-template.tar.gz"))
.and_then(|cache_file| fs::read(cache_file).ok())
.map(|data: Vec<u8>| Template { data, sha })
.inspect(|template| {log::debug!("Found cached template with sha: {:?}", template.sha)})
async fn get_cached_template() -> Option<Template> {
match cached_template_dir() {
Some(dir) => {
let cache_file = dir.with_file_name("vexide-template.tar.gz");
let sha_file = dir.with_file_name("cache-id.txt");
let sha = tokio::fs::read_to_string(sha_file).await.ok();
let data = tokio::fs::read(cache_file).await.ok();
data.map(|data| Template {data, sha})
}
None => {
None
}
}
}

#[cfg(feature = "fetch-template")]
fn store_cached_template(template: Template) -> () {
cached_template_dir()
.map(|path| path.with_file_name("vexide-template.tar.gz"))
.map(|cache_file| fs::write(cache_file, &template.data));
cached_template_dir()
.map(|path| path.with_file_name("cache-id.txt"))
.map(|sha_file| {
if let Some(sha) = template.sha {
let _ = fs::write(sha_file, sha);
}
});

async fn store_cached_template(template: Template) -> () {
if let Some(dir) = cached_template_dir() {
let cache_file = dir.with_file_name("vexide-template.tar.gz");
let sha_file = dir.with_file_name("cache-id.txt");
ion098 marked this conversation as resolved.
Show resolved Hide resolved
let _ = tokio::fs::write(cache_file, &template.data).await;
if let Some(sha) = template.sha {
let _ = tokio::fs::write(sha_file, sha).await;
}
}
}

#[cfg(feature = "fetch-template")]
Expand Down Expand Up @@ -138,7 +140,7 @@ pub async fn new(
info!("Creating new project at {:?}", dir);

#[cfg(feature = "fetch-template")]
let template = get_cached_template();
let template = get_cached_template().await;

ion098 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "fetch-template")]
let template = match (
Expand Down Expand Up @@ -175,9 +177,9 @@ pub async fn new(

debug!("Renaming project to {}...", &name);
let manifest_path = dir.join("Cargo.toml");
let manifest = std::fs::read_to_string(&manifest_path)?;
let manifest = tokio::fs::read_to_string(&manifest_path).await?;
let manifest = manifest.replace("vexide-template", &name);
std::fs::write(manifest_path, manifest)?;
tokio::fs::write(manifest_path, manifest).await?;

info!("Successfully created new project at {:?}", dir);
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub enum CliError {
#[diagnostic(code(cargo_v5::bad_response))]
ReqwestError(#[from] reqwest::Error),

#[cfg(feature = "fetch-template")]
ion098 marked this conversation as resolved.
Show resolved Hide resolved
#[error("Recieved a malformed HTTP response")]
#[diagnostic(code(cargo_v5::malformed_response))]
MalformedResponse,

#[error(transparent)]
#[diagnostic(code(cargo_v5::image_error))]
ImageError(#[from] ImageError),
Expand Down