mortise-core 1.1.0

Reads Laravel apps that use nwidart/laravel-modules. No interface.
Documentation
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
//! How PHP runs for an app: the kind of runner, the command line, and starting it.
//!
//! A command line is a program and a list of arguments. It never goes through a shell.
//! With the `run` feature, [`test_php`] runs `artisan --version` through a runner.

#[cfg(feature = "run")]
pub mod ansi;
#[cfg(feature = "run")]
mod detect;
#[cfg(feature = "run")]
pub use detect::{Detected, detect, herd_config_dir};
#[cfg(feature = "run")]
pub mod history;
pub(crate) mod process;
#[cfg(feature = "run")]
mod read;
#[cfg(feature = "run")]
pub use process::{Run, RunEnd, RunEvent, Runs, STOP_WAIT, StartError, Stream, stop_asks_first};
#[cfg(feature = "run")]
pub use read::{Read, ReadEnd};

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

use serde::{Deserialize, Serialize};

use crate::program::split_words;

/// The program through which PHP runs artisan for an app.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum RunnerKind {
    Herd,
    Sail,
    /// `docker compose exec` in the named service.
    Docker {
        service: String,
    },
    /// The words of a line the user typed.
    Custom {
        line: String,
    },
}

impl RunnerKind {
    /// The words before the artisan arguments, for this platform.
    /// An empty Custom line gives no words.
    pub fn prefix(&self) -> Vec<String> {
        let words: &[&str] = match self {
            RunnerKind::Herd if cfg!(windows) => &["herd.bat", "php", "artisan"],
            RunnerKind::Herd => &["herd", "php", "artisan"],
            RunnerKind::Sail => &["./vendor/bin/sail", "artisan"],
            RunnerKind::Docker { service } => {
                return ["docker", "compose", "exec", "-T", service, "php", "artisan"]
                    .map(String::from)
                    .to_vec();
            }
            RunnerKind::Custom { line } => return split_words(line),
        };
        words.iter().map(|word| word.to_string()).collect()
    }

    /// The text of the command field: the prefix as words one space apart, or the
    /// Custom line as the user typed it. [`RunnerKind::from_line`] reads it back.
    pub fn line(&self) -> String {
        match self {
            RunnerKind::Custom { line } => line.clone(),
            other => other.prefix().join(" "),
        }
    }

    /// The kind a command field holds. The words of Herd, or of Sail off Windows,
    /// give that kind. `docker compose exec [-T] <service> php artisan` gives Docker
    /// with that service, unless the service is empty or starts with `-`. Any other
    /// text gives Custom with the text.
    pub fn from_line(line: &str) -> RunnerKind {
        let words = split_words(line);
        let words: Vec<&str> = words.iter().map(String::as_str).collect();
        for kind in [RunnerKind::Herd, RunnerKind::Sail] {
            if (kind != RunnerKind::Sail || !cfg!(windows)) && words == kind.prefix() {
                return kind;
            }
        }
        let service = match words.as_slice() {
            ["docker", "compose", "exec", "-T", service, "php", "artisan"]
            | ["docker", "compose", "exec", service, "php", "artisan"] => Some(*service),
            _ => None,
        };
        match service {
            Some(service) if !service.is_empty() && !service.starts_with('-') => {
                RunnerKind::Docker {
                    service: service.to_string(),
                }
            }
            _ => RunnerKind::Custom {
                line: line.to_string(),
            },
        }
    }
}

/// How PHP runs for one app.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PhpSettings {
    /// `None`: not chosen yet. Detection proposes one; nothing runs.
    pub runner: Option<RunnerKind>,
    /// `None`: the app folder. A relative path starts at the app folder.
    pub working_folder: Option<PathBuf>,
    /// Minutes before a command is stopped. 10 by default (S8.2).
    pub time_limit_minutes: u16,
}

impl Default for PhpSettings {
    fn default() -> Self {
        Self {
            runner: None,
            working_folder: None,
            time_limit_minutes: 10,
        }
    }
}

impl PhpSettings {
    /// The folder a command runs in, for the app in `root`.
    pub fn folder(&self, root: &Path) -> PathBuf {
        match &self.working_folder {
            Some(folder) => root.join(folder),
            None => root.to_owned(),
        }
    }

