Skip to content

Commit 914e931

Browse files
authored
Merge pull request #22 from QIUZHILEI/dev
Remove the function of executing js command
2 parents d69586f + c6e53b3 commit 914e931

27 files changed

+218
-365
lines changed

.github/workflows/base.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ jobs:
2222
- uses: actions-rs/cargo@v1
2323
with:
2424
command: check
25+
- uses: actions/setup-node@v3
26+
with:
27+
node-version: 16
2528

2629
test:
2730
name: Test Suite

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ keywords = ["DAG", "task", "async", "parallel", "concurrent"]
1313
[dependencies]
1414
yaml-rust = "0.4.5"
1515
bimap = "0.6.1"
16-
deno_core = "0.191.0"
1716
clap = { version = "4.2.2", features = ["derive"] }
1817
anymap2 = "0.13.0"
1918
thiserror = "1.0.30"

README.md

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ Task executed successfully. [name: Task d]
136136
Process finished with exit code 0
137137
```
138138

139-
140-
141-
### Yaml configuration file
139+
### `Yaml` configuration file
142140

143141
A standard yaml configuration file format is given below:
144142

@@ -147,50 +145,34 @@ dagrs:
147145
a:
148146
name: "Task 1"
149147
after: [ b, c ]
150-
run:
151-
type: sh
152-
script: echo a
148+
cmd: echo a
153149
b:
154150
name: "Task 2"
155151
after: [ c, f, g ]
156-
run:
157-
type: sh
158-
script: echo b
152+
cmd: echo b
159153
c:
160154
name: "Task 3"
161155
after: [ e, g ]
162-
run:
163-
type: sh
164-
script: echo c
156+
cmd: echo c
165157
d:
166158
name: "Task 4"
167159
after: [ c, e ]
168-
run:
169-
type: sh
170-
script: echo d
160+
cmd: echo d
171161
e:
172162
name: "Task 5"
173163
after: [ h ]
174-
run:
175-
type: sh
176-
script: echo e
164+
cmd: echo e
177165
f:
178166
name: "Task 6"
179167
after: [ g ]
180-
run:
181-
type: deno
182-
script: Deno.core.print("f\n")
168+
cmd: python3 ./tests/config/test.py
183169
g:
184170
name: "Task 7"
185171
after: [ h ]
186-
run:
187-
type: deno
188-
script: Deno.core.print("g\n")
172+
cmd: node ./tests/config/test.js
189173
h:
190174
name: "Task 8"
191-
run:
192-
type: sh
193-
script: echo h
175+
cmd: echo h
194176
```
195177
196178
These yaml-defined task items form a complex dependency graph. In the yaml configuration file:
@@ -199,7 +181,8 @@ These yaml-defined task items form a complex dependency graph. In the yaml confi
199181
- Similar to `a`, `b`, `c`... is the unique identifier of the task
200182
- `name` is a required attribute, which is the name of the task
201183
- `after` is an optional attribute (only the first executed task does not have this attribute), which represents which tasks are executed after the task, that is, specifies dependencies for tasks
202-
- `run` is a required attribute, followed by `type` and `script`, they are all required attributes, where `type` represents the type of task. The framework provides default implementations of the `Action` trait for two types of script tasks, namely sh and javascript. If users want to customize other types of script tasks, or implement their own script execution logic, they can implement the `Action` trait by programming. Although this is cumbersome, this method will be more flexible. In addition, when parsing the configuration file, the user also needs to provide the parser with a specific type that implements the `Action` trait, and the method should be in the form of a key-value pair: <id,action>
184+
- `cmd` is a optional attribute. You need to point out the command to be executed, such as the basic shell command: `echo hello`, execute the python script `python test.py`, etc. The user must ensure that the interpreter that executes the script exists in the environment variable. `CommandAction` is the implementation of the specific execution logic of the script, which is put into a specific `Task` type.
185+
If users want to customize other types of script tasks, or implement their own script execution logic, they can implement the "Action" feature through programming, and when parsing the configuration file, provide the parser with a specific type that implements the `Action` feature, and the method should be in the form of a key-value pair: <id,action>. Although this is more troublesome, this method will be more flexible.
203186

204187
To parse the yaml configured file, you need to compile this project, requiring rust version >= 1.70:
205188

@@ -230,9 +213,7 @@ $./target/release/dagrs --yaml=./tests/config/correct.yaml --log-path=./dagrs.lo
230213
Executing Task[name: Task 8]
231214
Executing Task[name: Task 5]
232215
Executing Task[name: Task 7]
233-
g
234216
Executing Task[name: Task 6]
235-
f
236217
Executing Task[name: Task 3]
237218
Executing Task[name: Task 2]
238219
Executing Task[name: Task 4]

