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

183 wildcard include does not work in windows #187

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions .github/workflows/build-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ jobs:
run: yarn run build
working-directory: ./frontend
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
rust: [stable]
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
- name: Install ${{ matrix.rust }}
uses: actions-rs/toolchain@v1
with:
toolchain: stable
toolchain: ${{ matrix.rust }}
override: true
- uses: actions/cache@v3
with:
path: |
Expand Down
9 changes: 5 additions & 4 deletions core/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock};

use bigdecimal::Zero;
use glob::Pattern;
use itertools::Itertools;
use itertools::{Either, Itertools};
use log::{error, info};
use zhang_ast::{Directive, DirectiveType, Spanned, Transaction};

Expand All @@ -21,7 +21,7 @@ use crate::ZhangResult;
pub struct Ledger {
pub entry: (PathBuf, String),

pub visited_files: Vec<Pattern>,
pub visited_files: Vec<Either<Pattern, PathBuf>>,

pub options: InMemoryOptions,

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Ledger {
}

fn process(
directives: Vec<Spanned<Directive>>, entry: (PathBuf, String), visited_files: Vec<Pattern>, transformer: Arc<dyn Transformer>,
directives: Vec<Spanned<Directive>>, entry: (PathBuf, String), visited_files: Vec<Either<Pattern, PathBuf>>, transformer: Arc<dyn Transformer>,
) -> ZhangResult<Ledger> {
let (meta_directives, dated_directive): (Vec<Spanned<Directive>>, Vec<Spanned<Directive>>) =
directives.into_iter().partition(|it| it.datetime().is_none());
Expand Down Expand Up @@ -188,6 +188,7 @@ mod test {
use std::sync::Arc;

use glob::Pattern;
use itertools::Either;
use tempfile::tempdir;
use zhang_ast::{Directive, SpanInfo, Spanned};

Expand Down Expand Up @@ -222,7 +223,7 @@ mod test {
Ledger::process(
test_parse_zhang(content),
(temp_dir.clone(), "example.zhang".to_string()),
vec![Pattern::new(temp_dir.join("example.zhang").as_path().to_str().unwrap()).unwrap()],
vec![Either::Left(Pattern::new(temp_dir.join("example.zhang").as_path().to_str().unwrap()).unwrap())],
Arc::new(TestTransformer {}),
)
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod test {
use std::sync::Arc;

use glob::Pattern;
use itertools::Either;
use serde_json_path::JsonPath;
use tempfile::tempdir;
use zhang_ast::{Directive, Spanned};
Expand All @@ -42,7 +43,7 @@ mod test {
let result: Vec<Spanned<Directive>> = parse_zhang(&string, file).expect("cannot read file");
Ok(TransformResult {
directives: result,
visited_files: vec![Pattern::new("example.zhang").unwrap()],
visited_files: vec![Either::Left(Pattern::new("example.zhang").unwrap())],
})
}
}
Expand Down
89 changes: 56 additions & 33 deletions core/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::collections::{HashSet, VecDeque};
use std::collections::VecDeque;
use std::path::PathBuf;
use std::str::FromStr;

use glob::{glob, Pattern};
use itertools::Itertools;
use itertools::Either;
use log::debug;
use zhang_ast::{Directive, Spanned};

use crate::error::IoErrorIntoZhangError;
use crate::utils::to_glob_or_path;
use crate::{utils, ZhangResult};

pub struct TransformResult {
pub directives: Vec<Spanned<Directive>>,
pub visited_files: Vec<Pattern>,
pub visited_files: Vec<Either<Pattern, PathBuf>>,
}

pub trait Transformer
Expand Down Expand Up @@ -43,46 +45,67 @@
let entry = entry.canonicalize().with_path(&entry)?;
let main_endpoint = entry.join(endpoint);
let main_endpoint = main_endpoint.canonicalize().with_path(&main_endpoint)?;
let mut load_queue: VecDeque<Pattern> = VecDeque::new();
load_queue.push_back(Pattern::new(main_endpoint.as_path().to_str().unwrap()).unwrap());
let mut load_queue: VecDeque<Either<Pattern, PathBuf>> = VecDeque::new();
let a = to_glob_or_path(main_endpoint);
load_queue.push_back(a);

Check warning on line 50 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L48-L50

Added lines #L48 - L50 were not covered by tests

let mut visited: HashSet<Pattern> = HashSet::new();
let mut visited: Vec<Either<Pattern, PathBuf>> = Vec::new();

Check warning on line 52 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L52

Added line #L52 was not covered by tests
let mut directives = vec![];
while let Some(load_entity) = load_queue.pop_front() {
debug!("visited path pattern: {}", load_entity);
for entry in glob(load_entity.as_str()).unwrap() {
match entry {
Ok(path) => {
debug!("visited entry file: {:?}", path.display());
if utils::has_path_visited(&visited, &path) {
continue;
}
let file_content = self.get_file_content(path.clone())?;
let entity_directives = self.parse(&file_content, path.clone())?;
// debug!("visited path pattern: {}", load_entity);

match &load_entity {
Either::Left(pattern) => {
for entry in glob(pattern.as_str()).unwrap() {
match entry {
Ok(path) => {
debug!("visited entry file: {:?}", path.display());
if utils::has_path_visited(&visited, &path) {
continue;

Check warning on line 64 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L57-L64

Added lines #L57 - L64 were not covered by tests
}
let file_content = self.get_file_content(path.clone())?;
let entity_directives = self.parse(&file_content, path.clone())?;

Check warning on line 67 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L66-L67

Added lines #L66 - L67 were not covered by tests

entity_directives.iter().filter_map(|directive| self.go_next(directive)).for_each(|buf| {
let fullpath = if buf.starts_with('/') {
buf
} else {
path.parent()
.map(|it| it.join(buf))
.map(|it| it.as_path().to_str().unwrap().to_owned())
.unwrap()
};
load_queue.push_back(Pattern::new(&fullpath).unwrap());
});
directives.extend(entity_directives)
entity_directives.iter().filter_map(|directive| self.go_next(directive)).for_each(|buf| {
let fullpath = if buf.starts_with('/') {
to_glob_or_path(PathBuf::from_str(&buf).unwrap())

Check warning on line 71 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L69-L71

Added lines #L69 - L71 were not covered by tests
} else {
path.parent().map(|it| it.join(buf)).map(to_glob_or_path).unwrap()

Check warning on line 73 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L73

Added line #L73 was not covered by tests
};
load_queue.push_back(fullpath);

Check warning on line 75 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L75

Added line #L75 was not covered by tests
});
directives.extend(entity_directives);

Check warning on line 77 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L77

Added line #L77 was not covered by tests
}
// if the path matched but was unreadable,
// thereby preventing its contents from matching
Err(e) => println!("{:?}", e),

Check warning on line 81 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L81

Added line #L81 was not covered by tests
}
}
// if the path matched but was unreadable,
// thereby preventing its contents from matching
Err(e) => println!("{:?}", e),
}
Either::Right(pathbuf) => {
debug!("visited entry file: {:?}", pathbuf.display());
if utils::has_path_visited(&visited, pathbuf) {
continue;

Check warning on line 88 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L85-L88

Added lines #L85 - L88 were not covered by tests
}
let file_content = self.get_file_content(pathbuf.clone())?;
let entity_directives = self.parse(&file_content, pathbuf.clone())?;

Check warning on line 91 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L90-L91

Added lines #L90 - L91 were not covered by tests

entity_directives.iter().filter_map(|directive| self.go_next(directive)).for_each(|buf| {
let fullpath = if buf.starts_with('/') {
to_glob_or_path(PathBuf::from_str(&buf).unwrap())

Check warning on line 95 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L93-L95

Added lines #L93 - L95 were not covered by tests
} else {
pathbuf.parent().map(|it| it.join(buf)).map(to_glob_or_path).unwrap()

Check warning on line 97 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L97

Added line #L97 was not covered by tests
};
load_queue.push_back(fullpath);

Check warning on line 99 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L99

Added line #L99 was not covered by tests
});
directives.extend(entity_directives);

Check warning on line 101 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L101

Added line #L101 was not covered by tests
}
}
visited.insert(load_entity);
visited.push(load_entity);

Check warning on line 104 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L104

Added line #L104 was not covered by tests
}
Ok(TransformResult {
directives: self.transform(directives)?,
visited_files: visited.into_iter().collect_vec(),
visited_files: visited,

Check warning on line 108 in core/src/transform.rs

View check run for this annotation

Codecov / codecov/patch

core/src/transform.rs#L108

Added line #L108 was not covered by tests
})
}
}
21 changes: 18 additions & 3 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;
use std::path::{Path, PathBuf};

use glob::Pattern;
use itertools::Either;

pub mod bigdecimal_ext;
pub mod calculable;
Expand All @@ -11,6 +12,20 @@
pub mod price_grip;
pub mod string_;

pub fn has_path_visited<'a>(visited: impl IntoIterator<Item = &'a Pattern>, path: &Path) -> bool {
visited.into_iter().any(|pattern| pattern.matches_path(path))
pub fn has_path_visited<'a>(visited: impl IntoIterator<Item = &'a Either<Pattern, PathBuf>>, path: &Path) -> bool {
visited.into_iter().any(|pattern| match pattern {
Either::Left(pattern) => pattern.matches_path(path),
Either::Right(pathbuf) => pathbuf.eq(path),

Check warning on line 18 in core/src/utils/mod.rs

View check run for this annotation

Codecov / codecov/patch

core/src/utils/mod.rs#L15-L18

Added lines #L15 - L18 were not covered by tests
})
}

const GLOB_CHAR: char = '*';
pub(crate) fn to_glob_or_path(path: PathBuf) -> Either<Pattern, PathBuf> {
let path_str = path.as_path().to_str().unwrap();

Check warning on line 24 in core/src/utils/mod.rs

View check run for this annotation

Codecov / codecov/patch

core/src/utils/mod.rs#L23-L24

Added lines #L23 - L24 were not covered by tests

if path_str.contains(GLOB_CHAR) {
Either::Left(Pattern::new(path.as_path().to_str().unwrap()).unwrap())

Check warning on line 27 in core/src/utils/mod.rs

View check run for this annotation

Codecov / codecov/patch

core/src/utils/mod.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
} else {
Either::Right(path)

Check warning on line 29 in core/src/utils/mod.rs

View check run for this annotation

Codecov / codecov/patch

core/src/utils/mod.rs#L29

Added line #L29 was not covered by tests
}
}
22 changes: 15 additions & 7 deletions server/src/routes/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use actix_web::web::{Data, Json, Path};
use actix_web::{get, put};
use itertools::Either;
use log::error;
use tokio::sync::RwLock;
use zhang_core::ledger::Ledger;
Expand All @@ -16,14 +17,21 @@
let entry_path = &ledger.entry.0;

let mut ret = vec![];
for patten in &ledger.visited_files {
for entry in glob::glob(patten.as_str()).unwrap() {
match entry {
Ok(path) => {
let p = path.strip_prefix(entry_path).unwrap().to_str().map(|it| it.to_string());
ret.push(p);
for visited in &ledger.visited_files {
match visited {
Either::Left(pattern) => {
for entry in glob::glob(pattern.as_str()).unwrap() {
match entry {
Ok(path) => {
let p = path.strip_prefix(entry_path).unwrap().to_str().map(|it| it.to_string());
ret.push(p);

Check warning on line 27 in server/src/routes/file.rs

View check run for this annotation

Codecov / codecov/patch

server/src/routes/file.rs#L20-L27

Added lines #L20 - L27 were not covered by tests
}
Err(e) => error!("{:?}", e),

Check warning on line 29 in server/src/routes/file.rs

View check run for this annotation

Codecov / codecov/patch

server/src/routes/file.rs#L29

Added line #L29 was not covered by tests
}
}
Err(e) => error!("{:?}", e),
}
Either::Right(path) => {
ret.push(path.to_str().map(|it| it.to_string()));

Check warning on line 34 in server/src/routes/file.rs

View check run for this annotation

Codecov / codecov/patch

server/src/routes/file.rs#L33-L34

Added lines #L33 - L34 were not covered by tests
}
}
}
Expand Down
Loading