From 80ab984b6dce2b2645ef19b9d769786454dc292d Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sun, 10 Sep 2023 13:10:16 +0900 Subject: [PATCH] feat: copy `.erg` --- src/copy.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 src/copy.rs diff --git a/src/copy.rs b/src/copy.rs new file mode 100644 index 0000000..353c543 --- /dev/null +++ b/src/copy.rs @@ -0,0 +1,38 @@ +use std::path::Path; +use std::fs::{copy, read_dir, create_dir_all}; + +use erg_common::env::{erg_path, python_site_packages}; + +fn copy_dir(from: impl AsRef, to: impl AsRef) -> std::io::Result<()> { + let from = from.as_ref(); + let to = to.as_ref(); + if !from.exists() { + return Ok(()); + } + if !to.exists() { + create_dir_all(to)?; + } + for entry in read_dir(from)? { + let entry = entry?; + if entry.file_type()?.is_dir() { + copy_dir(entry.path(), to.join(entry.file_name()))?; + } else { + copy(entry.path(), to.join(entry.file_name()))?; + } + } + Ok(()) +} + +#[allow(unused)] +pub(crate) fn copy_dot_erg() { + if erg_path().exists() { + return; + } + for site_packages in python_site_packages() { + if site_packages.join(".erg").exists() { + println!("Copying site-package/.erg to {}", erg_path().display()); + copy_dir(site_packages.join(".erg"), erg_path()) + .expect("Failed to copy .erg"); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index f83b4e6..ff4fa97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod analyze; mod config; mod handle_err; +mod copy; pub use analyze::PythonAnalyzer; diff --git a/src/main.rs b/src/main.rs index 06edf37..775caea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ mod analyze; mod config; mod handle_err; +mod copy; use analyze::{PythonAnalyzer, SimplePythonParser}; use els::Server; use erg_common::config::ErgMode; use erg_common::spawn::exec_new_thread; +use crate::copy::copy_dot_erg; + fn run() { + copy_dot_erg(); let cfg = config::parse_args(); if cfg.mode == ErgMode::LanguageServer { let mut lang_server = Server::::new(cfg, None);