Skip to content

Commit

Permalink
content scanning moved to content.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Feb 20, 2024
1 parent 8c697d9 commit f35d181
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
64 changes: 64 additions & 0 deletions crates/project/src/content.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::manifest::{Manifest, ManifestError};
use crate::source_tree::SourceTree;
use crate::FileError;


#[derive(Debug, Clone)]
Expand All @@ -17,6 +19,46 @@ impl ContentDirectory {
}
}

pub fn find_in<P>(path: P, manifest_required: bool, scan_recursively: bool) -> (Vec<Self>, Vec<FileError>)
where P: AsRef<Path> {
let mut contents = Vec::new();
let mut errors = Vec::new();

match std::fs::read_dir(path.as_ref()) {
Ok(iter) => {
for entry in iter {
match entry {
Ok(entry) => {
let content_path_candidate = entry.path();
if is_content_dir(&content_path_candidate, manifest_required) {
contents.push(ContentDirectory::new(content_path_candidate));
} else if content_path_candidate.is_dir() && scan_recursively {
let (inner_contents, inner_errors) = Self::find_in(content_path_candidate, manifest_required, scan_recursively);
contents.extend(inner_contents);
errors.extend(inner_errors);
}
},
Err(err) => {
errors.push(FileError {
path: path.as_ref().to_owned(),
error: Arc::new(err)
});
}
}
}
},
Err(err) => {
errors.push(FileError {
path: path.as_ref().to_owned(),
error: Arc::new(err)
});
}
}

(contents, errors)
}


pub fn path(&self) -> &Path {
&self.path
}
Expand Down Expand Up @@ -48,3 +90,25 @@ impl ContentDirectory {
SourceTree::new(script_root)
}
}


fn is_content_dir(path: &Path, manifest_required: bool) -> bool {
if !path.is_dir() {
return false;
}

let manifest_path = path.join(Manifest::FILE_NAME);
if manifest_path.exists() {
return true;
}
if manifest_required {
return false;
}

let scripts_path = path.join("content").join("scripts");
if scripts_path.is_dir() {
return true;
}

false
}
52 changes: 5 additions & 47 deletions crates/project/src/content_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{path::{Path, PathBuf}, sync::Arc};
use crate::{ContentDirectory, FileError, Manifest};
use std::path::{Path, PathBuf};
use crate::{ContentDirectory, FileError};


/// Looks for content in provided "repository" directories.
Expand Down Expand Up @@ -40,51 +40,9 @@ impl ContentRepositories {
self.errors.clear();

for repo in &self.repository_paths {
match std::fs::read_dir(repo) {
Ok(iter) => {
for entry in iter {
match entry {
Ok(entry) => {
let path = entry.path();
if path.is_dir() && is_content_dir(&path) {
self.found_content.push(ContentDirectory::new(path));
}
},
Err(err) => {
self.errors.push(FileError {
path: repo.to_owned(),
error: Arc::new(err)
});
}
}
}
},
Err(err) => {
self.errors.push(FileError {
path: repo.to_owned(),
error: Arc::new(err)
});
}
}
let (contents, errors) = ContentDirectory::find_in(repo, false, false);
self.found_content.extend(contents);
self.errors.extend(errors);
}
}
}


fn is_content_dir(path: &Path) -> bool {
if !path.is_dir() {
return false;
}

let manifest_path = path.join(Manifest::FILE_NAME);
if manifest_path.exists() {
return true;
}

let scripts_path = path.join("content").join("scripts");
if scripts_path.is_dir() {
return true;
}

false
}

0 comments on commit f35d181

Please sign in to comment.