Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 7 additions & 115 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ include = [
[dependencies]
antex = "0.2.6"
clars = "0.5.0"
dagis = "0.1.0"
diml = "0.2.1"
petgraph = "0.8.3"
# indexmap lowered because of petgraph - remove when petgraph upgrades
indexmap = "=2.6.0"

[dev-dependencies]
cli-assert = "0.1.13"
Expand Down
32 changes: 12 additions & 20 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use super::errors::{
};
use crate::command::execute;
use antex::{ColorMode, StyledText, Text};
use petgraph::Graph;
use petgraph::algo::toposort;
use petgraph::graph::NodeIndex;
use dagis::TypedDiGraph;
use std::collections::{BTreeMap, HashMap};
use std::process::ExitStatus;

Expand Down Expand Up @@ -296,36 +294,30 @@ impl Tasks {

/// Sorts tasks topologically, detect non-existing subtasks and dependency cycles.
fn sort_tasks_topologically(tasks: &Self) -> Result<Vec<String>> {
let mut graph = Graph::<String, ()>::new();
let mut name_by_index: HashMap<NodeIndex, String> = HashMap::new();
let mut index_by_name: HashMap<String, NodeIndex> = HashMap::new();
let mut graph = TypedDiGraph::<String>::default();
let mut vertex_by_name: HashMap<String, usize> = HashMap::new();
// Add all nodes to the graph.
for (name, _) in tasks {
let node_index = graph.add_node(name.to_owned());
name_by_index.insert(node_index, name.to_owned());
index_by_name.insert(name.to_owned(), node_index);
let node_index = graph.vertex(name.to_owned());
vertex_by_name.insert(name.to_owned(), node_index);
}
// Add directed edges to the graph, from child node to parent node.
for (name, task) in tasks {
let parent_index = index_by_name.get(name).copied().unwrap();
let parent_vertex = vertex_by_name.get(name).copied().unwrap();
for action in &task.actions {
if let Action::Task(name) = action {
let Some(child_index) = index_by_name.get(name).copied() else {
let Some(child_vertex) = vertex_by_name.get(name).copied() else {
return Err(err_task_not_found(name.to_owned()));
};
graph.add_edge(child_index, parent_index, ());
graph.edge(child_vertex, parent_vertex);
}
}
}
// Sort topologically, detect cycles.
match toposort(&graph, None) {
Ok(indexes) => Ok(indexes
.iter()
.map(|index| name_by_index.get(index).cloned().unwrap())
.collect()),
Err(cycle) => Err(err_dependency_cycle(
name_by_index.get(&cycle.node_id()).cloned().unwrap(),
)),
if graph.acyclic() {
Ok(graph.sort())
} else {
Err(err_dependency_cycle(graph.cycle()[0].clone()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/dependency_cycle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ fn _0001() {
.failure()
.code(1)
.stdout("")
.stderr("error: dependency cycle for task: uncle\n")
.stderr("error: dependency cycle for task: child\n")
.execute();
}
Loading