Skip to content

Commit

Permalink
internal: Implement new common and target crates. (#816)
Browse files Browse the repository at this point in the history
* Start on new crate.

* Update other crates to use.

* Fix tests.

* Delete old crate.

* Rename import.

* Add common crate.

* Add ID.

* Update tests.

* Fix patterns.

* Update docs.
  • Loading branch information
milesj committed May 1, 2023
1 parent c9b1508 commit 49642b5
Show file tree
Hide file tree
Showing 45 changed files with 674 additions and 506 deletions.
17 changes: 14 additions & 3 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ members = [
"crates/ruby/*",
"crates/rust/*",
"crates/system/*",
"crates/typescript/*"
"crates/typescript/*",

# Next-gen
"nextgen/*"
]
default-members = ["crates/cli"]

Expand All @@ -26,6 +29,7 @@ clap = { version = "4.2.4", features = ["derive", "env", "wrap_help"] }
clap_complete = "4.2.1"
console = "0.15.5"
criterion = { version = "0.4.0", features = ["async_tokio"] }
miette = "5.7.0"
once_cell = "1.17.1"
petgraph = { version = "0.6.3", default-features = false, features = ["serde-1"] }
proto_cli = "0.7.2"
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ moon_project = { path = "../core/project" }
moon_project_graph = { path = "../core/project-graph" }
moon_query = { path = "../core/query" }
moon_system_platform = { path = "../system/platform" }
moon_target = { path = "../core/target" }
moon_target = { path = "../../nextgen/target" }
moon_task = { path = "../core/task" }
moon_terminal = { path = "../core/terminal" }
moon_tool = { path = "../core/tool" }
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starbase_styles::color;
pub async fn task(id: String, json: bool) -> Result<(), AnyError> {
let target = Target::parse(&id)?;

let Some(project_id) = target.project_id else {
let Some(project_id) = target.scope_id else {
return Err("A project ID is required.".into());
};

Expand Down
10 changes: 5 additions & 5 deletions crates/cli/tests/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ mod outputs {
});

assert!(predicates::str::contains(
"Project dependencies scope (^:) is not supported in run contexts."
"Dependencies scope (^:) is not supported in run contexts."
)
.eval(&assert.output()));
}
Expand All @@ -1127,10 +1127,10 @@ mod outputs {
cmd.arg("run").arg("outputs:noOutput");
});

