From 2525f8174542543e80a165d073e6adc8c69c1f0b Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 10 Dec 2024 12:32:00 +0100 Subject: [PATCH] feat: store source info of a download, re-download if source changes --- src/data/import/download.rs | 22 +++++++++++++++------- src/download.rs | 9 ++++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/data/import/download.rs b/src/data/import/download.rs index 2285b995..0c748d98 100644 --- a/src/data/import/download.rs +++ b/src/data/import/download.rs @@ -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}; @@ -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() { @@ -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, @@ -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}"); @@ -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; @@ -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}")); } diff --git a/src/download.rs b/src/download.rs index 10af45ca..c4443e66 100644 --- a/src/download.rs +++ b/src/download.rs @@ -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; @@ -72,6 +72,13 @@ impl Download { } } + pub fn create_tagfile>(&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,