|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +use anyhow::{bail, Context, Result}; |
| 4 | + |
| 5 | +/// Runs Anchor-specific lints on the workspace |
| 6 | +pub fn run(path: &str, pattern: &str) -> Result<()> { |
| 7 | + ensure_dylint()?; |
| 8 | + let status = Command::new("cargo") |
| 9 | + .args(["dylint", "--path", path, "--pattern", pattern]) |
| 10 | + .status() |
| 11 | + .context("executing dylint")?; |
| 12 | + if !status.success() { |
| 13 | + bail!("dylint did not execute successfully"); |
| 14 | + } |
| 15 | + Ok(()) |
| 16 | +} |
| 17 | + |
| 18 | +/// Ensures dylint is installed |
| 19 | +fn ensure_dylint() -> Result<()> { |
| 20 | + let output = Command::new("cargo") |
| 21 | + .arg("install") |
| 22 | + .arg("--list") |
| 23 | + .output()?; |
| 24 | + let mut has_cargo_dylint = false; |
| 25 | + let mut has_dylint_link = false; |
| 26 | + for line in String::from_utf8(output.stdout) |
| 27 | + .context("parsing `cargo install --list` output")? |
| 28 | + .lines() |
| 29 | + { |
| 30 | + let line = line.trim(); |
| 31 | + if line == "cargo-dylint" { |
| 32 | + has_cargo_dylint = true; |
| 33 | + } else if line == "dylint-link" { |
| 34 | + has_dylint_link = true; |
| 35 | + } |
| 36 | + if has_cargo_dylint && has_dylint_link { |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + if has_cargo_dylint && has_dylint_link { |
| 41 | + return Ok(()); |
| 42 | + } |
| 43 | + |
| 44 | + eprintln!("Installing required packages"); |
| 45 | + let mut cmd = Command::new("cargo"); |
| 46 | + cmd.arg("install"); |
| 47 | + if !has_cargo_dylint { |
| 48 | + cmd.arg("cargo-dylint"); |
| 49 | + } |
| 50 | + if !has_dylint_link { |
| 51 | + cmd.arg("dylint-link"); |
| 52 | + } |
| 53 | + |
| 54 | + if !cmd.status().context("installing dylint")?.success() { |
| 55 | + bail!("installing dylint failed"); |
| 56 | + } |
| 57 | + Ok(()) |
| 58 | +} |
0 commit comments