examples/custom_parser.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//! The content of the configuration file is as follows:
33
//!
44
//! ```
5-
//! a,Task a,b c,sh,echo a
6-
//! b,Task b,c f g,sh,echo b
7-
//! c,Task c,e g,sh,echo c
8-
//! d,Task d,c e,sh,echo d
9-
//! e,Task e,h,sh,echo e
10-
//! f,Task f,g,deno,Deno.core.print("f\n")
11-
//! g,Task g,h,deno,Deno.core.print("g\n")
12-
//! h,Task h,,sh,echo h
5+
//! a,Task a,b c,echo a
6+
//! b,Task b,c f g,echo b
7+
//! c,Task c,e g,echo c
8+
//! d,Task d,c e,echo d
9+
//! e,Task e,h,echo e
10+
//! f,Task f,g,python3 tests/config/test.py
11+
//! g,Task g,h,node tests/config/test.js
12+
//! h,Task h,,echo h
1313
//! ```
1414
1515
extern crate dagrs;
@@ -18,7 +18,7 @@ use std::{fs, sync::Arc};
1818
use std::collections::HashMap;
1919
use std::fmt::{Display, Formatter};
2020

21-
use dagrs::{Action, Dag, log,LogLevel, JavaScript, Parser, ParserError, ShScript, Task};
21+
use dagrs::{Action, Dag, log,LogLevel, Parser, ParserError, CommandAction, Task};
2222

2323
struct MyTask {
2424
tid: (String, usize),
@@ -99,23 +99,13 @@ impl ConfigParser {
9999

100100
let id = *attr.get(0).unwrap();
101101
let name = attr.get(1).unwrap().to_string();
102-
let script = *attr.get(4).unwrap();
103-
let t_type = *attr.get(3).unwrap();
104-
if t_type.eq("sh") {
105-
MyTask::new(
106-
id,
107-
pres,
108-
name,
109-
ShScript::new(script),
110-
)
111-
} else {
112-
MyTask::new(
113-
id,
114-
pres,
115-
name,
116-
JavaScript::new(script),
117-
)
118-
}
102+
let cmd = *attr.get(3).unwrap();
103+
MyTask::new(
104+
id,
105+
pres,
106+
name,
107+
CommandAction::new(cmd),
108+
)
119109
}
120110
}
121111

