Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fd4b589

Browse files
committedFeb 21, 2025
feat: multiple files & glob input
1 parent 71f5da0 commit fd4b589

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed
 

‎Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ traditional_chinese = ["erg_common/traditional_chinese", "els/traditional_chines
5353
pylyzer_core = { version = "0.0.80", path = "./crates/pylyzer_core" }
5454
erg_common = { workspace = true }
5555
els = { workspace = true }
56+
glob = "0.3.2"
5657

5758
[dev-dependencies]
5859
erg_compiler = { workspace = true }

‎README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ Make sure that `cargo`/`rustc` is up-to-date, as pylyzer may be written with the
4141
pylyzer file.py
4242
```
4343

44+
## Check multiple files
45+
46+
```sh
47+
# glob patterns are supported
48+
pylyzer file1.py file2.py dir/file*.py
49+
```
50+
4451
### Check an entire package
4552

4653
If you don't specify a file path, pylyzer will automatically search for the entry point.
@@ -177,7 +184,7 @@ pylyzer converts Python ASTs to Erg ASTs and passes them to Erg's type checker.
177184
* [x] type assertion (`typing.cast`)
178185
* [x] type narrowing (`is`, `isinstance`)
179186
* [x] `pyi` (stub) files support
180-
* [ ] glob pattern file check
187+
* [x] glob pattern file check
181188
* [x] type comment (`# type: ...`)
182189
* [x] virtual environment support
183190
* [x] package manager support

‎crates/pylyzer_core/analyze.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl PythonAnalyzer {
295295
res
296296
}
297297

298-
pub fn run(&mut self) {
298+
pub fn run(&mut self) -> i32 {
299299
/*if self.cfg.dist_dir.is_some() {
300300
reserve_decl_er(self.cfg.input.clone());
301301
}*/
@@ -317,7 +317,7 @@ impl PythonAnalyzer {
317317
dump_decl_package(&self.checker.shared().mod_cache);
318318
println!("A declaration file has been generated to __pycache__ directory.");
319319
}
320-
std::process::exit(0);
320+
0
321321
}
322322
Err(artifact) => {
323323
if !artifact.warns.is_empty() {
@@ -345,7 +345,7 @@ impl PythonAnalyzer {
345345
dump_decl_package(&self.checker.shared().mod_cache);
346346
println!("A declaration file has been generated to __pycache__ directory.");
347347
}
348-
std::process::exit(code);
348+
code
349349
}
350350
}
351351
}

‎src/config.rs

+22
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,25 @@ For more information try `pylyzer --help`"
212212
cfg.runtime_args = runtime_args.into();
213213
cfg
214214
}
215+
216+
pub(crate) fn files_to_be_checked() -> Vec<PathBuf> {
217+
let file_or_patterns = env::args()
218+
.skip(1)
219+
.rev()
220+
.take_while(|arg| !arg.starts_with("-"));
221+
let mut files = vec![];
222+
for file_or_pattern in file_or_patterns {
223+
if PathBuf::from(&file_or_pattern).is_file() {
224+
files.push(PathBuf::from(&file_or_pattern));
225+
} else {
226+
for entry in glob::glob(&file_or_pattern).expect("Failed to read glob pattern") {
227+
match entry {
228+
Err(e) => eprintln!("err: {e}"),
229+
Ok(path) if path.is_file() => files.push(path),
230+
_ => {}
231+
}
232+
}
233+
}
234+
}
235+
files
236+
}

‎src/main.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use erg_common::config::ErgMode;
66
use erg_common::spawn::exec_new_thread;
77
use pylyzer_core::{PythonAnalyzer, SimplePythonParser};
88

9+
use crate::config::files_to_be_checked;
910
use crate::copy::copy_dot_erg;
1011

1112
fn run() {
@@ -15,8 +16,22 @@ fn run() {
1516
let lang_server = Server::<PythonAnalyzer, SimplePythonParser>::new(cfg, None);
1617
lang_server.run();
1718
} else {
18-
let mut analyzer = PythonAnalyzer::new(cfg);
19-
analyzer.run();
19+
let mut code = 0;
20+
let files = files_to_be_checked();
21+
if files.is_empty() {
22+
let mut analyzer = PythonAnalyzer::new(cfg);
23+
code = analyzer.run();
24+
} else {
25+
for path in files {
26+
let cfg = cfg.inherit(path);
27+
let mut analyzer = PythonAnalyzer::new(cfg);
28+
let c = analyzer.run();
29+
if c != 0 {
30+
code = 1;
31+
}
32+
}
33+
}
34+
std::process::exit(code);
2035
}
2136
}
2237

0 commit comments

Comments
 (0)
Failed to load comments.