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
use toml_edit::{value, Array, Document};

use ansi_term::*;
use std::collections::HashMap;
use std::fs::{read_to_string, OpenOptions};
use std::io::Write;
use std::process::exit;





/// The heart of your spec
pub struct Program {
    name: &'static str,
    authors: Array,
    dependencies: HashMap<&'static str, &'static str>,
    version: &'static str,
    edition: &'static str,
    readme: Option<&'static str>,
    repo: Option<&'static str>,
    docs: Option<&'static str>,
    desc: Option<&'static str>,

    license: &'static str,
}




impl Program {
    /// Adds a license
    ///
    /// Required but defaults to **MIT**
    pub fn license(mut self, license: &'static str) -> Self {
        info("License is...");
        info(license);
        self.license = license;
        self
    }

    /// Creates a new program struct.
    ///
    ///
    pub fn new() -> Program {
        Program {
            // 0000 means required value not set
            // blank means optional
            name: "placeholder name",
            authors: Array::default(),
            version: "plaeceholder",
            edition: "2021",
            dependencies: HashMap::new(),
            readme: None,
            repo: None,
            docs: None,
            desc: None,

            license: "MIT",
        }
    }



    /// Adds a readme path
    ///
    /// **OPTIONAL**
    pub fn readme(mut self, readmepath: &'static str) -> Self {
        self.readme = Some(readmepath);
        info("Readme path is...");
        info(readmepath);
        self
    }
    /// Adds a documentation path
    ///
    /// **OPTIONAL**
    pub fn docs(mut self, docspath: &'static str) -> Self {
        self.docs = Some(docspath);
        info("Docs path is...");
        info(docspath);
        self
    }
    /// Adds a readme path
    ///
    /// **OPTIONAL**
    pub fn description(mut self, description: &'static str) -> Self {
        self.desc = Some(description);
        info("Description  is...");
        info(description);
        self
    }

    /// Edition
    ///
    /// Specifies the rust **edition**
    /// It is required but defaults to rust 2018
    pub fn edition(mut self, editon: &'static str) -> Self {
        info("With Edition");
        info(editon);
        self.edition = editon;
        self
    }

    /// Adds an author
    ///
    /// **It is required**
    pub fn author(mut self, author: &'static str) -> Self {
        info("Author name is");
        info(author);
        self.authors.push(author);
        self
    }
    /// Sets the version
    ///
    /// **It is required**
    pub fn version(mut self, version: &'static str) -> Self {
        info("Package Version is");
        info(version);
        self.version = version;
        self
    }
    /// Checks the package for invalid config
    pub fn check(&self) {
        if self.name == "placeholder name"
            || self.authors.is_empty()
            || self.version == "placeholder"
        {
            error(" Missing required fields. Name, Author and Version are required in spec.rs");
        }
    }

    /// Sets the repository
    /// Optional

    pub fn repo(mut self, repopath: &'static str) -> Self {
        info("With repository...");
        info(repopath);
        self.repo = Some(repopath);
        self
    }

    /// Adds a name propety under  the package section
    ///
    /// Names are **required**
    pub fn name(mut self, name: &'static str) -> Self {
        info("Package name is");
        info(name);
        self.name = name;

        self
    }
    /// Adds a dependency
    pub fn dependencie(mut self, name: &'static str, version: &'static str) -> Self {
        info("Dependencie");
        info(name);
        info("With version");
        info(version);
        self.dependencies.insert(name, version);
        self
    }
    /// Generates cargo toml.
    ///
    /// __Also runs the `check` function__
    pub fn gen(self) {
        good("Generating....");
        self.check();
        let cargo_toml = read_to_string("Cargo.toml");
        if let Result::Ok(cargo_toml) = cargo_toml {
            let cargo_toml_doc = cargo_toml.parse::<Document>();
            if let Ok(mut tomlspec) = cargo_toml_doc {
                tomlspec["package"]["name"] = value(self.name);
                tomlspec["package"]["authors"] = value(self.authors);
                tomlspec["package"]["version"] = value(self.version);
                tomlspec["package"]["edition"] = value(self.edition);
                tomlspec["package"]["license"] = value(self.license);
                // ------Depednecies-----

                for dep in self.dependencies {
                    tomlspec["dependencies"][dep.0] = value(dep.1)
                }

                // Optional fields-
                if let Some(readmepath) = self.readme {
                    tomlspec["package"]["readme"] = value(readmepath);
                }
                if let Some(repo) = self.repo {
                    tomlspec["package"]["repository"] = value(repo);
                }
                if let Some(desc) = self.desc {
                    tomlspec["package"]["description"] = value(desc);
                }
                if let Some(docs) = self.docs {
                    tomlspec["package"]["documentation"] = value(docs);
                }
                let mut cargo_file = OpenOptions::new().write(true).open("Cargo.toml")
                .expect("Fatal error. Cargo toml not found. This is probably a bug. Please report it on github.");
                cargo_file
                    .write(tomlspec.to_string().as_bytes())
                    .expect("Cargo.toml erorr");
            }
        } else {
            error(" Previous Cargo.toml is invalid. Run cargo check to see the mistakes")
        }
    }
}

#[cfg(debug_assertions)]
fn warn(reason: &str) {
    eprintln!(
        "{} : {}",
        ansi_term::Color::Yellow.bold().paint("WARNING"),
        Color::Cyan.paint(reason)
    );
}
fn info(reason: &str) {
    eprintln!(
        "{} {}",
        ansi_term::Color::Blue.bold().paint("Info:"),
        Color::Cyan.paint(reason)
    );
}
fn good(reason: &str) {
    eprintln!(
        "{} {}",
        ansi_term::Color::Green.bold().paint("Good news!"),
        Color::Cyan.paint(reason)
    );
}
fn error(reason: &str) {
    eprintln!(
        "{}: {}",
        ansi_term::Color::Red
            .bold()
            .paint("Whoops an error has occured"),
        Color::Cyan.paint(reason)
    );
    exit(1);
}