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
//! Linux specific utilities

use std::{
    env, fs,
    path::Path
};
use crate::chainable;
use chrono::{Local, Datelike, Timelike};
use crate::utils::run_command;

mod manpage;

pub use manpage::ManpageBuilder;

const MONTHS: [&str; 12] = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
];

/// Builder to create a basic `.desktop` file
#[chainable]
pub struct DesktopEntryFileBuilder {
    name: String,
    exec: String,
    terminal: bool,
    version: String,

    #[chainable(collapse_option, use_into_impl, doc = "Add a comment to the desktop file")]
    comment: Option<String>,

    #[chainable(collapse_option, use_into_impl, doc = "Add an icon to the desktop file")]
    icon: Option<String>,

    categories: Vec<String>
}

impl DesktopEntryFileBuilder {
    /// Create a new `DesktopEntryFileBuilder`
    pub fn new(name: &str, exec: &str, terminal: bool, version: &str) -> Self {
        DesktopEntryFileBuilder {
            name: name.to_string(),
            exec: exec.to_string(),
            terminal,
            version: version.to_string(),
            comment: None,
            icon: None,
            categories: vec![]
        }
    }

    /// Add a category to the desktop file
    pub fn category(mut self, category: &str) -> Self {
        self.categories.push(category.to_string());
        self
    }

    /// Build and save the desktop file to the specified path
    pub fn build<P: AsRef<Path>>(&self, path: P) {
        let mut file_str = format!(
            "[Desktop Entry]\n\
            Version={}\n\
            Type=Application\n\
            Name={}\n\
            Exec={}\n\
            Terminal={}\n\
            StartupNotify=false",
            self.version, self.name, self.exec, self.terminal
        );

        if let Some(ref comment) = self.comment {
            file_str.push_str(&format!("\nComment={comment}"));
        }

        if let Some(ref icon) = self.icon {
            file_str.push_str(&format!("\nIcon={icon}"));
        }

        if self.categories.len() > 0 {
            file_str.push_str("\nCategories=");
            for category in &self.categories {
                file_str.push_str(&format!("{category};"));
            }
        }

        fs::write(path.as_ref(), &file_str).unwrap();
    }
}

/// Builder to create a basic [Arch Linux PKGBUILD](https://wiki.archlinux.org/title/PKGBUILD) for an app
pub struct AurPkgbuildBuilder {
    name: String,
    version: String,
    author_name: String,
    author_email: String,
    desc: String,
    source_url: String,
    url: String,
    license: String,
    deps: Vec<String>,
    make_deps: Vec<String>,
    build_bash: String,
    pkg_bash: String
}

impl AurPkgbuildBuilder {
    /// Create a new `AurPkgbuildBuilder`
    pub fn new(
        name: &str,
        version: &str,
        author_name: &str,
        author_email: &str,
        desc: &str,
        source_url: &str,
        url: &str,
        license: &str,
        build_bash: &str,
        pkg_bash: &str
    ) -> Self {
        AurPkgbuildBuilder {
            name: name.to_string(),
            version: version.to_string(),
            author_name: author_name.to_string(),
            author_email: author_email.to_string(),
            desc: desc.to_string(),
            source_url: source_url.to_string(),
            url: url.to_string(),
            license: license.to_string(),
            build_bash: build_bash.to_string(),
            pkg_bash: pkg_bash.to_string(),
            deps: vec![],
            make_deps: vec![]
        }
    }

    /// Add a dependency for the PKGBUILD
    pub fn dependency(mut self, dep: &str) -> Self {
        self.deps.push(dep.to_string());
        self
    }

    /// Add make a dependency for the PKGBUILD
    pub fn make_dependency(mut self, dep: &str) -> Self {
        self.make_deps.push(dep.to_string());
        self
    }

    /// Build and save the PKGBUILD
    pub fn build<P: AsRef<Path>>(&self, dir: P) {
        if env::set_current_dir(dir.as_ref()).is_err() { return; }

        // get the current date and time (formatted)
        let date = Local::now().date_naive();
        let month = MONTHS[date.month() as usize - 1];
        let day = date.day();
        let year = date.year();
        let time = Local::now().time();
        let hour = time.hour();
        let minute = time.minute();
        let second = time.second();
        let time_str = format!("{hour:02}:{minute:02}:{second:02}");
        let mut deps_str = String::new();
        let mut make_deps_str = String::new();        

        for (i, dep) in self.deps.iter().enumerate() {
            if i > 0 { deps_str.push(' '); }
            deps_str.push_str(&format!("'{dep}'"));
        }

        for (i, dep) in self.make_deps.iter().enumerate() {
            if i > 0 { make_deps_str.push(' '); }
            make_deps_str.push_str(&format!("'{dep}'"));
        }

        let file_str = format!(
            "# Maintainer: {} <{}>\n\
            # Generated by cargo on {month} {day}, {year} at {time_str}\n\
            pkgname={}\n\
            pkgver={}\n\
            pkgrel=1\n\
            pkgdesc=\"{}\"\n\
            arch=('i686' 'x86_64')\n\
            url=\"{}\"\n\
            license=('{}')\n\
            depends=({deps_str})\n\
            makedepends=({make_deps_str})\n\
            source=(\"{}\")\n\
            md5sums=('SKIP')\n\
            build() {{\n{}\n}}\n\
            package() {{\n{}\n}}",
            self.author_name,
            self.author_email,
            self.name,
            self.version,
            self.desc,
            self.url,
            self.license,
            self.source_url,
            self.build_bash,
            self.pkg_bash
        );

        fs::write("PKGBUILD", &file_str).unwrap();
        let cmd = run_command("makepkg", false, ["--printsrcinfo"]);
        fs::write(".SRCINFO", cmd.output).unwrap();
    }
}