Skip to content
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

naga-cli: Support reading input from stdin #1701

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::manual_strip)]
#[allow(unused_imports)]
use std::fs;
use std::{error::Error, fmt, path::Path, str::FromStr};
use std::{error::Error, fmt, io::Read, path::Path, str::FromStr};

/// Translate shaders to different formats
#[derive(argh::FromArgs, Debug, Clone)]
Expand Down Expand Up @@ -60,13 +60,20 @@ struct Args {
#[argh(switch)]
keep_coordinate_space: bool,

/// the input file
#[argh(positional)]
input: String,
/// specify file path to process STDIN as
#[argh(option)]
stdin_file_path: Option<String>,

/// the output file. If not specified, only validation will be performed
/// the input and output files.
///
/// First positional argument is the input file. If not specified, the
/// input will be read from stdin. In the case, --stdin-file-path must also
/// be specified.
///
/// The rest arguments are the output files. If not specified, only
/// validation will be performed.
#[argh(positional)]
output: Vec<String>,
files: Vec<String>,
}

/// Newtype so we can implement [`FromStr`] for `BoundsCheckPolicy`.
Expand Down Expand Up @@ -202,8 +209,21 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {

// Parse commandline arguments
let args: Args = argh::from_env();
let input_path = Path::new(&args.input);
let output_paths = args.output;
let (input_path, input) = if let Some(path) = args.files.first() {
let path = Path::new(path);
(path, fs::read(path)?)
} else if let Some(path) = &args.stdin_file_path {
let mut input = vec![];
std::io::stdin().lock().read_to_end(&mut input)?;
(Path::new(path), input)
} else {
return Err(CliError("Input file path is not specified").into());
};
let output_paths = if !args.files.is_empty() {
&args.files[1..]
} else {
&[]
};

// Update parameters from commandline arguments
if let Some(bits) = args.validate {
Expand Down Expand Up @@ -245,11 +265,10 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
.spv_block_ctx_dump_prefix
.map(std::path::PathBuf::from),
};
let input = fs::read(input_path)?;
naga::front::spv::parse_u8_slice(&input, &options).map(|m| (m, None))?
}
"wgsl" => {
let input = fs::read_to_string(input_path)?;
let input = String::from_utf8(input)?;
let result = naga::front::wgsl::parse_str(&input);
match result {
Ok(v) => (v, Some(input)),
Expand All @@ -260,7 +279,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
}
}
ext @ "vert" | ext @ "frag" | ext @ "comp" => {
let input = fs::read_to_string(input_path)?;
let input = String::from_utf8(input)?;
let mut parser = naga::front::glsl::Parser::default();

(
Expand Down