Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/cmds/system/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ use crate::core::guard::never_worse;
use crate::core::tracking;
use crate::core::truncate::{reduced, CAP_WARNINGS};
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use std::fs;
use std::path::Path;

lazy_static! {
static ref CARGO_DEP_RE: Regex =
Regex::new(r#"^([a-zA-Z0-9_-]+)\s*=\s*(?:"([^"]+)"|.*version\s*=\s*"([^"]+)")"#).unwrap();
static ref CARGO_SECTION_RE: Regex = Regex::new(r"^\[([^\]]+)\]").unwrap();
static ref REQUIREMENTS_DEP_RE: Regex =
Regex::new(r"^([a-zA-Z0-9_-]+)([=<>!~]+.*)?$").unwrap();
}

const MAX_DEPS: usize = CAP_WARNINGS;
// dev deps are secondary to prod — show fewer.
const MAX_DEV_DEPS: usize = reduced(CAP_WARNINGS, 5);
Expand Down Expand Up @@ -82,21 +91,18 @@ pub fn run(path: &Path, verbose: u8) -> Result<()> {

fn summarize_cargo_str(path: &Path) -> Result<String> {
let content = fs::read_to_string(path)?;
let dep_re =
Regex::new(r#"^([a-zA-Z0-9_-]+)\s*=\s*(?:"([^"]+)"|.*version\s*=\s*"([^"]+)")"#).unwrap();
let section_re = Regex::new(r"^\[([^\]]+)\]").unwrap();
let mut current_section = String::new();
let mut deps = Vec::new();
let mut dev_deps = Vec::new();
let mut out = String::new();

for line in content.lines() {
if let Some(caps) = section_re.captures(line) {
if let Some(caps) = CARGO_SECTION_RE.captures(line) {
current_section = caps
.get(1)
.map(|m| m.as_str().to_string())
.unwrap_or_default();
} else if let Some(caps) = dep_re.captures(line) {
} else if let Some(caps) = CARGO_DEP_RE.captures(line) {
let name = caps.get(1).map(|m| m.as_str()).unwrap_or("");
let version = caps
.get(2)
Expand Down Expand Up @@ -171,7 +177,6 @@ fn summarize_package_json_str(path: &Path) -> Result<String> {

fn summarize_requirements_str(path: &Path) -> Result<String> {
let content = fs::read_to_string(path)?;
let dep_re = Regex::new(r"^([a-zA-Z0-9_-]+)([=<>!~]+.*)?$").unwrap();
let mut deps = Vec::new();
let mut out = String::new();

Expand All @@ -180,7 +185,7 @@ fn summarize_requirements_str(path: &Path) -> Result<String> {
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some(caps) = dep_re.captures(line) {
if let Some(caps) = REQUIREMENTS_DEP_RE.captures(line) {
let name = caps.get(1).map(|m| m.as_str()).unwrap_or("");
let version = caps.get(2).map(|m| m.as_str()).unwrap_or("");
deps.push(format!("{}{}", name, version));
Expand Down