Skip to content

Commit

Permalink
Add pristine? cli command
Browse files Browse the repository at this point in the history
related to #88
  • Loading branch information
thumbnail committed Oct 22, 2019
1 parent f5baad2 commit 0c55c8a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
46 changes: 38 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Clojure CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-clojure/ for more details
#
version: 2
version: 2.1
jobs:
build:
docker:
Expand Down Expand Up @@ -50,7 +46,7 @@ jobs:

jfrog:
docker:
- image: circleci/clojure:lein-2.8.1-node
- image: circleci/clojure:lein-2.9.1-node

working_directory: ~/repo

Expand All @@ -73,8 +69,35 @@ jobs:
name: release to Clojars
command: lein deploy clojars

format-and-lint:
docker:
- image: circleci/clojure:lein-2.9.1-node

working_directory: ~/repo

steps:

- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "project.clj" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: lein with-profile -dev,+ci,+pedantic deps

- save_cache:
paths:
- ~/.m2
key: v1-dependencies-{{ checksum "project.clj" }}

- run:
command: lein with-profile -dev,+ci run -m formatting-stack.branch-formatter pristine?

workflows:
version: 2
version: 2.1
CircleCI:
jobs:
- build:
Expand All @@ -83,7 +106,14 @@ workflows:
branches:
only: /.*/
tags:
only: /^v\d+\.\d+\.\d+(-alpha\d+)?$/
only: /^v\d+\.\d+\.\d+(-alpha\d+)?$/

- format-and-lint:
context: JFrog
filters:
branches:
ignore: master

- jfrog:
context: JFrog
requires:
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ That's by design, to avoid intrincate DSLs or data structures.
If you need something finer-grained, you are encouraged to copy the contents of the `formatting-stack.defaults` ns to your project, adapting things as needed.
That ns is a deliberately thin and data-only one, with the precise purpose of being forked at no cost.

## CI integration

**formatting-stack** can be used in a CI configuration to assert that there's no formatting errors or new warnings introduced.
```bash
lein with-profile -dev,+ci run -m formatting-stack.branch-formatter pristine?
```

will yield assertion errors if the build introduces new warnings / formatting changes in files touched against the `master`-branch.

## [FAQ](https://github.com/nedap/formatting-stack/wiki/FAQ)

## License
Expand Down
29 changes: 28 additions & 1 deletion src/formatting_stack/branch_formatter.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns formatting-stack.branch-formatter
"A set of defaults apt for formatting a git branch (namely, the files that a branch has modified, respective to another)."
(:require
[clojure.string :as str]
[formatting-stack.core]
[formatting-stack.formatters.cider :as formatters.cider]
[formatting-stack.formatters.clean-ns :as formatters.clean-ns]
Expand All @@ -15,7 +16,8 @@
[formatting-stack.linters.loc-per-ns :as linters.loc-per-ns]
[formatting-stack.linters.ns-aliases :as linters.ns-aliases]
[formatting-stack.strategies :as strategies]
[medley.core :refer [mapply]]))
[medley.core :refer [mapply]]
[nedap.speced.def :as speced]))

(def third-party-indent-specs formatting-stack.indent-specs/default-third-party-indent-specs)

Expand Down Expand Up @@ -75,3 +77,28 @@
:compilers []
:linters linters
:in-background? in-background?)))

(defn format-branch! [& {:keys [target-branch in-background?]
:or {target-branch "master"
in-background? (not (System/getenv "CI"))}}]
(let [default-strategies [(fn [& {:as options}]
(mapply strategies/git-diff-against-default-branch (assoc options :target-branch target-branch)))]
formatters (default-formatters default-strategies)]
(formatting-stack.core/format! :strategies default-strategies
:formatters formatters
:in-background? in-background?)))

(speced/defn -main [command & _args]
(assert (#{"pristine?"} command) (str "Command not recognised.\n\n"
"Available commands: pristine?.\n\n"
"You can refer to the documentation in: https://github.com/nedap/formatting-stack"))

(println "Running linter ...")
(let [output (-> (with-out-str (lint-branch! :in-background? false)) str/trim)]
(assert (#{""} output) (str "Linting yielded output: " output)))

(println "Running formatter ...")
(let [output (-> (with-out-str (format-branch! :in-background? false)) str/trim)]
(assert (#{""} output) (str "Formatting yielded output: " output)))

(println "Success!"))
36 changes: 17 additions & 19 deletions src/formatting_stack/linters/ns_aliases.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,20 @@
(let [acceptable-aliases-whitelist (or acceptable-aliases-whitelist
default-acceptable-aliases-whitelist)]
(->> filenames
(process-in-parallel! (fn [filename]
(let [bad-require-clauses (->> filename
file/read-file-ns-decl
formatting-stack.util/require-from-ns-decl
(rest)
(remove (partial acceptable-require-clause?
acceptable-aliases-whitelist)))]
(when (seq bad-require-clauses)
(let [formatted-bad-requires (->> bad-require-clauses
(map (fn [x]
(str " " x)))
(string/join "\n"))]
(-> (str "Warning for "
filename
": the following :require aliases are not derived from their refered namespace:"
"\n"
formatted-bad-requires
". See https://stuartsierra.com/2015/05/10/clojure-namespace-aliases\n")
(println)))))))))))
(process-in-parallel!
(fn [filename]
(let [bad-require-clauses (->> filename
file/read-file-ns-decl
formatting-stack.util/require-from-ns-decl
(rest)
(remove (partial acceptable-require-clause?
acceptable-aliases-whitelist)))]
(when (seq bad-require-clauses)
(let [formatted-bad-requires (->> bad-require-clauses
(map (fn [x]
(str " " x)))
(string/join "\n"))]
(-> (str "Warning for " filename
": the following :require aliases are not derived from their referred namespace:\n"
formatted-bad-requires ". See https://stuartsierra.com/2015/05/10/clojure-namespace-aliases\n")
(println)))))))))))
3 changes: 2 additions & 1 deletion src/formatting_stack/project_formatter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
[formatting-stack.linters.kondo :as linters.kondo]
[formatting-stack.linters.loc-per-ns :as linters.loc-per-ns]
[formatting-stack.linters.ns-aliases :as linters.ns-aliases]
[formatting-stack.strategies :as strategies]))
[formatting-stack.strategies :as strategies])
(:gen-class))

(def third-party-indent-specs formatting-stack.indent-specs/default-third-party-indent-specs)

Expand Down
1 change: 1 addition & 0 deletions src/formatting_stack/util.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns formatting-stack.util
(:require
[clojure.java.shell :refer [sh]]
[clojure.tools.namespace.file :as file]
[clojure.tools.namespace.parse :as parse]
[formatting-stack.project-parsing :refer [project-namespaces]]
Expand Down

0 comments on commit 0c55c8a

Please sign in to comment.