From e16965eafbacbe3f3a327d5c95614d9e6c7ca458 Mon Sep 17 00:00:00 2001 From: roblabla Date: Thu, 16 Apr 2020 19:43:21 +0000 Subject: [PATCH] Cargo-nro: Don't try to parse PackageID --- Cargo.lock | 50 ----------------------------- Cargo.toml | 1 - src/bin/cargo-nro.rs | 76 +++++++------------------------------------- 3 files changed, 12 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efab7bb..a876df3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -393,16 +393,6 @@ dependencies = [ "unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "idna" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "itoa" version = "0.4.4" @@ -441,7 +431,6 @@ dependencies = [ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -471,11 +460,6 @@ dependencies = [ "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "nodrop" version = "0.1.13" @@ -491,11 +475,6 @@ name = "opaque-debug" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "percent-encoding" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "plain" version = "0.2.3" @@ -813,19 +792,6 @@ name = "typenum" version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-segmentation" version = "1.2.1" @@ -846,16 +812,6 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "url" -version = "1.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "vec_map" version = "0.8.1" @@ -948,17 +904,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum goblin 0.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "5911d7df7b8f65ab676c5327b50acea29d3c6a1a4ad05e444cf5dce321b26db2" "checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82" -"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)" = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum lz4 1.23.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43c94a9f09a60017f373020cc93d4291db4cd92b0db64ff25927f27d09dc23d5" "checksum lz4-sys 1.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "20ab022822e9331c58d373acdd6b98085bace058ac6837b8266f213a2fccdafe" -"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum opaque-debug 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "51ecbcb821e1bd256d456fe858aaa7f380b63863eab2eb86eee1bd9f33dd6682" -"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3" @@ -998,13 +951,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" -"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -"checksum unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25" "checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" -"checksum url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2a321979c09843d272956e73700d12c4e7d3d92b2ee112b31548aef0d4efc5a6" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" "checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" diff --git a/Cargo.toml b/Cargo.toml index 27ef584..2d2d0de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ serde_derive = "1" serde_json = "1" cargo_metadata = { version = "0.9.1", optional = true } semver = {version = "0.9.0", optional = true } -url = "1.7.1" goblin = { version = "0.0.17", optional = true } rust-ini = "0.13" failure = "0.1" diff --git a/src/bin/cargo-nro.rs b/src/bin/cargo-nro.rs index 0d33601..67f18ce 100644 --- a/src/bin/cargo-nro.rs +++ b/src/bin/cargo-nro.rs @@ -3,7 +3,6 @@ extern crate clap; extern crate linkle; extern crate serde; extern crate serde_json; -extern crate url; #[macro_use] extern crate serde_derive; extern crate cargo_metadata; @@ -26,7 +25,6 @@ use failure::Fail; use goblin::elf::section_header::{SHT_NOBITS, SHT_STRTAB, SHT_SYMTAB}; use goblin::elf::{Elf, Header as ElfHeader, ProgramHeader}; use linkle::format::{nacp::NacpFile, nxo::NxoFile, romfs::RomFs}; -use url::Url; #[derive(Debug, Fail, Display)] enum Error { @@ -54,15 +52,6 @@ impl From for Error { } } -fn find_project_root(path: &Path) -> Option<&Path> { - for parent in path.ancestors() { - if parent.join("Cargo.toml").is_file() { - return Some(parent); - } - } - None -} - // TODO: Run cargo build --help to get the list of options! const CARGO_OPTIONS: &str = "CARGO OPTIONS: -p, --package ... Package to build @@ -97,32 +86,6 @@ const CARGO_OPTIONS: &str = "CARGO OPTIONS: -Z ... Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Prints help information"; -fn get_metadata( - manifest_path: &Path, - package_id: cargo_metadata::PackageId, - target_name: &str, -) -> (Package, PackageMetadata) { - let mut cmd = cargo_metadata::MetadataCommand::new(); - cmd.manifest_path(manifest_path); - - let metadata = cmd.exec().unwrap(); - - let package = metadata - .packages - .into_iter() - .find(|v| v.id == package_id) - .unwrap(); - let package_metadata = serde_json::from_value( - package - .metadata - .pointer(&format!("linkle/{}", target_name)) - .cloned() - .unwrap_or(serde_json::Value::Null), - ) - .unwrap_or_default(); - (package, package_metadata) -} - trait BetterIOWrite: IOwrite { fn iowrite_with_try< N: scroll::ctx::SizeWith + scroll::ctx::TryIntoCtx, @@ -300,13 +263,10 @@ fn main() { .after_help(CARGO_OPTIONS) .get_matches_from(args); + let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap(); + let rust_target_path = match env::var("RUST_TARGET_PATH") { - Err(VarError::NotPresent) => { - // TODO: Handle workspace - find_project_root(&env::current_dir().unwrap()) - .unwrap() - .into() - } + Err(VarError::NotPresent) => metadata.workspace_root.clone(), s => PathBuf::from(s.unwrap()), }; @@ -346,28 +306,16 @@ fn main() { if artifact.target.kind.contains(&"bin".into()) || artifact.target.kind.contains(&"cdylib".into()) => { - // Find the artifact's source. This is not going to be pretty. - // For whatever reason, cargo thought it'd be a *great idea* to make file URLs use - // the non-standard "path+file:///" scheme, instead of, y'know, the ""file:///" everyone - // knows. - // - // So we check if it starts with path+file, and if it does, we skip the path+ part when - // parsing it. - let url = if artifact.package_id.url().starts_with("path+file") { - &artifact.package_id.url()["path+".len()..] - } else { - artifact.package_id.url() + let package: &Package = match metadata.packages.iter().find(|v| v.id == artifact.package_id) { + Some(v) => v, + None => continue }; - let url = Url::parse(url).unwrap(); - if url.scheme() != "file" { - continue; - } - - let root = url.to_file_path().unwrap(); - let manifest = root.join("Cargo.toml"); - let (package, target_metadata) = - get_metadata(&manifest, artifact.package_id.clone(), &artifact.target.name); + let root = package.manifest_path.parent().unwrap(); + let target_metadata : PackageMetadata = serde_json::from_value(package.metadata + .pointer(&format!("linkle/{}", artifact.target.name)) + .cloned() + .unwrap_or(serde_json::Value::Null)).unwrap_or_default(); let romfs = if let Some(romfs) = target_metadata.romfs { let romfs_path = root.join(romfs); @@ -399,7 +347,7 @@ fn main() { let icon_file = icon_file.as_ref().map(|v| v.as_ref()); let mut nacp = target_metadata.nacp.unwrap_or_default(); - nacp.name.get_or_insert(package.name); + nacp.name.get_or_insert(package.name.clone()); nacp.author.get_or_insert(package.authors[0].clone()); nacp.version.get_or_insert(package.version.to_string()); if nacp.title_id.is_none() {