    /// How long a command may run. 0 minutes reads as 1 minute.
    pub fn time_limit(&self) -> Duration {
        Duration::from_secs(u64::from(self.time_limit_minutes.max(1)) * 60)
    }
}

/// A program and its arguments, started without a shell.
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandLine {
    pub program: String,
    pub args: Vec<String>,
}

#[cfg(feature = "run")]
impl CommandLine {
    /// The line that runs artisan with `arguments` through `runner`.
    /// `--no-interaction --ansi` come right after the first argument, so a `--`
    /// later in the list cannot turn them into plain words. A flag that the prefix,
    /// or the arguments before a `--`, already hold is not added again.
    /// `None` when the runner gives no program (an empty Custom line).
    pub fn artisan(runner: &RunnerKind, arguments: &[&str]) -> Option<Self> {
        let mut words = runner.prefix().into_iter();
        let program = words.next()?;
        let mut args: Vec<String> = words.collect();
        let options = arguments.iter().take_while(|word| **word != "--");
        let held: Vec<&str> = args
            .iter()
            .map(String::as_str)
            .chain(options.copied())
            .collect();
        let flags = ["--no-interaction", "--ansi"]
            .into_iter()
            .filter(|flag| !held.contains(flag))
            .map(String::from)
            .collect::<Vec<_>>();
        let (first, rest) = match arguments.split_first() {
            Some((first, rest)) => (Some(*first), rest),
            None => (None, arguments),
        };
        args.extend(first.map(String::from));
        args.extend(flags);
        args.extend(rest.iter().map(|word| word.to_string()));
        Some(Self { program, args })
    }

    /// The line to show and copy, quoted for the shell of this platform.
    /// The process always gets the list, not this text.
    pub fn display(&self) -> String {
        if cfg!(windows) {
            powershell(self)
        } else {
            posix(self)
        }
    }

    fn words(&self) -> impl Iterator<Item = &str> {
        std::iter::once(self.program.as_str()).chain(self.args.iter().map(String::as_str))
    }
}

/// How long "Test" waits for `artisan --version` before it stops it.
#[cfg(feature = "run")]
pub const TEST_LIMIT: Duration = Duration::from_secs(20);

/// What came of a test of the runner of an app.
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestOutcome {
    /// The program ended with code 0. The first line it printed, without colour codes.
    Answered(String),
    Failed(Failure),
}

/// Why a test failed. The interface writes the text; this is data only.
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Failure {
    /// The runner gives no program: an empty Custom line.
    NoCommand,
    /// The working folder is not a folder. Without this check a missing folder
    /// reads as a missing program on macOS and Linux.
    NoFolder { folder: PathBuf },
    /// The system found no program by that name.
    NotFound { program: String },
    /// The program did not start for another reason, with the message of the system.
    NotStarted { program: String, message: String },
    /// The program ended with another code, or by a signal (`None`). The first line
    /// of stderr that is not empty, else of stdout, else empty; without colour codes.
    Exited { code: Option<i32>, line: String },
    /// The program ran longer than the limit, in whole seconds, and was stopped.
    TimedOut { seconds: u64 },
}

/// Runs `artisan --version` through `runner` in `folder`, for at most `limit`.
/// It blocks, so an interface calls it off its drawing thread.
#[cfg(feature = "run")]
pub fn test_php(runner: &RunnerKind, folder: &Path, limit: Duration) -> TestOutcome {
    let Some(line) = CommandLine::artisan(runner, &["--version"]) else {
        return TestOutcome::Failed(Failure::NoCommand);
    };
    if !folder.is_dir() {
        return TestOutcome::Failed(Failure::NoFolder {
            folder: folder.to_owned(),
        });
    }
    let captured = match process::capture(&line, folder, limit) {
        Ok(captured) => captured,
        Err(error) => return TestOutcome::Failed(process::not_started(error, &line.program)),
    };
    let first = |lines: &[String]| {
        lines
            .iter()
            .map(|text| ansi::plain(text))
            .find(|text| !text.trim().is_empty())
    };
    if captured.timed_out {
        TestOutcome::Failed(Failure::TimedOut {
            seconds: limit.as_secs(),
        })
    } else if captured.code == Some(0) {
        TestOutcome::Answered(first(&captured.stdout).unwrap_or_default())
    } else {
        let line = first(&captured.stderr)
            .or_else(|| first(&captured.stdout))
            .unwrap_or_default();
        TestOutcome::Failed(Failure::Exited {
            code: captured.code,
            line,
        })
    }
}

