2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) struct AssignmentEvaluator<'a: 'b, 'b> {
|
|
|
|
pub(crate) assignments: &'b BTreeMap<&'a str, Expression<'a>>,
|
|
|
|
pub(crate) invocation_directory: &'b Result<PathBuf, String>,
|
|
|
|
pub(crate) dotenv: &'b BTreeMap<String, String>,
|
|
|
|
pub(crate) dry_run: bool,
|
|
|
|
pub(crate) evaluated: BTreeMap<&'a str, String>,
|
|
|
|
pub(crate) exports: &'b BTreeSet<&'a str>,
|
|
|
|
pub(crate) overrides: &'b BTreeMap<&'b str, &'b str>,
|
|
|
|
pub(crate) quiet: bool,
|
|
|
|
pub(crate) scope: &'b BTreeMap<&'a str, String>,
|
|
|
|
pub(crate) shell: &'b str,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn evaluate_assignments(
|
2019-04-11 15:23:14 -07:00
|
|
|
assignments: &BTreeMap<&'a str, Expression<'a>>,
|
2018-06-19 10:04:03 -07:00
|
|
|
invocation_directory: &Result<PathBuf, String>,
|
2019-04-11 15:23:14 -07:00
|
|
|
dotenv: &'b BTreeMap<String, String>,
|
|
|
|
overrides: &BTreeMap<&str, &str>,
|
2018-12-08 14:29:41 -08:00
|
|
|
quiet: bool,
|
|
|
|
shell: &'a str,
|
|
|
|
dry_run: bool,
|
2019-04-11 15:23:14 -07:00
|
|
|
) -> RunResult<'a, BTreeMap<&'a str, String>> {
|
2017-11-18 01:18:04 -08:00
|
|
|
let mut evaluator = AssignmentEvaluator {
|
2018-03-05 13:21:35 -08:00
|
|
|
evaluated: empty(),
|
2018-12-08 14:29:41 -08:00
|
|
|
exports: &empty(),
|
|
|
|
scope: &empty(),
|
2018-03-05 13:21:35 -08:00
|
|
|
assignments,
|
2018-06-19 10:04:03 -07:00
|
|
|
invocation_directory,
|
2018-03-05 13:21:35 -08:00
|
|
|
dotenv,
|
|
|
|
dry_run,
|
|
|
|
overrides,
|
|
|
|
quiet,
|
|
|
|
shell,
|
2017-11-18 01:18:04 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
for name in assignments.keys() {
|
|
|
|
evaluator.evaluate_assignment(name)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(evaluator.evaluated)
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn evaluate_line(
|
2017-11-16 23:30:08 -08:00
|
|
|
&mut self,
|
2018-12-08 14:29:41 -08:00
|
|
|
line: &[Fragment<'a>],
|
2019-04-11 15:23:14 -07:00
|
|
|
arguments: &BTreeMap<&str, Cow<str>>,
|
2017-11-17 17:28:06 -08:00
|
|
|
) -> RunResult<'a, String> {
|
2017-11-16 23:30:08 -08:00
|
|
|
let mut evaluated = String::new();
|
|
|
|
for fragment in line {
|
|
|
|
match *fragment {
|
2019-04-15 22:40:02 -07:00
|
|
|
Fragment::Text { ref text } => evaluated += text.lexeme(),
|
2018-12-08 14:29:41 -08:00
|
|
|
Fragment::Expression { ref expression } => {
|
2017-11-16 23:30:08 -08:00
|
|
|
evaluated += &self.evaluate_expression(expression, arguments)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(evaluated)
|
|
|
|
}
|
|
|
|
|
2017-11-17 17:28:06 -08:00
|
|
|
fn evaluate_assignment(&mut self, name: &'a str) -> RunResult<'a, ()> {
|
2017-11-16 23:30:08 -08:00
|
|
|
if self.evaluated.contains_key(name) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(expression) = self.assignments.get(name) {
|
|
|
|
if let Some(value) = self.overrides.get(name) {
|
|
|
|
self.evaluated.insert(name, value.to_string());
|
|
|
|
} else {
|
|
|
|
let value = self.evaluate_expression(expression, &empty())?;
|
|
|
|
self.evaluated.insert(name, value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::Internal {
|
2018-12-08 14:29:41 -08:00
|
|
|
message: format!("attempted to evaluated unknown assignment {}", name),
|
2017-11-16 23:30:08 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn evaluate_expression(
|
2017-11-16 23:30:08 -08:00
|
|
|
&mut self,
|
|
|
|
expression: &Expression<'a>,
|
2019-04-11 15:23:14 -07:00
|
|
|
arguments: &BTreeMap<&str, Cow<str>>,
|
2017-11-17 17:28:06 -08:00
|
|
|
) -> RunResult<'a, String> {
|
2017-12-02 05:37:10 -08:00
|
|
|
match *expression {
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Variable { name, .. } => {
|
2017-11-16 23:30:08 -08:00
|
|
|
if self.evaluated.contains_key(name) {
|
2017-12-02 05:37:10 -08:00
|
|
|
Ok(self.evaluated[name].clone())
|
2017-11-16 23:30:08 -08:00
|
|
|
} else if self.scope.contains_key(name) {
|
2017-12-02 05:37:10 -08:00
|
|
|
Ok(self.scope[name].clone())
|
2017-11-16 23:30:08 -08:00
|
|
|
} else if self.assignments.contains_key(name) {
|
|
|
|
self.evaluate_assignment(name)?;
|
2017-12-02 05:37:10 -08:00
|
|
|
Ok(self.evaluated[name].clone())
|
2017-11-16 23:30:08 -08:00
|
|
|
} else if arguments.contains_key(name) {
|
2017-12-02 05:37:10 -08:00
|
|
|
Ok(arguments[name].to_string())
|
2017-11-16 23:30:08 -08:00
|
|
|
} else {
|
2017-12-02 05:37:10 -08:00
|
|
|
Err(RuntimeError::Internal {
|
2018-12-08 14:29:41 -08:00
|
|
|
message: format!("attempted to evaluate undefined variable `{}`", name),
|
2017-12-02 05:37:10 -08:00
|
|
|
})
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Call {
|
|
|
|
name,
|
|
|
|
arguments: ref call_arguments,
|
|
|
|
ref token,
|
|
|
|
} => {
|
|
|
|
let call_arguments = call_arguments
|
|
|
|
.iter()
|
|
|
|
.map(|argument| self.evaluate_expression(argument, arguments))
|
|
|
|
.collect::<Result<Vec<String>, RuntimeError>>()?;
|
2018-03-17 09:17:41 -07:00
|
|
|
let context = FunctionContext {
|
2018-06-19 10:04:03 -07:00
|
|
|
invocation_directory: &self.invocation_directory,
|
2018-03-17 09:17:41 -07:00
|
|
|
dotenv: self.dotenv,
|
|
|
|
};
|
2019-04-19 02:40:25 -07:00
|
|
|
Function::evaluate(token, name, &context, &call_arguments)
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
2019-04-11 23:58:08 -07:00
|
|
|
Expression::String { ref cooked_string } => Ok(cooked_string.cooked.to_string()),
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Backtick { raw, ref token } => {
|
2017-11-17 20:21:37 -08:00
|
|
|
if self.dry_run {
|
2017-12-02 05:37:10 -08:00
|
|
|
Ok(format!("`{}`", raw))
|
2017-11-17 20:21:37 -08:00
|
|
|
} else {
|
2018-03-05 13:21:35 -08:00
|
|
|
Ok(self.run_backtick(self.dotenv, raw, token)?)
|
2017-11-17 20:21:37 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2018-12-08 14:29:41 -08:00
|
|
|
Expression::Concatination { ref lhs, ref rhs } => {
|
|
|
|
Ok(self.evaluate_expression(lhs, arguments)? + &self.evaluate_expression(rhs, arguments)?)
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2019-04-11 23:58:08 -07:00
|
|
|
Expression::Group { ref expression } => self.evaluate_expression(&expression, arguments),
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2017-11-17 20:21:37 -08:00
|
|
|
|
|
|
|
fn run_backtick(
|
|
|
|
&self,
|
2019-04-11 15:23:14 -07:00
|
|
|
dotenv: &BTreeMap<String, String>,
|
2018-12-08 14:29:41 -08:00
|
|
|
raw: &str,
|
|
|
|
token: &Token<'a>,
|
2017-11-17 20:21:37 -08:00
|
|
|
) -> RunResult<'a, String> {
|
|
|
|
let mut cmd = Command::new(self.shell);
|
|
|
|
|
2019-04-16 19:52:16 -07:00
|
|
|
cmd.arg("-cu").arg(raw);
|
|
|
|
|
2018-03-05 13:21:35 -08:00
|
|
|
cmd.export_environment_variables(self.scope, dotenv, self.exports)?;
|
2017-11-17 20:21:37 -08:00
|
|
|
|
2019-04-16 19:52:16 -07:00
|
|
|
cmd.stdin(process::Stdio::inherit());
|
2017-11-17 20:21:37 -08:00
|
|
|
|
|
|
|
cmd.stderr(if self.quiet {
|
|
|
|
process::Stdio::null()
|
|
|
|
} else {
|
|
|
|
process::Stdio::inherit()
|
|
|
|
});
|
|
|
|
|
2018-12-08 14:29:41 -08:00
|
|
|
InterruptHandler::guard(|| {
|
2019-07-13 01:55:06 -07:00
|
|
|
output(cmd).map_err(|output_error| RuntimeError::Backtick {
|
2018-12-08 14:29:41 -08:00
|
|
|
token: token.clone(),
|
|
|
|
output_error,
|
|
|
|
})
|
|
|
|
})
|
2017-11-17 20:21:37 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2019-04-19 02:17:43 -07:00
|
|
|
use crate::testing::parse;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2018-06-19 10:04:03 -07:00
|
|
|
fn no_cwd_err() -> Result<PathBuf, String> {
|
|
|
|
Err(String::from("no cwd in tests"))
|
|
|
|
}
|
|
|
|
|
2017-11-17 17:28:06 -08:00
|
|
|
#[test]
|
|
|
|
fn backtick_code() {
|
2019-04-19 02:17:43 -07:00
|
|
|
match parse("a:\n echo {{`f() { return 100; }; f`}}")
|
2018-12-08 14:29:41 -08:00
|
|
|
.run(&no_cwd_err(), &["a"], &Default::default())
|
|
|
|
.unwrap_err()
|
|
|
|
{
|
|
|
|
RuntimeError::Backtick {
|
|
|
|
token,
|
|
|
|
output_error: OutputError::Code(code),
|
|
|
|
} => {
|
2017-11-17 17:28:06 -08:00
|
|
|
assert_eq!(code, 100);
|
2019-04-15 22:40:02 -07:00
|
|
|
assert_eq!(token.lexeme(), "`f() { return 100; }; f`");
|
2018-12-08 14:29:41 -08:00
|
|
|
}
|
2017-11-17 17:28:06 -08:00
|
|
|
other => panic!("expected a code run error, but got: {}", other),
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:28:06 -08:00
|
|
|
#[test]
|
|
|
|
fn export_assignment_backtick() {
|
|
|
|
let text = r#"
|
2017-11-16 23:30:08 -08:00
|
|
|
export exported_variable = "A"
|
|
|
|
b = `echo $exported_variable`
|
|
|
|
|
|
|
|
recipe:
|
|
|
|
echo {{b}}
|
|
|
|
"#;
|
2019-10-07 02:06:45 -07:00
|
|
|
let config = Config {
|
2017-11-17 17:28:06 -08:00
|
|
|
quiet: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2019-04-19 02:17:43 -07:00
|
|
|
match parse(text)
|
2019-10-07 02:06:45 -07:00
|
|
|
.run(&no_cwd_err(), &["recipe"], &config)
|
2018-12-08 14:29:41 -08:00
|
|
|
.unwrap_err()
|
|
|
|
{
|
|
|
|
RuntimeError::Backtick {
|
|
|
|
token,
|
|
|
|
output_error: OutputError::Code(_),
|
|
|
|
} => {
|
2019-04-15 22:40:02 -07:00
|
|
|
assert_eq!(token.lexeme(), "`echo $exported_variable`");
|
2018-12-08 14:29:41 -08:00
|
|
|
}
|
2017-11-17 17:28:06 -08:00
|
|
|
other => panic!("expected a backtick code errror, but got: {}", other),
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|