lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
604 stars 105 forks source link

[Feature request] support for Puppet #240

Open rolandmas opened 11 months ago

rolandmas commented 11 months ago

It would be nice to support formatting Puppet code. The best formatter I found for that task is puppet-lint -f. I'd propose a pull request to add it, but unfortunately that tool doesn't work as a pipe/filter, it can only read a file and replace it with a fixed version. I could of course write a shell wrapper that reads stdin, saves it, applies puppet-lint, then prints the results to its stdout, but I guess a pure Lisp implementation would be cleaner. However, it goes beyond my skills…

rolandmas commented 11 months ago

I went on to write a wrapper script, which I attach in the hope it can be useful to someone.

Date: Sun, 15 Oct 2023 12:17:13 +0200
Subject: Add support for Puppet manifests

---
 format-all.el       | 10 +++++++++-
 puppet-lint-wrapper |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 puppet-lint-wrapper

diff --git a/format-all.el b/format-all.el
index 77c9bc1..aa0cd81 100644
--- a/format-all.el
+++ b/format-all.el
@@ -63,6 +63,7 @@
 ;; - Perl (perltidy)
 ;; - PHP (prettier plugin)
 ;; - Protocol Buffers (clang-format)
+;; - Puppet (puppet-lint)
 ;; - PureScript (purty)
 ;; - Python (black, yapf)
 ;; - R (styler)
@@ -154,6 +155,7 @@
     ("Perl" perltidy)
     ("PHP" prettier)
     ("Protocol Buffer" clang-format)
+    ("Puppet" puppet-lint)
     ("PureScript" purty)
     ("Python" black)
     ("R" styler)
@@ -919,6 +921,12 @@ Consult the existing formatters for examples of BODY."
     (let ((ignore-file (format-all--locate-file ".prettierignore")))
       (when ignore-file (list "--ignore-path" ignore-file))))))

+(define-format-all-formatter puppet-lint
+  (:executable "puppet-lint-wrapper")
+  (:install "gem install --global puppet-lint")
+  (:languages "Puppet")
+  (:format (format-all--buffer-easy executable "-f")))
+
 (define-format-all-formatter purty
   (:executable "purty")
   (:install "npm install --global purty")
@@ -933,7 +941,7 @@ Consult the existing formatters for examples of BODY."
    (format-all--buffer-easy
     executable "format" "-stdin"
     (let ((ext (if (not (buffer-file-name)) ""
-                   (file-name-extension (buffer-file-name)))))
+                 (file-name-extension (buffer-file-name)))))
       (concat "." (if (equal ext "") "res" ext))))))

 (define-format-all-formatter rubocop
diff --git a/puppet-lint-wrapper b/puppet-lint-wrapper
new file mode 100644
index 0000000..842f01d
--- /dev/null
+++ b/puppet-lint-wrapper
@@ -0,0 +1,7 @@
+#! /bin/sh
+
+t=$(mktemp)
+cat >$t
+puppet-lint -f $t | egrep -v "^(FIXED|WARNING|ERROR):"
+cat $t
+rm -f $t
anarcat commented 11 months ago

that looks pretty good! could you make a merge request out of this?

rolandmas commented 11 months ago

@anarcat See #243