-
Notifications
You must be signed in to change notification settings - Fork 0
Invoke ninja subprocess #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6beb3a0
Invoke ninja subprocess
leynos 7d861d5
Stream Ninja output without buffering
leynos 9bb8cbb
Skip visited nodes during cycle detection
leynos f52e184
Share fake Ninja helper and document steps
leynos 386a0bf
Refine cycle check and tighten lint expectations
leynos bc9047a
Document world fields and simplify process steps
leynos 1a4cb9d
Refactor PATH handling in process steps
leynos 32e563c
Fix manifest example doctest
leynos 119db99
Refine process test path handling
leynos 987a5ac
Apply formatting
leynos baab381
Fix collapsible if statement
leynos 17c45de
Enable crush
leynos 29f3e13
Simplify ci workflow
leynos 826876c
Run tests in isolation
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| target/ | ||
| **/*.rs.bk | ||
| .crush |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| AGENTS.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| //! Application entry point. | ||
| //! | ||
| //! Parses command-line arguments and delegates execution to [`runner::run`]. | ||
|
|
||
| use netsuke::{cli::Cli, runner}; | ||
| use std::process::ExitCode; | ||
|
|
||
| fn main() { | ||
| fn main() -> ExitCode { | ||
| let cli = Cli::parse_with_default(); | ||
| runner::run(cli); | ||
| match runner::run(&cli) { | ||
| Ok(()) => ExitCode::SUCCESS, | ||
| Err(err) => { | ||
| eprintln!("{err}"); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,96 @@ | ||
| //! CLI execution and command dispatch logic. | ||
| //! | ||
| //! This module keeps [`main`] minimal by providing a single entry point that | ||
| //! handles command execution. It currently prints which command was invoked. | ||
| //! handles command execution. It now delegates build requests to the Ninja | ||
| //! subprocess, streaming its output back to the user. | ||
|
|
||
| use crate::cli::{Cli, Commands}; | ||
| use std::io::{self, BufRead, BufReader, Write}; | ||
| use std::path::Path; | ||
| use std::process::{Command, Stdio}; | ||
| use std::thread; | ||
|
|
||
| /// Execute the parsed [`Cli`] commands. | ||
| pub fn run(cli: Cli) { | ||
| match cli.command.unwrap_or(Commands::Build { | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an [`io::Error`] if the Ninja process fails to spawn or exits with a | ||
| /// non-zero status code. | ||
| pub fn run(cli: &Cli) -> io::Result<()> { | ||
| let command = cli.command.clone().unwrap_or(Commands::Build { | ||
| targets: Vec::new(), | ||
| }) { | ||
| Commands::Build { targets } => { | ||
| println!("Building targets: {targets:?}"); | ||
| } | ||
| }); | ||
| match command { | ||
| Commands::Build { targets } => run_ninja(Path::new("ninja"), cli, &targets), | ||
| Commands::Clean => { | ||
| println!("Clean requested"); | ||
| Ok(()) | ||
| } | ||
| Commands::Graph => { | ||
| println!("Graph requested"); | ||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Invoke the Ninja executable with the provided CLI settings. | ||
| /// | ||
| /// The function forwards the job count and working directory to Ninja and | ||
| /// streams its standard output and error back to the user. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an [`io::Error`] if the Ninja process fails to spawn or reports a | ||
| /// non-zero exit status. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the child's output streams cannot be captured. | ||
| pub fn run_ninja(program: &Path, cli: &Cli, targets: &[String]) -> io::Result<()> { | ||
| let mut cmd = Command::new(program); | ||
| if let Some(dir) = &cli.directory { | ||
| cmd.current_dir(dir).arg("-C").arg(dir); | ||
| } | ||
| if let Some(jobs) = cli.jobs { | ||
| cmd.arg("-j").arg(jobs.to_string()); | ||
| } | ||
| cmd.args(targets); | ||
| cmd.stdout(Stdio::piped()); | ||
| cmd.stderr(Stdio::piped()); | ||
|
|
||
| let mut child = cmd.spawn()?; | ||
| let stdout = child.stdout.take().expect("child stdout"); | ||
| let stderr = child.stderr.take().expect("child stderr"); | ||
|
|
||
| let out_handle = thread::spawn(move || { | ||
| let reader = BufReader::new(stdout); | ||
| let mut handle = io::stdout(); | ||
| for line in reader.lines().map_while(Result::ok) { | ||
| let _ = writeln!(handle, "{line}"); | ||
| } | ||
| }); | ||
| let err_handle = thread::spawn(move || { | ||
| let reader = BufReader::new(stderr); | ||
| let mut handle = io::stderr(); | ||
| for line in reader.lines().map_while(Result::ok) { | ||
| let _ = writeln!(handle, "{line}"); | ||
| } | ||
| }); | ||
|
|
||
| let status = child.wait()?; | ||
| let _ = out_handle.join(); | ||
| let _ = err_handle.join(); | ||
|
|
||
| if status.success() { | ||
| Ok(()) | ||
| } else { | ||
| #[expect( | ||
| clippy::io_other_error, | ||
| reason = "use explicit error kind for compatibility with older Rust" | ||
| )] | ||
| Err(io::Error::new( | ||
| io::ErrorKind::Other, | ||
| format!("ninja exited with {status}"), | ||
| )) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Feature: Ninja process execution | ||
|
|
||
| Scenario: Ninja succeeds | ||
| Given a fake ninja executable that exits with 0 | ||
| And the CLI is parsed with "" | ||
| When the ninja process is run | ||
| Then the command should succeed | ||
|
|
||
| Scenario: Ninja fails | ||
| Given a fake ninja executable that exits with 1 | ||
| And the CLI is parsed with "" | ||
| When the ninja process is run | ||
| Then the command should fail with error "ninja exited with exit status: 1" | ||
|
|
||
| Scenario: Ninja missing | ||
| Given no ninja executable is available | ||
| And the CLI is parsed with "" | ||
| When the ninja process is run | ||
| Then the command should fail with error "No such file or directory" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //! Unit tests for Ninja process invocation. | ||
|
|
||
| use netsuke::cli::{Cli, Commands}; | ||
| use netsuke::runner; | ||
| use rstest::rstest; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| /// Creates a default CLI configuration for testing Ninja invocation. | ||
| fn test_cli() -> Cli { | ||
| Cli { | ||
| file: PathBuf::from("Netsukefile"), | ||
| directory: None, | ||
| jobs: None, | ||
| command: Some(Commands::Build { | ||
| targets: Vec::new(), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| mod support; | ||
|
|
||
| #[rstest] | ||
| #[case(0, true)] | ||
| #[case(1, false)] | ||
| fn run_ninja_status(#[case] code: i32, #[case] succeeds: bool) { | ||
| let (_dir, path) = support::fake_ninja(code); | ||
| let cli = test_cli(); | ||
| let result = runner::run_ninja(&path, &cli, &[]); | ||
| assert_eq!(result.is_ok(), succeeds); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn run_ninja_not_found() { | ||
| let cli = test_cli(); | ||
| let err = | ||
| runner::run_ninja(Path::new("does-not-exist"), &cli, &[]).expect_err("process should fail"); | ||
| assert_eq!(err.kind(), std::io::ErrorKind::NotFound); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ mod cli_steps; | |
| mod ir_steps; | ||
| mod manifest_steps; | ||
| mod ninja_steps; | ||
| mod process_steps; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.