Skip to content

Commit

Permalink
new: Migrate pipeline to new action graph. (#1104)
Browse files Browse the repository at this point in the history
* Update moon.

* Update impl.

* Update pipeline.

* Update commands.

* Remove dep graph.

* Get running.

* Redo graph iterator.

* Get pipeline working.

* Get tests working.

* Fix persistent.

* Renable test.

* Renable test.

* Change method.

* Use a channel.

* Fix dep chain.

* Remove log.
  • Loading branch information
milesj authored Oct 8, 2023
1 parent 0babec4 commit 03a2c73
Show file tree
Hide file tree
Showing 57 changed files with 722 additions and 2,434 deletions.
35 changes: 5 additions & 30 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ crate-type = ["rlib"]
[dependencies]
moon = { path = "../core/moon" }
moon_action_context = { path = "../core/action-context" }
moon_action_graph = { path = "../../nextgen/action-graph" }
moon_action_pipeline = { path = "../core/action-pipeline" }
moon_actions = { path = "../core/actions" }
moon_api = { path = "../../nextgen/api" }
moon_cache = { path = "../../nextgen/cache" }
moon_codegen = { path = "../../nextgen/codegen" }
moon_common = { path = "../../nextgen/common" }
moon_config = { path = "../../nextgen/config" }
moon_dep_graph = { path = "../core/dep-graph" }
moon_lang = { path = "../core/lang" }
moon_node_lang = { path = "../node/lang" }
moon_node_platform = { path = "../node/platform" }
Expand Down
33 changes: 17 additions & 16 deletions crates/cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::queries::touched_files::{query_touched_files, QueryTouchedFilesOption
use ci_env::CiOutput;
use clap::Args;
use itertools::Itertools;
use moon::{build_dep_graph, generate_project_graph};
use moon::{build_action_graph, generate_project_graph};
use moon_action_context::ActionContext;
use moon_action_graph::ActionGraph;
use moon_action_pipeline::Pipeline;
use moon_common::path::WorkspaceRelativePathBuf;
use moon_dep_graph::DepGraph;
use moon_project_graph::ProjectGraph;
use moon_target::Target;
use moon_terminal::safe_exit;
Expand Down Expand Up @@ -162,31 +162,31 @@ fn distribute_targets_across_jobs(
}

/// Generate a dependency graph with the runnable targets.
fn generate_dep_graph(
fn generate_action_graph(
provider: &CiOutput,
project_graph: &ProjectGraph,
targets: &TargetList,
touched_files: &FxHashSet<WorkspaceRelativePathBuf>,
) -> AppResult<DepGraph> {
print_header(provider, "Generating dependency graph");
) -> AppResult<ActionGraph> {
print_header(provider, "Generating action graph");

let mut dep_builder = build_dep_graph(project_graph);
let mut action_graph_builder = build_action_graph(project_graph)?;

// Run dependents to ensure consumers still work correctly
action_graph_builder.include_dependents();

for target in targets {
// Run the target and its dependencies
dep_builder.run_target(target, Some(touched_files))?;

// And also run its dependents to ensure consumers still work correctly
dep_builder.run_dependents_for_target(target)?;
action_graph_builder.run_task_by_target(target, Some(touched_files))?;
}

let dep_graph = dep_builder.build();
let action_graph = action_graph_builder.build()?;

println!("Target count: {}", targets.len());
println!("Action count: {}", dep_graph.get_node_count());
println!("Action count: {}", action_graph.get_node_count());
print_footer(provider);

Ok(dep_graph)
Ok(action_graph)
}

#[system]
Expand All @@ -210,9 +210,10 @@ pub async fn ci(
}

let targets = distribute_targets_across_jobs(&ci_provider, args, targets);
let dep_graph = generate_dep_graph(&ci_provider, &project_graph, &targets, &touched_files)?;
let action_graph =
generate_action_graph(&ci_provider, &project_graph, &targets, &touched_files)?;

if dep_graph.is_empty() {
if action_graph.is_empty() {
println!(
"{}",
color::invalid("No targets to run based on touched files")
Expand All @@ -239,7 +240,7 @@ pub async fn ci(

let results = pipeline
.generate_report("ciReport.json")
.run(dep_graph, Some(context))
.run(action_graph, Some(context))
.await?;

print_footer(&ci_provider);
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/src/commands/docker/setup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::MANIFEST_NAME;
use crate::commands::docker::scaffold::DockerManifest;
use moon::{build_dep_graph, generate_project_graph};
use moon::{build_action_graph, generate_project_graph};
use moon_action_pipeline::Pipeline;
use moon_terminal::safe_exit;
use moon_workspace::Workspace;
Expand All @@ -18,17 +18,17 @@ pub async fn setup(workspace: ResourceMut<Workspace>) {

let manifest: DockerManifest = json::read_file(manifest_path)?;
let project_graph = generate_project_graph(workspace).await?;
let mut dep_builder = build_dep_graph(&project_graph);
let mut action_graph_builder = build_action_graph(&project_graph)?;

for project_id in &manifest.focused_projects {
let project = project_graph.get(project_id)?;

dep_builder.install_deps(&project, None)?;
action_graph_builder.install_deps(&project, None)?;
}

let dep_graph = dep_builder.build();
let action_graph = action_graph_builder.build()?;

Pipeline::new(workspace.to_owned(), project_graph)
.run(dep_graph, None)
.run(action_graph, None)
.await?;
}
19 changes: 12 additions & 7 deletions crates/cli/src/commands/graph/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::graph::utils::{action_graph_repr, respond_to_request, setup_server};
use clap::Args;
use miette::IntoDiagnostic;
use moon::{build_dep_graph, generate_project_graph};
use moon::{build_action_graph, generate_project_graph};
use moon_target::TargetLocator;
use moon_workspace::Workspace;
use starbase::{system, SystemResult};
Expand All @@ -11,6 +11,9 @@ pub struct ActionGraphArgs {
#[arg(help = "Target to *only* graph")]
target: Option<TargetLocator>,

#[arg(long, help = "Include dependents of the focused target")]
dependents: bool,

#[arg(long, help = "Print the graph in DOT format")]
dot: bool,

Expand All @@ -23,24 +26,26 @@ pub async fn internal_action_graph(
workspace: &mut Workspace,
) -> SystemResult {
let project_graph = generate_project_graph(workspace).await?;
let mut action_graph_builder = build_dep_graph(&project_graph);
let mut action_graph_builder = build_action_graph(&project_graph)?;

// Focus a target and its dependencies/dependents
if let Some(locator) = args.target.clone() {
for target in action_graph_builder.run_targets_by_locator(&[locator], None)? {
action_graph_builder.run_dependents_for_target(&target)?;
if args.dependents {
action_graph_builder.include_dependents();
}

action_graph_builder.run_task_by_target_locator(locator, None)?;

// Show all targets and actions
} else {
for project in project_graph.get_all_unexpanded() {
for project in project_graph.get_all()? {
for task in project.tasks.values() {
action_graph_builder.run_target(&task.target, None)?;
action_graph_builder.run_task(&project, task, None)?;
}
}
}

let action_graph = action_graph_builder.build();
let action_graph = action_graph_builder.build()?;

if args.dot {
println!("{}", action_graph.to_dot());
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/commands/graph/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::dto::{GraphEdgeDto, GraphInfoDto, GraphNodeDto};
use miette::IntoDiagnostic;
use moon_dep_graph::DepGraph;
use moon_action_graph::ActionGraph;
use moon_project_graph::ProjectGraph;
use petgraph::{graph::NodeIndex, Graph};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -84,8 +84,8 @@ pub async fn project_graph_repr(project_graph: &ProjectGraph) -> GraphInfoDto {
}

/// Get a serialized representation of the dependency graph.
pub async fn action_graph_repr(dep_graph: &DepGraph) -> GraphInfoDto {
let labeled_graph = dep_graph.labeled_graph();
pub async fn action_graph_repr(action_graph: &ActionGraph) -> GraphInfoDto {
let labeled_graph = action_graph.labeled_graph();
extract_nodes_and_edges_from_graph(&labeled_graph, false)
}

Expand Down
51 changes: 27 additions & 24 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::helpers::map_list;
use crate::queries::touched_files::{query_touched_files, QueryTouchedFilesOptions};
use clap::Args;
use miette::miette;
use moon::{build_dep_graph, generate_project_graph};
use moon::{build_action_graph, generate_project_graph};
use moon_action_context::{ActionContext, ProfileType};
use moon_action_pipeline::Pipeline;
use moon_common::is_test_env;
Expand All @@ -26,10 +26,7 @@ pub struct RunArgs {
#[arg(required = true, help = "List of targets to run")]
pub targets: Vec<TargetLocator>,

#[arg(
long,
help = "Run dependents of the primary targets, as well as dependencies"
)]
#[arg(long, help = "Run dependents of the primary targets")]
pub dependents: bool,

#[arg(
Expand Down Expand Up @@ -136,21 +133,34 @@ pub async fn run_target(
};

// Generate a dependency graph for all the targets that need to be ran
let mut dep_builder = build_dep_graph(&project_graph);
let mut action_graph_builder = build_action_graph(&project_graph)?;

// Run dependents for all primary targets
if args.dependents {
action_graph_builder.include_dependents();
}

if let Some(query_input) = &args.query {
dep_builder.set_query(query_input)?;
action_graph_builder.set_query(query_input)?;
}

// Run targets, optionally based on affected files
let primary_targets = dep_builder.run_targets_by_locator(
target_locators,
if should_run_affected {
Some(&touched_files)
} else {
None
},
)?;
let mut primary_targets = vec![];

for locator in target_locators {
primary_targets.extend(
action_graph_builder
.run_task_by_target_locator(
locator,
if should_run_affected {
Some(&touched_files)
} else {
None
},
)?
.0,
);
}

if primary_targets.is_empty() {
let targets_list = map_list(target_locators, |id| color::label(id));
Expand Down Expand Up @@ -183,13 +193,6 @@ pub async fn run_target(
));
}

// Run dependents for all primary targets
if args.dependents {
for target in &primary_targets {
dep_builder.run_dependents_for_target(target)?;
}
}

// Process all tasks in the graph
let context = ActionContext {
affected_only: should_run_affected,
Expand All @@ -203,7 +206,7 @@ pub async fn run_target(
..ActionContext::default()
};

let dep_graph = dep_builder.build();
let action_graph = action_graph_builder.build()?;
let mut pipeline = Pipeline::new(workspace.to_owned(), project_graph);

if let Some(concurrency) = concurrency {
Expand All @@ -213,7 +216,7 @@ pub async fn run_target(
let results = pipeline
.bail_on_error()
.generate_report("runReport.json")
.run(dep_graph, Some(context))
.run(action_graph, Some(context))
.await?;

pipeline.render_stats(&results, true)?;
Expand Down
Loading

0 comments on commit 03a2c73

Please sign in to comment.