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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Macros for creating
//! [`std::process::Command`](https://static.rust-lang.org/doc/master/std/process/struct.Command.html)
//! with shell-like syntax.
//!
//! The `command!` macro is a syntax extension and requires nightly,
//! the `cmd!` is simpler version built using `macro_rules!`.
//!
//! This page describes syntax used by both `command!` and `cmd!` macros.
//! See the [github page](https://github.com/krdln/command-macros) for more general introduction.
//!
//! Features marked with \* are unavailable for `cmd!`.
//!
//! ## Naked idents
//!
//! First ident is treated as command name,
//! the rest are parsed as arugments. This invocation:
//!
//! ```
//! # #[macro_use] extern crate command_macros;
//! # fn main() {
//! cmd!(echo foo bar).status().unwrap();
//! # }
//! ```
//!
//! expands to
//!
//! ```ignore
//! {
//!     let cmd = ::std::process::Command::new("echo");
//!     cmd.arg("foo");
//!     cmd.arg("bar");
//!     cmd
//! }.status().unwrap()
//! ```
//!
//! ## `(expression)` (OsStr expression)
//!
//! Interior of `( )` is parsed as Rust expression
//! which should evaluate to `T: AsRef<OsStr>`.
//! This will be put in `cmd.arg(& $expr)`.
//! The `&` is added automatically, like in `println!`,
//! to prevent accidentally moving arguments.
//!
//! ```no_run
//! # #[macro_use] extern crate command_macros;
//! # fn main() {
//! let filename = String::new("foo bar");
//! let get_command = || "touch";
//! cmd!( (get_command()) (filename) ).status().unwrap();
//! # }
//! ```
//!
//! ## `((expression))` (ToString expression)
//!
//! Interior of `(( ))` is parsed as Rust expression
//! which should evaluate to `T: ToString`.
//! Similar rules as with `( )` apply.
//!
//! The following should echo `4`
//!
//! ```
//! # #[macro_use] extern crate command_macros;
//! # fn main() {
//! cmd!( echo ((2+2)) ).status().unwrap();
//! # }
//! ```
//!
//! ## `[expression]` (args expression)
//!
//! Interior of `[ ]` is parsed as Rust expression
//! which should evaluate to `[T: AsRef<OsStr>]`
//! (modulo `Deref`).
//! This expression will be put in `cmd.args(& $expr)`
//!
//! ```no_run
//! # #[macro_use] extern crate command_macros;
//! # fn main() {
//! let args: Vec<_> = std::env::args_os().collect();
//! cmd!( (args[1]) [args[2..]] ).status().unwrap();
//! # }
//! ```
//!
//! ## `{expression}` (Command expression)
//!
//! Interior of `{ }` is parsed as Rust expression
//! which should evaluate to `Command` or `&mut Command`
//! (or anything that has `arg` and `args` methods).
//! It is allowed only at the beginning of macro.
//! It is helpful when you want to append arguments to
//! existing command:
//!
//! ```no_run
//! # #[macro_use] extern crate command_macros;
//! # fn main() {
//! let cmd = ::std::process::Command::new("echo");
//! cmd!( {&mut cmd} bar baz )
//! # }
//! ```
//!
//! ## Strings\*
//!
//! String literals work like in shell – they expand to single argument or part of it.
//! Character literals and raw string literals are also supported.
//! Note that shell-style `"$variables"` won't work here.
//!
//! ```ignore
//! command!("echo" "single argument" "***")
//! ```
//!
//! `cmd!` workaroud:
//!
//! ```ignore
//! cmd!(echo ("single argument") ("***"))
//! ```
//!
//! ## Arbitrary tokens\*
//!
//! Everything that is not [block], {block}, (block) or string literal,
//! will be stringified. This is mostly helpful for unix-like flags.
//! Everything within a single whitespace-separated chunk will be treated
//! as a single argument. In the following example we are passing
//! three arguments to a `foo.2.5` command.
//!
//! ```ignore
//! command!(foo.2.5 --flag   -v:c -=|.|=-).status().unwrap();
//! ```
//!
//! `cmd!` workaround: `("--flag")`.
//!
//! ## `(-flags)`
//!
//! When your flag contains only `-+=,.;:`
//! and idents, you can omit the quotes. The flag has to start
//! with `-` or `+`.
//!
//! This is only necessary for `cmd!`,
//! when using `command!`, it will generate warning,
//! as you can just remove the surrounding parens.
//!
//! So instead
//!
//! ```ignore
//! cmd!(foo ("--bar=baz"))
//! ```
//!
//! you can write
//!
//! ```ignore
//! cmd!(foo (--bar=baz)
//! ```
//!
//! Please note that all whitespace will be ignored.
//! 
//! ## Multi-part arguments\*
//!
//! You can mix `((e))`, `(e)`, tokens and strings within a single
//! arguments as long as they are not separated by whitespace.
//! The following will touch the `foo.v5.special edition` file.
//!
//! ```ignore
//! let p = Path::new("foo");
//! let version = 5;
//! command!(touch (p).v((version))".special edition")
//! ```
//!
//! This is roughly equivalent to `(format!(...))`, but the macro version
//! can handle `OsStr`s (such as `Path`).
//!
//! Please note that this is **not** supported by `cmd!`, which would evaluate
//! every part as *separate* argument.
//!
//! ## `{}`\*
//!
//! Empty `{}` is treated as `"{}"`. This is handy when using commands like `find`.
//! There has to be no space between braces.
//!
//!
//! ## If
//!
//! The `if` token should be surrounded by whitespace.
//! The expression (and pattern in if-let) is parsed as Rust,
//! the inside of {block} is parsed using regular `commmand!` syntax
//! and can evaluate to multiple arguments (or 0).
//! The following should pass `--number` `5` to command `foo`.
//!
//! ```ignore
//! let bar = 5;
//! let option = Some(5);
//! command!(foo
//!     if bar > 10 { zzz }
//!     else if let Some(a) = option { --number ((a)) }
//! ).status().unwrap();
//! ```
//!
//! `cmd!` limitations: `else if` is not supported, expression has to be in parens.
//!
//! ```ignore
//! cmd!(foo
//!     if (bar > 10) { zzz }
//!     else { if let Some(a) = (option) { ("--number") ((a)) } }
//! ).status().unwrap();
//! ```
//!
//! ## Match
//!
//! The `match` token should be surrounded by whitespace.
//! The expression and patterns are parsed as Rust.
//! On the right side of `=>` there should be a {block},
//! which will be parsed (similarly to if blocks)
//! using regular `command!` syntax.
//!
//! This example will pass a single argument `yes` to `foo` command.
//!
//! ```ignore
//! let option = Some(5);
//! command!(foo
//!     match option {
//!         Some(x) if x > 10 => {}
//!         _ => { yes }
//!     }
//! ).status().unwrap()
//! ```
//!
//! `cmd!` limitation: expression after `match` has to be in parens.
//!
//! ## For
//!
//! The `for` token should be surrounded by whitespace.
//! The expression and patterns are parsed as Rust.
//! The interior of block is parsed using `command!` syntax,
//! and will be evaluated in every iteration.
//!
//! This example will pass three arguments `1` `2` `3`.
//!
//! ```ignore
//! command!(echo
//!     for x in 1..4 {
//!         ((x))
//!     }
//! ).status().unwrap()
//! ```

