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

@ -0,0 +1,15 @@
use std::path::PathBuf;
pub fn erg_path() -> Option<PathBuf> {
option_env!("ERG_PATH")
.or_else(|| option_env!("CARGO_ERG_PATH"))
.map(PathBuf::from)
}
pub fn erg_std_path() -> Option<PathBuf> {
erg_path().map(|path| path.join("lib").join("std"))
}
pub fn erg_external_lib_path() -> Option<PathBuf> {
erg_path().map(|path| path.join("lib").join("external"))
}

View file

@ -7,6 +7,7 @@ pub mod color;
pub mod config; pub mod config;
pub mod datetime; pub mod datetime;
pub mod dict; pub mod dict;
pub mod env;
pub mod error; pub mod error;
pub mod fxhash; pub mod fxhash;
pub mod help_messages; pub mod help_messages;

View file

@ -14,20 +14,36 @@ fn main() -> std::io::Result<()> {
+ "/.erg"; + "/.erg";
if !path::Path::new(&erg_path).exists() { if !path::Path::new(&erg_path).exists() {
fs::create_dir(&erg_path)?; 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_PATH={erg_path}");
// println!("cargo:rustc-env=CARGO_ERG_STD_PATH={erg_path}/std");
// create a std library in ".erg" // 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 entry = res?;
let path = entry.path(); let entry_path = entry.path();
let filename = path if entry_path.is_dir() {
.file_name() dirs.push(entry);
.expect("this is not a file") } else {
.to_str() let filename = entry_path
.unwrap(); .file_name()
fs::copy(&path, format!("{erg_path}/std/{filename}"))?; .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(()) Ok(())
} }

View file

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