|
| 1 | +(ns java-time.dev.gen |
| 2 | + (:require [clojure.string :as str] |
| 3 | + [clojure.set :as set])) |
| 4 | + |
| 5 | +(defn normalize-argv [argv] |
| 6 | + {:post [(or (empty? %) |
| 7 | + (apply distinct? %))]} |
| 8 | + (into [] (map-indexed (fn [i arg] |
| 9 | + (if (symbol? arg) |
| 10 | + (do (assert (not (namespace arg))) |
| 11 | + (if (some #(Character/isDigit (char %)) (name arg)) |
| 12 | + (symbol (apply str (concat |
| 13 | + (remove #(Character/isDigit (char %)) (name arg)) |
| 14 | + [i]))) |
| 15 | + arg)) |
| 16 | + (symbol (str "arg" i))))) |
| 17 | + argv)) |
| 18 | + |
| 19 | +(defn import-fn [sym] |
| 20 | + (let [vr (find-var sym) |
| 21 | + m (meta vr) |
| 22 | + n (:name m) |
| 23 | + arglists (:arglists m) |
| 24 | + protocol (:protocol m) |
| 25 | + forward-meta (into (sorted-map) (select-keys m [:doc :tag :deprecated])) |
| 26 | + forward-meta (cond-> forward-meta |
| 27 | + (nil? (:tag forward-meta)) (dissoc :tag)) |
| 28 | + impl '+impl+] |
| 29 | + (when (:macro m) |
| 30 | + (throw (IllegalArgumentException. |
| 31 | + (str "Calling import-fn on a macro: " sym)))) |
| 32 | + (list 'let [impl (list 'delay |
| 33 | + (list 'load-java-time) |
| 34 | + (list 'deref (list 'resolve (list 'quote sym))))] |
| 35 | + (list* 'defn n |
| 36 | + (concat |
| 37 | + (some-> (not-empty forward-meta) list) |
| 38 | + (map (fn [argv] |
| 39 | + (let [argv (normalize-argv argv)] |
| 40 | + (list argv |
| 41 | + (if (some #{'&} argv) |
| 42 | + (list* 'apply (list 'deref impl) (remove #{'&} argv)) |
| 43 | + (list* (list 'deref impl) argv))))) |
| 44 | + arglists)))))) |
| 45 | + |
| 46 | +(defn import-macro [sym] |
| 47 | + (let [vr (find-var sym) |
| 48 | + m (meta vr) |
| 49 | + _ (when-not (:macro m) |
| 50 | + (throw (IllegalArgumentException. |
| 51 | + (str "Calling import-macro on a non-macro: " sym)))) |
| 52 | + n (:name m) |
| 53 | + arglists (:arglists m)] |
| 54 | + (list* 'defmacro n |
| 55 | + (concat |
| 56 | + (some-> (not-empty (into (sorted-map) (select-keys m [:doc :deprecated]))) |
| 57 | + list) |
| 58 | + (map (fn [argv] |
| 59 | + (let [argv (normalize-argv argv)] |
| 60 | + (list argv |
| 61 | + (if (some #{'&} argv) |
| 62 | + (list* 'list* (list 'quote sym) (remove #{'&} argv)) |
| 63 | + (list* 'list (list 'quote sym) argv))))) |
| 64 | + arglists))))) |
| 65 | + |
| 66 | +(defn import-vars |
| 67 | + "Imports a list of vars from other namespaces." |
| 68 | + [& syms] |
| 69 | + (let [unravel (fn unravel [x] |
| 70 | + (if (sequential? x) |
| 71 | + (->> x |
| 72 | + rest |
| 73 | + (mapcat unravel) |
| 74 | + (map |
| 75 | + #(symbol |
| 76 | + (str (first x) |
| 77 | + (when-let [n (namespace %)] |
| 78 | + (str "." n))) |
| 79 | + (name %)))) |
| 80 | + [x])) |
| 81 | + syms (mapcat unravel syms)] |
| 82 | + (map (fn [sym] |
| 83 | + (let [_ (require (-> sym namespace symbol)) |
| 84 | + vr (resolve sym) |
| 85 | + m (meta vr)] |
| 86 | + (if (:macro m) |
| 87 | + (import-macro sym) |
| 88 | + (import-fn sym)))) |
| 89 | + syms))) |
| 90 | + |
| 91 | +(def impl-info |
| 92 | + {:macros '[[java-time.clock with-clock] |
| 93 | + [java-time.util when-joda-time-loaded]] |
| 94 | + :threeten-extra-fns ['[java-time.interval interval interval?] |
| 95 | + |
| 96 | + '[java-time.single-field |
| 97 | + am-pm am-pm? quarter quarter? day-of-month day-of-month? |
| 98 | + day-of-year day-of-year? year-quarter year-quarter?]] |
| 99 | + :fns ['[java-time.clock with-clock-fn] |
| 100 | + '[java-time.core |
| 101 | + zero? negative? negate abs max min |
| 102 | + before? not-after? after? not-before? |
| 103 | + supports? |
| 104 | + fields units properties property |
| 105 | + as value range min-value max-value largest-min-value smallest-max-value |
| 106 | + truncate-to time-between with-zone |
| 107 | + plus minus multiply-by |
| 108 | + ;; TODO below here needs unit tests |
| 109 | + chronology leap? with-value with-min-value with-max-value with-largest-min-value with-smallest-max-value] |
| 110 | + |
| 111 | + '[java-time.amount |
| 112 | + duration period period? duration? |
| 113 | + nanos micros millis seconds minutes hours standard-days |
| 114 | + days weeks months years] |
| 115 | + |
| 116 | + '[java-time.properties |
| 117 | + unit? unit field? field] |
| 118 | + |
| 119 | + '[java-time.temporal |
| 120 | + value-range instant instant?] |
| 121 | + |
| 122 | + '[java-time.local |
| 123 | + local-date local-date-time local-time |
| 124 | + local-date? local-date-time? local-time?] |
| 125 | + |
| 126 | + '[java-time.single-field |
| 127 | + year year? month month? day-of-week day-of-week? month-day month-day? |
| 128 | + year-month year-month?] |
| 129 | + |
| 130 | + '[java-time.zone |
| 131 | + available-zone-ids zone-id zone-offset |
| 132 | + offset-date-time offset-time zoned-date-time |
| 133 | + system-clock fixed-clock offset-clock tick-clock clock? |
| 134 | + zone-id? zoned-date-time? offset-date-time? offset-time? |
| 135 | + with-zone-same-instant with-offset with-offset-same-instant] |
| 136 | + |
| 137 | + '[java-time.mock mock-clock advance-clock! set-clock!] |
| 138 | + |
| 139 | + '[java-time.convert |
| 140 | + as-map convert-amount to-java-date to-sql-date to-sql-timestamp |
| 141 | + to-millis-from-epoch] |
| 142 | + |
| 143 | + '[java-time.sugar |
| 144 | + monday? tuesday? wednesday? thursday? friday? saturday? sunday? |
| 145 | + weekend? weekday?] |
| 146 | + |
| 147 | + '[java-time.seqs iterate] |
| 148 | + |
| 149 | + '[java-time.adjuster adjust] |
| 150 | + |
| 151 | + '[java-time.format format formatter] |
| 152 | + |
| 153 | + '[java-time.pre-java8 java-date sql-date sql-timestamp instant->sql-timestamp sql-time] |
| 154 | + |
| 155 | + '[java-time.interval |
| 156 | + move-start-to move-end-to move-start-by move-end-by |
| 157 | + start end contains? overlaps? abuts? overlap gap]]}) |
| 158 | + |
| 159 | +(defn gen-java-time-ns-forms [] |
| 160 | + (let [require-macros (into #{} (map first) (:macros impl-info)) |
| 161 | + require-fns #_(set/difference (into #{} (map first) |
| 162 | + (concat (:threeten-extra-fns impl-info) (:fns impl-info))) |
| 163 | + require-macros) |
| 164 | + ;;FIXME implementations must be loaded in this order for a stable graph traversal (I think) |
| 165 | + (into #{} (map #(symbol (str "java-time." %))) |
| 166 | + '[core properties temporal amount zone single-field local chrono |
| 167 | + convert sugar seqs adjuster interval format joda clock pre-java8 mock])] |
| 168 | + (concat |
| 169 | + [";; NOTE: This namespace is generated by java-time.dev.gen" |
| 170 | + `(~'ns ~'java-time |
| 171 | + (:refer-clojure :exclude ~'(zero? range iterate max min contains? format abs)) |
| 172 | + (:require ~'[java-time.util :as jt.u] |
| 173 | + ~@(sort require-macros))) |
| 174 | + (format "(let [lock (Object.) do-load (delay (locking lock (require %s #?@(:bb [] :default ['java-time.mock]))))]\n (defn load-java-time \"Load java-time implementation\" [] @do-load))" |
| 175 | + (str/join " " (map #(str "'" %) (sort (disj require-fns 'java-time.mock))))) |
| 176 | + '(when *compile-files* (load-java-time))] |
| 177 | + (apply import-vars (:macros impl-info)) |
| 178 | + (apply import-vars (:fns impl-info)) |
| 179 | + (map #(list 'jt.u/when-threeten-extra %) (apply import-vars (:threeten-extra-fns impl-info)))))) |
| 180 | + |
| 181 | +(defn print-java-time-ns [] |
| 182 | + (run! #(println (cond-> % (not (string? %)) pr-str)) (gen-java-time-ns-forms))) |
| 183 | + |
| 184 | +(defn spit-java-time-ns [] |
| 185 | + (spit "src/java_time.cljc" (with-out-str (print-java-time-ns)))) |
| 186 | + |
| 187 | +(comment |
| 188 | + (print-java-time-ns) |
| 189 | + (spit-java-time-ns) |
| 190 | + ) |
0 commit comments