Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Last active June 26, 2022 17:05
Show Gist options
  • Select an option

  • Save mikeananev/aa190ea948d35f354e28ee76bf59e2fd to your computer and use it in GitHub Desktop.

Select an option

Save mikeananev/aa190ea948d35f354e28ee76bf59e2fd to your computer and use it in GitHub Desktop.
Babashka script for updating current day in Gantt EDN-files
(ns update-current-day-in-gantt
"Script for updating current day in Gantt EDN-files"
(:require [babashka.fs :as fs]
[rewrite-clj.node :as node]
[rewrite-clj.parser :as parser]
[rewrite-clj.zip :as z])
(:import (java.time LocalDate)
(java.time.temporal ChronoUnit)))
(defn diff-between-days
"Calculate difference in days between two dates."
[date1 date2]
(let [d1 (if (string? date1) (LocalDate/parse date1) date1)
d2 (if (string? date2) (LocalDate/parse date2) date2)]
(.between ChronoUnit/DAYS d1 d2)))
(defn modify-current-day
"Update current day in file: difference between project start and today"
[^String edn-filename]
(let [edn-string (slurp edn-filename)
nodes (parser/parse-string edn-string)
zloc (z/of-string edn-string)
project-start-date (:project-starts (node/sexpr nodes))]
(when project-start-date
(let [today (LocalDate/now)
diff-days (diff-between-days project-start-date today)]
(when (pos-int? diff-days)
(println (format "%s: project starts %s, diff %s" edn-filename project-start-date diff-days))
(when-let [zip-node (-> zloc (z/find-value z/next :days-after-start))]
(spit edn-filename (-> zip-node
z/next
(z/edit (constantly diff-days))
z/root-string))))))))
(defn update-today
"Modify current date in planes"
[^String plan-path]
(let [edn-files (mapv str (fs/glob plan-path "**.edn"))]
(run! modify-current-day edn-files)))
(if (pos-int? (count *command-line-args*))
(if (fs/exists? (first *command-line-args*))
(update-today (first *command-line-args*))
(println "Path is not exist:" (first *command-line-args*)))
(println "Usage: <this-program> <path-to-edn-files>"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment