Skip to content

Commit

Permalink
Allow the sixtyfps-compiler and sixtyfps-viewer to read from stdin
Browse files Browse the repository at this point in the history
By specifing `-` as a path
  • Loading branch information
ogoffart committed Sep 9, 2021
1 parent 011a9ff commit fe015a8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.

### Added

- sixtyfps-compiler and sixtyfps-viewer can read the .60 file content from stdin by passing `-`

### Fixed

## [0.1.2] - 2021-09-09
Expand Down
13 changes: 12 additions & 1 deletion sixtyfps_compiler/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Please contact info@sixtyfps.io for more information.
LICENSE END */

use std::io::Read;
use std::path::{Path, PathBuf};
use std::rc::Rc;

Expand Down Expand Up @@ -115,7 +116,17 @@ impl SourceFileInner {
pub type SourceFile = Rc<SourceFileInner>;

pub fn load_from_path(path: &Path) -> Result<String, Diagnostic> {
std::fs::read_to_string(path).map_err(|err| Diagnostic {
(if path == Path::new("-") {
let mut buffer = Vec::new();
let r = std::io::stdin().read_to_end(&mut buffer);
r.and_then(|_| {
String::from_utf8(buffer)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))
})
} else {
std::fs::read_to_string(path)
})
.map_err(|err| Diagnostic {
message: format!("Could not load {}: {}", path.display(), err),
span: SourceLocation {
source_file: Some(SourceFileInner::from_path_only(path.to_owned())),
Expand Down
2 changes: 2 additions & 0 deletions sixtyfps_runtime/interpreter/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ impl ComponentCompiler {
///
/// Diagnostics from previous calls are cleared when calling this function.
///
/// If the path is `"-"`, the file will be read from stdin.
///
/// This function is `async` but in practice, this is only asynchronous if
/// [`Self::set_file_loader`] was called and its future is actually asynchronous.
/// If that is not used, then it is fine to use a very simple executor, such as the one
Expand Down
2 changes: 1 addition & 1 deletion tools/compiler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Cli {
#[structopt(short = "I", name = "include path", number_of_values = 1)]
include_paths: Vec<std::path::PathBuf>,

/// Path to .60 file
/// Path to .60 file ('-' for stdin)
#[structopt(name = "file", parse(from_os_str))]
path: std::path::PathBuf,

Expand Down
1 change: 1 addition & 0 deletions tools/viewer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Cli {
#[structopt(short = "I", name = "include path for other .60 files", number_of_values = 1)]
include_paths: Vec<std::path::PathBuf>,

/// The .60 file to load ('-' for stdin)
#[structopt(name = "path to .60 file", parse(from_os_str))]
path: std::path::PathBuf,

Expand Down

0 comments on commit fe015a8

Please sign in to comment.