#[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;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum RunnerKind {
Herd,
Sail,
Docker {
service: String,
},
Custom {
line: String,
},
}
impl RunnerKind {
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()
}
pub fn line(&self) -> String {
match self {
RunnerKind::Custom { line } => line.clone(),
other => other.prefix().join(" "),
}
}
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(),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PhpSettings {
pub runner: Option<RunnerKind>,
pub working_folder: Option<PathBuf>,
pub time_limit_minutes: u16,
}
impl Default for PhpSettings {
fn default() -> Self {
Self {
runner: None,
working_folder: None,
time_limit_minutes: 10,
}
}
}
impl PhpSettings {
pub fn folder(&self, root: &Path) -> PathBuf {
match &self.working_folder {
Some(folder) => root.join(folder),
None => root.to_owned(),
}
}
pub fn time_limit(&self) -> Duration {
Duration::from_secs(u64::from(self.time_limit_minutes.max(1)) * 60)
}
}
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandLine {
pub program: String,
pub args: Vec<String>,
}
#[cfg(feature = "run")]
impl CommandLine {
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 })
}
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))
}
}
#[cfg(feature = "run")]
pub const TEST_LIMIT: Duration = Duration::from_secs(20);
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestOutcome {
Answered(String),
Failed(Failure),
}
#[cfg(feature = "run")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Failure {
NoCommand,
NoFolder { folder: PathBuf },
NotFound { program: String },
NotStarted { program: String, message: String },
Exited { code: Option<i32>, line: String },
TimedOut { seconds: u64 },
}
#[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,
})
}
}
#[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))
}
#[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(" ")
}
#[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);
}
#[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);
}
}
}