/// Whether a word can stand without quotes: not empty, and only ASCII letters,
/// digits, `_ - . / : = +` and the letters in `more`.
#[cfg(feature = "run")]
fn bare(word: &str, more: &[char]) -> bool {
    !word.is_empty()
        && word
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || "_-./:=+".contains(c) || more.contains(&c))
}

/// The line for POSIX `sh`. A word that is not bare goes in single quotes, and a
/// `'` inside becomes `'\''`. A program with `=` is quoted too: `sh` reads
/// `a=b cmd` as an assignment.
#[cfg(feature = "run")]
fn posix(line: &CommandLine) -> String {
    line.words()
        .enumerate()
        .map(|(at, word)| {
            if bare(word, &[]) && (at > 0 || !word.contains('=')) {
                word.to_string()
            } else {
                format!("'{}'", word.replace('\'', r"'\''"))
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// The line for PowerShell. A word that is not bare goes in single quotes, which
/// expand nothing. A quote inside is doubled; PowerShell also reads the curly
/// quotes as single quotes. A quoted program needs `& ` before it.
///
/// Known limit: Windows PowerShell 5.1 drops an empty argument and mangles a `"`
/// inside an argument to a program. PowerShell 7.3 and later pass both, except to a
/// `.bat` or `.cmd`: those still get their arguments the legacy way. So an empty
/// argument can vanish when the copied Herd line (`herd.bat`) is pasted on Windows.
#[cfg(feature = "run")]
fn powershell(line: &CommandLine) -> String {
    const QUOTES: [char; 5] = ['\'', '\u{2018}', '\u{2019}', '\u{201A}', '\u{201B}'];
    let text = line
        .words()
        .map(|word| {
            if bare(word, &['\\']) {
                return word.to_string();
            }
            let mut quoted = String::from("'");
            for c in word.chars() {
                if QUOTES.contains(&c) {
                    quoted.push(c);
                }
                quoted.push(c);
            }
            quoted.push('\'');
            quoted
        })
        .collect::<Vec<_>>()
        .join(" ");
    if bare(&line.program, &['\\']) {
        text
    } else {
        format!("& {text}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_line_reads_each_kind() {
        let docker = RunnerKind::Docker {
            service: "web".into(),
        };
        assert_eq!(
            RunnerKind::from_line(&RunnerKind::Herd.line()),
            RunnerKind::Herd
        );
        assert_eq!(RunnerKind::from_line(&docker.line()), docker);
        assert_eq!(
            RunnerKind::from_line("docker  compose exec web php artisan"),
            docker
        );
        let sail = RunnerKind::from_line("./vendor/bin/sail artisan");
        let custom = |line: &str| RunnerKind::Custom { line: line.into() };
        if cfg!(windows) {
            assert_eq!(sail, custom("./vendor/bin/sail artisan"));
        } else {
            assert_eq!(sail, RunnerKind::Sail);
        }
        for line in [
            "\"/opt/my php/bin/php\" artisan",
            "docker compose exec -T php artisan",
            "docker compose exec -T \"\" php artisan",
            "herd php artisan tinker",
            "",
        ] {
            assert_eq!(RunnerKind::from_line(line), custom(line), "{line}");
        }
    }

    #[test]
    fn php_settings_give_the_folder_and_the_time_limit() {
        let root = Path::new("/code/shop-api");
        let mut php = PhpSettings {
            time_limit_minutes: 0,
            ..PhpSettings::default()
        };
        assert_eq!(php.folder(root), root);
        assert_eq!(php.time_limit(), Duration::from_secs(60));
        php.working_folder = Some("api".into());
        assert_eq!(php.folder(root), root.join("api"));
    }

    #[test]
    fn runner_kinds_keep_their_json_form() {
        let json = r#"[{"kind":"herd"},{"kind":"sail"},{"kind":"docker","service":"app"},{"kind":"custom","line":"php artisan"}]"#;
        let kinds: Vec<RunnerKind> = serde_json::from_str(json).unwrap();
        assert_eq!(serde_json::to_string(&kinds).unwrap(), json);
    }

    #[cfg(feature = "run")]
    fn line(words: &[&str]) -> CommandLine {
        CommandLine {
            program: words[0].into(),
            args: words[1..].iter().map(|word| word.to_string()).collect(),
        }
    }

    #[cfg(feature = "run")]
    #[test]
    fn artisan_puts_the_flags_after_the_first_argument() {
        let custom = |line: &str| RunnerKind::Custom { line: line.into() };
        let php = custom("php artisan");
        assert_eq!(
            CommandLine::artisan(&php, &["module:make", "Blog", "--", "--ansi"]),
            Some(line(&[
                "php",
                "artisan",
                "module:make",
                "--no-interaction",
                "--ansi",
                "Blog",
                "--",
                "--ansi"
            ]))
        );
        assert_eq!(
            CommandLine::artisan(&php, &[]),
            Some(line(&["php", "artisan", "--no-interaction", "--ansi"]))
        );
        let docker = RunnerKind::Docker {
            service: "app".into(),
        };
        let version = CommandLine::artisan(&docker, &["--version"]).unwrap();
        assert_eq!(version.program, "docker");
        assert_eq!(
            version.args.join(" "),
            "compose exec -T app php artisan --version --no-interaction --ansi"
        );
        assert_eq!(
            CommandLine::artisan(&custom("php artisan --ansi"), &["list", "--no-interaction"]),
            Some(line(&[
                "php",
                "artisan",
                "--ansi",
                "list",
                "--no-interaction"
            ]))
        );
        assert_eq!(CommandLine::artisan(&custom(" "), &["--version"]), None);
        // The line that Test runs for Herd, as the shell of the platform reads it (S35).
        let herd = CommandLine::artisan(&RunnerKind::Herd, &["--version"]).unwrap();
        let program = if cfg!(windows) { "herd.bat" } else { "herd" };
        assert_eq!(
            herd.display(),
            format!("{program} php artisan --version --no-interaction --ansi")
        );
    }

    #[cfg(feature = "run")]
    #[test]
    fn posix_quotes_what_sh_would_read() {
        for (words, shown) in [
            (
                &["php", "module:make", "--name=Blog"][..],
                "php module:make --name=Blog",
            ),
            (&["php", "My Blog"], "php 'My Blog'"),
            (&["php", "it's"], r"php 'it'\''s'"),
            (&["php", r#"say "hi""#], r#"php 'say "hi"'"#),
            (&["php", ""], "php ''"),
            (&["php", "$HOME"], "php '$HOME'"),
            (&["php", r"a\b"], r"php 'a\b'"),
            (
                &[r"C:\Program Files\php\php.exe"],
                r"'C:\Program Files\php\php.exe'",
            ),
            (&["php", "Füße"], "php 'Füße'"),
            (
                &["php", "a,b", "@x", "%PATH%", "a;b", "a*"],
                "php 'a,b' '@x' '%PATH%' 'a;b' 'a*'",
            ),
            (&["bin/a=b", "x=y"], "'bin/a=b' x=y"),
        ] {
            assert_eq!(posix(&line(words)), shown);
        }
    }

    #[cfg(feature = "run")]
    #[test]
    fn powershell_quotes_what_powershell_would_read() {
        for (words, shown) in [
            (
                &["php", "module:make", "--name=Blog"][..],
                "php module:make --name=Blog",
            ),
            (&["php", "My Blog"], "php 'My Blog'"),
            (
                &["php", "it's", "it\u{2019}s"],
                "php 'it''s' 'it\u{2019}\u{2019}s'",
            ),
            (&["php", r#"say "hi""#], r#"php 'say "hi"'"#),
            (&["php", ""], "php ''"),
            (&["php", "$HOME"], "php '$HOME'"),
            (&["php", r"a\b"], r"php a\b"),
            (
                &[r"C:\Program Files\php\php.exe"],
                r"& 'C:\Program Files\php\php.exe'",
            ),
            (&["php", "Füße"], "php 'Füße'"),
            (
                &["php", "a,b", "@x", "%PATH%", "a;b", "a*"],
                "php 'a,b' '@x' '%PATH%' 'a;b' 'a*'",
            ),
        ] {
            assert_eq!(powershell(&line(words)), shown);
        }
    }
}