Skip to content

Commit

Permalink
Cargo-nro: Don't try to parse PackageID
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Apr 16, 2020
1 parent 3dd7a51 commit e16965e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 115 deletions.
50 changes: 0 additions & 50 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
76 changes: 12 additions & 64 deletions src/bin/cargo-nro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -54,15 +52,6 @@ impl From<std::io::Error> 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 <SPEC>... Package to build
Expand Down Expand Up @@ -97,32 +86,6 @@ const CARGO_OPTIONS: &str = "CARGO OPTIONS:
-Z <FLAG>... 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<Ctx: Copy>: IOwrite<Ctx> {
fn iowrite_with_try<
N: scroll::ctx::SizeWith<Ctx, Units = usize> + scroll::ctx::TryIntoCtx<Ctx>,
Expand Down Expand Up @@ -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()),
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit e16965e

Please sign in to comment.