#![cfg_attr(
    feature = "nightly",
    feature(
        plugin_registrar,
        rustc_private,
        question_mark,
        quote,
    ),
)]
#![cfg_attr(feature = "nightly", crate_type = "dylib")]

#[cfg(feature = "nightly")] mod plugin;

#[cfg(feature = "nightly")] extern crate syntax;
#[cfg(feature = "nightly")] extern crate rustc;
#[cfg(feature = "nightly")] extern crate rustc_plugin;

#[cfg(feature = "nightly")]
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut rustc_plugin::Registry) {
    reg.register_macro("command", plugin::expand_command);
}

/// Simple macro for creating `Command`.
///
/// Please read the syntax description in the crate's [documentation](index.html).
///
/// Please note that this macro is **not** whitespace-sensitive and the following
/// will evaluate to four separate arguments (as opposed to one in `command!`):
///
/// ```ignore
/// cmd!(echo (foo)(bar)baz(qux)) // don't do it!
/// ```
///
/// # Examples
///
/// ```
/// #[macro_use] extern crate command_macros;
///
/// fn main() {
///     cmd!( echo ((2+2)) ).status.unwrap();
/// }
/// ```
#[macro_export]
macro_rules! cmd {
    ({$e:expr}) => ($e);

    // arg ToString splice
    ({$e:expr} (($a:expr)) $($tail:tt)*) =>
    {
        {
            let mut cmd = $e;
            cmd.arg((&$a).to_string());
            cmd!( {cmd} $($tail)* )
        }
    };

    // (-flag)
    (@stringify {}) => { "" };
    (@stringify {- $($rest:tt)*}) => { concat!(stringify!(-), cmd!(@stringify {$($rest)*})) };
    (@stringify {+ $($rest:tt)*}) => { concat!("+", cmd!(@stringify {$($rest)*})) };
    (@stringify {= $($rest:tt)*}) => { concat!("=", cmd!(@stringify {$($rest)*})) };
    (@stringify {, $($rest:tt)*}) => { concat!(",", cmd!(@stringify {$($rest)*})) };
    (@stringify {. $($rest:tt)*}) => { concat!(".", cmd!(@stringify {$($rest)*})) };
    (@stringify {; $($rest:tt)*}) => { concat!(";", cmd!(@stringify {$($rest)*})) };
    (@stringify {: $($rest:tt)*}) => { concat!(":", cmd!(@stringify {$($rest)*})) };
    (@stringify {$i:ident $($rest:tt)*}) => {
        // "soon'ah"
        // stringify!($i)
        concat!(stringify!($i), cmd!(@stringify {$($rest)*}))
    };
    ({$e:expr} (-$($tts:tt)*) $($tail:tt)*) => {
        cmd!({$e} (cmd!(@stringify {-$($tts)*})) $($tail)*)
    };
    ({$e:expr} (+$($tts:tt)*) $($tail:tt)*) => {
        cmd!({$e} (cmd!(@stringify {+$($tts)*})) $($tail)*)
    };

    // arg splice
    ({$e:expr} ($a:expr) $($tail:tt)*) => 
    {
        {
            let mut cmd = $e;
            cmd.arg(&$a);
            cmd!( {cmd} $($tail)* )
        }
    };

    // args splice
    ({$e:expr} [$aa:expr] $($tail:tt)*) => {
        {
            let mut cmd = $e;
            cmd.args(&$aa);
            cmd!( {cmd} $($tail)* )
        }
    };

    // match
    ({$e:expr} match ($m:expr) { $($($p:pat)|+ $(if $g:expr)* => {$($rr:tt)*} ),* } $($tail:tt)*) => {
        cmd!({$e} match ($m) { $($($p)|+ $(if $g)* => {$($rr)*})* } $($tail)*)
    };
    ({$e:expr} match ($m:expr) { $($($p:pat)|+ $(if $g:expr)* => {$($rr:tt)*},)* } $($tail:tt)*) => {
        cmd!({$e} match ($m) { $($($p)|+ $(if $g)* => {$($rr)*})* } $($tail)*)
    };
    ({$e:expr} match ($m:expr) { $($($p:pat)|+ $(if $g:expr)* => {$($rr:tt)*} )* } $($tail:tt)*) => {
        {
            let cmd = $e;
            cmd!( {match $m { $($($p)|+ $(if $g)* => cmd!({cmd} $($rr)*)),* }} $($tail)* )
        }
    };

    // if let
    ({$e:expr} if let $p:pat = ($m:expr) { $($then:tt)* } else { $($els:tt)* } $($tail:tt)*) => {
        {
            let cmd = $e;
            cmd!( {
                    if let $p = $m { cmd!({cmd} $($then)*) } else { cmd!({cmd} $($els)*) }
                  } $($tail)*)
        } 
    };
    ({$e:expr} if let $p:pat = ($m:expr) { $($then:tt)* } $($tail:tt)*) => {
        cmd!( {$e}if let $p = ($m) { $($then)* } else {} $($tail)* )
    };

    // if else
    ({$e:expr} if ($b:expr) { $($then:tt)* } else { $($els:tt)* } $($tail:tt)*) => {
        {
            let cmd = $e;
            cmd!( {
                    if $b { cmd!({cmd} $($then)*) } else { cmd!({cmd} $($els)*) }
                  } $($tail)*)
        } 
    };
    ({$e:expr} if ($b:expr) { $($then:tt)* } $($tail:tt)*) => {
        cmd!( {$e}if ($b) { $($then)* } else {} $($tail)* )
    };

    // for
    ({$e:expr} for $p:pat in ($i:expr) { $($body:tt)* } $($tail:tt)*) => {
        {
            let mut cmd = $e;
            for $p in $i { cmd = cmd!( {cmd} $($body)* ); }
            cmd
        }
    };

    // naked ident
    ({$e:expr} $a:ident $($tail:tt)*) => (cmd!( {$e} (stringify!($a)) $($tail)* ));

    // Main entry points (command name)
    (($c:expr) $($tail:tt)*) => {
        cmd!( {::std::process::Command::new(&$c)} $($tail)* )
    };
    ($c:ident $($tail:tt)*) => (cmd!( (stringify!($c)) $($tail)* ));
}

