forked from amber-lang/amber
-
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.
* init: postprocessors * init: bshchk postprocessor * fix: tests failing * docs: mention bshchk in readme * refactor: change --disable-postprocessors arg name and properly explain how to pass it * tests: use the proper argument name in cli test * feat: filter postprocessors with a wildcard * tests: postprocessor * fix: clippy * fix: disable postprocessors in tests * fix: merge issue * refactor: remove input/output enums * test: disable all postprocessors in most tests * tests: disable postprocessors by wildcard in tests::cli * tests: fix tests::postprocessor * remove useless section from readme * revert variable name * rewrite only to have a filter_default method * pipe stdin,out,err to rust * always remove file in all_exist test * remove useless function * rewrite postprocessor test to be simpler but require all default postprocessors to be present * assert that output is the same as well * do not clone postprocessor where it doesnt have to be * remove formatter.rs * fix header * do not use hashmap in PostProcessor::get_default * change --disable-postprocessor to --no-proc and edit help message to be more explanatory * put --no-proc help string in a comment above it * fix type * remove useless inner scopes * fix filtering * use Rc<RefCell> instead of Arc<Mutex> * install bshchk in ci * fix perms * make clippy happy * make clippy even more happier * return false if kill failed * dont pad blocks --------- Co-authored-by: Huw Walters <[email protected]>
- Loading branch information
Showing
14 changed files
with
191 additions
and
142 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,92 @@ | ||
use std::cell::RefCell; | ||
use std::io::{BufWriter, Write}; | ||
use std::path::PathBuf; | ||
use std::process::{Command, Stdio}; | ||
use std::rc::Rc; | ||
|
||
use itertools::Itertools; | ||
use wildmatch::WildMatchPattern; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct PostProcessor { | ||
pub name: String, | ||
pub bin: PathBuf, | ||
command: Rc<RefCell<Command>>, | ||
} | ||
|
||
impl PostProcessor { | ||
pub fn new<N: Into<String>, B: Into<PathBuf>>(name: N, bin: B) -> Self { | ||
let name: String = name.into(); | ||
let bin: PathBuf = bin.into(); | ||
let mut command = Command::new(bin.clone()); | ||
command.stdin(Stdio::piped()); | ||
command.stdout(Stdio::piped()); | ||
command.stderr(Stdio::piped()); | ||
let command = Rc::new(RefCell::new(command)); | ||
Self { | ||
name, | ||
bin, | ||
command, | ||
} | ||
} | ||
|
||
pub fn cmd(&self) -> Rc<RefCell<Command>> { | ||
self.command.clone() | ||
} | ||
|
||
pub fn is_available(&self) -> bool { | ||
match Command::new(self.bin.clone()).spawn() { | ||
Ok(mut v) => { | ||
v.kill().is_ok() | ||
}, | ||
Err(_) => false | ||
} | ||
} | ||
|
||
pub fn execute(&self, code: String) -> Result<String, Box<dyn std::error::Error>> { | ||
if !self.is_available() { return Ok(code) } | ||
|
||
let mut spawned = self.cmd().borrow_mut().spawn()?; | ||
|
||
// send to stdin | ||
if let Some(stdin) = spawned.stdin.as_mut() { | ||
let mut writer = BufWriter::new(stdin); | ||
writer.write_all(code.as_bytes())?; | ||
writer.flush()?; | ||
} else { | ||
return Err(String::new().into()) | ||
} | ||
|
||
// read from stdout | ||
let res = spawned.wait_with_output()?; | ||
Ok(String::from_utf8(res.stdout)?) | ||
} | ||
|
||
pub fn get_default() -> Vec<Self> { | ||
let mut postprocessors = Vec::new(); | ||
|
||
let shfmt = PostProcessor::new("shfmt", "/usr/bin/shfmt"); | ||
shfmt.cmd().borrow_mut().arg("-i").arg("4"); | ||
shfmt.cmd().borrow_mut().arg("-ln").arg("bash"); | ||
postprocessors.push(shfmt); | ||
|
||
let bshchk = PostProcessor::new("bshchk", "/usr/bin/bshchk"); | ||
bshchk.cmd().borrow_mut().arg("--ignore-shebang"); | ||
postprocessors.push(bshchk); | ||
|
||
postprocessors | ||
} | ||
|
||
pub fn filter_default(filters: Vec<WildMatchPattern<'*', '?'>>) -> Vec<Self> { | ||
let default = Self::get_default(); | ||
|
||
default | ||
.iter() | ||
.filter(|x| { | ||
filters.iter() | ||
.all(|xx| !xx.matches(&x.name)) | ||
}) | ||
.cloned() | ||
.collect_vec() | ||
} | ||
} |
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,2 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Written in [Amber](https://amber-lang.com/) | ||
# Written in [Amber](https://amber-lang.com/) | ||
# version: {{ version }} | ||
# date: {{ date }} |
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 was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.