rudimentary reporting

This commit is contained in:
Shautvast 2025-07-31 17:04:01 +02:00
parent 098cb5e0bc
commit 25a5bde49c
10 changed files with 636 additions and 27 deletions

493
Cargo.lock generated
View file

@ -2,6 +2,23 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aes"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.1.3" version = "1.1.3"
@ -61,12 +78,149 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "arbitrary"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223"
dependencies = [
"derive_arbitrary",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
[[package]]
name = "bzip2"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bea8dcd42434048e4f7a304411d9273a411f647446c1234a65ce0554923f4cff"
dependencies = [
"libbz2-rs-sys",
]
[[package]]
name = "cc"
version = "1.2.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7"
dependencies = [
"jobserver",
"libc",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]] [[package]]
name = "colorchoice" name = "colorchoice"
version = "1.0.4" version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "constant_time_eq"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "deflate64"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da692b8d1080ea3045efaab14434d40468c3d8657e42abddfffca87b428f4c1b"
[[package]]
name = "deranged"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e"
dependencies = [
"powerfmt",
]
[[package]]
name = "derive_arbitrary"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]] [[package]]
name = "env_filter" name = "env_filter"
version = "0.1.3" version = "0.1.3"
@ -90,6 +244,79 @@ dependencies = [
"log", "log",
] ]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "flate2"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a3d7db9596fecd151c5f638c0ee5d5bd487b6e0ea232e5dc96d5250f6f94b1d"
dependencies = [
"crc32fast",
"libz-rs-sys",
"miniz_oxide",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "indexmap"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "inout"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
version = "1.70.1" version = "1.70.1"
@ -126,6 +353,57 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "jobserver"
version = "0.1.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a"
dependencies = [
"getrandom",
"libc",
]
[[package]]
name = "libbz2-rs-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "775bf80d5878ab7c2b1080b5351a48b2f737d9f6f8b383574eebcc22be0dfccb"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "liblzma"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0791ab7e08ccc8e0ce893f6906eb2703ed8739d8e89b57c0714e71bad09024c8"
dependencies = [
"liblzma-sys",
]
[[package]]
name = "liblzma-sys"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01b9596486f6d60c3bbe644c0e1be1aa6ccc472ad630fe8927b456973d7cb736"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "libz-rs-sys"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "172a788537a2221661b480fee8dc5f96c580eb34fa88764d3205dc356c7e4221"
dependencies = [
"zlib-rs",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.27" version = "0.4.27"
@ -160,12 +438,43 @@ version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]] [[package]]
name = "once_cell_polyfill" name = "once_cell_polyfill"
version = "1.70.1" version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "pbkdf2"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2"
dependencies = [
"digest",
"hmac",
]
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]] [[package]]
name = "portable-atomic" name = "portable-atomic"
version = "1.11.1" version = "1.11.1"
@ -181,6 +490,18 @@ dependencies = [
"portable-atomic", "portable-atomic",
] ]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppmd-rust"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c834641d8ad1b348c9ee86dec3b9840d805acd5f24daa5f90c788951a52ff59b"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.95" version = "1.0.95"
@ -211,6 +532,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@ -260,6 +587,35 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "sha1"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "simd-adler32"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.104" version = "2.0.104"
@ -271,6 +627,31 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "time"
version = "0.3.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40"
dependencies = [
"deranged",
"num-conv",
"powerfmt",
"serde",
"time-core",
]
[[package]]
name = "time-core"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c"
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]] [[package]]
name = "undeepend" name = "undeepend"
version = "0.1.0" version = "0.1.0"
@ -279,6 +660,7 @@ dependencies = [
"log", "log",
"maud", "maud",
"regex", "regex",
"zip",
] ]
[[package]] [[package]]
@ -299,6 +681,15 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
@ -371,3 +762,105 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
dependencies = [
"zeroize_derive",
]
[[package]]
name = "zeroize_derive"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zip"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aed4ac33e8eb078c89e6cbb1d5c4c7703ec6d299fc3e7c3695af8f8b423468b"
dependencies = [
"aes",
"arbitrary",
"bzip2",
"constant_time_eq",
"crc32fast",
"deflate64",
"flate2",
"getrandom",
"hmac",
"indexmap",
"liblzma",
"memchr",
"pbkdf2",
"ppmd-rust",
"sha1",
"time",
"zeroize",
"zopfli",
"zstd",
]
[[package]]
name = "zlib-rs"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "626bd9fa9734751fc50d6060752170984d7053f5a39061f524cda68023d4db8a"
[[package]]
name = "zopfli"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edfc5ee405f504cd4984ecc6f14d02d55cfda60fa4b689434ef4102aae150cd7"
dependencies = [
"bumpalo",
"crc32fast",
"log",
"simd-adler32",
]
[[package]]
name = "zstd"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "7.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.15+zstd.1.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237"
dependencies = [
"cc",
"pkg-config",
]

