From bf70bda2c9b386a0d48164843dbd3fbfc31f9625 Mon Sep 17 00:00:00 2001 From: sergerad Date: Thu, 14 Nov 2024 17:33:40 +1300 Subject: [PATCH] Move outdated code to separate module --- scripts/outdated.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ scripts/xtask.rs | 54 +++++---------------------------------------- 2 files changed, 59 insertions(+), 49 deletions(-) create mode 100644 scripts/outdated.rs diff --git a/scripts/outdated.rs b/scripts/outdated.rs new file mode 100644 index 000000000..8fd09f428 --- /dev/null +++ b/scripts/outdated.rs @@ -0,0 +1,54 @@ +use std::process::{Command, Stdio}; + +use anyhow::Result; +use anyhow::{ensure, Context as _}; +use serde::Deserialize; + +#[derive(Deserialize)] +struct Outdated<'a> { + crate_name: &'a str, + dependencies: Vec>, +} + +#[derive(Deserialize)] +struct Dependency<'a> { + name: &'a str, + project: &'a str, + latest: &'a str, +} + +pub fn list_outdated_deps() -> Result<()> { + let output = Command::new("cargo") + .args(["outdated", "--root-deps-only", "--format=json"]) + .stderr(Stdio::inherit()) + .stdout(Stdio::piped()) + .output() + .context("couldn't exec `cargo`")?; + ensure!( + output.status.success(), + "command failed with {}", + output.status + ); + + let outdated_items = serde_json::Deserializer::from_slice(&output.stdout) + .into_iter::>() + .collect::, _>>() + .context("failed to parse output from `cargo outdated`")?; + for Outdated { + crate_name, + dependencies, + } in outdated_items + { + for Dependency { + name, + project, + latest, + } in dependencies + { + // https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-warning-message + println!("::warning title=outdated-dependency::dependency {name} of crate {crate_name} is at version {project}, but the latest is {latest}") + } + } + + Ok(()) +} diff --git a/scripts/xtask.rs b/scripts/xtask.rs index c60770e28..9218327f1 100644 --- a/scripts/xtask.rs +++ b/scripts/xtask.rs @@ -1,10 +1,10 @@ //! General purpose scripts for development -use std::process::{Command, Stdio}; +mod outdated; -use anyhow::{ensure, Context as _}; +use anyhow::Result; use clap::Parser; -use serde::Deserialize; +use outdated::list_outdated_deps; #[derive(Parser)] enum Args { @@ -18,52 +18,8 @@ enum Args { Outdated, } -#[derive(Deserialize)] -struct Outdated<'a> { - crate_name: &'a str, - dependencies: Vec>, -} - -#[derive(Deserialize)] -struct Dependency<'a> { - name: &'a str, - project: &'a str, - latest: &'a str, -} - -fn main() -> anyhow::Result<()> { +fn main() -> Result<()> { match Args::parse() { - Args::Outdated => { - let output = Command::new("cargo") - .args(["outdated", "--root-deps-only", "--format=json"]) - .stderr(Stdio::inherit()) - .stdout(Stdio::piped()) - .output() - .context("couldn't exec `cargo`")?; - ensure!( - output.status.success(), - "command failed with {}", - output.status - ); - for Outdated { - crate_name, - dependencies, - } in serde_json::Deserializer::from_slice(&output.stdout) - .into_iter::>() - .collect::, _>>() - .context("failed to parse output from `cargo outdated`")? - { - for Dependency { - name, - project, - latest, - } in dependencies - { - // https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-warning-message - println!("::warning title=outdated-dependency::dependency {name} of crate {crate_name} is at version {project}, but the latest is {latest}") - } - } - } + Args::Outdated => list_outdated_deps(), } - Ok(()) }