<commit_before>(ns cake.task
  (:use cake
        [clojure.set :only [difference]]
        [bake.core :only [print-stacktrace log]]
        [cake.file :only [file newer? touch]]
        [cake.utils.useful :only [update verify append]]
        [uncle.core :only [*task-name*]]
        [clojure.java.io :only [writer]]
        [clojure.contrib.prxml :only [*prxml-indent* prxml]]))

(declare tasks)
(declare run?)

(def implicit-tasks
  {'upgrade  ["Upgrade cake to the most current version."]
   'ps       ["List running cake jvm processes for all projects."]
   'log      ["Tail the cake log file. Optionally pass the number of lines of history to show."]
   'kill     ["Kill running cake jvm processes. Use -9 to force."]
   'killall  ["Kill all running cake jvm processes for all projects."]
   'console  ["Open a jconsole connection to the cake jvm. Optionally pass the number of tiled windows."]})

(defn parse-task-opts [forms]
  (let [[deps forms] (if (set? (first forms))
                      [(first forms) (rest forms)]
                      [#{} forms])
        deps (set (map #(if-not (symbol? %) (eval %) %) deps))
        [docs forms] (split-with string? forms)
        [destruct forms] (if (map? (first forms))
                           [(first forms) (rest forms)]
                           [{} forms])
        [pred forms] (if (= :when (first forms))
                       `[~(second forms) ~(drop 2 forms)]
                       [true forms])]
    {:deps deps :docs docs :actions forms :destruct destruct :pred pred}))

(defmacro append-task! [name task]
  `(do (defonce ~'task-defs (atom {}))
       (swap! ~'task-defs update '~name append '~task)))

(defn- expand-prefix
  "Converts a vector of the form [prefix sym1 sym1] to (prefix.sym1 prefix.sym2)"
  [ns]
  (if (sequential? ns)
    (map #(symbol (str (name (first ns)) "." (name %)))
         (rest ns))
    (list ns)))

(defmacro require-tasks!
  "Require all the specified namespaces and add them to the list of task namespaces."
  [namespaces]
  (let [namespaces (mapcat expand-prefix namespaces)]
    `(do (defonce ~'required-tasks (atom []))
         (apply require '~namespaces)
         (swap! ~'required-tasks into '~namespaces))))

(defn- resolve-var [ns sym]
  (when-let [ns (find-ns ns)]
    (when-let [var (ns-resolve ns sym)]
      @@var)))

(defn task-namespaces
  "Returns all required task namespaces for the given namespace (including transitive requirements)."
  [namespace]
  (try (require namespace)
       (catch java.io.FileNotFoundException e
         (when-not (= 'tasks namespace)
           (println "warning: unable to find tasks namespace" namespace)
           (println "  if you've added a new plugin to :dev-dependencies you must run 'cake deps' to install it"))))
  (into [namespace]
        (mapcat task-namespaces
                (resolve-var namespace 'required-tasks))))

(defn default-tasks []
  (if (= "global" (:artifact-id *project*))
    '[cake.tasks.global  tasks]
    '[cake.tasks.default tasks]))

(defn combine-task [task1 task2]
  (when-not (= {:replace true} task2)
    (let [task1 (or (when-not (:replace task2) task1)
                    {:actions [] :docs [] :deps #{}})]
      (append (update task1 :deps difference (:remove-deps task2))
              (select-keys task2 [:actions :docs :deps])))))

(defn get-tasks []
  (reduce
   (fn [tasks ns]
     (if-let [ns-tasks (resolve-var ns 'task-defs)]
       (merge-with combine-task tasks ns-tasks)
       tasks))
   {}
   (mapcat task-namespaces
           (into (default-tasks) (:tasks *project*)))))

(defn to-taskname [taskname]
  (gensym
   (str (name (ns-name *ns*))
        "/"
        (if (string? taskname)
          (str "file-" (.replaceAll taskname "/" "-"))
          (str "task-" (name taskname))))))

(defn task-run-file [taskname]
  (file ".cake" "run" taskname))

(defn run-file-task? [target-file deps]
  (let [{file-deps true task-deps false} (group-by string? deps)]
    (or (not (.exists target-file))
        (some #(newer? % target-file)
              (into file-deps
                    (map #(task-run-file %)
                         task-deps)))
        (empty? deps))))

(defn- expand-defile-path [path]
  (file (.replaceAll path "\\+context\\+" (str (:context *project*)))))

(defmulti generate-file
  (fn [forms]
    (let [parts (-> *File* (.toString) (.split "\\."))]
      (when (< 1 (count parts))
        (-> parts last keyword)))))

(defmacro with-outfile [& forms]
  `(with-open [f# (writer *File*)]
     (binding [*out* f#]
       ~@forms)))

(defmethod generate-file nil
  [forms]
  (with-outfile
    (doseq [form forms]
      (println form)
      (println))))

(defmethod generate-file :clj
  [forms]
  (with-outfile
    (doseq [form forms]
      (prn form)
      (println))))

(defmethod generate-file :xml
  [forms]<commit_msg>remove extra newline in generate-file nil<commit_after>(ns cake.task
  (:use cake
        [clojure.set :only [difference]]
        [bake.core :only [print-stacktrace log]]
        [cake.file :only [file newer? touch]]
        [cake.utils.useful :only [update verify append]]
        [uncle.core :only [*task-name*]]
        [clojure.java.io :only [writer]]
        [clojure.contrib.prxml :only [*prxml-indent* prxml]]))

(declare tasks)
(declare run?)

(def implicit-tasks
  {'upgrade  ["Upgrade cake to the most current version."]
   'ps       ["List running cake jvm processes for all projects."]
   'log      ["Tail the cake log file. Optionally pass the number of lines of history to show."]
   'kill     ["Kill running cake jvm processes. Use -9 to force."]
   'killall  ["Kill all running cake jvm processes for all projects."]
   'console  ["Open a jconsole connection to the cake jvm. Optionally pass the number of tiled windows."]})

(defn parse-task-opts [forms]
  (let [[deps forms] (if (set? (first forms))
                      [(first forms) (rest forms)]
                      [#{} forms])
        deps (set (map #(if-not (symbol? %) (eval %) %) deps))
        [docs forms] (split-with string? forms)
        [destruct forms] (if (map? (first forms))
                           [(first forms) (rest forms)]
                           [{} forms])
        [pred forms] (if (= :when (first forms))
                       `[~(second forms) ~(drop 2 forms)]
                       [true forms])]
    {:deps deps :docs docs :actions forms :destruct destruct :pred pred}))

(defmacro append-task! [name task]
  `(do (defonce ~'task-defs (atom {}))
       (swap! ~'task-defs update '~name append '~task)))

(defn- expand-prefix
  "Converts a vector of the form [prefix sym1 sym1] to (prefix.sym1 prefix.sym2)"
  [ns]
  (if (sequential? ns)
    (map #(symbol (str (name (first ns)) "." (name %)))
         (rest ns))
    (list ns)))

(defmacro require-tasks!
  "Require all the specified namespaces and add them to the list of task namespaces."
  [namespaces]
  (let [namespaces (mapcat expand-prefix namespaces)]
    `(do (defonce ~'required-tasks (atom []))
         (apply require '~namespaces)
         (swap! ~'required-tasks into '~namespaces))))

(defn- resolve-var [ns sym]
  (when-let [ns (find-ns ns)]
    (when-let [var (ns-resolve ns sym)]
      @@var)))

(defn task-namespaces
  "Returns all required task namespaces for the given namespace (including transitive requirements)."
  [namespace]
  (try (require namespace)
       (catch java.io.FileNotFoundException e
         (when-not (= 'tasks namespace)
           (println "warning: unable to find tasks namespace" namespace)
           (println "  if you've added a new plugin to :dev-dependencies you must run 'cake deps' to install it"))))
  (into [namespace]
        (mapcat task-namespaces
                (resolve-var namespace 'required-tasks))))

(defn default-tasks []
  (if (= "global" (:artifact-id *project*))
    '[cake.tasks.global  tasks]
    '[cake.tasks.default tasks]))

(defn combine-task [task1 task2]
  (when-not (= {:replace true} task2)
    (let [task1 (or (when-not (:replace task2) task1)
                    {:actions [] :docs [] :deps #{}})]
      (append (update task1 :deps difference (:remove-deps task2))
              (select-keys task2 [:actions :docs :deps])))))

(defn get-tasks []
  (reduce
   (fn [tasks ns]
     (if-let [ns-tasks (resolve-var ns 'task-defs)]
       (merge-with combine-task tasks ns-tasks)
       tasks))
   {}
   (mapcat task-namespaces
           (into (default-tasks) (:tasks *project*)))))

(defn to-taskname [taskname]
  (gensym
   (str (name (ns-name *ns*))
        "/"
        (if (string? taskname)
          (str "file-" (.replaceAll taskname "/" "-"))
          (str "task-" (name taskname))))))

(defn task-run-file [taskname]
  (file ".cake" "run" taskname))

(defn run-file-task? [target-file deps]
  (let [{file-deps true task-deps false} (group-by string? deps)]
    (or (not (.exists target-file))
        (some #(newer? % target-file)
              (into file-deps
                    (map #(task-run-file %)
                         task-deps)))
        (empty? deps))))

(defn- expand-defile-path [path]
  (file (.replaceAll path "\\+context\\+" (str (:context *project*)))))

(defmulti generate-file
  (fn [forms]
    (let [parts (-> *File* (.toString) (.split "\\."))]
      (when (< 1 (count parts))
        (-> parts last keyword)))))

(defmacro with-outfile [& forms]
  `(with-open [f# (writer *File*)]
     (binding [*out* f#]
       ~@forms)))

(defmethod generate-file nil
  [forms]
  (with-outfile
    (doseq [form forms]
      (println form))))

(defmethod generate-file :clj
  [forms]
  (with-outfile
    (doseq [form forms]
      (prn form)
      (println))))

(defmethod generate-file :xml
  [forms]
{'fraction_non_alphanumeric': 0.156121187139324, 'fraction_numerical': 0.0032976092333058533, 'mean_word_length': 2.511215629522431, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5958412', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3090, 'n_words': 983}