provenant-cli 0.1.14

Rust scanner for ScanCode-compatible workflows, licenses, package metadata, SBOMs, and provenance data.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::path::Path;

use crate::parser_warn as warn;
use packageurl::PackageUrl;
use serde_json::{Map as JsonMap, Value};

use crate::models::{DatasourceId, Dependency, PackageData, PackageType, Party, PartyType};
use crate::parsers::utils::{capped_iteration_limit, split_name_email, truncate_field};

use super::PackageParser;

pub struct VcpkgManifestParser;
pub struct VcpkgLockParser;

impl PackageParser for VcpkgManifestParser {
    const PACKAGE_TYPE: PackageType = PackageType::Vcpkg;

    fn is_match(path: &Path) -> bool {
        path.file_name().and_then(|name| name.to_str()) == Some("vcpkg.json")
    }

    fn extract_packages(path: &Path) -> Vec<PackageData> {
        let content = match crate::parsers::utils::read_file_to_string(path, None) {
            Ok(content) => content,
            Err(e) => {
                warn!("Failed to read vcpkg.json at {:?}: {}", path, e);
                return vec![default_package_data()];
            }
        };

        let json: Value = match serde_json::from_str(&content) {
            Ok(json) => json,
            Err(e) => {
                warn!("Failed to parse vcpkg.json at {:?}: {}", path, e);
                return vec![default_package_data()];
            }
        };

        vec![parse_vcpkg_manifest(path, &json)]
    }

    fn metadata() -> Vec<super::metadata::ParserMetadata> {
        vec![super::metadata::ParserMetadata {
            description: "vcpkg manifest file",
            file_patterns: &["**/vcpkg.json"],
            package_type: "vcpkg",
            primary_language: "",
            documentation_url: Some("https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json"),
        }]
    }
}

impl PackageParser for VcpkgLockParser {
    const PACKAGE_TYPE: PackageType = PackageType::Vcpkg;

    fn is_match(path: &Path) -> bool {
        path.file_name().and_then(|name| name.to_str()) == Some("vcpkg-lock.json")
    }

    fn extract_packages(path: &Path) -> Vec<PackageData> {
        let content = match crate::parsers::utils::read_file_to_string(path, None) {
            Ok(content) => content,
            Err(e) => {
                warn!("Failed to read vcpkg-lock.json at {:?}: {}", path, e);
                return vec![default_lock_package_data()];
            }
        };

        let json: Value = match serde_json::from_str(&content) {
            Ok(json) => json,
            Err(e) => {
                warn!("Failed to parse vcpkg-lock.json at {:?}: {}", path, e);
                return vec![default_lock_package_data()];
            }
        };

        vec![parse_vcpkg_lock(&json)]
    }

    fn metadata() -> Vec<super::metadata::ParserMetadata> {
        vec![super::metadata::ParserMetadata {
            description: "vcpkg registry lockfile",
            file_patterns: &["**/vcpkg-lock.json"],
            package_type: "vcpkg",
            primary_language: "C++",
            documentation_url: Some("https://github.com/microsoft/vcpkg-tool"),
        }]
    }
}

fn default_package_data() -> PackageData {
    PackageData {
        package_type: Some(PackageType::Vcpkg),
        datasource_id: Some(DatasourceId::VcpkgJson),
        ..Default::default()
    }
}

fn default_lock_package_data() -> PackageData {
    PackageData {
        package_type: Some(PackageType::Vcpkg),
        datasource_id: Some(DatasourceId::VcpkgLockJson),
        is_private: true,
        ..Default::default()
    }
}

fn parse_vcpkg_manifest(path: &Path, json: &Value) -> PackageData {
    let name = get_non_empty_string(json, "name").map(truncate_field);
    let version = manifest_version(json).map(truncate_field);
    let description = get_string_or_array(json, "description").map(truncate_field);
    let homepage_url = get_non_empty_string(json, "homepage").map(truncate_field);
    let extracted_license_statement = get_string_or_array(json, "license").map(truncate_field);
    let parties = extract_maintainers(json);
    let dependencies = extract_dependencies(json);
    let extra_data = build_extra_data(path, json);

    PackageData {
        package_type: Some(PackageType::Vcpkg),
        namespace: None,
        name: name.clone(),
        version: version.clone(),
        primary_language: Some("C++".to_string()),
        description,
        parties,
        homepage_url,
        extracted_license_statement,
        is_private: name.is_none(),
        dependencies,
        extra_data,
        datasource_id: Some(DatasourceId::VcpkgJson),
        purl: name
            .as_deref()
            .and_then(|name| build_vcpkg_purl(name, version.as_deref()))
            .map(truncate_field),
        ..default_package_data()
    }
}

