|
| 1 | +use crate::{load_ingot, CheckArgs}; |
| 2 | +use common::input::IngotDependency; |
| 3 | +use common::{input::IngotKind, InputIngot}; |
| 4 | +use fe_driver2::DriverDataBase; |
| 5 | +use include_dir::{include_dir, Dir}; |
| 6 | +use semver::Version; |
| 7 | +use std::fs; |
| 8 | +use std::path::PathBuf; |
| 9 | +use std::{collections::BTreeSet, path::Path}; |
| 10 | + |
| 11 | +const STD_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../library/std"); |
| 12 | + |
| 13 | +fn write_std_files(std_path: &Path) { |
| 14 | + write_files_recursive(&STD_DIR, std_path); |
| 15 | +} |
| 16 | + |
| 17 | +fn write_files_recursive(dir: &Dir<'_>, base_path: &Path) { |
| 18 | + for file in dir.files() { |
| 19 | + let file_path = base_path.join(file.path()); |
| 20 | + if let Some(parent_dir) = file_path.parent() { |
| 21 | + std::fs::create_dir_all(parent_dir).unwrap(); |
| 22 | + } |
| 23 | + std::fs::write(file_path, file.contents()).unwrap(); |
| 24 | + } |
| 25 | + |
| 26 | + for subdir in dir.dirs() { |
| 27 | + let subdir_path = base_path.join(subdir.path()); |
| 28 | + std::fs::create_dir_all(&subdir_path).unwrap(); |
| 29 | + write_files_recursive(subdir, &base_path); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub fn run_check(args: &CheckArgs) { |
| 34 | + let std_path = match &args.std_path { |
| 35 | + Some(path) => PathBuf::from(path.to_owned()), |
| 36 | + None => { |
| 37 | + let home_dir = dirs::home_dir().expect("Failed to get user home directory"); |
| 38 | + home_dir.join(".fe/std") |
| 39 | + } |
| 40 | + }; |
| 41 | + if !std_path.exists() { |
| 42 | + println!("The standard library is not installed. Do you want to perform the write? (y/n)"); |
| 43 | + let mut input = String::new(); |
| 44 | + std::io::stdin().read_line(&mut input).unwrap(); |
| 45 | + let input = input.trim().to_lowercase(); |
| 46 | + if input == "y" || input == "yes" { |
| 47 | + write_std_files(&std_path); |
| 48 | + } else { |
| 49 | + eprintln!( |
| 50 | + "Cannot perform the write without the standard library being installed on disk" |
| 51 | + ); |
| 52 | + std::process::exit(2); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let path = Path::new(&args.path); |
| 57 | + if !path.exists() { |
| 58 | + eprintln!("Path '{}' does not exist", path.display()); |
| 59 | + std::process::exit(2); |
| 60 | + } |
| 61 | + |
| 62 | + let mut db = DriverDataBase::default(); |
| 63 | + |
| 64 | + let std_ingot = load_ingot(&std_path, &mut db, IngotKind::Std, &mut BTreeSet::new()); |
| 65 | + |
| 66 | + if path.is_file() { |
| 67 | + let source = fs::read_to_string(path).unwrap(); |
| 68 | + check_single_file(path, source, std_ingot, &mut db, args.dump_scope_graph); |
| 69 | + } else if path.is_dir() { |
| 70 | + check_ingot(path, std_ingot, &mut db, args.dump_scope_graph); |
| 71 | + } else { |
| 72 | + eprintln!( |
| 73 | + "Path '{}' is neither a file nor a directory", |
| 74 | + path.display() |
| 75 | + ); |
| 76 | + std::process::exit(2); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +pub fn check_single_file( |
| 81 | + path: &Path, |
| 82 | + source: String, |
| 83 | + std_ingot: InputIngot, |
| 84 | + db: &mut DriverDataBase, |
| 85 | + dump_scope_graph: bool, |
| 86 | +) { |
| 87 | + let mut dependencies = BTreeSet::from([IngotDependency::new("std", std_ingot)]); |
| 88 | + let ingot = InputIngot::new( |
| 89 | + db, |
| 90 | + path.parent().unwrap().to_str().unwrap(), |
| 91 | + IngotKind::StandAlone, |
| 92 | + Version::new(0, 1, 0), |
| 93 | + dependencies.clone(), |
| 94 | + ); |
| 95 | + |
| 96 | + // let input_file = InputFile::new( |
| 97 | + // db, |
| 98 | + // ingot.clone(), |
| 99 | + // path.file_name().unwrap().to_str().unwrap().into(), |
| 100 | + // source, |
| 101 | + // ); |
| 102 | + // ingot.set_files(db, BTreeSet::from([input_file.clone()])); |
| 103 | + // ingot.set_root_file(db, input_file); |
| 104 | + |
| 105 | + let top_mod = db.top_mod_from_file(path, &source); |
| 106 | + db.run_on_top_mod(top_mod); |
| 107 | + db.emit_diags(); |
| 108 | + |
| 109 | + // if dump_scope_graph { |
| 110 | + // println!("{}", dump_scope_graph(db, top_mod)); |
| 111 | + // } |
| 112 | +} |
| 113 | + |
| 114 | +pub fn check_ingot( |
| 115 | + path: &Path, |
| 116 | + std_ingot: InputIngot, |
| 117 | + db: &mut DriverDataBase, |
| 118 | + dump_scope_graph: bool, |
| 119 | +) { |
| 120 | + let mut dependencies = BTreeSet::from([IngotDependency::new("std", std_ingot)]); |
| 121 | + let mut main_ingot = load_ingot(path, db, IngotKind::Local, &mut dependencies); |
| 122 | + |
| 123 | + main_ingot.set_external_ingots(db, dependencies); |
| 124 | + |
| 125 | + db.run_on_ingot(std_ingot); |
| 126 | + |
| 127 | + let diags = db.format_diags(); |
| 128 | + if !diags.is_empty() { |
| 129 | + panic!("{diags}") |
| 130 | + } |
| 131 | + |
| 132 | + db.run_on_ingot(main_ingot); |
| 133 | + |
| 134 | + let diags = db.format_diags(); |
| 135 | + if !diags.is_empty() { |
| 136 | + panic!("{diags}") |
| 137 | + } |
| 138 | + |
| 139 | + // if dump_scope_graph { |
| 140 | + // println!("{}", dump_scope_graph(db, top_mod)); |
| 141 | + // } |
| 142 | +} |
0 commit comments