Change the structure of the .erg directory

This commit is contained in:
Shunsuke Shibayama 2022-10-16 12:04:39 +09:00
parent 8c6997d3c9
commit 7bc37aa14a
7 changed files with 44 additions and 12 deletions

View file

@ -14,20 +14,36 @@ fn main() -> std::io::Result<()> {
+ "/.erg";
if !path::Path::new(&erg_path).exists() {
fs::create_dir(&erg_path)?;
fs::create_dir(format!("{erg_path}/std"))?;
}
println!("cargo:rustc-env=CARGO_ERG_PATH={erg_path}");
// println!("cargo:rustc-env=CARGO_ERG_STD_PATH={erg_path}/std");
// create a std library in ".erg"
for res in fs::read_dir("std")? {
copy_dir(&erg_path, "lib")?;
Ok(())
}
fn copy_dir(erg_path: &str, path: &str) -> std::io::Result<()> {
let full_path = format!("{erg_path}/{path}");
if !path::Path::new(&full_path).exists() {
fs::create_dir(&full_path)?;
}
let mut dirs = vec![];
for res in fs::read_dir(path)? {
let entry = res?;
let path = entry.path();
let filename = path
.file_name()
.expect("this is not a file")
.to_str()
.unwrap();
fs::copy(&path, format!("{erg_path}/std/{filename}"))?;
let entry_path = entry.path();
if entry_path.is_dir() {
dirs.push(entry);
} else {
let filename = entry_path
.file_name()
.expect("this is not a file")
.to_str()
.unwrap();
let filename = format!("{full_path}/{filename}");
fs::copy(&entry_path, filename)?;
}
}
for dir in dirs {
copy_dir(erg_path, dir.path().to_str().unwrap())?;
}
Ok(())
}

View file

@ -8,6 +8,7 @@ use crate::ty::codeobj::{CodeObj, CodeObjFlags};
use erg_common::astr::AtomicStr;
use erg_common::cache::CacheSet;
use erg_common::config::{ErgConfig, Input};
use erg_common::env::erg_std_path;
use erg_common::error::{ErrorDisplay, Location};
use erg_common::opcode::Opcode;
use erg_common::traits::{Locational, Stream};
@ -2052,8 +2053,7 @@ impl CodeGenerator {
}
fn load_prelude_py(&mut self) {
if let Some(erg_path) = option_env!("ERG_PATH").or_else(|| option_env!("CARGO_ERG_PATH")) {
let std_path = std::path::Path::new(erg_path).join("std");
if let Some(std_path) = erg_std_path() {
self.emit_global_import_items(
Identifier::public("sys"),
vec![(