r4d 3.2.0-beta.1

Text oriented macro processor
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Cli processor for rado binary

use crate::utils::Utils;
use crate::RadResult;
use crate::{RadCli, RadError};
use clap::ArgAction;
use once_cell::sync::Lazy;
use std::io::Write;
use std::path::{Path, PathBuf};

/// Temporary rado directory
pub static RADO_DIR: Lazy<std::path::PathBuf> = Lazy::new(|| std::env::temp_dir().join("rado"));

#[cfg(windows)]
/// Default rado editor
pub(crate) const RADO_EDITOR: &str = "notepad.exe";
#[cfg(not(windows))]
/// Default rado editor
pub(crate) const RADO_EDITOR: &str = "vim";

/// Cli arguments parser for rado binary
pub struct RadoCli {
    flag_arguments: Vec<String>,
    flag_out: Option<PathBuf>,
}

impl Default for RadoCli {
    fn default() -> Self {
        Self::new()
    }
}

impl RadoCli {
    /// Create a new instance
    pub fn new() -> Self {
        Self {
            flag_arguments: vec![],
            flag_out: None,
        }
    }

    /// Parse rado arguments
    pub fn parse(&mut self) -> RadResult<()> {
        let cli_args = self.args_builder();
        self.parse_flags(&cli_args);
        self.run_rado(&cli_args)
    }

    /// Parse flag occurrences from matches
    fn parse_flags(&mut self, args: &clap::ArgMatches) {
        if let Some((_, args)) = args.subcommand() {
            if let Some(path) = args.get_one::<&str>("out") {
                self.flag_out.replace(PathBuf::from(path));
            }

            if let Some(values) = args.get_many::<&str>("argument") {
                self.flag_arguments = values.map(|s| s.to_string()).collect();
            }
        }
    }

    /// Run rado binary according to flag matches
    fn run_rado(&mut self, args: &clap::ArgMatches) -> RadResult<()> {
        match args.subcommand() {
            Some(("env", _)) => {
                write!(
                    std::io::stdout(),
                    "RADO_EDITOR\t: {}\nRADO_DIR\t: {}\n",
                    RADO_EDITOR,
                    (RADO_DIR).display()
                )?;
            }
            Some(("package", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    self.package_file(Path::new(input))?;
                }
            }
            Some(("execute", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    self.execute(Path::new(input))?;
                }
            }
            Some(("replace", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    self.replace_file(Path::new(input))?;
                }
            }
            Some(("edit", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    self.view_file(Path::new(input))?;
                }
            }
            Some(("read", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    let file = self.update_file(input, false)?;
                    self.view_file(&file)?;
                }
            }
            #[cfg(feature = "debug")]
            Some(("diff", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    self.show_diff(input, sub_m.get_flag("force"))?;
                }
            }
            Some(("clear", _)) => {
                let temp_dir = &*RADO_DIR;
                // Remake a directory
                std::fs::remove_dir_all(temp_dir)?;
                std::fs::create_dir(temp_dir)?;
            }
            Some(("force", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    let path = self.update_file(input, true)?;
                    if sub_m.get_flag("read") {
                        self.view_file(&path)?;
                    }
                }
            }
            Some(("sync", sub_m)) => {
                if let Some(input) = sub_m.get_one::<&str>("INPUT") {
                    use filetime::{set_file_mtime, FileTime};
                    let temp_file = self.get_temp_path(input)?;
                    set_file_mtime(temp_file, FileTime::now())?;
                }
            }
            _ => (),
        }
        // Read file

        Ok(())
    }