fn parse_vcpkg_lock(json: &Value) -> PackageData {
    PackageData {
        primary_language: Some("C++".to_string()),
        extra_data: build_lock_extra_data(json),
        ..default_lock_package_data()
    }
}

fn build_lock_extra_data(json: &Value) -> Option<HashMap<String, Value>> {
    let registry_locks = extract_lock_registry_entries(json);
    if registry_locks.is_empty() {
        return None;
    }

    let mut extra = HashMap::new();
    extra.insert("registry_locks".to_string(), Value::Array(registry_locks));
    Some(extra)
}

fn extract_lock_registry_entries(json: &Value) -> Vec<Value> {
    let Some(registries) = json.as_object() else {
        return Vec::new();
    };

    let registry_limit = capped_iteration_limit(registries.len(), "vcpkg lock registries");
    registries
        .iter()
        .take(registry_limit)
        .filter_map(|(location, references)| {
            let references = references.as_object()?;
            let reference_limit = capped_iteration_limit(references.len(), "vcpkg lock references");
            let mut normalized_references = JsonMap::new();
            for (reference, revision) in references.iter().take(reference_limit) {
                let Some(revision) = revision
                    .as_str()
                    .map(str::trim)
                    .filter(|revision| !revision.is_empty())
                else {
                    continue;
                };

                normalized_references.insert(
                    truncate_field(reference.to_string()),
                    Value::String(truncate_field(revision.to_string())),
                );
            }

            if normalized_references.is_empty() {
                return None;
            }

            let mut entry = JsonMap::new();
            entry.insert(
                "location".to_string(),
                Value::String(truncate_field(location.to_string())),
            );
            entry.insert(
                "references".to_string(),
                Value::Object(normalized_references),
            );
            Some(Value::Object(entry))
        })
        .collect()
}

fn manifest_version(json: &Value) -> Option<String> {
    let version = [
        "version",
        "version-semver",
        "version-date",
        "version-string",
    ]
    .into_iter()
    .find_map(|field| get_non_empty_string(json, field));

    match (version, json.get("port-version").and_then(Value::as_i64)) {
        (Some(version), Some(port_version)) if port_version > 0 => {
            Some(format!("{}#{}", version, port_version))
        }
        (version, _) => version,
    }
}

fn extract_maintainers(json: &Value) -> Vec<Party> {
    let Some(value) = json.get("maintainers") else {
        return Vec::new();
    };

    let maintainers: Vec<String> = match value {
        Value::String(s) => vec![s.clone()],
        Value::Array(values) => {
            let limit = capped_iteration_limit(values.len(), "vcpkg maintainers");
            values
                .iter()
                .take(limit)
                .filter_map(Value::as_str)
                .map(ToOwned::to_owned)
                .collect()
        }
        _ => Vec::new(),
    };

    maintainers
        .into_iter()
        .map(|entry| {
            let (name, email) = split_name_email(&entry);
            Party {
                r#type: Some(PartyType::Person),
                role: Some("maintainer".to_string()),
                name: name.map(truncate_field),
                email: email.map(truncate_field),
                url: None,
                organization: None,
                organization_url: None,
                timezone: None,
            }
        })
        .collect()
}

fn extract_dependencies(json: &Value) -> Vec<Dependency> {
    let mut dependencies: Vec<Dependency> = json
        .get("dependencies")
        .and_then(Value::as_array)
        .map(|deps| {
            let limit = capped_iteration_limit(deps.len(), "vcpkg dependencies");
            deps.iter()
                .take(limit)
                .filter_map(parse_dependency_entry)
                .collect()
        })
        .unwrap_or_default();

    if let Some(features) = json.get("features").and_then(Value::as_object) {
        let features_limit = capped_iteration_limit(features.len(), "vcpkg features");
        for (feature_name, feature_value) in features.iter().take(features_limit) {
            let Some(feature_dependencies) =
                feature_value.get("dependencies").and_then(Value::as_array)
            else {
                continue;
            };

            let feature_deps_limit =
                capped_iteration_limit(feature_dependencies.len(), "vcpkg feature dependencies");
            for dependency in feature_dependencies
                .iter()
                .take(feature_deps_limit)
                .filter_map(parse_dependency_entry)
                .map(|mut dependency| {
                    let mut extra_data = dependency.extra_data.take().unwrap_or_default();
                    extra_data.insert(
                        "feature".to_string(),
                        Value::String(feature_name.to_string()),
                    );
                    dependency.extra_data = Some(extra_data);
                    dependency
                })
            {
                dependencies.push(dependency);
            }
        }
    }

    dependencies
}

