diff --git a/docs/options/options.md b/docs/options/options.md index c3dfa04..1fc8255 100644 --- a/docs/options/options.md +++ b/docs/options/options.md @@ -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. diff --git a/src/config.rs b/src/config.rs index a68ab23..2518828 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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!( "\ diff --git a/src/copy.rs b/src/copy.rs index 5443299..2fbbad6 100644 --- a/src/copy.rs +++ b/src/copy.rs @@ -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); + } + } +}