feat: cache cleaning option

This commit is contained in:
Shunsuke Shibayama 2024-11-04 18:05:33 +09:00
parent a710224ccc
commit f2153372d0
3 changed files with 62 additions and 1 deletions

View File

@ -4,6 +4,10 @@
Launch as a language server.
## --clear-cache
Clear the cache files.
## --dump-decl
Dump a type declarations file (d.er) after type checking.

View File

@ -6,6 +6,8 @@ use erg_common::config::{ErgConfig, ErgMode};
use erg_common::io::Input;
use erg_common::switch_lang;
use crate::copy::clear_cache;
fn command_message() -> &'static str {
switch_lang!(
"japanese" =>
@ -21,6 +23,7 @@ OPTIONS
--version/-V
--verbose 0|1|2
--server Language Serverを起動
--clear-cache
--code/-c cmd
--dump-decl
--disable ",
@ -38,6 +41,7 @@ OPTIONS
--version/-V
--verbose 0|1|2
--server Language Server
--clear-cache
--code/-c cmd
--dump-decl
--disable ",
@ -55,6 +59,7 @@ OPTIONS
--version/-V
--verbose 0|1|2
--server Language Server
--clear-cache
--code/-c cmd
--dump-decl
--disable ",
@ -72,6 +77,7 @@ OPTIONS
--version/-V show version
--verbose 0|1|2 verbosity level
--server start the Language Server
--clear-cache clear cache
--code/-c cmd program passed in as string
--dump-decl output type declaration file
--disable disable specified features",
@ -125,6 +131,10 @@ pub(crate) fn parse_args() -> ErgConfig {
println!("pylyzer {}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
}
"--clear-cache" => {
clear_cache();
std::process::exit(0);
}
other if other.starts_with('-') => {
println!(
"\

View File

@ -1,4 +1,4 @@
use std::fs::{copy, create_dir_all, read_dir};
use std::fs::{copy, create_dir_all, read_dir, remove_file, DirEntry};
use std::path::Path;
use erg_common::env::{erg_path, python_site_packages};
@ -34,3 +34,50 @@ pub(crate) fn copy_dot_erg() {
}
}
}
pub(crate) fn clear_cache() {
for dir in read_dir(".").expect("Failed to read dir") {
let Ok(dir) = dir else {
continue;
};
rec_clear_cache(dir);
}
for site_packages in python_site_packages() {
for pkg in site_packages
.read_dir()
.expect("Failed to read site-packages")
{
let Ok(pkg) = pkg else {
continue;
};
rec_clear_cache(pkg);
}
}
}
fn rec_clear_cache(pkg: DirEntry) {
if pkg.file_type().expect("Failed to get file type").is_dir() {
let cache = if pkg.path().ends_with("__pycache__") {
pkg.path()
} else {
pkg.path().join("__pycache__")
};
if cache.exists() {
for cache_file in cache.read_dir().expect("Failed to read cache") {
let Ok(cache_file) = cache_file else {
continue;
};
if cache_file.file_name().to_string_lossy().ends_with(".d.er") {
println!("Removing cache file {}", cache_file.path().display());
remove_file(cache_file.path()).expect("Failed to remove cache file");
}
}
}
for entry in pkg.path().read_dir().expect("Failed to read dir") {
let Ok(entry) = entry else {
continue;
};
rec_clear_cache(entry);
}
}
}