Skip to content

Commit

Permalink
Implementing the dependency resolvers and adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Oct 8, 2024
1 parent 01e5681 commit f12a4e0
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 30 deletions.
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/query/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl Package {
async fn tasks(&self) -> Array<Task> {
self.get_tasks()
.into_iter()
.sorted_by(|a, b| a.0.cmp(&b.0))
.map(|(name, script)| Task {
name,
package: self.clone(),
Expand Down
134 changes: 118 additions & 16 deletions crates/turborepo-lib/src/query/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl Task {
self.package.clone()
}

async fn full_name(&self) -> String {
format!("{}#{}", self.package.name, self.name)
}

async fn script(&self) -> Option<String> {
self.script.as_ref().map(|script| script.value.to_string())
}
Expand All @@ -59,14 +63,14 @@ impl Task {
.flatten()
.filter_map(|task| match task {
TaskNode::Root => None,
TaskNode::Task(task) => Some(Task {
name: task.task().to_string(),
package: Package {
run: self.package.run.clone(),
name: task.package().to_string().into(),
},
script: self.package.get_tasks().get(task.task()).cloned(),
}),
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}
Expand All @@ -82,14 +86,112 @@ impl Task {
.flatten()
.filter_map(|task| match task {
TaskNode::Root => None,
TaskNode::Task(task) => Some(Task {
name: task.task().to_string(),
package: Package {
run: self.package.run.clone(),
name: task.package().to_string().into(),
},
script: self.package.get_tasks().get(task.task()).cloned(),
}),
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}

async fn indirect_dependents(&self) -> Array<Task> {
let task_id = TaskId::from_static(self.package.name.to_string(), self.name.clone());
let direct_dependents = self
.package
.run
.engine()
.dependencies(&task_id)
.unwrap_or_default();
self.package
.run
.engine()
.transitive_dependents(&task_id)
.into_iter()
.filter(|node| !direct_dependents.contains(node))
.filter_map(|node| match node {
TaskNode::Root => None,
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}

async fn indirect_dependencies(&self) -> Array<Task> {
let task_id = TaskId::from_static(self.package.name.to_string(), self.name.clone());
let direct_dependencies = self
.package
.run
.engine()
.dependencies(&task_id)
.unwrap_or_default();
self.package
.run
.engine()
.transitive_dependencies(&task_id)
.into_iter()
.filter(|node| !direct_dependencies.contains(node))
.filter_map(|node| match node {
TaskNode::Root => None,
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}

async fn all_dependents(&self) -> Array<Task> {
let task_id = TaskId::from_static(self.package.name.to_string(), self.name.clone());
self.package
.run
.engine()
.transitive_dependents(&task_id)
.into_iter()
.filter_map(|node| match node {
TaskNode::Root => None,
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}

async fn all_dependencies(&self) -> Array<Task> {
let task_id = TaskId::from_static(self.package.name.to_string(), self.name.clone());
self.package
.run
.engine()
.transitive_dependencies(&task_id)
.into_iter()
.filter_map(|node| match node {
TaskNode::Root => None,
TaskNode::Task(task) if task == &task_id => None,
TaskNode::Task(task) => Some(Task::new(task, &self.package.run)),
})
.sorted_by(|a, b| {
a.package
.name
.cmp(&b.package.name)
.then_with(|| a.name.cmp(&b.name))
})
.collect()
}
Expand Down
Loading

0 comments on commit f12a4e0

Please sign in to comment.