examples/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use dagrs::{
99
};
1010
fn main() {
1111
// initialization log.
12-
log::init_logger(LogLevel::Error, None);
12+
log::init_logger(LogLevel::Info, None);
1313
// Create an Engine.
1414
let mut engine = Engine::default();
1515

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
extern crate anymap2;
22
extern crate bimap;
33
extern crate clap;
4-
extern crate deno_core;
54
extern crate yaml_rust;
65

76
pub use engine::{Dag, DagError, Engine};
87
pub use parser::*;
9-
pub use task::{Action, DefaultTask, alloc_id, Input, JavaScript, Output, RunningError, ShScript, Task, YamlTask};
8+
pub use task::{Action, DefaultTask, alloc_id, Input, Output, RunningError, CommandAction, Task, YamlTask};
109
pub use utils::{EnvVar, gen_macro,LogLevel,Logger,log};
1110

1211
mod engine;

src/parser/error.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ pub enum YamlTaskError {
3737
/// The specified task predecessor was not found.
3838
#[error("Task cannot find the specified predecessor. [{0}]")]
3939
NotFoundPrecursor(String),
40-
/// `run` is not defined.
41-
#[error("The 'run' attribute is not defined. [{0}]")]
42-
NoRunAttr(String),
43-
/// `type` is not defined.
44-
#[error("The 'type' attribute is not defined. [{0}]")]
45-
NoTypeAttr(String),
46-
/// Unsupported script type.
47-
#[error("Unsupported script type [{0}]")]
48-
UnsupportedType(String),
4940
/// `script` is not defined.
5041
#[error("The 'script' attribute is not defined. [{0}]")]
5142
NoScriptAttr(String),

src/parser/mod.rs

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,40 @@
1414
//! # The basic format of the yaml configuration file is as follows:
1515
//! ```yaml
1616
//! dagrs:
17-
//! a:
18-
//! name: "Task 1"
19-
//! after: [b, c]
20-
//! run:
21-
//! type: sh
22-
//! script: echo a
23-
//! b:
24-
//! name: "Task 2"
25-
//! after: [c, f, g]
26-
//! run:
27-
//! type: sh
28-
//! script: echo b
29-
//! c:
30-
//! name: "Task 3"
31-
//! after: [e, g]
32-
//! run:
33-
//! type: sh
34-
//! script: echo c
35-
//! d:
36-
//! name: "Task 4"
37-
//! after: [c, e]
38-
//! run:
39-
//! type: sh
40-
//! script: echo d
41-
//! e:
42-
//! name: "Task 5"
43-
//! after: [h]
44-
//! run:
45-
//! type: sh
46-
//! script: echo e
47-
//! f:
48-
//! name: "Task 6"
49-
//! after: [g]
50-
//! run:
51-
//! type: deno
52-
//! script: Deno.core.print("f\n")
53-
//! g:
54-
//! name: "Task 7"
55-
//! after: [h]
56-
//! run:
57-
//! type: deno
58-
//! script: Deno.core.print("g\n")
59-
//! h:
60-
//! name: "Task 8"
61-
//! run:
62-
//! type: sh
63-
//! script: echo h
17+
//! a:
18+
//! name: "Task 1"
19+
//! after: [ b, c ]
20+
//! cmd: echo a
21+
//! b:
22+
//! name: "Task 2"
23+
//! after: [ c, f, g ]
24+
//! cmd: echo b
25+
//! c:
26+
//! name: "Task 3"
27+
//! after: [ e, g ]
28+
//! cmd: echo c
29+
//! d:
30+
//! name: "Task 4"
31+
//! after: [ c, e ]
32+
//! cmd: echo d
33+
//! e:
34+
//! name: "Task 5"
35+
//! after: [ h ]
36+
//! cmd: echo e
37+
//! f:
38+
//! name: "Task 6"
39+
//! after: [ g ]
40+
//! cmd: python3 ./tests/config/test.py
41+
//! g:
42+
//! name: "Task 7"
43+
//! after: [ h ]
44+
//! cmd: node ./tests/config/test.js
45+
//! h:
46+
//! name: "Task 8"
47+
//! cmd: echo h
6448
//! ```
6549
//!
66-
//! Currently, the framework supports sh and javascript script task types by default. If users
50+
//! Users can execute arbitrary commands of the operating system. If users
6751
//! want to run other types of script tasks, they need to implement the [`Action`] trait by themselves,
6852
//! and before parsing the configuration file, they need to provide a specific type that implements
6953
//! the [`Action`] trait in the form of key-value pairs: <id, action>.

src/parser/yaml_parser.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{collections::HashMap, fs::File, io::Read, sync::Arc};
55
use yaml_rust::{Yaml, YamlLoader};
66

77
use crate::{
8-
task::{JavaScript, ShScript, Task, YamlTask},
8+
task::{CommandAction, Task, YamlTask},
99
Action,
1010
};
1111

@@ -54,43 +54,19 @@ impl YamlParser {
5454
.map(|task_id| precursors.push(task_id.as_str().unwrap().to_owned()))
5555
.count();
5656
}
57-
// Get run script
58-
let run = &item["run"];
59-
if run.is_badvalue() {
60-
return Err(YamlTaskError::NoRunAttr(name));
61-
}
62-
let script_type = run["type"]
63-
.as_str()
64-
.ok_or(YamlTaskError::NoTypeAttr(name.clone()))?;
65-
57+
6658
if let Some(action) = specific_action {
6759
Ok(YamlTask::new(id, precursors, name, action))
6860
} else {
69-
match script_type {
70-
"sh" => {
71-
let sh_script = run["script"]
72-
.as_str()
73-
.ok_or(YamlTaskError::NoScriptAttr(name.clone()))?;
74-
Ok(YamlTask::new(
75-
id,
76-
precursors,
77-
name,
78-
Arc::new(ShScript::new(sh_script)) as Arc<dyn Action+Send+Sync+'static>,
79-
))
80-
}
81-
"deno" => {
82-
let js_script = run["script"]
83-
.as_str()
84-
.ok_or(YamlTaskError::NoScriptAttr(name.clone()))?;
85-
Ok(YamlTask::new(
86-
id,
87-
precursors,
88-
name,
89-
Arc::new(JavaScript::new(js_script)) as Arc<dyn Action+Send+Sync+'static>,
90-
))
91-
}
92-
_ => Err(YamlTaskError::UnsupportedType(name)),
93-
}
61+
let cmd = item["cmd"]
62+
.as_str()
63+
.ok_or(YamlTaskError::NoScriptAttr(name.clone()))?;
64+
Ok(YamlTask::new(
65+
id,
66+
precursors,
67+
name,
68+
Arc::new(CommandAction::new(cmd)) as Arc<dyn Action + Send + Sync + 'static>,
69+
))
9470
}
9571
}
9672
}
@@ -99,7 +75,7 @@ impl Parser for YamlParser {
9975
fn parse_tasks(
10076
&self,
10177
file: &str,
102-
mut specific_actions: HashMap<String,Arc<dyn Action+Send+Sync+'static>>,
78+
mut specific_actions: HashMap<String, Arc<dyn Action + Send + Sync + 'static>>,
10379
) -> Result<Vec<Box<dyn Task>>, ParserError> {
10480
let content = self.load_file(file)?;
10581
// Parse Yaml

0 commit comments

Comments
 (0)