Compare commits
4 Commits
v1.3.0
...
0cdd0bbc07
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cdd0bbc07 | |||
| a5d442f7ff | |||
| ac948f193b | |||
| 2d7d6cf641 |
20
CHANGELOG.md
20
CHANGELOG.md
@@ -2,12 +2,32 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [1.4.0] - 2025-04-06
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- Default env variables
|
||||
|
||||
## [1.3.1] - 2025-01-31
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- Better enforce typing for commands
|
||||
|
||||
### ⚙️ Miscellaneous Tasks
|
||||
|
||||
- Release
|
||||
|
||||
## [1.3.0] - 2025-01-31
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
- Pipe
|
||||
|
||||
### ⚙️ Miscellaneous Tasks
|
||||
|
||||
- Release
|
||||
|
||||
## [1.2.0] - 2025-01-14
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -40,7 +40,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "comlexr"
|
||||
version = "1.3.0"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"comlexr_macro",
|
||||
"rstest",
|
||||
@@ -51,7 +51,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "comlexr_macro"
|
||||
version = "1.3.0"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
@@ -4,7 +4,7 @@ members = ["macro"]
|
||||
[workspace.package]
|
||||
description = "Dynamically build Command objects with conditional expressions"
|
||||
repository = "https://gitlab.com/wunker-bunker/comlexr"
|
||||
version = "1.3.0"
|
||||
version = "1.4.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.60"
|
||||
license = "MIT"
|
||||
@@ -43,7 +43,7 @@ pre-release-replacements = [
|
||||
all-features = true
|
||||
|
||||
[dependencies]
|
||||
comlexr_macro = { version = "=1.3.0", path = "./macro" }
|
||||
comlexr_macro = { version = "=1.4.0", path = "./macro" }
|
||||
thiserror = "1.0.65"
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
23
README.md
23
README.md
@@ -8,7 +8,7 @@ Add `comlexr` to your project's `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
comlexr = "1.3.0"
|
||||
comlexr = "1.4.0"
|
||||
```
|
||||
|
||||
## MSRV
|
||||
@@ -185,6 +185,27 @@ let command = cmd!(
|
||||
assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
|
||||
```
|
||||
|
||||
#### Conditional
|
||||
You can have a default value set for an environment variable.
|
||||
|
||||
```rust
|
||||
use comlexr::cmd;
|
||||
|
||||
const NEW_VAR: &str = "NEW_VAR";
|
||||
std::env::set_var("TEST", "realvalue");
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST":? "test",
|
||||
NEW_VAR: "new_var"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
|
||||
```
|
||||
|
||||
#### Current Directory and Env Variable Order Matters
|
||||
Environment variable declarations **MUST** come after the current directory declaration.
|
||||
|
||||
|
||||
@@ -437,25 +437,51 @@ impl ToTokens for EnvVars {
|
||||
struct EnvVar {
|
||||
key: Value,
|
||||
value: Value,
|
||||
conditional: bool,
|
||||
}
|
||||
|
||||
impl Parse for EnvVar {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let key = input.parse()?;
|
||||
|
||||
_ = input.parse::<Token![:]>()?;
|
||||
|
||||
let conditional = if input.lookahead1().peek(Token![?]) {
|
||||
input.parse::<Token![?]>()?;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let value = input.parse()?;
|
||||
|
||||
Ok(Self { key, value })
|
||||
Ok(Self {
|
||||
key,
|
||||
value,
|
||||
conditional,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for EnvVar {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
let Self { key, value } = self;
|
||||
let Self {
|
||||
key,
|
||||
value,
|
||||
conditional,
|
||||
} = self;
|
||||
|
||||
if *conditional {
|
||||
tokens.extend(quote! {
|
||||
if ::std::env::var(#key).ok().is_none() {
|
||||
_c.env(#key, #value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
tokens.extend(quote! { _c.env(#key, #value); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CurrentDir(Option<Value>);
|
||||
|
||||
|
||||
@@ -56,6 +56,26 @@ mod pipe;
|
||||
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" TEST="test" "echo" "test""#);
|
||||
/// ```
|
||||
///
|
||||
/// #### Conditional
|
||||
/// You can have a default value set for an environment variable.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use comlexr_macro::cmd;
|
||||
/// const NEW_VAR: &str = "NEW_VAR";
|
||||
/// std::env::set_var("TEST", "realvalue");
|
||||
///
|
||||
/// let command = cmd!(
|
||||
/// env {
|
||||
/// "TEST":? "test",
|
||||
/// NEW_VAR: "new_var"
|
||||
/// };
|
||||
/// "echo",
|
||||
/// "test",
|
||||
/// );
|
||||
///
|
||||
/// assert_eq!(format!("{command:?}"), r#"NEW_VAR="new_var" "echo" "test""#);
|
||||
/// ```
|
||||
///
|
||||
/// ### Current Directory and Environment Variable Order
|
||||
/// ```
|
||||
/// # use comlexr_macro::cmd;
|
||||
|
||||
@@ -6,7 +6,7 @@ use syn::{
|
||||
Error, Token,
|
||||
};
|
||||
|
||||
use crate::{cmd::Value, macros::enum_to_tokens};
|
||||
use crate::cmd::Value;
|
||||
|
||||
pub struct Pipe {
|
||||
stdin: Option<Value>,
|
||||
@@ -47,7 +47,7 @@ impl ToTokens for Pipe {
|
||||
let first_command = commands_iter.next().unwrap();
|
||||
|
||||
let initial = quote! {
|
||||
let mut _c_0 = #first_command;
|
||||
let mut _c_0: #first_command;
|
||||
_c_0.stdout(::std::process::Stdio::piped());
|
||||
_c_0.stdin(::std::process::Stdio::piped());
|
||||
let mut _child_0 = _c_0.spawn()?;
|
||||
@@ -95,7 +95,7 @@ impl ToTokens for Pipe {
|
||||
});
|
||||
}
|
||||
|
||||
let mut #com_ident = #command;
|
||||
let mut #com_ident: #command;
|
||||
#com_ident.stdout(::std::process::Stdio::piped());
|
||||
#com_ident.stdin(::std::process::Stdio::piped());
|
||||
|
||||
@@ -171,4 +171,21 @@ impl Parse for CommandExpr {
|
||||
}
|
||||
}
|
||||
|
||||
enum_to_tokens! {CommandExpr: Macro, Reference, Function, Block}
|
||||
impl ToTokens for CommandExpr {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
tokens.extend(match self {
|
||||
Self::Macro(macr) => quote! {
|
||||
::std::process::Command = #macr
|
||||
},
|
||||
Self::Reference(refer) => quote! {
|
||||
&mut ::std::process::Command = #refer
|
||||
},
|
||||
Self::Function(fun) => quote! {
|
||||
::std::process::Command = #fun
|
||||
},
|
||||
Self::Block(block) => quote! {
|
||||
::std::process::Command = #block
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
33
tests/cmd.rs
33
tests/cmd.rs
@@ -1,6 +1,8 @@
|
||||
extern crate comlexr;
|
||||
extern crate rstest;
|
||||
|
||||
use std::env;
|
||||
|
||||
use comlexr::cmd;
|
||||
use rstest::rstest;
|
||||
|
||||
@@ -35,6 +37,37 @@ fn env_vars() {
|
||||
assert_eq!(format!("{command:?}"), r#"TEST="test" "echo" "test""#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conditional_env_vars() {
|
||||
env::set_var("TEST", "realvalue");
|
||||
env::set_var("TEST2", "won't see");
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST":? "test",
|
||||
"TEST2": "test2"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(format!("{command:?}"), r#"TEST2="test2" "echo" "test""#);
|
||||
|
||||
let command = cmd!(
|
||||
env {
|
||||
"TEST": "test",
|
||||
"TEST2": "test2"
|
||||
};
|
||||
"echo",
|
||||
"test",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
format!("{command:?}"),
|
||||
r#"TEST="test" TEST2="test2" "echo" "test""#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cd_env_vars() {
|
||||
let command = cmd!(
|
||||
|
||||
Reference in New Issue
Block a user