smaug_lib/sources/
file_source.rs

1use crate::dependency::Dependency;
2use crate::source::Source;
3use crate::sources::dir_source::DirSource;
4use log::*;
5use std::path::Path;
6use std::path::PathBuf;
7use walkdir::WalkDir;
8use zip_extensions::zip_extract;
9
10#[derive(Clone, Debug)]
11pub struct FileSource {
12    pub path: PathBuf,
13}
14
15impl Source for FileSource {
16    fn install(&self, dependency: &Dependency, destination: &Path) -> std::io::Result<()> {
17        trace!("Installing file at {}", self.path.display());
18        let cached = crate::smaug::cache_dir().join(dependency.clone().name);
19
20        rm_rf::ensure_removed(cached.clone()).expect("Couldn't remove directory");
21
22        trace!("Extracting zip to {}", cached.display());
23        zip_extract(&self.path.to_path_buf(), &cached)?;
24
25        trace!(
26            "Finding top level package directory in {}",
27            cached.display()
28        );
29
30        match find_package_dir(&cached) {
31            None => Err(std::io::Error::new(
32                std::io::ErrorKind::NotFound,
33                format!("No Smaug.toml file found in {}", cached.display()).as_str(),
34            )),
35            Some(dir) => DirSource { path: dir }.install(dependency, destination),
36        }
37    }
38}
39
40fn find_package_dir(path: &Path) -> Option<PathBuf> {
41    for entry in WalkDir::new(path) {
42        let entry = entry.unwrap();
43
44        if entry.file_name() == "Smaug.toml" {
45            return Some(entry.path().parent().unwrap().to_path_buf());
46        }
47    }
48
49    None
50}