aboutsummaryrefslogtreecommitdiff
path: root/mling/src/proj_mgr/show_binaries.rs
blob: 9d5caf0123236236e2ee451665bf48837b813329 (plain) (blame)
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
use std::path::PathBuf;

use colored::Colorize;
use mingling::{
    Groupped, RenderResult,
    macros::{chain, pack, renderer},
};
use serde::Serialize;
use std::io::Write as _;

use crate::{
    Next,
    proj_mgr::{
        EntryShowBinaries,
        metadata::{CargoLockFile, read_metadata},
    },
    res::ResManifestPath,
};

#[derive(Serialize, Groupped)]
pub struct ResultBinaries {
    pub binaries: Vec<DataBinary>,
}

#[derive(Serialize)]
pub struct DataBinary {
    pub name: String,
    pub path: PathBuf,
}

#[chain]
pub fn handle_show_binaries(_args: EntryShowBinaries, manifest_path: &ResManifestPath) -> Next {
    let metadata = read_metadata(manifest_path.resolved()).unwrap();
    let CargoLockFile {
        packages,
        workspace_members,
        ..
    } = metadata;

    let binaries: Vec<DataBinary> = packages
        .into_iter()
        .filter(|pkg| workspace_members.contains(&pkg.id))
        .flat_map(|pkg| {
            pkg.targets
                .into_iter()
                .filter(|target| target.kind.iter().any(|k| k == "bin"))
                .map(move |target| DataBinary {
                    name: target.name,
                    path: PathBuf::from(pkg.manifest_path.clone())
                        .parent()
                        .unwrap_or(&PathBuf::from("."))
                        .join("src")
                        .join(&target.src_path),
                })
        })
        .collect();

    ResultBinaries { binaries }.to_render()
}

#[renderer]
pub fn render_binaries(binaries: ResultBinaries) -> RenderResult {
    let mut result = RenderResult::default();
    writeln!(result, "{}", "Binaries:".bright_cyan().bold()).ok();
    if let Some(max_name_len) = binaries.binaries.iter().map(|b| b.name.len()).max() {
        for binary in &binaries.binaries {
            writeln!(
                result,
                "  {:width$} `{}`",
                binary.name.bright_yellow().bold(),
                binary.path.display().to_string().italic(),
                width = max_name_len
            )
            .ok();
        }
    }
    result
}