Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(yaml): add support for yamlfix #170

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ jobs:
run: taplo --version

- name: ruff
run: pip install ruff && ruff --version
run: pipx install ruff && ruff --version

- name: sqlfluff
run: pip install sqlfluff && sqlfluff format --version
run: pipx install sqlfluff && sqlfluff format --version

- name: biome
run: npx --yes @biomejs/biome --version
Expand Down Expand Up @@ -151,25 +151,25 @@ jobs:
run: go install mvdan.cc/gofumpt@latest && gofumpt --version

- name: black
run: pip install black && black --version
run: pipx install black && black --version

- name: blue
run: pip install blue && blue --version
run: pipx install blue && blue --version

- name: yapf
run: pip install yapf && yapf --version
run: pipx install yapf && yapf --version

- name: autopep
run: pip install autopep8 && autopep8 --version
run: pipx install autopep8 && autopep8 --version

- name: clang-format
run: pip install clang-format && clang-format --version
run: pipx install clang-format && clang-format --version

- name: isort
run: pip install isort
run: pipx install isort && isort --version

- name: usort
run: pip install usort
run: pipx install usort && usort --version

#- name: ocamlformat
# run: eval $(opam env) && opam install ocamlformat && eval $(opam env) && ocamlformat --version
Expand All @@ -181,16 +181,16 @@ jobs:
run: crystal tool format --help

- name: perltidy
run: sudo apt-get install -y perltidy
run: sudo apt-get install -y perltidy && perltidy --version

- name: goimports
run: go install golang.org/x/tools/cmd/goimports@latest
run: go install golang.org/x/tools/cmd/goimports@latest && which goimports

- name: beautysh
run: pip install beautysh
run: pipx install beautysh && beautysh --version

- name: cljstyle
run: curl -sLO https://raw.githubusercontent.com/greglook/cljstyle/main/util/install-cljstyle && chmod +x install-cljstyle && sudo ./install-cljstyle
run: curl -sLO https://raw.githubusercontent.com/greglook/cljstyle/main/util/install-cljstyle && chmod +x install-cljstyle && sudo ./install-cljstyle && cljstyle version

- name: fourmolu
run: curl -sLO https://github.com/fourmolu/fourmolu/releases/download/v0.15.0.0/fourmolu-0.15.0.0-linux-x86_64 && chmod +x fourmolu-0.15.0.0-linux-x86_64 && sudo mv fourmolu-0.15.0.0-linux-x86_64 /usr/local/bin/fourmolu && fourmolu --version
Expand All @@ -199,25 +199,28 @@ jobs:
run: curl -sSLO https://github.com/mihaimaruseac/hindent/releases/download/v6.1.1/hindent && chmod a+x hindent && sudo mv hindent /usr/local/bin/ && hindent --version

- name: ktlint
run: curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.2.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/
run: curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.2.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/ && ktlint --version

- name: rufo
run: gem install rufo
run: gem install rufo && rufo --version

- name: yamlfmt
run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest
run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest && yamlfmt --version

- name: standardrb
run: gem install standardrb
run: gem install standardrb && standardrb --version

- name: fantomas
run: dotnet tool install -g fantomas
run: dotnet tool install -g fantomas && fantomas --version

- name: csharpier
run: dotnet tool install -g csharpier
run: dotnet tool install -g csharpier && dotnet csharpier --version

- name: xmlformatter/xmlformat
run: pip install xmlformatter
run: pipx install xmlformatter && which xmlformat

- name: yamlfix
run: pipx install yamlfix && yamlfix --version

- name: run tests
run: cargo test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mdsf init
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `prettier` |
| Xml | `xmlformat`, `xmllint` |
| Yaml | `prettier`, `yamlfmt` |
| Yaml | `prettier`, `yamlfix`, `yamlfmt` |
| Zig | `zigfmt` |

