Skip to content

Commit

Permalink
[Fix #7] sort dependencies acc their inter dependencies
Browse files Browse the repository at this point in the history
- order first level dependencies according
  - to their relation: if A depends on B, A will occur later
  - if they don't depend on each other, sorted alphabetically
  • Loading branch information
benedekfazekas committed Jul 19, 2015
1 parent deac21b commit 16501a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/leiningen/source_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,21 @@
(debug "file: " file " orig import:" orig-import " new import:" new-import)
(spit file new))))))

(defn- dep-frequency [dep-hierarchy]
(let [frequency (atom {})
freq-fn (fn [node]
(when-let [pkg (and (vector? node) (symbol? (first node)) (first node))]
(swap! frequency #(if (contains? % pkg)
(update-in % [pkg] inc)
(assoc % pkg 1))))
node)]
(clojure.walk/postwalk freq-fn dep-hierarchy)
@frequency))
(defn- flat-dep-tree [individual-dep]
(let [deps (atom [])
flat-dep-fn (fn [node]
(when-let [pkg (and (vector? node) (symbol? (first node)) (first node))]
(swap! deps conj pkg))
node)]
(clojure.walk/postwalk flat-dep-fn (-> individual-dep vals first))
[(-> individual-dep keys ffirst) @deps]))

(defn- compare-deps
[flat-deps dep-left dep-right]
(let [dep-left-name (first dep-left)
dep-right-name (first dep-right)]
(not (or (some #{dep-left-name} (flat-deps dep-right-name))
(< 0 (compare (str dep-left-name) (str dep-right-name)))))))

(defn lookup-opt [opt-key opts]
(second (drop-while #(not= % opt-key) opts)))
Expand Down Expand Up @@ -291,13 +296,14 @@
project-prefix (lookup-opt :project-prefix opts)
pprefix (or project-prefix (clean-name-version "mranderson" (mranderson-version)))
srcdeps-relative (str (apply str (drop (inc (count root)) target-path)) "/srcdeps")
dep-frequencies (->> (map #(aether/resolve-dependencies :coordinates [%] :repositories repositories) source-dependencies)
flat-deps (->> (map #(aether/resolve-dependencies :coordinates [%] :repositories repositories) source-dependencies)
(map #(aether/dependency-hierarchy source-dependencies %))
dep-frequency)
dep-frequency-comp (comparator #(<= (-> %1 first dep-frequencies) (-> %2 first dep-frequencies)))
(mapcat flat-dep-tree)
(apply hash-map))
dep-comparator (comparator (partial compare-deps flat-deps))
dep-hierarchy (->> (aether/resolve-dependencies :coordinates source-dependencies :repositories repositories)
(aether/dependency-hierarchy source-dependencies))
ordered-hierarchy (into (sorted-map-by dep-frequency-comp) dep-hierarchy)
ordered-hierarchy (into (sorted-map-by dep-comparator) dep-hierarchy)
uuid (str (UUID/randomUUID))
prefix-exclusions (lookup-opt :prefix-exclusions opts)
skip-repackage-java-classes (lookup-opt :skip-javaclass-repackage opts)
Expand Down
3 changes: 1 addition & 2 deletions src/mranderson/plugin.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns mranderson.plugin
(:require [mranderson.util :refer [first-src-path clojure-source-files source-dep?]]
[clojure.tools.namespace.file :refer [read-file-ns-decl]]
[leiningen.core.main :refer [info debug]])
)
[leiningen.core.main :refer [info debug]]))

(defn- find-gen-class-ns [found file]
(let [ns-decl (read-file-ns-decl file)]
Expand Down

0 comments on commit 16501a6

Please sign in to comment.