Skip to content

Commit 9636e34

Browse files
authored
Merge pull request #45 from devrc-hub/feature/raw_variable_modifier
Feature/raw variable modifier
2 parents 2989e20 + 04d5f87 commit 9636e34

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "devrc"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Alex <hey.turbo.driver@gmail.com>"]
55
description = "devrc is an easy to use task runner tool on steroids for developers"
66
edition = "2018"

Devrcfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
variables:
33
message: "Hello, $USER!"
4-
message_1 +template: "Hello, $USER"
4+
message_1 +raw: "Hello, $USER"
55

66
environment:
77
var_1: "env_var_1"

examples/simple_demo.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
variables:
22
first_name: "Alex"
33
second_name: "Alice"
4+
raw_var +raw: "Raw variable {{ task_variable_global }}"
45

56
environment:
67
ENV_NAME: "{{ second_name }}"
78

8-
task_name:
9+
first_task:
910
desc: "Task description"
1011
variables:
1112
task_variable: "Task variable value"
@@ -23,3 +24,11 @@ second_task:
2324
var: "simple var"
2425
exec: |
2526
echo "Second task: {{ task_variable_global }} "
27+
28+
29+
third_task:
30+
desc: "Third task description"
31+
variables:
32+
local_var: "{{ raw_var }}"
33+
exec: |
34+
echo "Third task: {{ raw_var }} "

src/scope.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ impl Scope {
5050
variables::ValueKind::None => return Err(DevrcError::EmptyVariable),
5151
variables::ValueKind::String(inner) => {
5252
let key = VariableKey::try_from(original_key.clone())?;
53-
let value = VariableValue::new(original_key, inner).with_render_value(self)?;
53+
54+
let value: VariableValue = if key.raw {
55+
VariableValue::new(original_key, inner).as_raw()?
56+
} else {
57+
VariableValue::new(original_key, inner).with_render_value(self)?
58+
};
59+
5460
self.variables.insert(key.clone(), value.clone());
5561

5662
if key.set_global && self.parent.is_some() {

src/variables.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct VariableKey {
6060
pub original: String,
6161
pub name: String,
6262
pub set_global: bool,
63+
pub raw: bool,
6364
}
6465

6566
// impl From<String> for VariableKey {
@@ -85,30 +86,40 @@ impl VariableKey {
8586
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone)]
8687
pub struct VariableValue {
8788
pub name: String,
88-
pub raw: String,
89+
pub original: String,
8990
pub rendered: Option<String>,
91+
pub raw: bool,
9092
}
9193

9294
impl VariableValue {
9395
pub fn new(name: &str, raw: &str) -> Self {
9496
Self {
9597
name: name.to_owned(),
96-
raw: raw.to_owned(),
98+
original: raw.to_owned(),
9799
rendered: None,
100+
raw: false,
98101
}
99102
}
100103

101104
pub fn get_rendered_value(&self) -> String {
105+
if self.raw {
106+
return self.original.clone();
107+
}
102108
self.rendered.clone().unwrap_or_default()
103109
}
104110

105111
pub fn render_value(&mut self, name: &str, scope: &Scope) -> DevrcResult<()> {
106-
self.rendered = Some(render_string(name, &self.raw, scope)?);
112+
self.rendered = Some(render_string(name, &self.original, scope)?);
107113
Ok(())
108114
}
109115

110116
pub fn with_render_value(mut self, scope: &Scope) -> DevrcResult<Self> {
111-
self.rendered = Some(render_string(&self.name, &self.raw, scope)?);
117+
self.rendered = Some(render_string(&self.name, &self.original, scope)?);
118+
Ok(self)
119+
}
120+
121+
pub fn as_raw(mut self) -> DevrcResult<Self> {
122+
self.raw = true;
112123
Ok(self)
113124
}
114125
}

src/variables_parser.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ use crate::{
44
};
55

66
const GLOBAL_MODIFIER: &str = "+global";
7+
const RAW_MODIFIER: &str = "+raw";
78

89
pub fn parse_key(value: &str) -> DevrcResult<VariableKey> {
9-
let mut parts = value.splitn(2, |c| c == ' ' || c == '\t');
10+
let mut parts = value.split(|c| c == ' ' || c == '\t');
1011

1112
let name = parts.next().ok_or(DevrcError::InvalidVariableName)?;
1213

1314
let mut key = VariableKey {
1415
original: value.to_string(),
1516
name: name.to_string(),
1617
set_global: false,
18+
raw: false,
1719
};
1820

19-
if let Some(value) = parts.next() {
20-
if value == GLOBAL_MODIFIER {
21+
for part in parts {
22+
if part == GLOBAL_MODIFIER {
2123
key.set_global = true;
24+
} else if part == RAW_MODIFIER {
25+
key.raw = true;
2226
} else {
2327
return Err(DevrcError::InvalidVariableModifier);
2428
}
@@ -34,12 +38,13 @@ mod tests {
3438

3539
#[test]
3640
fn test_parse_variables_key_with_global() {
37-
let result = parse_key("name +global").unwrap();
41+
let result = parse_key("name +global +raw").unwrap();
3842

3943
let control = VariableKey {
40-
original: "name +global".to_string(),
44+
original: "name +global +raw".to_string(),
4145
name: "name".to_string(),
4246
set_global: true,
47+
raw: true,
4348
};
4449
assert_eq!(result, control);
4550
}
@@ -52,13 +57,14 @@ mod tests {
5257
original: "name".to_string(),
5358
name: "name".to_string(),
5459
set_global: false,
60+
raw: false,
5561
};
5662
assert_eq!(result, control);
5763
}
5864

5965
#[test]
6066
fn test_invalid_key() {
61-
let result = parse_key("name +invalid");
67+
let result = parse_key("name +invalid +global +raw");
6268

6369
match result {
6470
Err(DevrcError::InvalidVariableModifier) => {}

0 commit comments

Comments
 (0)