<!-- END_SECTION:supported-languages -->
2 changes: 1 addition & 1 deletion mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
},
"yaml": {
"enabled": true,
"formatter": [["prettier", "yamlfmt"]]
"formatter": [["prettier", "yamlfmt", "yamlfix"]]
},
"zig": {
"enabled": true,
Expand Down
4 changes: 2 additions & 2 deletions schemas/v0.0.3/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@
"yaml": {
"default": {
"enabled": true,
"formatter": [["prettier", "yamlfmt"]]
"formatter": [["prettier", "yamlfmt", "yamlfix"]]
},
"allOf": [
{
Expand Down Expand Up @@ -1900,7 +1900,7 @@
},
"Yaml": {
"type": "string",
"enum": ["prettier", "yamlfmt"]
"enum": ["prettier", "yamlfmt", "yamlfix"]
},
"Zig": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub mod taplo;
pub mod usort;
pub mod xmlformat;
pub mod xmllint;
pub mod yamlfix;
pub mod yamlfmt;
pub mod yapf;
pub mod zigfmt;
Expand Down
26 changes: 21 additions & 5 deletions src/formatters/standardjs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
use super::execute_command;
use crate::{runners::setup_npm_script, terminal::print_formatter_info};

use super::execute_command;
#[inline]
fn set_standardjs_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
cmd.arg("--fix").arg(snippet_path);
}

#[inline]
fn invoke_standardjs(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
set_standardjs_args(&mut cmd, snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[inline]
pub fn format_using_standardjs(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_formatter_info("standardjs");

let mut cmd = setup_npm_script("standard");
let global_result = invoke_standardjs(std::process::Command::new("standard"), snippet_path)?;

cmd.arg("--fix").arg(snippet_path);
if !global_result.0 {
return Ok(global_result);
}

execute_command(&mut cmd, snippet_path)
invoke_standardjs(setup_npm_script("standard"), snippet_path)
}

#[cfg(test)]
Expand All @@ -22,7 +38,7 @@ mod test_standardjs {
languages::{JavaScriptFlavor, Language},
};

#[test_with::executable(npx)]
#[test_with::executable(standard)]
#[test]
fn it_should_format_javascript() {
let input = "
Expand Down
76 changes: 76 additions & 0 deletions src/formatters/yamlfix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::execute_command;
use crate::terminal::print_formatter_info;

#[inline]
pub fn format_using_yamlfix(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_formatter_info("yamlfix");

let mut cmd = std::process::Command::new("yamlfix");

cmd.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_yamlfix {
use super::format_using_yamlfix;
use crate::{formatters::setup_snippet, languages::Language};

#[test_with::executable(yamlfix)]
#[test]
fn it_should_format_yaml() {
let input = "


version: 2
updates:
- package-ecosystem: \"cargo\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25

- package-ecosystem: \"github-actions\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25


";

let expected_output = "---
version: 2
updates:
- package-ecosystem: cargo
directory: /
schedule:
interval: monthly
assignees: [hougesen]
open-pull-requests-limit: 25
- package-ecosystem: github-actions
directory: /
schedule:
interval: monthly
assignees: [hougesen]
open-pull-requests-limit: 25
";

let snippet = setup_snippet(input, Language::Yaml.to_file_ext())
.expect("it to create a snippet file");

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

assert_eq!(output, expected_output);
}
}
8 changes: 3 additions & 5 deletions src/languages/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use schemars::JsonSchema;

use super::{Lang, LanguageFormatter};
use crate::formatters::{
biome::format_using_biome, clang_format::format_using_clang_format,
deno_fmt::format_using_deno_fmt, prettier::format_using_prettier,
standardjs::format_using_standardjs, MdsfFormatter,
};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum JavaScript {
Expand Down Expand Up @@ -64,13 +63,12 @@ impl LanguageFormatter for JavaScript {

#[cfg(test)]
mod test_javascript {
use super::JavaScript;
use crate::{
formatters::{setup_snippet, MdsfFormatter},
languages::{JavaScriptFlavor, Lang},
};

use super::JavaScript;

const INPUT: &str = "
async function asyncAddition(
a,b
Expand Down Expand Up @@ -214,7 +212,7 @@ mod test_javascript {
assert_eq!(expected_output, output);
}

#[test_with::executable(npx)]
#[test_with::executable(standard)]
#[test]
fn test_standardjs() {
let input = "
Expand Down
1 change: 1 addition & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub enum ShellFlavor {
}

impl core::fmt::Display for ShellFlavor {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Shell => f.write_str("shell"),
Expand Down
Loading
Loading