View file

@ -7,4 +7,5 @@ edition = "2024"
log = "0.4" log = "0.4"
env_logger = "0.11" env_logger = "0.11"
regex="1.11" regex="1.11"
maud = "*" maud = "*"
zip = "4.3"

View file

@ -1,13 +1,25 @@
use std::{env, fs}; use std::hash::Hash;
use std::path::PathBuf; use std::path::PathBuf;
use std::{env, fs};
use undeepend::maven::project::parse_project; use undeepend::maven::project::parse_project;
use undeepend::maven::reporter::report;
fn main() { fn main() {
let home_dir = env::var("HOME").unwrap();
let args = std::env::args().collect::<Vec<String>>(); let args = std::env::args().collect::<Vec<String>>();
let dir = if args.len() ==1 { let dir = if args.len() == 1 {
env::current_dir().expect("Could not access current directory") env::current_dir().expect("Could not access current directory")
} else { PathBuf::from(&args[1]) }; } else {
PathBuf::from(&args[1])
};
let project = parse_project(&dir).unwrap(); let project = parse_project(&dir).unwrap();
//
// fs::write(
// PathBuf::from("index.html"),
// project.generate_dependency_html(),
// )
// .unwrap();
fs::write(PathBuf::from("index.html"), project.generate_dependency_html()).unwrap(); report(&project);
} }

View file

@ -2,3 +2,4 @@ pub mod metadata;
pub mod pom; pub mod pom;
pub mod pom_parser; pub mod pom_parser;
pub mod project; pub mod project;
pub mod reporter;

View file

@ -1,8 +1,12 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::{Path, PathBuf}; use std::env;
use std::path::PathBuf;
use std::sync::LazyLock;
/// the maven object model /// the maven object model
const HOME: LazyLock<String> = LazyLock::new(|| env::var("HOME").unwrap());
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct Pom { pub struct Pom {
pub parent: Option<Parent>, pub parent: Option<Parent>,
@ -20,9 +24,7 @@ pub struct Pom {
pub directory: PathBuf, pub directory: PathBuf,
} }
impl Pom { impl Pom {}
}
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct License { pub struct License {
@ -49,4 +51,28 @@ pub struct Dependency {
pub group_id: String, pub group_id: String,
pub artifact_id: String, pub artifact_id: String,
pub version: Option<String>, pub version: Option<String>,
} // pub scope: Option<String>, // TODO need this?
}
impl Dependency {
/// returns a relative path to the dependency location
pub fn to_path(&self) -> PathBuf {
let mut path = PathBuf::new();
path.push(self.group_id.replace(".", "/"));
path.push(&self.artifact_id);
let version = self.version.clone().unwrap_or_else(|| "latest".to_string());
path.push(&version);
path.push(format!("{}-{}.jar", &self.artifact_id, &version));
path
// why is the version (in the filename) wrong when I use PathBuf::set_extension("jar") ???
}
/// returns an absolute path based on the default maven localRepository location
// useful?
pub fn to_absolute_path(&self) -> PathBuf {
let mut absolute_path = PathBuf::from(HOME.as_str());
absolute_path.push(".m2/repository");
absolute_path.push(self.to_path());
absolute_path
}
}

View file

@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
/// parse the pom.xml into a Pom object (struct) /// parse the pom.xml into a Pom object (struct)
pub fn get_pom(xml: impl Into<String>) -> Result<Pom, SaxError> { pub fn get_pom(home_dir: PathBuf, xml: impl Into<String>) -> Result<Pom, SaxError> {
let mut group_id = None; let mut group_id = None;
let mut artefact_id = None; let mut artefact_id = None;
let mut parent = None; let mut parent = None;
@ -47,7 +47,7 @@ pub fn get_pom(xml: impl Into<String>) -> Result<Pom, SaxError> {
properties, properties,
module_names, module_names,
modules: vec![], modules: vec![],
directory: PathBuf::new(), // resolved later, make optional? directory: home_dir,
}) })
} }

