Merge remote changes
This commit is contained in:
commit
69f1bf3c4b
23 changed files with 163 additions and 478 deletions
1
cli/.gitignore
vendored
Normal file
1
cli/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
10
cli/Cargo.toml
Normal file
10
cli/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "hyperbuild-cli"
|
||||
publish = false
|
||||
version = "0.1.1"
|
||||
authors = ["Wilson Lin <code@wilsonl.in>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
hyperbuild = { path = ".." }
|
||||
structopt = "0.3.5"
|
||||
55
cli/src/main.rs
Normal file
55
cli/src/main.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use std::fs::File;
|
||||
use std::io::{Read, stdin, stdout, Write};
|
||||
|
||||
use structopt::StructOpt;
|
||||
|
||||
use hyperbuild::{FriendlyError, hyperbuild_friendly_error};
|
||||
|
||||
#[derive(StructOpt)]
|
||||
struct Cli {
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
src: Option<std::path::PathBuf>,
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
out: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
macro_rules! io_expect {
|
||||
($expr:expr, $msg:literal) => {
|
||||
match $expr {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", $msg);
|
||||
eprintln!("{}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Cli::from_args();
|
||||
let mut code = Vec::<u8>::new();
|
||||
let mut src_file: Box<dyn Read> = match args.src {
|
||||
Some(p) => Box::new(io_expect!(File::open(p), "could not open source file")),
|
||||
None => Box::new(stdin()),
|
||||
};
|
||||
io_expect!(src_file.read_to_end(&mut code), "could not load source code");
|
||||
match hyperbuild_friendly_error(&mut code) {
|
||||
Ok(out_len) => {
|
||||
let mut out_file: Box<dyn Write> = match args.out {
|
||||
Some(p) => Box::new(io_expect!(File::create(p), "could not open output file")),
|
||||
None => Box::new(stdout()),
|
||||
};
|
||||
io_expect!(out_file.write_all(&code[..out_len]), "could not save minified code");
|
||||
}
|
||||
Err(FriendlyError { position, message, code_context }) => {
|
||||
eprintln!("Failed at character {}:", position);
|
||||
eprintln!("{}", message);
|
||||
if args.out.is_some() {
|
||||
eprintln!("The output file has not been touched.");
|
||||
};
|
||||
eprintln!("--------");
|
||||
eprintln!("{}", code_context);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in a new issue