#[cfg(test)]
use ::std::process::Command;

#[test]
fn expr() {
    let mut base: Command = cmd!(echo a);
    base.env("FOO", "bar");
    quicktest(cmd!({base} b), "a b");
}

#[cfg(test)]
fn quicktest(mut echocmd: Command, target: &str) {
    let out = echocmd.output().expect("quicktest: can't echo").stdout;
    assert_eq!(String::from_utf8_lossy(&out).trim(), target);
}

#[test]
fn simple() {
    let output = cmd!(echo raz dwa trzy).output().expect("can't echo");
    assert_eq!(output.stdout, &b"raz dwa trzy\n"[..]);
}

#[test]
fn ffmpeg() {
    let moreargs = ["-pix_fmt", "yuv420p"];
    let file = "file.mp4".to_string();
    let preset = "slow";
    let tmpname = "tmp.mkv";
    let output = cmd!(echo
            (-i) (file)
            (-c:v) libx264 (-preset) (preset) [moreargs]
            (-c:a) copy
            (tmpname))
        .output()
        .expect("can't echo");
    assert_eq!(
        String::from_utf8_lossy(&output.stdout),
        "-i file.mp4 -c:v libx264 -preset slow -pix_fmt yuv420p -c:a copy tmp.mkv\n"
    );
}

