From a5cab167c5e9473ff24c5b50bf23fae4aa1d3e2b Mon Sep 17 00:00:00 2001
From: Christian Westrom <c.westrom@westrom.xyz>
Date: Mon, 6 May 2024 00:48:37 +0900
Subject: [PATCH] load the modules programmatically instead of manually

---
 repbuild/src/main.rs | 81 +++++++++++++++++++++++++++-----------------
 1 file changed, 50 insertions(+), 31 deletions(-)

diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs
index dfc601612..d9752f358 100644
--- a/repbuild/src/main.rs
+++ b/repbuild/src/main.rs
@@ -124,6 +124,18 @@ fn assemble() -> Result<(), Error> {
 fn get_fs() -> Result<FileSystem<impl ReadWriteSeek>, io::Error> {
     let filename = "sysdata/system_config.toml";
 
+    let mut img = File::options()
+        .read(true)
+        .write(true)
+        .create(true)
+        .open(Path::new("target/disk.img"))?;
+
+    img.set_len(1024 * 1024 * 64)?;
+
+    fatfs::format_volume(&mut img, FormatVolumeOptions::new())?;
+
+    let fs = FileSystem::new(img, FsOptions::new())?;
+
     // Read the contents of the file using a `match` block
     // to return the `data: Ok(c)` as a `String`
     // or handle any `errors: Err(_)`.
@@ -175,6 +187,16 @@ TERM_BACKDROP={}
         term_wallpaper.as_str().unwrap(),
         term_background
     );
+
+    // Copy the term_wallpaper to the image
+    let term_wallpaper_path = term_wallpaper
+        .as_str()
+        .unwrap()
+        .split("boot:///")
+        .last()
+        .unwrap();
+    copy_file_to_img(&format!("sysdata/{}", term_wallpaper_path), &fs);
+
     limine_str.push_str(&base);
 
     let boot_entries = limine_table.as_table_mut().unwrap();
@@ -230,18 +252,23 @@ TERM_BACKDROP={}
                 limine_str.push_str(&a);
             }
         }
+
+        // Copy modules into the test_programs directory
+        modules.into_iter().for_each(|(key, value)| {
+            if value.is_table() {
+                let path = value
+                    .get("path")
+                    .expect("You must have a `path` as a value")
+                    .as_str()
+                    .unwrap()
+                    .split("boot:///")
+                    .last()
+                    .unwrap();
+                let fpath = format!("target/test-programs/{}", path);
+                copy_file_to_img(&fpath, &fs);
+            }
+        });
     }
-    let mut img = File::options()
-        .read(true)
-        .write(true)
-        .create(true)
-        .open(Path::new("target/disk.img"))?;
-
-    img.set_len(1024 * 1024 * 64)?;
-
-    fatfs::format_volume(&mut img, FormatVolumeOptions::new())?;
-
-    let fs = FileSystem::new(img, FsOptions::new())?;
 
     let bootdir = fs.root_dir().create_dir("efi")?.create_dir("boot")?;
 
@@ -262,31 +289,23 @@ TERM_BACKDROP={}
             .attach_printable("Copying Limine (ARM): have you pulled the submodule?")?,
         &mut bootdir.create_file("bootaa64.efi")?,
     )?;
-    // TODO: Remove this and replace it with pulling executables from the system_config
-    for fpath in [
-        "sysdata/background.bmp",
-        "target/test-programs/failure.hbf",
-        "target/test-programs/ecall.hbf",
-        "target/test-programs/main.hbf",
-        "target/test-programs/serial_driver.hbf",
-        "target/test-programs/vfs_test.hbf",
-        "target/test-programs/sds_test.hbf",
-        "target/test-programs/limine_framebuffer_driver.hbf",
-        "target/test-programs/keyboard_driver.hbf",
-    ] {
-        let path = Path::new(fpath);
-        io::copy(
-            &mut File::open(path)?,
-            &mut fs
-                .root_dir()
-                .create_file(&path.file_name().unwrap().to_string_lossy())?,
-        )?;
-    }
 
     drop(bootdir);
     Ok(fs)
 }
 
+fn copy_file_to_img(fpath: &str, fs: &FileSystem<File>) {
+    let path = Path::new(fpath);
+    io::copy(
+        &mut File::open(path).expect(&format!("Could not open file {fpath}")),
+        &mut fs
+            .root_dir()
+            .create_file(&path.file_name().unwrap().to_string_lossy())
+            .expect("Unable to create file"),
+    )
+    .expect("Copy failed");
+}
+
 fn build(release: bool, target: Target) -> Result<(), Error> {
     let fs = get_fs().change_context(Error::Io)?;
     let mut com = Command::new("cargo");