-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add shfmt compatibility * add formatter tests * add BashFormatter::as_cmd method * Update src/modules/formatter.rs Co-authored-by: Phoenix Himself <pkaras.it@gmail.com> * refactor formatter.rs a little bit * fix(rust): some changes * fix(rust): some changes * feat(readme): added line * fix(merge): previous * Update src/compiler.rs Co-authored-by: Hubert Jabłoński <hubik080@gmail.com> * Update src/modules/formatter.rs Co-authored-by: Hubert Jabłoński <hubik080@gmail.com> * Update src/modules/formatter.rs Co-authored-by: Hubert Jabłoński <hubik080@gmail.com> --------- Co-authored-by: Phoenix Himself <pkaras.it@gmail.com> Co-authored-by: Daniele Scasciafratte <mte90net@gmail.com> Co-authored-by: Hubert Jabłoński <hubik080@gmail.com>
- Loading branch information
1 parent
969d06c
commit 03c6be2
Showing
10 changed files
with
176 additions
and
20 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
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,75 @@ | ||
use std::{io::{BufWriter, Write}, process::{Command, Stdio}}; | ||
|
||
|
||
/// This mechanism is built to support multiple formatters. | ||
/// | ||
/// The idea is that amber should find the one installed, verify that its compatible and use the best one possible. | ||
#[derive(Debug, Clone, Copy)] | ||
#[allow(non_camel_case_types)] | ||
pub enum BashFormatter { | ||
/// https://github.com/mvdan/sh | ||
shfmt | ||
} | ||
|
||
impl BashFormatter { | ||
/// Get all available formatters, ordered: best ones at the start, worst at the end | ||
pub fn get_all() -> Vec<BashFormatter> { | ||
vec![ | ||
BashFormatter::shfmt | ||
] | ||
} | ||
|
||
/// Get available formatter | ||
pub fn get_available() -> Option<BashFormatter> { | ||
Self::get_all() | ||
.iter() | ||
.find(|fmt| fmt.is_available()) | ||
.map(|fmt| *fmt) | ||
} | ||
|
||
/// Check if current formatter is present in $PATH | ||
pub fn is_available(self: &Self) -> bool { | ||
match self { | ||
BashFormatter::shfmt => | ||
Command::new("shfmt") | ||
.arg("--version") | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
.map(|mut x| x.wait()) | ||
.is_ok() | ||
} | ||
} | ||
|
||
#[allow(dead_code)] // used in tests | ||
pub fn as_cmd<T: From<&'static str>>(self: &Self) -> T { | ||
match self { | ||
BashFormatter::shfmt => "shfmt".into() | ||
} | ||
} | ||
|
||
/// Format code using the formatter | ||
pub fn format(self: &Self, code: String) -> String { | ||
match self { | ||
BashFormatter::shfmt => { | ||
let mut command = Command::new("shfmt") | ||
.stdout(Stdio::piped()) | ||
.stdin(Stdio::piped()) | ||
.arg("-i").arg("4") // indentation | ||
.arg("-ln").arg("bash") // language | ||
.spawn().expect("Couldn't spawn shfmt"); | ||
|
||
{ | ||
let cmd_stdin = command.stdin.as_mut().expect("Couldn't get shfmt's stdin"); | ||
let mut writer = BufWriter::new(cmd_stdin); | ||
writer.write_all(code.as_bytes()).expect("Couldn't write code to shfmt"); | ||
writer.flush().expect("Couldn't flush shfmt's stdin"); | ||
} | ||
|
||
let res = command.wait_with_output().expect("Couldn't wait for shfmt"); | ||
|
||
String::from_utf8(res.stdout).expect("shfmt returned non utf-8 output") | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
use std::{env, fs::{self, Permissions}, os::unix::fs::PermissionsExt}; | ||
|
||
use crate::modules::formatter::BashFormatter; | ||
|
||
fn create_fake_binary(fmt: BashFormatter) { | ||
let body = if cfg!(unix) { | ||
"#!/usr/bin/env bash\nexit 0" | ||
} else { | ||
panic!("this test is not available for non-unix platforms") | ||
}; | ||
|
||
let name: String = fmt.as_cmd(); | ||
|
||
fs::write(&name, body).expect("Couldn't write fake script"); | ||
fs::set_permissions(&name, Permissions::from_mode(0o755)).expect("Couldn't set perms for fake script"); | ||
} | ||
|
||
#[test] | ||
fn all_exist() { | ||
let path = env::var("PATH").expect("Cannot get $PATH"); | ||
|
||
env::set_var("PATH", format!("{path}:./")); // temporary unset to ensure that shfmt exists in $PATH | ||
let fmts = BashFormatter::get_all(); | ||
for fmt in fmts { | ||
create_fake_binary(fmt); | ||
assert_eq!(fmt.is_available(), true); | ||
assert_eq!(BashFormatter::get_available().is_some(), true); | ||
fs::remove_file(fmt.as_cmd::<String>()).expect("Couldn't remove formatter's fake binary"); | ||
} | ||
|
||
env::set_var("PATH", &path); | ||
assert_eq!(env::var("PATH").expect("Cannot get $PATH"), path); | ||
} |
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,19 +1,21 @@ | ||
use crate::compiler::AmberCompiler; | ||
use crate::Cli; | ||
|
||
pub mod cli; | ||
pub mod formatter; | ||
pub mod stdlib; | ||
pub mod validity; | ||
|
||
#[macro_export] | ||
macro_rules! test_amber { | ||
($code:expr, $result:expr) => {{ | ||
match AmberCompiler::new($code.to_string(), None).test_eval() { | ||
match AmberCompiler::new($code.to_string(), None, Cli::default()).test_eval() { | ||
Ok(result) => assert_eq!(result.trim_end_matches('\n'), $result), | ||
Err(err) => panic!("ERROR: {}", err.message.unwrap()), | ||
} | ||
}}; | ||
} | ||
|
||
pub fn compile_code<T: Into<String>>(code: T) -> String { | ||
AmberCompiler::new(code.into(), None).compile().unwrap().1 | ||
AmberCompiler::new(code.into(), None, Cli::default()).compile().unwrap().1 | ||
} |
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