2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2019-12-25 06:12:06 -08:00
|
|
|
use Function::*;
|
2017-12-02 05:37:10 -08:00
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) enum Function {
|
|
|
|
Nullary(fn(&FunctionContext) -> Result<String, String>),
|
|
|
|
Unary(fn(&FunctionContext, &str) -> Result<String, String>),
|
|
|
|
Binary(fn(&FunctionContext, &str, &str) -> Result<String, String>),
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
lazy_static! {
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) static ref TABLE: BTreeMap<&'static str, Function> = vec![
|
2019-12-25 06:12:06 -08:00
|
|
|
("arch", Nullary(arch)),
|
|
|
|
("os", Nullary(os)),
|
|
|
|
("os_family", Nullary(os_family)),
|
|
|
|
("justfile_directory", Nullary(justfile_directory)),
|
|
|
|
("justfile", Nullary(justfile)),
|
|
|
|
("invocation_directory", Nullary(invocation_directory)),
|
|
|
|
("env_var", Unary(env_var)),
|
|
|
|
("env_var_or_default", Binary(env_var_or_default)),
|
2021-03-28 15:44:02 -07:00
|
|
|
("just_executable", Nullary(just_executable)),
|
2018-12-08 14:29:41 -08:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) fn argc(&self) -> usize {
|
2017-12-02 14:59:07 -08:00
|
|
|
match *self {
|
|
|
|
Nullary(_) => 0,
|
2018-12-08 14:29:41 -08:00
|
|
|
Unary(_) => 1,
|
|
|
|
Binary(_) => 2,
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn arch(_context: &FunctionContext) -> Result<String, String> {
|
2021-02-15 01:18:31 -08:00
|
|
|
Ok(target::arch().to_owned())
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn os(_context: &FunctionContext) -> Result<String, String> {
|
2021-02-15 01:18:31 -08:00
|
|
|
Ok(target::os().to_owned())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn os_family(_context: &FunctionContext) -> Result<String, String> {
|
2021-02-15 01:18:31 -08:00
|
|
|
Ok(target::os_family().to_owned())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn invocation_directory(context: &FunctionContext) -> Result<String, String> {
|
2021-02-15 01:18:31 -08:00
|
|
|
Platform::convert_native_path(
|
2019-12-25 06:12:06 -08:00
|
|
|
&context.search.working_directory,
|
|
|
|
context.invocation_directory,
|
|
|
|
)
|
|
|
|
.map_err(|e| format!("Error getting shell path: {}", e))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn justfile(context: &FunctionContext) -> Result<String, String> {
|
|
|
|
context
|
|
|
|
.search
|
|
|
|
.justfile
|
|
|
|
.to_str()
|
|
|
|
.map(str::to_owned)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
format!(
|
|
|
|
"Justfile path is not valid unicode: {}",
|
|
|
|
context.search.justfile.to_string_lossy()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn justfile_directory(context: &FunctionContext) -> Result<String, String> {
|
|
|
|
let justfile_directory = context.search.justfile.parent().ok_or_else(|| {
|
|
|
|
format!(
|
|
|
|
"Could not resolve justfile directory. Justfile `{}` had no parent.",
|
|
|
|
context.search.justfile.display()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
justfile_directory
|
|
|
|
.to_str()
|
|
|
|
.map(str::to_owned)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
format!(
|
|
|
|
"Justfile directory is not valid unicode: {}",
|
|
|
|
justfile_directory.to_string_lossy()
|
|
|
|
)
|
|
|
|
})
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn env_var(context: &FunctionContext, key: &str) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
use std::env::VarError::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
match env::var(key) {
|
|
|
|
Err(NotPresent) => Err(format!("environment variable `{}` not present", key)),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn env_var_or_default(
|
2018-03-17 09:17:41 -07:00
|
|
|
context: &FunctionContext,
|
2018-12-08 14:29:41 -08:00
|
|
|
key: &str,
|
|
|
|
default: &str,
|
2018-03-17 09:17:41 -07:00
|
|
|
) -> Result<String, String> {
|
2020-01-15 02:16:13 -08:00
|
|
|
use std::env::VarError::*;
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
match env::var(key) {
|
2021-02-15 01:18:31 -08:00
|
|
|
Err(NotPresent) => Ok(default.to_owned()),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
2021-03-28 15:44:02 -07:00
|
|
|
|
|
|
|
fn just_executable(_context: &FunctionContext) -> Result<String, String> {
|
|
|
|
let exe_path =
|
|
|
|
std::env::current_exe().map_err(|e| format!("Error getting current executable: {}", e))?;
|
|
|
|
|
|
|
|
exe_path.to_str().map(str::to_owned).ok_or_else(|| {
|
|
|
|
format!(
|
|
|
|
"Executable path is not valid unicode: {}",
|
|
|
|
exe_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|