Skip to content

Commit

Permalink
feat: cache cleaning option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Nov 4, 2024
1 parent a710224 commit f215337
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/options/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" =>
Expand All @@ -21,6 +23,7 @@ OPTIONS
--version/-V バージョンを表示
--verbose 0|1|2 冗長性レベルを指定
--server Language Serverを起動
--clear-cache キャッシュをクリア
--code/-c cmd 文字列をプログラムに渡す
--dump-decl 型宣言ファイルを出力
--disable 指定した機能を無効化",
Expand All @@ -38,6 +41,7 @@ OPTIONS
--version/-V 显示版本
--verbose 0|1|2 指定细致程度
--server 启动 Language Server
--clear-cache 清除缓存
--code/-c cmd 作为字符串传入程序
--dump-decl 输出类型声明文件
--disable 禁用指定功能",
Expand All @@ -55,6 +59,7 @@ OPTIONS
--version/-V 顯示版本
--verbose 0|1|2 指定細緻程度
--server 啟動 Language Server
--clear-cache 清除快取
--code/-c cmd 作為字串傳入程式
--dump-decl 輸出類型宣告檔案
--disable 禁用指定功能",
Expand All @@ -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",
Expand Down Expand Up @@ -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!(
"\
Expand Down
49 changes: 48 additions & 1 deletion src/copy.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit f215337

Please sign in to comment.