#[test]
fn match_test() {
    let option = Some(5);

    quicktest(
        cmd!(echo
            match (option) {
                Some(x) => {("--number") (x.to_string())}
                None => {}
            }
            tail),
        "--number 5 tail"
    );

    for &(x, target) in &[
        (Ok(1), ". 0101 ."),
        (Ok(5), ". small 5 ."),
        (Ok(10), ". 10 ."),
        (Err("bu"), ". err bu ."),
    ] {
        quicktest(cmd!(
                echo (".") match (x) {
                    Ok(0) | Ok(1) => { ("0101") },
                    Ok(x) if x < 7 => { small (x.to_string()) },
                    Ok(x) => { (x.to_string()) },
                    Err(x) => { err (x) }
                } (".")
            ),
            target
        );
    }
}

#[test]
fn iflet() {
    let option = Some(5);
    quicktest(
        cmd!(echo
            if let Some(x) = (option) { ("--number") ((x)) }
            tail),
        "--number 5 tail"
    );

    let option: Option<()> = None;
    quicktest(
        cmd!(echo
            if let Some(_) = (option) {} else { ok }
            tail),
        "ok tail"
    );
}


#[test]
fn ifelse() {
    quicktest(
        cmd!(echo
            if (true) { abc (1.to_string()) }
            tail),
        "abc 1 tail"
    );

    let counter = ::std::cell::Cell::new(0);
    quicktest(
        cmd!(echo
            ({counter.set(counter.get() + 1); "blah"})
            if (true) { a } else { b }
            if (false) { c } else { d }
            tail),
        "blah a d tail"
    );
    assert_eq!(counter.get(), 1);
}

#[test]
fn test_mutref() {
    let cmd = &mut Command::new("echo");
    let cmd: &mut Command = cmd!({cmd} foo);
    assert_eq!(cmd.output().unwrap().stdout, &b"foo\n"[..]);
}

#[test]
fn test_parenparen() {
    quicktest(cmd!( echo ((2+2)) ), "4");
    let foo = || "a";
    quicktest(cmd!( echo ((foo)()) ), "a");
}

#[test]
fn for_loop() {
    quicktest(cmd!(
            echo
            for x in (&["a", "b"]) {
                foo (x)
            }
            end
        ),
        "foo a foo b"
    );
}

#[test]
fn not_moving() {
    let s = String::new();
    cmd!((s));
    cmd!(((s)));
    cmd!((s));
}

#[test]
fn flags() {
    quicktest(
        cmd!(echo (".") (-e) (-c:a=aac) (+a:b;c,d.txt)),
        ". -e -c:a=aac +a:b;c,d.txt",
    );
}