Skip to content

Commit

Permalink
Convert build_smdh to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-h-chamberlain committed Jun 8, 2024
1 parent 5ec238b commit e499df1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::OnceLock;
use cargo_metadata::Message;
use clap::{Args, Parser, Subcommand};

use crate::{build_3dsx, build_smdh, cargo, get_metadata, link, print_command, CTRConfig};
use crate::{build_3dsx, cargo, get_metadata, link, print_command, CTRConfig};

#[derive(Parser, Debug)]
#[command(name = "cargo", bin_name = "cargo")]
Expand Down Expand Up @@ -349,7 +349,7 @@ impl Build {
fn callback(&self, config: &Option<CTRConfig>) {
if let Some(config) = config {
eprintln!("Building smdh: {}", config.path_smdh());
build_smdh(config, self.verbose);
config.build_smdh(self.verbose);

eprintln!("Building 3dsx: {}", config.path_3dsx());
build_3dsx(config, self.verbose);
Expand Down
75 changes: 38 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,43 +328,6 @@ fn get_romfs_dir(package: &Package) -> Option<Utf8PathBuf> {
}
}

/// Builds the smdh using `smdhtool`.
/// This will fail if `smdhtool` is not within the running directory or in a directory found in $PATH
// TODO: maybe this should be a method of CTRConfig?
pub fn build_smdh(config: &CTRConfig, verbose: bool) {
let author = if config.authors.is_empty() {
String::from("Unspecified Author") // as standard with the devkitPRO toolchain
} else {
config.authors.join(", ")
};

let mut command = Command::new("smdhtool");
command
.arg("--create")
.arg(&config.name)
.arg(&config.description)
.arg(author)
.arg(&config.icon_path)
.arg(config.path_smdh())
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if verbose {
print_command(&command);
}

let mut process = command
.spawn()
.expect("smdhtool command failed, most likely due to 'smdhtool' not being in $PATH");

let status = process.wait().unwrap();

if !status.success() {
process::exit(status.code().unwrap_or(1));
}
}

/// Builds the 3dsx using `3dsxtool`.
/// This will fail if `3dsxtool` is not within the running directory or in a directory found in $PATH
pub fn build_3dsx(config: &CTRConfig, verbose: bool) {
Expand Down Expand Up @@ -438,13 +401,51 @@ pub struct CTRConfig {
}

impl CTRConfig {
/// Get the path to the output `.3dsx` file.
pub fn path_3dsx(&self) -> Utf8PathBuf {
self.target_path.with_extension("3dsx")
}

/// Get the path to the output `.smdh` file.
pub fn path_smdh(&self) -> Utf8PathBuf {
self.target_path.with_extension("smdh")
}

/// Builds the smdh using `smdhtool`.
/// This will fail if `smdhtool` is not within the running directory or in a directory found in $PATH
pub fn build_smdh(&self, verbose: bool) {
let author = if self.authors.is_empty() {
String::from("Unspecified Author") // as standard with the devkitPRO toolchain
} else {
self.authors.join(", ")
};

let mut command = Command::new("smdhtool");
command
.arg("--create")
.arg(&self.name)
.arg(&self.description)
.arg(author)
.arg(&self.icon_path)
.arg(self.path_smdh())
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

if verbose {
print_command(&command);
}

let mut process = command
.spawn()
.expect("smdhtool command failed, most likely due to 'smdhtool' not being in $PATH");

let status = process.wait().unwrap();

if !status.success() {
process::exit(status.code().unwrap_or(1));
}
}
}

#[derive(Ord, PartialOrd, PartialEq, Eq, Debug)]
Expand Down

0 comments on commit e499df1

Please sign in to comment.