From 2813bbc21f597a2d11849255d41d24b054136878 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sun, 23 Jan 2022 18:50:26 -0800 Subject: [PATCH 1/2] Read the RomFS path from Cargo.toml --- src/main.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3fa406c..41e24fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use cargo_metadata::{MetadataCommand, Package}; use rustc_version::{Channel, Version}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::{ env, fmt, io, process::{self, Command, Stdio}, @@ -13,6 +13,7 @@ struct CTRConfig { description: String, icon: String, target_path: String, + cargo_manifest_path: PathBuf, } #[derive(Ord, PartialOrd, PartialEq, Eq, Debug)] @@ -258,6 +259,7 @@ fn get_metadata(args: &[&str], opt_level: &str) -> CTRConfig { .unwrap_or_else(|| String::from("Homebrew Application")), icon, target_path, + cargo_manifest_path: package.manifest_path.clone().into(), } } @@ -290,8 +292,10 @@ fn build_3dsx(config: &CTRConfig) { .arg(format!("--smdh={}.smdh", config.target_path)); // If romfs directory exists, automatically include it - if Path::new("./romfs").is_dir() { - process = process.arg("--romfs=./romfs"); + let romfs_path = get_romfs_path(config); + if romfs_path.is_dir() { + println!("Adding RomFS from {}", romfs_path.display()); + process = process.arg(format!("--romfs={}", romfs_path.display())); } let mut process = process @@ -323,3 +327,29 @@ fn link(config: &CTRConfig) { process::exit(status.code().unwrap_or(1)); } } + +/// Read the RomFS path from the Cargo manifest. If it's unset, use the default. +fn get_romfs_path(config: &CTRConfig) -> PathBuf { + let manifest_path = &config.cargo_manifest_path; + let manifest_str = std::fs::read_to_string(manifest_path) + .unwrap_or_else(|e| panic!("Could not open {}: {e}", manifest_path.display())); + let manifest_data: toml::Value = + toml::de::from_str(&manifest_str).expect("Could not parse Cargo manifest as TOML"); + + // Find the romfs setting and compute the path + let romfs_dir_setting = manifest_data + .as_table() + .and_then(|table| table.get("package")) + .and_then(toml::Value::as_table) + .and_then(|table| table.get("metadata")) + .and_then(toml::Value::as_table) + .and_then(|table| table.get("cargo-3ds")) + .and_then(toml::Value::as_table) + .and_then(|table| table.get("romfs_dir")) + .and_then(toml::Value::as_str) + .unwrap_or("romfs"); + let mut romfs_path = manifest_path.clone(); + romfs_path.pop(); // Pop Cargo.toml + romfs_path.push(romfs_dir_setting); + romfs_path +} From 6ca3f96a395ce5e379d52f57d41db9cba7e22c06 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sun, 23 Jan 2022 19:01:43 -0800 Subject: [PATCH 2/2] Return an error if the non-default RomFS directory isn't found --- src/main.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 41e24fd..6791218 100644 --- a/src/main.rs +++ b/src/main.rs @@ -292,10 +292,16 @@ fn build_3dsx(config: &CTRConfig) { .arg(format!("--smdh={}.smdh", config.target_path)); // If romfs directory exists, automatically include it - let romfs_path = get_romfs_path(config); + let (romfs_path, is_default_romfs) = get_romfs_path(config); if romfs_path.is_dir() { println!("Adding RomFS from {}", romfs_path.display()); process = process.arg(format!("--romfs={}", romfs_path.display())); + } else if !is_default_romfs { + eprintln!( + "Could not find configured RomFS dir: {}", + romfs_path.display() + ); + process::exit(1); } let mut process = process @@ -329,7 +335,8 @@ fn link(config: &CTRConfig) { } /// Read the RomFS path from the Cargo manifest. If it's unset, use the default. -fn get_romfs_path(config: &CTRConfig) -> PathBuf { +/// The returned boolean is true when the default is used. +fn get_romfs_path(config: &CTRConfig) -> (PathBuf, bool) { let manifest_path = &config.cargo_manifest_path; let manifest_str = std::fs::read_to_string(manifest_path) .unwrap_or_else(|e| panic!("Could not open {}: {e}", manifest_path.display())); @@ -337,6 +344,7 @@ fn get_romfs_path(config: &CTRConfig) -> PathBuf { toml::de::from_str(&manifest_str).expect("Could not parse Cargo manifest as TOML"); // Find the romfs setting and compute the path + let mut is_default = false; let romfs_dir_setting = manifest_data .as_table() .and_then(|table| table.get("package")) @@ -347,9 +355,13 @@ fn get_romfs_path(config: &CTRConfig) -> PathBuf { .and_then(toml::Value::as_table) .and_then(|table| table.get("romfs_dir")) .and_then(toml::Value::as_str) - .unwrap_or("romfs"); + .unwrap_or_else(|| { + is_default = true; + "romfs" + }); let mut romfs_path = manifest_path.clone(); romfs_path.pop(); // Pop Cargo.toml romfs_path.push(romfs_dir_setting); - romfs_path + + (romfs_path, is_default) }