~danyspin97/rinstall

ref: 26cfc10edba215a83c3fe0fb066f893da367b816 rinstall/src/package.rs -rw-r--r-- 4.7 KiB
26cfc10eDanilo Spinella Use "cargo metadata" to get target_directory in rust projects 1 year, 3 months ago
                                                                                
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
use color_eyre::eyre::{Context, Result};
use serde::Deserialize;

use std::path::{Path, PathBuf};

use crate::install_entry::InstallEntry;
use crate::install_target::InstallTarget;
use crate::project::Project;
use crate::Dirs;

#[derive(Deserialize, Clone)]
pub enum Type {
    #[serde(rename(deserialize = "custom"))]
    Custom,
    #[serde(rename(deserialize = "rust"))]
    Rust,
}

#[derive(Deserialize)]
enum Completion {
    #[serde(rename(deserialize = "bash"))]
    Bash(PathBuf),
    #[serde(rename(deserialize = "fish"))]
    Fish(PathBuf),
    #[serde(rename(deserialize = "zsh"))]
    Zsh(PathBuf),
}

#[derive(Deserialize)]
pub struct Package {
    name: String,
    #[serde(rename(deserialize = "type"))]
    pub project_type: Type,
    #[serde(default)]
    exe: Vec<InstallEntry>,
    #[serde(default)]
    libs: Vec<InstallEntry>,
    #[serde(default)]
    man: Vec<InstallEntry>,
    #[serde(default)]
    data: Vec<InstallEntry>,
    #[serde(default)]
    docs: Vec<InstallEntry>,
    #[serde(default)]
    config: Vec<InstallEntry>,
    #[serde(default, rename(deserialize = "desktop-files"))]
    desktop_files: Vec<InstallEntry>,
    #[serde(default)]
    appdata: Vec<InstallEntry>,
    #[serde(default)]
    completions: Vec<Completion>,
}

impl Package {
    pub fn targets(
        self,
        dirs: Dirs,
        project: Project,
    ) -> Result<Vec<InstallTarget>> {
        let mut results = Vec::new();

        macro_rules! install_files {
            ( $files:tt, $install_dir:expr, $parent_dir:expr, $name:literal ) => {
                self.$files
                    .into_iter()
                    .map(|entry| -> Result<InstallTarget> {
                        InstallTarget::new(entry, $install_dir, $parent_dir)
                    })
                    .collect::<Result<Vec<InstallTarget>>>()
                    .with_context(|| format!("error while iterating {} files", $name))?
            };
        }

        results.extend(install_files!(exe, &dirs.bindir, &project.outputdir, "exe"));
        results.extend(install_files!(
            libs,
            &dirs.libdir,
            &project.projectdir,
            "libs"
        ));
        results.extend(install_files!(
            data,
            &dirs.datadir,
            &project.projectdir,
            "data"
        ));
        results.extend(install_files!(
            config,
            &dirs.sysconfdir,
            &project.projectdir,
            "config"
        ));
        if let Some(mandir) = &dirs.mandir {
            results.extend(install_files!(man, mandir, &project.projectdir, "man"));
        }

        let package_doc_dir = self.name.to_owned();
        if let Some(docdir) = &dirs.docdir {
            results.extend(install_files!(
                docs,
                &docdir.join(Path::new(&package_doc_dir)),
                &project.projectdir,
                "docs"
            ));
        }

        results.extend(install_files!(
            desktop_files,
            &dirs.datarootdir.join("applications"),
            &project.projectdir,
            "desktop"
        ));

        results.extend(install_files!(
            appdata,
            &dirs.datarootdir.join("appdata"),
            &project.projectdir,
            "appdata"
        ));

        results.extend(
            self.completions
                .into_iter()
                .map(|completion| -> Result<InstallTarget> {
                    let (entry, parent_dir) = match completion {
                        Completion::Bash(path) => (
                            InstallEntry {
                                source: path,
                                destination: None,
                            },
                            "bash-completion/completions",
                        ),
                        Completion::Fish(path) => (
                            InstallEntry {
                                source: path,
                                destination: None,
                            },
                            "fish/vendor_completions.d",
                        ),
                        Completion::Zsh(path) => (
                            InstallEntry {
                                source: path,
                                destination: None,
                            },
                            "zsh/site-functions",
                        ),
                    };
                    InstallTarget::new(
                        entry,
                        &dirs.datarootdir.join(parent_dir),
                        &project.projectdir,
                    )
                })
                .collect::<Result<Vec<InstallTarget>>>()
                .context("error while iterating completion files")?,
        );

        Ok(results)
    }
}