Skip to content

Commit

Permalink
Move outdated code to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Nov 14, 2024
1 parent b63c8e2 commit bf70bda
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
54 changes: 54 additions & 0 deletions scripts/outdated.rs
Original file line number Diff line number Diff line change
@@ -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<Dependency<'a>>,
}

#[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::<Outdated<'_>>()
.collect::<Result<Vec<_>, _>>()
.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(())
}
54 changes: 5 additions & 49 deletions scripts/xtask.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,52 +18,8 @@ enum Args {
Outdated,
}

#[derive(Deserialize)]
struct Outdated<'a> {
crate_name: &'a str,
dependencies: Vec<Dependency<'a>>,
}

#[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::<Outdated<'_>>()
.collect::<Result<Vec<_>, _>>()
.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(())
}

0 comments on commit bf70bda

Please sign in to comment.