fn parse_dependency_entry(value: &Value) -> Option<Dependency> {
    match value {
        Value::String(name) => Some(Dependency {
            purl: build_vcpkg_purl(name, None).map(truncate_field),
            extracted_requirement: Some(truncate_field(name.clone())),
            scope: Some("dependencies".to_string()),
            is_runtime: Some(true),
            is_optional: Some(false),
            is_pinned: Some(false),
            is_direct: Some(true),
            resolved_package: None,
            extra_data: None,
        }),
        Value::Object(obj) => {
            let name = obj.get("name").and_then(Value::as_str)?.trim();
            if name.is_empty() {
                return None;
            }

            let extracted_requirement = obj
                .get("version>=")
                .and_then(Value::as_str)
                .map(|v| truncate_field(v.to_owned()))
                .or_else(|| Some(truncate_field(name.to_string())));

            let host = obj.get("host").and_then(Value::as_bool).unwrap_or(false);
            let mut extra = HashMap::new();
            for field in [
                "version>=",
                "features",
                "default-features",
                "host",
                "platform",
            ] {
                if let Some(field_value) = obj.get(field) {
                    extra.insert(field.to_string(), field_value.clone());
                }
            }

            Some(Dependency {
                purl: build_vcpkg_purl(name, None).map(truncate_field),
                extracted_requirement,
                scope: Some("dependencies".to_string()),
                is_runtime: Some(!host),
                is_optional: Some(false),
                is_pinned: Some(false),
                is_direct: Some(true),
                resolved_package: None,
                extra_data: (!extra.is_empty()).then_some(extra),
            })
        }
        _ => None,
    }
}

fn build_extra_data(path: &Path, json: &Value) -> Option<HashMap<String, Value>> {
    let mut extra = HashMap::new();
    for field in [
        "builtin-baseline",
        "overrides",
        "supports",
        "default-features",
        "features",
        "configuration",
        "vcpkg-configuration",
        "documentation",
    ] {
        if let Some(value) = json.get(field) {
            extra.insert(field.to_string(), value.clone());
        }
    }

    if !extra.contains_key("configuration")
        && !extra.contains_key("vcpkg-configuration")
        && let Some(config) = read_sibling_configuration(path)
    {
        extra.insert("configuration".to_string(), config);
    }

    (!extra.is_empty()).then_some(extra)
}

fn read_sibling_configuration(path: &Path) -> Option<Value> {
    let sibling_path = path.with_file_name("vcpkg-configuration.json");
    let content = crate::parsers::utils::read_file_to_string(&sibling_path, None).ok()?;
    match serde_json::from_str(&content) {
        Ok(value) => Some(value),
        Err(e) => {
            warn!(
                "Failed to parse sibling vcpkg-configuration.json at {:?}: {}",
                sibling_path, e
            );
            None
        }
    }
}

fn get_non_empty_string(json: &Value, field: &str) -> Option<String> {
    json.get(field)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(|value| value.to_string())
}

fn get_string_or_array(json: &Value, field: &str) -> Option<String> {
    match json.get(field) {
        Some(Value::String(s)) if !s.trim().is_empty() => Some(s.trim().to_string()),
        Some(Value::Array(values)) => {
            let collected: Vec<_> = values
                .iter()
                .filter_map(Value::as_str)
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .collect();
            (!collected.is_empty()).then(|| collected.join("\n"))
        }
        _ => None,
    }
}

fn build_vcpkg_purl(name: &str, version: Option<&str>) -> Option<String> {
    let mut purl = PackageUrl::new("generic", name).ok()?;
    purl.with_namespace("vcpkg").ok()?;
    if let Some(version) = version {
        purl.with_version(version).ok()?;
    }
    Some(purl.to_string())
}