From c3f803206abe0564c92fd9d36dd5f252d7a6042c Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 4 Aug 2022 21:53:18 +0100 Subject: [PATCH] Add the ability to read the dependencies of a crate --- tools/src/publish.rs | 52 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/tools/src/publish.rs b/tools/src/publish.rs index ada644d5..c0f9e016 100644 --- a/tools/src/publish.rs +++ b/tools/src/publish.rs @@ -20,6 +20,8 @@ pub enum Error { PublishCrate, Poll, CrateVersion, + ReadingDependencies, + CargoToml, } pub fn command() -> clap::Command<'static> { @@ -117,11 +119,7 @@ fn get_url_to_poll(crate_name: &str) -> String { } fn read_cargo_toml_version(folder: &Path) -> Result { - let cargo_toml_contents = - fs::read_to_string(folder.join("Cargo.toml")).map_err(|_| Error::CrateVersion)?; - let cargo_toml: Document = cargo_toml_contents - .parse() - .map_err(|_| Error::CrateVersion)?; + let cargo_toml = read_cargo_toml(folder)?; let version_value = cargo_toml["package"]["version"] .as_value() @@ -132,6 +130,33 @@ fn read_cargo_toml_version(folder: &Path) -> Result { Ok(version_value.to_owned()) } +fn get_agb_dependencies(folder: &Path) -> Result, Error> { + let cargo_toml = read_cargo_toml(folder)?; + + let dependencies = cargo_toml["dependencies"] + .as_table() + .ok_or(Error::ReadingDependencies)? + .get_values(); + + let mut result = vec![]; + + for (key, _) in dependencies { + let dep = key[0].get(); + if dep.starts_with("agb") { + result.push(dep.replace('_', "-")) + } + } + + Ok(result) +} + +fn read_cargo_toml(folder: &Path) -> Result { + let cargo_toml_contents = + fs::read_to_string(folder.join("Cargo.toml")).map_err(|_| Error::CargoToml)?; + let cargo_toml: Document = cargo_toml_contents.parse().map_err(|_| Error::CargoToml)?; + Ok(cargo_toml) +} + #[cfg(test)] mod test { use super::*; @@ -170,4 +195,21 @@ mod test { assert_eq!(my_version, "0.1.0"); Ok(()) } + + #[test] + fn should_detect_dependencies() -> Result<(), Error> { + let root_directory = find_agb_root_directory()?; + let deps = get_agb_dependencies(&root_directory.join("agb"))?; + + assert_eq!( + deps, + &[ + "agb-image-converter", + "agb-sound-converter", + "agb-macros", + "agb-fixnum" + ] + ); + Ok(()) + } }