Skip to content

Commit

Permalink
feat: retry if missing binary
Browse files Browse the repository at this point in the history
Retry using other method if missing binary
  • Loading branch information
hougesen committed Mar 9, 2024
1 parent ad01c66 commit 2e0c258
Show file tree
Hide file tree
Showing 33 changed files with 215 additions and 118 deletions.
19 changes: 10 additions & 9 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_biome(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_biome(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
// TODO: use installed biome instead
let mut cmd = std::process::Command::new("npx");

// Incase the use hasn't installed biome
// Incase the user hasn't installed biome
cmd.arg("--yes")
.arg("@biomejs/biome")
.arg("format")
.arg("--write")
.arg(file_path);
.arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand Down Expand Up @@ -52,6 +50,7 @@ mod test_biome {

let output = format_using_biome(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand All @@ -78,6 +77,7 @@ mod test_biome {

let output = format_using_biome(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down Expand Up @@ -107,6 +107,7 @@ number>

let output = format_using_biome(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
16 changes: 7 additions & 9 deletions src/formatters/gleam_format.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_gleam_format(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_gleam_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("gleam");

// Incase the use hasn't installed biome
cmd.arg("format").arg(file_path);
cmd.arg("format").arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand All @@ -34,6 +31,7 @@ mod test_gleam_format {

let output = format_using_gleam_format(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
16 changes: 7 additions & 9 deletions src/formatters/mix_format.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_mix_format(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_mix_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("mix");

// Incase the use hasn't installed biome
cmd.arg("format").arg(file_path);
cmd.arg("format").arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand All @@ -37,6 +34,7 @@ end

let output = format_using_mix_format(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
29 changes: 28 additions & 1 deletion src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,26 @@ pub fn read_snippet(file_path: &std::path::Path) -> std::io::Result<String> {
}

#[inline]
pub fn execute_command(cmd: &mut Command) -> std::io::Result<bool> {
fn handle_post_execution(
result: std::io::Result<bool>,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
if let Err(err) = result {
if err.kind() == std::io::ErrorKind::NotFound {
return Ok((true, None));
}

return Err(err);
}

if matches!(result, Ok(true)) {
return read_snippet(snippet_path).map(|code| (false, Some(code)));
}

Ok((false, None))
}

fn spawn_command(cmd: &mut Command) -> std::io::Result<bool> {
Ok(cmd
.stdout(Stdio::null())
.stderr(Stdio::null())
Expand All @@ -50,6 +69,14 @@ pub fn execute_command(cmd: &mut Command) -> std::io::Result<bool> {
.success())
}

#[inline]
pub fn execute_command(
cmd: &mut Command,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
handle_post_execution(spawn_command(cmd), snippet_path)
}

#[inline]
pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> String {
if let Ok(snippet) = setup_snippet(code, language.to_file_ext()) {
Expand Down
15 changes: 7 additions & 8 deletions src/formatters/nimpretty.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_nimpretty(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_nimpretty(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("nimpretty");

cmd.arg(file_path);
cmd.arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand All @@ -34,6 +32,7 @@ mod test_nimpretty {

let output = format_using_nimpretty(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
56 changes: 44 additions & 12 deletions src/formatters/prettier.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
use super::{execute_command, read_snippet};
use crate::runners::npx::new_npx_cmd;

use super::execute_command;

#[inline]
pub fn format_using_prettier(
fn set_prettier_args(
cmd: &mut std::process::Command,
snippet_path: &std::path::Path,
embedded_language_formatting: bool,
) -> std::io::Result<Option<String>> {
// TODO: use installed prettier instead
let mut cmd = std::process::Command::new("npx");

// Incase the use hasn't installed prettier
cmd.arg("--yes").arg("prettier");

) {
if !embedded_language_formatting {
cmd.arg("--embedded-language-formatting").arg("off");
}

cmd.arg("--write").arg(snippet_path);
}

if execute_command(&mut cmd)? {
return read_snippet(snippet_path).map(Some);
#[inline]
fn invoke_prettier(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
embedded_language_formatting: bool,
) -> std::io::Result<(bool, Option<String>)> {
set_prettier_args(&mut cmd, snippet_path, embedded_language_formatting);

execute_command(&mut cmd, snippet_path)
}

#[inline]
pub fn format_using_prettier(
snippet_path: &std::path::Path,
embedded_language_formatting: bool,
) -> std::io::Result<(bool, Option<String>)> {
let path_result = invoke_prettier(
std::process::Command::new("prettier"),
snippet_path,
embedded_language_formatting,
)?;

if !path_result.0 {
return Ok(path_result);
}

Ok(None)
invoke_prettier(
new_npx_cmd("prettier"),
snippet_path,
embedded_language_formatting,
)
}

#[cfg(test)]
Expand Down Expand Up @@ -57,6 +81,7 @@ mod test_prettier {

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand All @@ -83,6 +108,7 @@ mod test_prettier {

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down Expand Up @@ -112,6 +138,7 @@ number>

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand All @@ -138,6 +165,7 @@ this is a paragraph

let output = format_using_prettier(snippet.path(), false)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand All @@ -164,6 +192,7 @@ number>

let output = format_using_prettier(snippet.path(), false)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(input, output);
Expand Down Expand Up @@ -200,6 +229,7 @@ number>

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand All @@ -222,6 +252,7 @@ p {

let output = format_using_prettier(snippet.path(), true)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down Expand Up @@ -277,6 +308,7 @@ updates:

let output = format_using_prettier(snippet.path(), false)
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
16 changes: 7 additions & 9 deletions src/formatters/ruff.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_ruff(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_ruff(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("ruff");

cmd.arg("format");
cmd.arg("--quiet");
cmd.arg("--no-cache");
cmd.arg(file_path);
cmd.arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand All @@ -33,6 +30,7 @@ mod test_ruff {

let output = format_using_ruff(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
15 changes: 7 additions & 8 deletions src/formatters/rustfmt.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use super::{execute_command, read_snippet};
use super::execute_command;

#[inline]
pub fn format_using_rustfmt(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
pub fn format_using_rustfmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("rustfmt");

// Needed for async
cmd.arg("--edition").arg("2021");

cmd.arg(file_path);
cmd.arg(snippet_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
Expand All @@ -37,6 +35,7 @@ mod test_rustfmt {

let output = format_using_rustfmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
Expand Down
Loading

0 comments on commit 2e0c258

Please sign in to comment.