assert!(predicates::str::contains(
"Project self scope (~:) is not supported in run contexts."
)
.eval(&assert.output()));
assert!(
predicates::str::contains("Self scope (~:) is not supported in run contexts.")
.eval(&assert.output())
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
source: crates/cli/tests/run_test.rs
assertion_line: 240
expression: get_assert_output(&assert)
expression: assert.output()
---

ERROR Project dependencies scope (^:) is not supported in run contexts.
ERROR Dependencies scope (^:) is not supported in run contexts.



Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
source: crates/cli/tests/run_test.rs
assertion_line: 252
expression: get_assert_output(&assert)
expression: assert.output()
---

ERROR Project self scope (~:) is not supported in run contexts.
ERROR Self scope (~:) is not supported in run contexts.



2 changes: 1 addition & 1 deletion crates/core/action-context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
clap = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
2 changes: 1 addition & 1 deletion crates/core/action-pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ moon_platform = { path = "../platform" }
moon_project = { path = "../project" }
moon_project_graph = { path = "../project-graph" }
moon_runner = { path = "../runner" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_terminal = { path = "../terminal" }
moon_tool = { path = "../tool" }
moon_utils = { path = "../utils" }
Expand Down
7 changes: 5 additions & 2 deletions crates/core/action-pipeline/src/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ impl Estimator {
ActionNode::RunTarget(_, target) => {
let task_id = Target::parse(target).unwrap().task_id;

if let Some(task) = tasks.get_mut(&task_id) {
if let Some(task) = tasks.get_mut(task_id.as_str()) {
task.count += 1;
task.total += task_duration;
} else {
tasks.insert(task_id, TaskEstimate::new(task_duration.to_owned()));
tasks.insert(
task_id.to_string(),
TaskEstimate::new(task_duration.to_owned()),
);
}
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/action-pipeline/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub async fn process_action(
// Run a task within a project
ActionNode::RunTarget(runtime, target_id) => {
let target = Target::parse(target_id)?;
let project = local_project_graph.get(target.project_id.as_ref().unwrap())?;
let project = local_project_graph.get(target.scope_id.as_ref().unwrap())?;

local_emitter
.emit(Event::TargetRunning { target: &target })
Expand Down
2 changes: 1 addition & 1 deletion crates/core/dep-graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ moon_platform = { path = "../platform" }
moon_project = { path = "../project" }
moon_project_graph = { path = "../project-graph" }
moon_query = { path = "../query" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_task = { path = "../task" }
petgraph = { workspace = true }
rustc-hash = { workspace = true }
Expand Down
35 changes: 19 additions & 16 deletions crates/core/dep-graph/src/dep_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use moon_platform::{PlatformManager, Runtime};
use moon_project::Project;
use moon_project_graph::ProjectGraph;
use moon_query::build as build_query;
use moon_target::{Target, TargetError, TargetProjectScope};
use moon_target::{Target, TargetError, TargetScope};
use moon_task::{Task, TouchedFilePaths};
use petgraph::graph::NodeIndex;
use petgraph::Graph;
Expand Down Expand Up @@ -183,15 +183,16 @@ impl<'ws> DepGraphBuilder<'ws> {
color::label(&target.id),
);

let (project_id, task_id) = target.ids()?;
let project = self.project_graph.get(&project_id)?;
let dependents = self.project_graph.get_dependents_of(project)?;
if let TargetScope::Project(project_id) = &target.scope {
let project = self.project_graph.get(project_id)?;
let dependents = self.project_graph.get_dependents_of(project)?;

for dependent_id in dependents {
let dep_project = self.project_graph.get(&dependent_id)?;
for dependent_id in dependents {
let dep_project = self.project_graph.get(&dependent_id)?;

if let Some(dep_task) = dep_project.tasks.get(&task_id) {
self.run_target(&dep_task.target, None)?;
if let Some(dep_task) = dep_project.tasks.get(target.task_id.as_str()) {
self.run_target(&dep_task.target, None)?;
}
}
}

Expand All @@ -207,9 +208,9 @@ impl<'ws> DepGraphBuilder<'ws> {
let mut inserted_targets = FxHashSet::default();
let mut inserted_indexes = FxHashSet::default();

match &target.project {
match &target.scope {
// :task
TargetProjectScope::All => {
TargetScope::All => {
let mut projects = vec![];

if let Some(queried_projects) = &self.queried_projects {
Expand All @@ -219,7 +220,7 @@ impl<'ws> DepGraphBuilder<'ws> {
};

for project in projects {
if project.tasks.contains_key(&target.task_id) {
if project.tasks.contains_key(target.task_id.as_str()) {
let all_target = Target::new(&project.id, &target.task_id)?;

if let Some(index) =
Expand All @@ -232,11 +233,11 @@ impl<'ws> DepGraphBuilder<'ws> {
}
}
// ^:task
TargetProjectScope::Deps => {
target.fail_with(TargetError::NoProjectDepsInRunContext)?;
TargetScope::Deps => {
return Err(DepGraphError::Target(TargetError::NoDepsInRunContext));
}
// project:task
TargetProjectScope::Id(project_id) => {
TargetScope::Project(project_id) => {
let project = self.project_graph.get(project_id)?;
let task = project.get_task(&target.task_id)?;

Expand All @@ -247,9 +248,11 @@ impl<'ws> DepGraphBuilder<'ws> {
inserted_indexes.insert(index);
}
}
// #tag:task
TargetScope::Tag(_) => todo!(),
// ~:task
TargetProjectScope::OwnSelf => {
target.fail_with(TargetError::NoProjectSelfInRunContext)?;
TargetScope::OwnSelf => {
return Err(DepGraphError::Target(TargetError::NoSelfInRunContext));
}
};

Expand Down
4 changes: 2 additions & 2 deletions crates/core/dep-graph/tests/dep_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ mod run_target {
}

#[tokio::test]
#[should_panic(expected = "Target(NoProjectDepsInRunContext)")]
#[should_panic(expected = "Target(NoDepsInRunContext)")]
async fn errors_for_target_deps_scope() {
let (workspace, projects, _sandbox) = create_project_graph().await;

Expand All @@ -328,7 +328,7 @@ mod run_target {
}

#[tokio::test]
#[should_panic(expected = "Target(NoProjectSelfInRunContext)")]
#[should_panic(expected = "Target(NoSelfInRunContext)")]
async fn errors_for_target_self_scope() {
let (workspace, projects, _sandbox) = create_project_graph().await;

Expand Down
2 changes: 1 addition & 1 deletion crates/core/emitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ moon_cache = { path = "../cache" }
moon_error = { path = "../error" }
moon_platform_runtime = { path = "../platform-runtime" }
moon_project = { path = "../project" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_task = { path = "../task" }
moon_utils = { path = "../utils" }
moon_workspace = { path = "../workspace" }
Expand Down
2 changes: 1 addition & 1 deletion crates/core/project-graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ moon_logger = { path = "../logger" }
moon_platform_detector = { path = "../platform-detector" }
moon_project = { path = "../project" }
moon_query = { path = "../query" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_task = { path = "../task" }
moon_utils = { path = "../utils" }
moon_vcs = { path = "../vcs" }
Expand Down
18 changes: 11 additions & 7 deletions crates/core/project-graph/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use moon_hasher::{convert_paths_to_strings, to_hash};
use moon_logger::{debug, map_list, trace, warn, Logable};
use moon_platform_detector::{detect_project_language, detect_task_platform};
use moon_project::{Project, ProjectDependency, ProjectDependencySource, ProjectError};
use moon_target::{Target, TargetError, TargetProjectScope};
use moon_target::{Target, TargetError, TargetScope};
use moon_task::{Task, TaskError, TaskFlag};
use moon_utils::path::expand_to_workspace_relative;
use moon_utils::regex::{ENV_VAR, ENV_VAR_SUBSTITUTE};
Expand Down Expand Up @@ -324,34 +324,38 @@ impl<'ws> ProjectGraphBuilder<'ws> {
};

for target in &task.deps {
match &target.project {
match &target.scope {
// ^:task
TargetProjectScope::Deps => {
TargetScope::Deps => {
for dep_id in project.get_dependency_ids() {
let dep_index = self.indices.get(dep_id).unwrap();
let dep_project = self.graph.node_weight(*dep_index).unwrap();

if let Some(dep_task) = dep_project.tasks.get(&target.task_id) {
if let Some(dep_task) = dep_project.tasks.get(target.task_id.as_str()) {
push_target(dep_task.target.clone());
}
}
}
// ~:task
TargetProjectScope::OwnSelf => {
TargetScope::OwnSelf => {
if target.task_id != task.id {
push_target(Target::new(&project.id, &target.task_id)?);
}
}
// project:task
TargetProjectScope::Id(project_id) => {
TargetScope::Project(project_id) => {
if project_id == &project.id && target.task_id == task.id {
// Avoid circular references
} else {
push_target(target.clone());
}
}
// #tag:task
TargetScope::Tag(_) => todo!(),
_ => {
target.fail_with(TargetError::NoProjectAllInTaskDeps(target.id.clone()))?;
return Err(ProjectGraphError::Target(TargetError::NoAllInTaskDeps(
target.id.clone(),
)));
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/project-graph/tests/projects_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ mod task_expansion {
}

#[tokio::test]
#[should_panic(expected = "Target(NoProjectAllInTaskDeps(\":build\"))")]
#[should_panic(expected = "Target(NoAllInTaskDeps(\":build\"))")]
async fn errors_for_all_scope() {
tasks_sandbox_with_setup(|sandbox| {
sandbox.create_file(
Expand Down
2 changes: 1 addition & 1 deletion crates/core/project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ moon_constants = { path = "../constants" }
moon_error = { path = "../error" }
moon_logger = { path = "../logger" }
moon_query = { path = "../query" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_task = { path = "../task" }
moon_utils = { path = "../utils" }
rustc-hash = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/core/runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ moon_hasher = { path = "../hasher" }
moon_logger = { path = "../logger" }
moon_platform_runtime = { path = "../platform-runtime" }
moon_project = { path = "../project" }
moon_target = { path = "../target" }
moon_target = { path = "../../../nextgen/target" }
moon_task = { path = "../task" }
moon_tool = { path = "../tool" }
moon_terminal = { path = "../terminal" }
Expand Down
Loading

0 comments on commit 49642b5

Please sign in to comment.