    /// Create argument requirements
    fn args_builder(&self) -> clap::ArgMatches {
        use clap::{Arg, Command};
        #[allow(unused_mut)]
        let mut app = Command::new("rado")
            .version("0.1.0")
            .author("Simon creek <simoncreek@tutanota.com>")
            .about( "Rado is a high level wrapper around rad binary")
            .long_about("Rado is a high level wrapper around rad binary. You can either edit a source macro file or read final compiled file with rado wrapper. Rado only compiles when the final product has different timestamp with source file.")
            .override_usage("rado <FILE>
    rado read <FILE> -- arguments
    rado edit <FILE>
    rado force <FILE>
    rado execute <FILE>
    rado package <FILE>
    rado diff <FILE>
    rado sync <FILE>")
            .arg(Arg::new("argument")
                .last(true)
                .action(ArgAction::Append)
                .num_args(0..)
                .global(true)
                .help("Send arguments to rad binary"))
            .arg(Arg::new("out")
                .short('o')
                .long("out")
                .action(ArgAction::Set)
                .global(true)
                .help("Write to a file"))
            .subcommand(Command::new("replace")
                .about("In-replace a file's content")
                .after_help("This creates cache file in temp directory's rado_repl.txt")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("INPUT source to execute processing")
                )
            )
            .subcommand(Command::new("edit")
                .about("Edit a file as raw")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("INPUT source to execute processing")
                )
            )
            .subcommand(Command::new("clear")
                .about("Clear temp directory")
            )
            .subcommand(Command::new("env")
                .about("Print env information")
            )
            .subcommand(Command::new("package")
                .about("Package into a file")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("File name to package")
                )
            )
            .subcommand(Command::new("execute")
                .about("Execute a file")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("File name to execute")
                )
            )
            .subcommand(Command::new("read")
                .about("Read a file")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("File name to read from temp directory")
                )
            )
            .subcommand(Command::new("force")
                .about("Force update a file")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("File name to force update")
                )
                .arg(Arg::new("read")
                    .short('r')
                    .long("read")
                    .action(ArgAction::SetTrue)
                    .help("Also open a file")
                )
            )
            .subcommand(Command::new("sync")
                .about("Sync file's timestamp")
                .arg(Arg::new("INPUT")
                    .required(true)
                    .action(ArgAction::Set)
                    .help("File name to sync")
                )
            );

        #[cfg(feature = "debug")]
        {
            app = app.subcommand(
                Command::new("diff")
                    .about("Show difference between files")
                    .arg(
                        Arg::new("INPUT")
                            .required(true)
                            .action(ArgAction::Set)
                            .help("File name to read from temp directory"),
                    )
                    .arg(
                        Arg::new("force")
                            .short('f')
                            .long("force")
                            .action(ArgAction::SetTrue)
                            .help("Force update before showding diff"),
                    ),
            );
        }
        app.get_matches()
    }

    /// Execute rad processing
    fn execute(&self, file: &Path) -> RadResult<()> {
        if file.exists() {
            let file_string = file.display().to_string();
            let mut rad_args = vec!["rad", file_string.as_ref()];
            if !self.flag_arguments.is_empty() {
                rad_args.extend(self.flag_arguments.iter().map(|s| s.as_str()));
            }
            RadCli::new().parse_from(&rad_args)?;
        }
        Ok(())
    }

    /// Package a static script
    fn package_file(&self, file: &Path) -> RadResult<()> {
        if file.exists() {
            let file_string = file.display().to_string();
            let out_string = file.with_extension("r4c").display().to_string();
            let mut rad_args = vec!["rad", file_string.as_ref(), "--package", "-o", &out_string];
            if !self.flag_arguments.is_empty() {
                rad_args.extend(self.flag_arguments.iter().map(|s| s.as_str()));
            }
            RadCli::new().parse_from(&rad_args)?;
        }
        Ok(())
    }

    /// Replace file's content
    fn replace_file(&self, file: &Path) -> RadResult<()> {
        if file.exists() {
            let file_string = file.display().to_string();
            let mut rad_args = vec!["rad", file_string.as_ref()];
            if !self.flag_arguments.is_empty() {
                rad_args.extend(self.flag_arguments.iter().map(|s| s.as_str()));
            }
            let cached_path = std::env::temp_dir().join("rado_repl.txt");
            let cached_string = cached_path.display().to_string();

            rad_args.extend(["-o", &cached_string].iter());
            {
                RadCli::new().parse_from(&rad_args)?;
            }

            std::fs::copy(cached_path, file)?;
        }
        Ok(())
    }

    /// View a file
    fn view_file(&self, file: &Path) -> RadResult<()> {
        // View file
        let editor = if let Ok(editor) = std::env::var("RADO_EDITOR") {
            editor
        } else {
            RADO_EDITOR.to_string()
        };

        let proc_args = vec![editor.as_str(), file.to_str().as_ref().unwrap()];
        Utils::subprocess(&proc_args)?;
        Ok(())
    }

    #[cfg(feature = "debug")]
    /// Show difference of processing
    fn show_diff(&self, file: &str, force: bool) -> RadResult<()> {
        #[cfg(feature = "color")]
        use crate::consts::ColorDisplayFunc;
        use similar::ChangeTag;

        let temp_file = self.get_temp_path(file)?;

        // Prevent panicking
        if force | !temp_file.exists() {
            let mut out = std::io::stdout();
            writeln!(&out,"Try expanding source file without any arguments because diff target is not present.")?;
            out.flush()?;
            self.update_file(file, true)?;
        }

        let source_content = std::fs::read_to_string(file)?;
        let target_content = std::fs::read_to_string(&temp_file)?;

        let result = similar::TextDiff::from_lines(&source_content, &target_content);
        let mut log: String;
        // Color function reference
        #[cfg(feature = "color")]
        let mut colorfunc: Option<ColorDisplayFunc>;
        #[cfg(feature = "color")]
        let is_stdout: bool;
        #[cfg(feature = "color")]
        {
            colorfunc = None;
            is_stdout = atty::is(atty::Stream::Stdout);
            if is_stdout {
                colorfunc.replace(|string: &str, _| -> Box<dyn std::fmt::Display> {
                    Box::new(string.to_owned())
                });
            }
        }

        // Print changes with color support
        for change in result.iter_all_changes() {
            #[cfg(feature = "color")]
            {
                colorfunc = None;
            }
            match change.tag() {
                ChangeTag::Delete => {
                    log = format!("- {}", change);
                    #[cfg(feature = "color")]
                    {
                        if is_stdout {
                            colorfunc.replace(Utils::red);
                        }
                    }
                }
                ChangeTag::Insert => {
                    log = format!("+ {}", change);
                    #[cfg(feature = "color")]
                    {
                        if is_stdout {
                            colorfunc.replace(Utils::green);
                        }
                    }
                }
                ChangeTag::Equal => {
                    log = format!("  {}", change);
                }
            }

            #[cfg(feature = "color")]
            {
                if let Some(func) = colorfunc {
                    log = func(&log, !is_stdout).to_string(); // Commandly color
                }
            }
            write!(std::io::stdout(), "{}", log)?;
        }
        Ok(())
    }

    /// Get temporary file path
    fn get_temp_path(&self, path: &str) -> RadResult<PathBuf> {
        // Create temp directory if not present
        if !RADO_DIR.exists() {
            std::fs::create_dir(&*RADO_DIR)?;
        }

        let temp_dir = if let Ok(dir) = std::env::var("RADO_DIR") {
            PathBuf::from(dir)
        } else {
            RADO_DIR.to_path_buf()
        };
        let temp_file = temp_dir.join(path);
        Ok(temp_file)
    }

    /// Update file's content
    fn update_file(&self, path: &str, force: bool) -> RadResult<PathBuf> {
        let source_file = path;
        let mut target_file = self.get_temp_path(path)?;
        if target_file.exists() {
            // Compare file_stamp
            let source_modified = std::fs::metadata(source_file)?.modified()?;
            let temp_modified = std::fs::metadata(&target_file)?.modified()?;

            // Source is freshier or force
            if force | (source_modified > temp_modified) {
                let mut rad_args = vec!["rad", source_file];
                if !self.flag_arguments.is_empty() {
                    rad_args.extend(self.flag_arguments.iter().map(|s| s.as_str()));
                }

                // Respect user configured out option
                if let Some(path) = self.flag_out.as_ref() {
                    target_file = path.to_path_buf();
                }

                rad_args.extend(["-o", target_file.to_str().as_ref().unwrap()].iter());
                RadCli::new().parse_from(&rad_args)?;
            }
        } else {
            if !Path::new(source_file).exists() {
                return Err(RadError::InvalidCommandOption(format!(
                    "Cannot read from non-existent file : \"{}\"",
                    source_file
                )));
            }

            // File doesn't exist, run it anyway
            let mut rad_args = vec!["rad", source_file];
            if !self.flag_arguments.is_empty() {
                rad_args.extend(self.flag_arguments.iter().map(|s| s.as_str()));
            }
            rad_args.extend(["-o", target_file.to_str().as_ref().unwrap()].iter());
            RadCli::new().parse_from(&rad_args)?;
        }

        Ok(target_file)
    }
}