-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
195 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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,56 @@ | ||
use colored::*; | ||
use std::env; | ||
use std::io::{self, BufRead, Error}; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command, Stdio}; | ||
use tokio::io::{AsyncBufReadExt, BufReader}; | ||
use tokio::process::Command as TokioCommand; | ||
|
||
use crate::util::ShellCommand; | ||
use crate::Environment; | ||
|
||
pub async fn execute(working_directory: &str, environment: &Environment) -> Result<(), Error> { | ||
// TODO: Extract these out | ||
let cwd_path = Path::new(working_directory); | ||
let build_native_path = Path::new(&"build-native"); | ||
let build_native_wd = cwd_path.join(build_native_path); | ||
|
||
let build_wasm_path = Path::new(&"build-wasm"); | ||
let build_wasm_wd = cwd_path.join(build_wasm_path); | ||
|
||
let native_build_command = ShellCommand { | ||
prefix: "[2: Build Native]", | ||
cmd: "cargo", | ||
cwd: build_native_wd.to_str().unwrap(), | ||
env: vec![], | ||
args: vec!["build"], | ||
}; | ||
|
||
let wasm_build_command = ShellCommand { | ||
prefix: "[2: Build WASM]", | ||
cmd: "wasm-pack", | ||
cwd: build_wasm_wd.to_str().unwrap(), | ||
env: vec![], | ||
args: vec!["build", "--target=web"], | ||
}; | ||
|
||
let build_command = match environment { | ||
Environment::Native => native_build_command, | ||
Environment::Wasm => wasm_build_command, | ||
}; | ||
|
||
let commands = vec![ | ||
ShellCommand { | ||
prefix: "[1: Build F#]", | ||
cmd: "npm", | ||
cwd: working_directory, | ||
env: vec![], | ||
args: vec!["run", "build:examples:pong:rust"], | ||
}, | ||
build_command, | ||
]; | ||
|
||
ShellCommand::run_sequential(commands).await?; | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 +1,2 @@ | ||
pub mod build; | ||
pub mod develop; |
This file contains 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 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,2 @@ | ||
mod shell_command; | ||
pub use shell_command::*; |
This file contains 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,102 @@ | ||
use colored::*; | ||
use std::env; | ||
use std::io::{self, BufRead, Error}; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command, Stdio}; | ||
use tokio::io::{AsyncBufReadExt, BufReader}; | ||
use tokio::process::Command as TokioCommand; | ||
|
||
pub struct ShellCommand<'a> { | ||
pub prefix: &'a str, | ||
pub cmd: &'a str, | ||
pub cwd: &'a str, | ||
pub args: Vec<&'a str>, | ||
pub env: Vec<(&'a str, &'a str)>, | ||
} | ||
|
||
impl<'A> ShellCommand<'A> { | ||
pub async fn run_sequential(commands: Vec<ShellCommand<'A>>) -> Result<(), Error> { | ||
for command_spec in commands { | ||
println!("Using working dir: {}", &command_spec.cwd); | ||
let mut command = TokioCommand::new(&command_spec.cmd); | ||
println!("-- Running command: {}", &command_spec.cmd); | ||
|
||
// Set environment variables if any | ||
for (key, value) in command_spec.env { | ||
command.env(key, value); | ||
} | ||
|
||
command | ||
.current_dir(Path::new(&command_spec.cwd)) | ||
.args(&command_spec.args) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()); | ||
let mut child = command.spawn()?; | ||
let stdout = child.stdout.take().unwrap(); | ||
let stderr = child.stderr.take().unwrap(); | ||
|
||
// Handle stdout | ||
let stdout_handle = handle_output(command_spec.prefix.to_string(), stdout, true); | ||
|
||
// Handle stderr | ||
let stderr_handle = handle_output(command_spec.prefix.to_string(), stderr, false); | ||
|
||
// Wait for process to finish | ||
tokio::spawn(stdout_handle).await?; | ||
tokio::spawn(stderr_handle).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
pub async fn run_parallel(commands: Vec<ShellCommand<'A>>) -> Result<(), Error> { | ||
let mut handles = vec![]; | ||
for command_spec in commands { | ||
println!("Using working dir: {}", &command_spec.cwd); | ||
let mut command = TokioCommand::new(&command_spec.cmd); | ||
println!("-- Running command: {}", &command_spec.cmd); | ||
|
||
// Set environment variables if any | ||
for (key, value) in command_spec.env { | ||
command.env(key, value); | ||
} | ||
|
||
command | ||
.current_dir(Path::new(&command_spec.cwd)) | ||
.args(&command_spec.args) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()); | ||
let mut child = command.spawn()?; | ||
let stdout = child.stdout.take().unwrap(); | ||
let stderr = child.stderr.take().unwrap(); | ||
|
||
// Handle stdout | ||
let stdout_handle = handle_output(command_spec.prefix.to_string(), stdout, true); | ||
handles.push(tokio::spawn(stdout_handle)); | ||
|
||
// Handle stderr | ||
let stderr_handle = handle_output(command_spec.prefix.to_string(), stderr, false); | ||
handles.push(tokio::spawn(stderr_handle)); | ||
} | ||
|
||
// Wait for all processes to finish | ||
for handle in handles { | ||
handle.await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
async fn handle_output(prefix: String, stream: impl tokio::io::AsyncRead + Unpin, is_stdout: bool) { | ||
let mut reader = BufReader::new(stream).lines(); | ||
|
||
while let Some(line) = reader.next_line().await.unwrap_or(None) { | ||
let colored_prefix = if is_stdout { | ||
prefix.blue() | ||
} else { | ||
prefix.red() | ||
}; | ||
|
||
println!("{}: {}", colored_prefix, line); | ||
} | ||
} |