Skip to content

Commit

Permalink
feat: store source info of a download, re-download if source changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Dec 10, 2024
1 parent e212aff commit 2525f81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/data/import/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::{remove_dir_all, File};
use std::fs::remove_dir_all;

use anyhow::{Context as _, Error};
use camino::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -44,8 +44,15 @@ impl Import for Download {

let mut skip_download = false;
if tagfile.exists() {
// TODO: check if tagfile was created with same info
skip_download = true;
let tagfile_contents = std::fs::read_to_string(tagfile.as_path())?;
let tagfile_source =
bincode::deserialize_from::<_, Source>(tagfile_contents.as_bytes());

if let Ok(tagfile_source) = tagfile_source {
if tagfile_source == self.source {
skip_download = true;
}
}
}
if !skip_download {
if target_path.exists() {
Expand All @@ -61,7 +68,7 @@ impl Import for Download {
format!("cloning git url: \"{url}\" commit: \"{commit}\"")
})?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Git(Git::Branch {
url,
Expand All @@ -77,7 +84,7 @@ impl Import for Download {
format!("cloning git url: \"{url}\" branch/tag: \"{branch_or_tag}\"")
})?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Git(Git::Default { url }) => {
println!("IMPORT Git {url} -> {target_path}");
Expand All @@ -86,7 +93,7 @@ impl Import for Download {
.do_clone()
.with_context(|| format!("cloning git url: \"{url}\""))?;

File::create(tagfile)?;
self.create_tagfile(tagfile)?;
}
Source::Laze(name) => {
let mut at_least_one = false;
Expand All @@ -108,7 +115,8 @@ impl Import for Download {
.with_context(|| format!("creating {filename}"))?;
}
if at_least_one {
File::create(&tagfile).with_context(|| format!("creating {tagfile}"))?;
self.create_tagfile(&tagfile)
.with_context(|| format!("creating {tagfile}"))?;
} else {
return Err(anyhow!("could not import from laze defaults: {name}"));
}
Expand Down
9 changes: 8 additions & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module deals with "download:" directives
use std::borrow::Cow;
use std::{borrow::Cow, path::Path};

use anyhow::Result;
use im::HashMap;
Expand Down Expand Up @@ -72,6 +72,13 @@ impl Download {
}
}

pub fn create_tagfile<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let contents = bincode::serialize(&self.source)?;
std::fs::write(path, contents)?;
Ok(())
}

fn render(
&self,
module: &Module,
Expand Down

0 comments on commit 2525f81

Please sign in to comment.