View file

@ -26,10 +26,12 @@ pub fn parse_project(project_dir: &Path) -> Result<Project, String> {
} }
let pom_file = fs::read_to_string(pom_file).map_err(|e| e.to_string())?; let pom_file = fs::read_to_string(pom_file).map_err(|e| e.to_string())?;
let mut root = get_pom(pom_file).map_err(|e| e.to_string())?; let mut root = get_pom(project_dir.to_path_buf(), pom_file).map_err(|e| e.to_string())?;
resolve_modules(project_dir, &mut root); resolve_modules(project_dir, &mut root);
Ok(Project { root }) let project_home = project_dir.to_str().unwrap_or_else(|| "?").to_string(); //TODO unwrap can fail??
Ok(Project { project_home, root })
} }
// examines modules in pom and loads them // examines modules in pom and loads them
@ -54,16 +56,14 @@ fn read_module_pom(project_dir: &Path, module: &String) -> Pom {
let module_pom = let module_pom =
fs::read_to_string(module_file).expect(format!("Cannot read file {}", module).as_str()); fs::read_to_string(module_file).expect(format!("Cannot read file {}", module).as_str());
let mut pom = get_pom(module_dir, module_pom).expect(format!("Cannot create module pom {}", module).as_str())
get_pom(module_pom).expect(format!("Cannot create module pom {}", module).as_str());
pom.directory = module_dir;
pom
} }
//main entry to project //main entry to project
//the (root) pom holds the child references to modules //the (root) pom holds the child references to modules
#[derive(Debug)] #[derive(Debug)]
pub struct Project { pub struct Project {
pub project_home: String,
pub root: Pom, pub root: Pom,
} }
@ -119,11 +119,11 @@ impl Project {
pom: &'a Pom, pom: &'a Pom,
group_id: &str, group_id: &str,
artifact_id: &str, artifact_id: &str,
) -> Vec<&'a Dependency> { ) -> Vec<Dependency> {
fn collect<'a>( fn collect<'a>(
project: &Project, project: &'a Project,
pom: &'a Pom, pom: &'a Pom,
mut deps: Vec<&'a Dependency>, deps: &mut Vec<Dependency>,
group_id: &str, group_id: &str,
artifact_id: &str, artifact_id: &str,
) { ) {
@ -132,7 +132,8 @@ impl Project {
.dependency_management .dependency_management
.iter() .iter()
.filter(|d| d.group_id == group_id && d.artifact_id == artifact_id) .filter(|d| d.group_id == group_id && d.artifact_id == artifact_id)
.collect::<Vec<&'a Dependency>>(), .map(|d| d.clone())
.collect::<Vec<Dependency>>(),
); );
if let Some(parent) = &pom.parent { if let Some(parent) = &pom.parent {
if let Some(parent_pom) = project.get_pom(&parent.group_id, &parent.artifact_id) { if let Some(parent_pom) = project.get_pom(&parent.group_id, &parent.artifact_id) {
@ -142,14 +143,17 @@ impl Project {
} }
let mut dependencies = Vec::new(); let mut dependencies = Vec::new();
collect(self, pom, Vec::new(), group_id, artifact_id); collect(self, pom, &mut dependencies, group_id, artifact_id);
dependencies dependencies
} }
// recursively searches a property going up the chain towards parents // recursively searches a property going up the chain towards parents
fn get_property(&self, pom: &Pom, name: &str) -> Result<String, String> { fn get_property(&self, pom: &Pom, name: &str) -> Result<String, String> {
if pom.properties.contains_key(name) { if pom.properties.contains_key(name) {
pom.properties.get(name).cloned().ok_or(format!("Unknown property {}", name)) pom.properties
.get(name)
.cloned()
.ok_or(format!("Unknown property {}", name))
} else if let Some(parent) = &pom.parent { } else if let Some(parent) = &pom.parent {
if let Some(parent_pom) = self.get_pom(&parent.group_id, &parent.artifact_id) { if let Some(parent_pom) = self.get_pom(&parent.group_id, &parent.artifact_id) {
self.get_property(parent_pom, name) self.get_property(parent_pom, name)

71
src/maven/reporter.rs Normal file
View file

@ -0,0 +1,71 @@
use crate::maven::project::Project;
use regex::Regex;
use std::collections::HashSet;
use std::fs::File;
use std::path::Path;
use std::sync::LazyLock;
use zip::ZipArchive;
static CLASS_EXPR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(.+)/.+\.class").unwrap());
pub fn report(project: &Project) {
let pom = &project.root;
for dep in &project.get_dependencies(pom) {
println!("{:?}", dep);
//TODO all modules
let jar_file = File::open(dep.to_absolute_path()).expect("Can't open jar file");
let mut archive = ZipArchive::new(jar_file).expect("Can't read jar file");
let mut packages = HashSet::new();
for i in 0..archive.len() {
let file = archive.by_index(i).expect("Can't read file");
let name = file.name();
if CLASS_EXPR.is_match(name) {
let package = &CLASS_EXPR.captures(name).unwrap()[1];
packages.insert(package.replace("/", ".").to_string());
}
}
let mut src_main_java = pom.directory.clone();
src_main_java.push("src/main/java"); //TODO other src directories
traverse(&packages, &src_main_java);
let mut src_test_java = pom.directory.clone();
src_test_java.push("src/test/java"); //TODO other src directories
traverse(&packages, &src_test_java);
}
}
fn traverse(packages: &HashSet<String>, dir: &Path) {
if dir.exists() {
for entry in dir.read_dir().unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
traverse(packages, &path);
} else {
if path.extension().unwrap() == "java" {
analyse(packages, &path);
}
}
}
}
}
// TODO deal with import wildcards
fn analyse(packages: &HashSet<String>, path: &Path) {
let content = std::fs::read_to_string(path).unwrap();
let lines = content.lines();
for line in lines {
if line.contains("import") {
for package in packages {
if line.contains(package) {
println!("{:?}: {}", path, line);
}
}
}
}
}

View file

@ -1,5 +1,5 @@
use crate::maven::project::Project; use crate::maven::project::Project;
use maud::{Markup, PreEscaped, html}; use maud::{PreEscaped, html};
impl Project { impl Project {
pub fn generate_dependency_html(&self) -> String { pub fn generate_dependency_html(&self) -> String {

View file

@ -1,9 +1,10 @@
use std::path::PathBuf;
use undeepend::maven::pom_parser::get_pom; use undeepend::maven::pom_parser::get_pom;
#[test] #[test]
fn test_pom_parser_is_correct() { fn test_pom_parser_is_correct() {
let test_xml = include_str!("../maven/resources/pom.xml"); let test_xml = include_str!("../maven/resources/pom.xml");
let pom = get_pom(test_xml).expect("failed to get document"); let pom = get_pom(PathBuf::from("../maven/resources"), test_xml).expect("failed to get document");
assert_eq!(Some("Mockito".to_string()),pom.name); assert_eq!(Some("Mockito".to_string()),pom.name);
assert_eq!(Some("org.mockito".to_string()),pom.group_id); assert_eq!(Some("org.mockito".to_string()),pom.group_id);
assert_eq!("mockito-core",pom.artifact_id); assert_eq!("mockito-core",pom.artifact_id);
@ -27,7 +28,7 @@ fn test_pom_parser_is_correct() {
assert_eq!("objenesis", objenesis.artifact_id); assert_eq!("objenesis", objenesis.artifact_id);
assert_eq!(Some("1.0".to_string()), objenesis.version); assert_eq!(Some("1.0".to_string()), objenesis.version);
assert_eq!(2, pom.modules.len()); assert_eq!(2, pom.module_names.len());
assert_eq!("a", pom.module_names[0]); assert_eq!("a", pom.module_names[0]);
assert_eq!("b", pom.module_names[1]); assert_eq!("b", pom.module_names[1]);