Skip to content

Commit

Permalink
Improve cargo 3ds new and bump shlex
Browse files Browse the repository at this point in the history
  • Loading branch information
Meziu committed Apr 26, 2024
1 parent f6b9c6d commit e79f6dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ serde = { version = "1.0.139", features = ["derive"] }
tee = "0.1.0"
toml = "0.5.6"
clap = { version = "4.0.15", features = ["derive", "wrap_help"] }
shlex = "1.1.0"
shlex = "1.3.0"
serde_json = "1.0.108"
4 changes: 3 additions & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,11 @@ impl New {
let toml_path = project_path.join("Cargo.toml");
let romfs_path = project_path.join("romfs");
let main_rs_path = project_path.join("src/main.rs");
let dummy_romfs_path = romfs_path.join("PUT_YOUR_ROMFS_FILES_HERE.txt");

// Create the "romfs" directory
// Create the "romfs" directory, and place a dummy file within it.
fs::create_dir(romfs_path).unwrap();
fs::File::create(dummy_romfs_path).unwrap();

// Read the contents of `Cargo.toml` to a string
let mut buf = String::new();
Expand Down
44 changes: 26 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@ use crate::graph::UnitGraph;
pub fn run_cargo(input: &Input, message_format: Option<String>) -> (ExitStatus, Vec<Message>) {
let mut command = make_cargo_command(input, &message_format);

let libctru = if should_use_ctru_debuginfo(&command, input.verbose) {
"ctrud"
} else {
"ctru"
};
// The unit graph is needed only when compiling a program.
if input.cmd.should_compile() {
let libctru = if should_use_ctru_debuginfo(&command, input.verbose) {
"ctrud"
} else {
"ctru"
};

let rustflags = command
.get_envs()
.find(|(var, _)| var == &OsStr::new("RUSTFLAGS"))
.and_then(|(_, flags)| flags)
.unwrap_or_default()
.to_string_lossy();
let rustflags = command
.get_envs()
.find(|(var, _)| var == &OsStr::new("RUSTFLAGS"))
.and_then(|(_, flags)| flags)
.unwrap_or_default()
.to_string_lossy();

let rustflags = format!("{rustflags} -l{libctru}");
let rustflags = format!("{rustflags} -l{libctru}");

command.env("RUSTFLAGS", rustflags);
command.env("RUSTFLAGS", rustflags);
}

if input.verbose {
print_command(&command);
Expand Down Expand Up @@ -182,10 +185,13 @@ fn print_command(command: &Command) {
eprintln!(
" {}={} \\",
k.to_string_lossy(),
v.map_or_else(String::new, |s| shlex::quote(&s).to_string())
v.map_or_else(String::new, |s| shlex::try_quote(&s).unwrap().to_string())
);
}
eprintln!(" {}\n", shlex::join(cmd_str.iter().map(String::as_str)));
eprintln!(
" {}\n",
shlex::try_join(cmd_str.iter().map(String::as_str)).unwrap()
);
}

/// Finds the sysroot path of the current toolchain
Expand All @@ -206,11 +212,13 @@ pub fn find_sysroot() -> PathBuf {

/// Checks the current rust version and channel.
/// Exits if the minimum requirement is not met.
pub fn check_rust_version() {
pub fn check_rust_version(input: &Input) {
let rustc_version = rustc_version::version_meta().unwrap();

if rustc_version.channel > Channel::Nightly {
eprintln!("cargo-3ds requires a nightly rustc version.");
// If the channel isn't nightly, we can't make use the required unstable tools.
// However, `cargo new` doesn't have these requirements.
if rustc_version.channel > Channel::Nightly && input.cmd.should_compile() {
eprintln!("building with cargo-3ds requires a nightly rustc version.");
eprintln!(
"Please run `rustup override set nightly` to use nightly in the \
current directory, or use `cargo +nightly 3ds` to use it for a \
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use cargo_3ds::{check_rust_version, run_cargo};
use clap::Parser;

fn main() {
check_rust_version();

let Cargo::Input(mut input) = Cargo::parse();

// Depending on the command, we might have different base requirements for the Rust version.
check_rust_version(&input);

let message_format = match input.cmd.extract_message_format() {
Ok(fmt) => fmt,
Err(msg) => {
Expand Down

0 comments on commit e79f6dc

Please sign in to comment.