shell.cljs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (ns frontend.handler.shell
  2. (:require [electron.ipc :as ipc]
  3. [clojure.string :as string]
  4. [logseq.graph-parser.util :as gp-util]
  5. [frontend.handler.notification :as notification]
  6. [promesa.core :as p]
  7. [frontend.db :as db]
  8. [frontend.state :as state]
  9. [frontend.config :as config]))
  10. (defn run-git-command!
  11. [command]
  12. (ipc/ipc "runGit" command))
  13. (defn run-git-command2!
  14. [command]
  15. (ipc/ipc "runGitWithinCurrentGraph" command))
  16. ;; TODO: export to pdf/html/word
  17. (defn run-pandoc-command!
  18. [command]
  19. (ipc/ipc "runPandoc" command))
  20. (defn wrap-notification!
  21. [command f args]
  22. (p/let [result (f args)]
  23. (notification/show!
  24. (if (string/blank? result)
  25. [:p [:code.mr-1 (str command " " args) ]
  26. "was executed successfully!"]
  27. result)
  28. :success
  29. false)))
  30. (defn run-command!
  31. [command]
  32. (let [[command args] (gp-util/split-first " " command)
  33. command (and command (string/lower-case command))]
  34. (when (and (not (string/blank? command)) (not (string/blank? args)))
  35. (let [args (string/trim args)]
  36. (case (keyword command)
  37. :git
  38. (wrap-notification! command run-git-command! args)
  39. ;; :pandoc
  40. ;; (wrap-notification! command run-pandoc-command! args)
  41. (notification/show!
  42. [:div (str command " is not supported yet!")]
  43. :error))))))
  44. ;; git show $REV:$FILE
  45. (defn- get-versioned-file-content
  46. [hash path]
  47. (when (and hash path)
  48. (let [repo (state/get-current-repo)
  49. local-dir (config/get-local-dir repo)
  50. path (string/replace path (str local-dir "/") "")]
  51. (p/let [content (run-git-command! ["show" (str hash ":" path)])]
  52. (state/pub-event! [:modal/display-file-version path content hash])))))
  53. (defn get-file-latest-git-log
  54. [page n]
  55. (when (integer? n)
  56. (let [file-id (:db/id (:block/file page))]
  57. (when-let [path (:file/path (db/entity file-id))]
  58. (p/let [result (run-git-command! ["log" (str "-" n) "--pretty=format:Commit: %C(auto)%h$$$%s$$$%ad" "-p" path])
  59. lines (->> (string/split-lines result)
  60. (filter #(string/starts-with? % "Commit: ")))]
  61. (notification/show! [:div
  62. [:div.font-bold "File history - " path]
  63. (for [line lines]
  64. (let [[hash title time] (string/split line "$$$")
  65. hash (subs hash 8)]
  66. [:div.my-4 {:key hash}
  67. [:hr]
  68. [:div.mb-2
  69. [:a.font-medium.mr-1.inline
  70. {:on-click (fn [] (get-versioned-file-content hash path))}
  71. hash]
  72. title]
  73. [:div.opacity-50 time]]))] :success false))))))
  74. (defn set-git-username-and-email
  75. [username email]
  76. (p/let [_r1 (run-git-command! ["config" "--global" "user.name" username])
  77. _r2 (run-git-command! ["config" "--global" "user.email" email])]
  78. (state/close-modal!)
  79. (notification/show!
  80. [:div "git config successfully!"]
  81. :success)))