Skip to content

Commit

Permalink
Fix include-drivers lein middleware (metabase#12436)
Browse files Browse the repository at this point in the history
* Fix include-plugins middleware when we use multiple lein with-profiles

* Bump core project dep version
  • Loading branch information
camsaul authored Apr 30, 2020
1 parent 032a932 commit ec5f8f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lein-plugins/include-drivers/project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject metabase/lein-include-drivers "1.0.8"
(defproject metabase/lein-include-drivers "1.0.9"
:min-lein-version "2.5.0"
:eval-in-leiningen true
:deploy-repositories [["clojars" {:sign-releases false}]]
Expand Down
35 changes: 18 additions & 17 deletions lein-plugins/include-drivers/src/leiningen/include_drivers.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
(ns leiningen.include-drivers
(:require [clojure.string :as str]
[colorize.core :as colorize]
[leiningen.core.project :as p])
(:import java.io.File))
(:require [clojure.string :as str]
[colorize.core :as colorize]
[leiningen.core.project :as p])
(:import java.io.File))

(defonce ^:private ^{:arglists '([s]), :doc "Log a message `s` the first time we see it. This middleware might run
multiple times, and we only really need to log a message the first time we see it."}
log-once
(comp (memoize println) str))

(defn- file-exists? [^String filename]
(.exists (File. filename)))
Expand Down Expand Up @@ -35,12 +40,12 @@
{:pre [(string? driver) (seq driver)]}
(if-let [{:keys [include-drivers-dependencies]} (driver->project driver)]
(or (every? plugins-file-exists? include-drivers-dependencies)
(println
(log-once
(colorize/color
:red
(format "[include-drivers middleware] Not including %s because not all dependencies matching %s found in /plugins"
driver (set include-drivers-dependencies)))))
(println
(log-once
(colorize/color
:red
(format "[include-drivers middleware] Not including %s because we could not its project.clj" driver)))))
Expand All @@ -59,7 +64,7 @@
:else
(some-> (System/getenv "DRIVERS") (str/split #",") set (disj "h2" "postgres" "mysql")))

_ (println
_ (log-once
(colorize/color
:magenta
(format "[include-drivers middleware] Attempting to include these drivers: %s" (set drivers))))
Expand Down Expand Up @@ -120,7 +125,7 @@
(defn- test-drivers-profile [project]
(let [test-drivers (test-drivers project)
test-projects (test-drivers-projects test-drivers)]
(println
(log-once
(colorize/color
:magenta
(format "[include-drivers middleware] including these drivers: %s" (set test-drivers))))
Expand All @@ -130,17 +135,13 @@
:source-paths (test-drivers-source-paths test-drivers)
:test-paths (test-drivers-test-paths test-drivers)}))

;; When we merge a new profile into the project Leiningen will reload the project, which will cause our middleware to
;; run a second time. Make sure we don't add the profile a second time or we'll be stuck in an infinite loop of adding
;; a new profile and reloading.
(defonce ^:private has-added-test-drivers-profile? (atom false))

(defn middleware
"Add dependencies, source paths, and test paths for to Metabase drivers that are packaged as separate projects and
specified by the `DRIVERS` env var."
[project]
(if @has-added-test-drivers-profile?
;; When we merge a new profile into the project Leiningen will reload the project, which will cause our middleware
;; to run a second time. Make sure we don't add the profile a second time or we'll be stuck in an infinite loop of
;; adding a new profile and reloading.
(if (::has-driver-profiles? project)
project
(do
(reset! has-added-test-drivers-profile? true)
(p/merge-profiles project [(test-drivers-profile project)]))))
(p/merge-profiles project [(test-drivers-profile project) {::has-driver-profiles? true}])))
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@

:with-include-drivers-middleware
{:plugins
[[metabase/lein-include-drivers "1.0.8"]]
[[metabase/lein-include-drivers "1.0.9"]]

:middleware
[leiningen.include-drivers/middleware]}
Expand Down
8 changes: 5 additions & 3 deletions src/metabase/plugins/classloader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<3 Cam"
(:refer-clojure :exclude [require])
(:require [clojure.tools.logging :as log]
(:require [clojure.string :as str]
[clojure.tools.logging :as log]
[dynapath.util :as dynapath]
[metabase.util.i18n :refer [deferred-trs]])
(:import [clojure.lang DynamicClassLoader RT]
Expand Down Expand Up @@ -126,8 +127,9 @@
(apply clojure.core/require args)))
(catch Throwable e
(throw (ex-info (.getMessage e)
{:classloader (the-classloader)
:classpath-urls (map str (dynapath/all-classpath-urls (the-classloader)))}
{:classloader (the-classloader)
:classpath-urls (map str (dynapath/all-classpath-urls (the-classloader)))
:system-classpath (sort (str/split (System/getProperty "java.class.path") #"[:;]"))}
e)))))

(defonce ^:private already-added (atom #{}))
Expand Down

0 comments on commit ec5f8f1

Please sign in to comment.