Skip to content

Commit

Permalink
refactor: two walkers
Browse files Browse the repository at this point in the history
  • Loading branch information
sjfhsjfh committed Feb 15, 2025
1 parent 75993e6 commit 6a85b3c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
14 changes: 2 additions & 12 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::{fs, path::Path};
use anyhow::{bail, Result};
use clap::{Arg, Command};
use dialoguer::Confirm;
use glob::Pattern;
use log::warn;

use crate::utils::{read_manifest, typst_local_dir, walker_default};
use crate::utils::{read_manifest, typst_local_dir, walkers::walker_install};

pub fn cmd() -> Command {
Command::new("install")
Expand Down Expand Up @@ -82,18 +81,9 @@ pub fn install(src_dir: &Path, target: &str) -> Result<()> {
}
}

let mut excludes = vec![];
for exclude in &current.package.exclude {
let pattern = Pattern::new(&exclude)?;
excludes.push(pattern);
}

for entry in walker_default(src_dir) {
for entry in walker_install(src_dir)? {
if let Ok(entry) = entry {
let path = entry.path();
if excludes.iter().any(|p| p.matches_path(path)) {
continue;
}
let dest = version_dir.join(path.strip_prefix(src_dir).unwrap());
if path.is_file() {
fs::copy(&path, &dest)?;
Expand Down
5 changes: 3 additions & 2 deletions src/regs/universe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use std::str::FromStr;
use typst_syntax::package::{PackageManifest, PackageVersion};

use crate::config::CONFIG;
use crate::utils::{config_file, save_config, walker_default};
use crate::utils::walkers::walker_publish;
use crate::utils::{config_file, save_config};

// pub const UNIVERSE_REPO_ID: RepositoryId = RepositoryId::from("R_kgDOJ0PIWA");
pub const UNIVERSE_REPO_NAME: &str = "packages";
Expand Down Expand Up @@ -259,7 +260,7 @@ pub async fn publish(manifest: &PackageManifest, package_dir: &Path) -> Result<(
info!("Uploading files to personal fork...");
let mut files = Vec::new();

for entry in walker_default(package_dir) {
for entry in walker_publish(package_dir) {
if let Ok(entry) = entry {
if !entry.path().is_file() {
continue;
Expand Down
10 changes: 2 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pub mod walkers;

use std::{
env, fs,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use ignore::{Walk, WalkBuilder};
use log::info;
use sha2::{Digest, Sha256};
use typst_syntax::package::PackageManifest;
Expand Down Expand Up @@ -78,10 +79,3 @@ pub fn write_manifest(package_dir: &Path, manifest: &PackageManifest) -> Result<
.context("Failed to write the package manifest file")?;
Ok(())
}

pub fn walker_default(root: &Path) -> Walk {
WalkBuilder::new(root)
.standard_filters(true)
.add_custom_ignore_filename(".typstignore")
.build()
}
50 changes: 50 additions & 0 deletions src/utils/walkers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anyhow::Result;
use glob::Pattern;
use ignore::{Walk, WalkBuilder};
use std::path::Path;

use super::read_manifest;

pub fn walker_publish(root: &Path) -> Walk {
WalkBuilder::new(root)
.standard_filters(true)
.add_custom_ignore_filename(".typstignore")
.build()
}

pub fn walker_install(
root: &Path,
) -> Result<Vec<std::result::Result<ignore::DirEntry, ignore::Error>>> {
let excludes = read_manifest(root)?
.package
.exclude
.into_iter()
.map(|s| Pattern::new(&s))
.collect::<Vec<_>>();
let mut ok_excludes = vec![];
for pat in &excludes {
match pat {
Ok(p) => ok_excludes.push(p),
Err(e) => {
return Err(anyhow::anyhow!("Invalid pattern: {}", e));
}
}
}
Ok(WalkBuilder::new(root)
.standard_filters(true)
.add_custom_ignore_filename(".typstignore")
.build()
.filter(|entry| {
if let Ok(path) = entry {
return ok_excludes.iter().any(|p| p.matches_path(path.path()));
}
false
})
.map(|entry| {
if let Ok(path) = entry {
return Ok(path);
}
Err(entry.unwrap_err())
})
.collect())
}

0 comments on commit 6a85b3c

Please sign in to comment.