/cljfmt

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

cljfmt-graalvm

@NOTE: this is a clone of gitlab.com/konrad.mrozek/cljfmt-graalvm/ lightly modified to format input from stdin or a file. I've also built native images and attached them as a release for Linux and Mac for convenience.

To use, just download one of the binaries, chmod +x it and put it somewhere on your path. For the lazyefficient, pasting the snippet below into your term should get the job done:

wget -O ~/cljfmt "https://github.com/nikvdp/cljfmt/releases/download/v0.6.1/cljfmt.$(uname)"
chmod +x ~/cljfmt
sudo mv ~/cljfmt /usr/local/bin

A Clojure code formatter using cljfmt built with graalvm.

Usage

Format a file in place:

./cljfmt source-file.clj

Format from stdin:

cat some-file.clj | ./cljfmt

Build

For linux, a binary may be downloaded directly from Gitlab. To build the binary yourself, here are some instructions.

  • Install lein

  • Download GraalVM for your machine. You will need the EE version if you're using MacOS.

  • Set JAVA_HOME to the GraalVM home directory, e.g.

    export JAVA_HOME=~/Downloads/graalvm-1.0.0-rc1/Contents/Home
  • Set the PATH to use GraalVM's binaries, e.g.

    export PATH=$PATH:~/Downloads/graalvm-1.0.0-rc1/Contents/Home/bin
  • Create the uberjar:

    lein uberjar
  • Use GraalVM's assisted configuration to generate reflection files:

    mkdir -p ./META-INF/native-image 
    
    # with stdin
    cat project.clj | java -agentlib:native-image-agent=config-output-dir=./META-INF/native-image -jar target/cljfmt-graalvm-0.1.0-SNAPSHOT-standalone.jar
    
    # with cmdline arg, now using config-merge-dir to add any extra methods
    java -agentlib:native-image-agent=config-merge-dir=./META-INF/native-image -jar target/cljfmt-graalvm-0.1.0-SNAPSHOT-standalone.jar ./project.clj
  • Finally, create the binary adding the GraalVM assisted configuration output to the classpath:

    native-image \
        -jar target/cljfmt-graalvm-0.1.0-SNAPSHOT-standalone.jar \
        -H:Name="cljfmt" \
        -cp ./META-INF/native-image  \
        --no-fallback \
        --report-unsupported-elements-at-runtime \
        --allow-incomplete-classpath \
        -H:+InlineBeforeAnalysis \
        -H:ConfigurationFileDirectories=graalvm \
        --initialize-at-build-time

Integrate with Emacs

Place the cljfmt binary on your PATH. To execute it on a Clojure file in Emacs on save, add this to your init.el:

(defun cljfmt ()
  (when (or (eq major-mode 'clojure-mode)
            (eq major-mode 'clojurescript-mode))
    (shell-command-to-string (format "cljfmt %s" buffer-file-name))
    (revert-buffer :ignore-auto :noconfirm)))

(add-hook 'after-save-hook #'cljfmt)

License

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.