2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2022-01-18 11:02:15 -08:00
|
|
|
pub(crate) const DEFAULT_SHELL: &str = "sh";
|
|
|
|
pub(crate) const DEFAULT_SHELL_ARGS: &[&str] = &["-cu"];
|
|
|
|
pub(crate) const WINDOWS_POWERSHELL_SHELL: &str = "powershell.exe";
|
|
|
|
pub(crate) const WINDOWS_POWERSHELL_ARGS: &[&str] = &["-NoLogo", "-Command"];
|
|
|
|
|
2022-11-25 13:02:01 -08:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Default)]
|
2019-11-10 23:17:47 -08:00
|
|
|
pub(crate) struct Settings<'src> {
|
2022-02-14 18:37:06 -08:00
|
|
|
pub(crate) allow_duplicate_recipes: bool,
|
2024-05-14 18:39:42 -07:00
|
|
|
pub(crate) allow_duplicate_variables: bool,
|
2023-10-11 22:04:46 -07:00
|
|
|
pub(crate) dotenv_filename: Option<String>,
|
2024-05-30 16:12:07 -07:00
|
|
|
pub(crate) dotenv_load: bool,
|
2023-10-11 22:04:46 -07:00
|
|
|
pub(crate) dotenv_path: Option<PathBuf>,
|
2024-05-30 16:12:07 -07:00
|
|
|
pub(crate) dotenv_required: bool,
|
2021-09-16 06:44:40 -07:00
|
|
|
pub(crate) export: bool,
|
2022-10-19 19:00:09 -07:00
|
|
|
pub(crate) fallback: bool,
|
2022-10-04 17:32:30 -07:00
|
|
|
pub(crate) ignore_comments: bool,
|
2021-04-24 18:29:58 -07:00
|
|
|
pub(crate) positional_arguments: bool,
|
2024-01-12 12:38:23 -08:00
|
|
|
pub(crate) quiet: bool,
|
2022-05-31 13:01:59 -07:00
|
|
|
pub(crate) shell: Option<Shell<'src>>,
|
2022-10-25 16:57:20 -07:00
|
|
|
pub(crate) tempdir: Option<String>,
|
2024-07-07 20:45:03 -07:00
|
|
|
pub(crate) unstable: bool,
|
2022-01-18 11:02:15 -08:00
|
|
|
pub(crate) windows_powershell: bool,
|
2022-05-31 13:01:59 -07:00
|
|
|
pub(crate) windows_shell: Option<Shell<'src>>,
|
2019-11-10 23:17:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Settings<'src> {
|
2023-01-26 19:03:04 -08:00
|
|
|
pub(crate) fn from_setting_iter(iter: impl Iterator<Item = Setting<'src>>) -> Self {
|
|
|
|
let mut settings = Self::default();
|
|
|
|
|
|
|
|
for set in iter {
|
|
|
|
match set {
|
|
|
|
Setting::AllowDuplicateRecipes(allow_duplicate_recipes) => {
|
|
|
|
settings.allow_duplicate_recipes = allow_duplicate_recipes;
|
|
|
|
}
|
2024-05-14 18:39:42 -07:00
|
|
|
Setting::AllowDuplicateVariables(allow_duplicate_variables) => {
|
|
|
|
settings.allow_duplicate_variables = allow_duplicate_variables;
|
|
|
|
}
|
2023-10-11 22:04:46 -07:00
|
|
|
Setting::DotenvFilename(filename) => {
|
|
|
|
settings.dotenv_filename = Some(filename);
|
|
|
|
}
|
2023-01-26 19:03:04 -08:00
|
|
|
Setting::DotenvLoad(dotenv_load) => {
|
2024-05-30 16:12:07 -07:00
|
|
|
settings.dotenv_load = dotenv_load;
|
2023-01-26 19:03:04 -08:00
|
|
|
}
|
2023-10-11 22:04:46 -07:00
|
|
|
Setting::DotenvPath(path) => {
|
|
|
|
settings.dotenv_path = Some(PathBuf::from(path));
|
|
|
|
}
|
2024-05-30 16:12:07 -07:00
|
|
|
Setting::DotenvRequired(dotenv_required) => {
|
|
|
|
settings.dotenv_required = dotenv_required;
|
|
|
|
}
|
2023-01-26 19:03:04 -08:00
|
|
|
Setting::Export(export) => {
|
|
|
|
settings.export = export;
|
|
|
|
}
|
|
|
|
Setting::Fallback(fallback) => {
|
|
|
|
settings.fallback = fallback;
|
|
|
|
}
|
|
|
|
Setting::IgnoreComments(ignore_comments) => {
|
|
|
|
settings.ignore_comments = ignore_comments;
|
|
|
|
}
|
|
|
|
Setting::PositionalArguments(positional_arguments) => {
|
|
|
|
settings.positional_arguments = positional_arguments;
|
|
|
|
}
|
2024-01-12 12:38:23 -08:00
|
|
|
Setting::Quiet(quiet) => {
|
|
|
|
settings.quiet = quiet;
|
|
|
|
}
|
2023-01-26 19:03:04 -08:00
|
|
|
Setting::Shell(shell) => {
|
|
|
|
settings.shell = Some(shell);
|
|
|
|
}
|
2024-07-07 20:45:03 -07:00
|
|
|
Setting::Unstable(unstable) => {
|
|
|
|
settings.unstable = unstable;
|
|
|
|
}
|
2023-01-26 19:03:04 -08:00
|
|
|
Setting::WindowsPowerShell(windows_powershell) => {
|
|
|
|
settings.windows_powershell = windows_powershell;
|
|
|
|
}
|
|
|
|
Setting::WindowsShell(windows_shell) => {
|
|
|
|
settings.windows_shell = Some(windows_shell);
|
|
|
|
}
|
|
|
|
Setting::Tempdir(tempdir) => {
|
|
|
|
settings.tempdir = Some(tempdir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:33:56 -08:00
|
|
|
pub(crate) fn shell_command(&self, config: &Config) -> Command {
|
2022-08-08 19:50:31 -07:00
|
|
|
let (command, args) = self.shell(config);
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
let mut cmd = Command::new(command);
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
cmd.args(args);
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
cmd
|
2021-03-25 17:44:18 -07:00
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) {
|
|
|
|
match (&config.shell, &config.shell_args) {
|
|
|
|
(Some(shell), Some(shell_args)) => (shell, shell_args.iter().map(String::as_ref).collect()),
|
|
|
|
(Some(shell), None) => (shell, DEFAULT_SHELL_ARGS.to_vec()),
|
|
|
|
(None, Some(shell_args)) => (
|
|
|
|
DEFAULT_SHELL,
|
|
|
|
shell_args.iter().map(String::as_ref).collect(),
|
|
|
|
),
|
|
|
|
(None, None) => {
|
|
|
|
if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) {
|
|
|
|
(
|
|
|
|
shell.command.cooked.as_ref(),
|
|
|
|
shell
|
|
|
|
.arguments
|
|
|
|
.iter()
|
|
|
|
.map(|argument| argument.cooked.as_ref())
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
} else if cfg!(windows) && self.windows_powershell {
|
|
|
|
(WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec())
|
|
|
|
} else if let Some(shell) = &self.shell {
|
|
|
|
(
|
|
|
|
shell.command.cooked.as_ref(),
|
|
|
|
shell
|
|
|
|
.arguments
|
|
|
|
.iter()
|
|
|
|
.map(|argument| argument.cooked.as_ref())
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(DEFAULT_SHELL, DEFAULT_SHELL_ARGS.to_vec())
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_shell() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings::default();
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: false,
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("sh", vec!["-cu"]));
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_shell_powershell() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings {
|
|
|
|
windows_powershell: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: false,
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(
|
2022-08-08 19:50:31 -07:00
|
|
|
settings.shell(&config),
|
|
|
|
("powershell.exe", vec!["-NoLogo", "-Command"])
|
2022-01-18 11:02:15 -08:00
|
|
|
);
|
|
|
|
} else {
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("sh", vec!["-cu"]));
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overwrite_shell() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings::default();
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: true,
|
|
|
|
shell: Some("lol".to_string()),
|
|
|
|
shell_args: Some(vec!["-nice".to_string()]),
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("lol", vec!["-nice"]));
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overwrite_shell_powershell() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings {
|
|
|
|
windows_powershell: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: true,
|
|
|
|
shell: Some("lol".to_string()),
|
|
|
|
shell_args: Some(vec!["-nice".to_string()]),
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("lol", vec!["-nice"]));
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shell_cooked() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings {
|
|
|
|
shell: Some(Shell {
|
|
|
|
command: StringLiteral {
|
|
|
|
kind: StringKind::from_token_start("\"").unwrap(),
|
|
|
|
raw: "asdf.exe",
|
|
|
|
cooked: "asdf.exe".to_string(),
|
2024-05-18 22:41:38 -07:00
|
|
|
expand: false,
|
2022-10-04 17:32:30 -07:00
|
|
|
},
|
|
|
|
arguments: vec![StringLiteral {
|
|
|
|
kind: StringKind::from_token_start("\"").unwrap(),
|
|
|
|
raw: "-nope",
|
|
|
|
cooked: "-nope".to_string(),
|
2024-05-18 22:41:38 -07:00
|
|
|
expand: false,
|
2022-10-04 17:32:30 -07:00
|
|
|
}],
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: false,
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("asdf.exe", vec!["-nope"]));
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shell_present_but_not_shell_args() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings {
|
|
|
|
windows_powershell: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell: Some("lol".to_string()),
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config).0, "lol");
|
2022-01-18 11:02:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shell_args_present_but_not_shell() {
|
2022-10-04 17:32:30 -07:00
|
|
|
let settings = Settings {
|
|
|
|
windows_powershell: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-18 11:02:15 -08:00
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
shell_command: false,
|
|
|
|
shell_args: Some(vec!["-nice".to_string()]),
|
|
|
|
..testing::config(&[])
|
|
|
|
};
|
|
|
|
|
2022-08-08 19:50:31 -07:00
|
|
|
assert_eq!(settings.shell(&config), ("sh", vec!["-nice"]));
|
2019-11-10 23:17:47 -08:00
|
|
|
}
|
|
|
|
}
|