Skip to main content

provenant/parsers/
autotools.rs

1// SPDX-FileCopyrightText: Provenant contributors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Parser for Autotools configure scripts.
5//!
6//! Extracts basic package metadata from Autotools configure files by using
7//! the parent directory name as the package name.
8//!
9//! # Supported Formats
10//! - configure (Autotools configure script)
11//! - configure.ac (Autoconf input file)
12//!
13//! # Key Features
14//! - Lightweight detection based on parent directory name
15//! - No file content parsing required
16//!
17//! # Implementation Notes
18//! - This parser does NOT read file contents, only extracts parent directory name
19//! - configure.in is NOT supported (deprecated legacy format)
20//! - Returns minimal PackageData with only package_type and name fields
21
22use crate::models::PackageData;
23use crate::models::{DatasourceId, PackageType};
24use packageurl::PackageUrl;
25use std::path::Path;
26
27use super::PackageParser;
28
29/// Parser for Autotools configure scripts.
30///
31/// Extracts the parent directory name as the package name without parsing file contents.
32pub struct AutotoolsConfigureParser;
33
34impl PackageParser for AutotoolsConfigureParser {
35    const PACKAGE_TYPE: PackageType = PackageType::Autotools;
36
37    fn is_match(path: &Path) -> bool {
38        path.file_name()
39            .and_then(|name| name.to_str())
40            .is_some_and(|name| name == "configure" || name == "configure.ac")
41    }
42
43    fn extract_packages(path: &Path) -> Vec<PackageData> {
44        let name = path
45            .parent()
46            .and_then(|p| p.file_name())
47            .and_then(|n| n.to_str())
48            .map(|s| s.to_string())
49            .or_else(|| {
50                path.file_name()
51                    .is_some_and(|name| name == "configure" || name == "configure.ac")
52                    .then_some("input".to_string())
53            });
54
55        let purl = name.as_deref().and_then(|name| {
56            PackageUrl::new(Self::PACKAGE_TYPE.as_str(), name)
57                .ok()
58                .map(|purl| purl.to_string())
59        });
60
61        vec![PackageData {
62            package_type: Some(Self::PACKAGE_TYPE),
63            name,
64            datasource_id: Some(DatasourceId::AutotoolsConfigure),
65            purl,
66            ..Default::default()
67        }]
68    }
69}
70
71crate::register_parser!(
72    "Autotools configure script",
73    &["**/configure", "**/configure.ac"],
74    "autotools",
75    "C",
76    Some("https://www.gnu.org/software/autoconf/"),
77);