My attempt to optimize my emacs load time <1 second
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
9.3 KiB

  1. ;;; vlocitemacs.el --- Vlocity Build Tool :: Emacs integration
  2. ;;; Commentary:
  3. ;;
  4. ;; Essentially the following commands are abstracted into elisp and made dynamic:
  5. ;;
  6. ;; vlocity packDeploy -propertyfile build.properties -job job.yaml -key VlocityUITemplate/Test-test
  7. ;; vlocity packExport -propertyfile build.properties -job job.yaml -key VlocityUITemplate/Test-test
  8. ;;
  9. ;;
  10. ;;; Code:
  11. (require 'transient)
  12. (require 'json)
  13. (defvar vlo/project-file-name ".vemacs.txt"
  14. "The name of the VlocitEmacs configuration file.
  15. This file exists for the purpose of locating the project folder as there is no
  16. good way of knowing you are in the root vlocity project. It also contains the
  17. list of environments to be used for deployments/exports.")
  18. (defvar vlo/project-jobfile-template "projectPath: ."
  19. "Bare-bones template to use for =job.yaml=.")
  20. ;; DONE
  21. (defun vlo/generate-project-file ()
  22. "Create the project file and append it to .gitignore if file exists."
  23. (interactive)
  24. (let* ((dir (read-directory-name "Choose Project Root: "))
  25. (project-file (concat dir vlo/project-file-name))
  26. (job-file (concat dir "job.yaml"))
  27. (gitignore (concat dir ".gitignore")))
  28. ;; Create job.yaml if it doesn't exist
  29. (when (not (file-exists-p job-file))
  30. (with-temp-file job-file (insert vlo/project-jobfile-template)))
  31. ;; Create .vemacs.txt
  32. (with-temp-file project-file (insert ""))
  33. ;; Insert username into .vemacs.txt
  34. (let ((current-user (vlo/prompt-org-list)))
  35. (with-temp-file project-file (insert current-user))
  36. )
  37. ;; Add .vemacs.txt to .gitignore
  38. (when (file-exists-p gitignore)
  39. (append-to-file vlo/project-file-name nil gitignore))))
  40. ;; DONE
  41. (defun vlo/prompt-org-list ()
  42. "Get a list of authenticated orgs via SFDX cli and store the selected user in the project file."
  43. (interactive)
  44. (let* ((project-file (concat (vlo/project-path) vlo/project-file-name))
  45. (temp-json-file
  46. (make-temp-file "sfdx-org-list" nil ".json"
  47. (shell-command-to-string "sfdx force:org:list --json")))
  48. (json-object-type 'hash-table)
  49. (json-array-type 'list)
  50. (json-key-type 'string)
  51. (json (json-read-file temp-json-file))
  52. (result (gethash "result" json))
  53. (orgs (gethash "nonScratchOrgs" result))
  54. (usernames '())
  55. )
  56. (dolist (org orgs)
  57. (add-to-list 'usernames (gethash "username" org)))
  58. (let ((current-user (completing-read "SFDX user: " usernames)))
  59. (with-temp-file project-file current-user)
  60. current-user)))
  61. ;; DONE
  62. (defun vlo/in-vlocity-project ()
  63. "Check if you are currently inside a vlocity project."
  64. (if (vlo/project-path)
  65. t
  66. nil))
  67. ;; DONE
  68. (defun vlo/project-path ()
  69. "Return path to the project file, or nil.
  70. If project file exists in the current working directory, or a
  71. parent directory recursively, return its path. Otherwise, return
  72. nil."
  73. (locate-dominating-file default-directory vlo/project-file-name))
  74. ;; DONE
  75. (defun vlo/get-project-user ()
  76. "Return the user stored in the project file."
  77. (if (vlo/in-vlocity-project)
  78. (let ((project-dir (concat (vlo/project-path) vlo/project-file-name)))
  79. (with-temp-buffer (insert-file-contents project-dir) (buffer-string)))
  80. nil))
  81. ;; DONE
  82. (defun vlo/get-jobfile-name ()
  83. "The name of the job.yaml file."
  84. "job.yaml")
  85. ;; DONE
  86. (defun vlo/get-deployment-key ()
  87. "Return the Name of the component dynamically from the =_DataPpack.json= file."
  88. (let ((datapack-file (expand-file-name (concat (file-name-base) "_DataPack.json"))))
  89. (if (file-exists-p datapack-file)
  90. (let* ((json-object-type 'hash-table)
  91. (json-array-type 'list)
  92. (json-key-type 'string)
  93. (json (json-read-file datapack-file))
  94. (component-name (gethash "Name" json)))
  95. (concat "VlocityUITemplate/" component-name))
  96. nil)))
  97. ;; DONE
  98. (defun vlo/exec-process (cmd name &optional comint)
  99. "Execute a process running CMD and use NAME to generate a unique buffer name and optionally pass COMINT as t to put buffer in `comint-mode'."
  100. (let ((compilation-buffer-name-function
  101. (lambda (mode)
  102. (format "*%s*" name))))
  103. (message (concat "Running " cmd))
  104. (compile cmd comint)))
  105. (defun vlo/packExport (username job &optional key)
  106. "Run the packExport command with sfdx USERNAME or alias using the JOB file.
  107. Optionally specifying KEY to export. If KEY is nil, this command will run
  108. packExport using job.yaml provided (i.e. export all)."
  109. (if (and (vlo/in-vlocity-project)
  110. (file-exists-p (concat (vlo/project-path) job)))
  111. (let ((cmd
  112. (if key
  113. (format "cd %s; vlocity packExport -sfdx.username %s -job %s -key %s"
  114. (vlo/project-path)
  115. username
  116. job
  117. key)
  118. (format "cd %s; vlocity packExport -sfdx.username %s -job %s"
  119. (vlo/project-path)
  120. username
  121. job)
  122. )))
  123. (vlo/exec-process cmd "vlocity:retrieve" t))
  124. (message "ERROR Exporting:: project: %s, user: %s, job: %s, key: %s"
  125. (vlo/project-path)
  126. username
  127. job
  128. key)))
  129. (defun vlo/packDeploy (username job &optional key)
  130. "Run the packDeploy command with sfdx USERNAME or alias using the JOB file.
  131. Optionally specifying KEY to export. If KEY is nil, this command will run
  132. packDeploy using job.yaml provided (i.e. deploy all)."
  133. (if (and (vlo/in-vlocity-project)
  134. (file-exists-p (concat (vlo/project-path) job)))
  135. (let ((cmd
  136. (if key
  137. (format "cd %s; vlocity packDeploy -sfdx.username %s -job %s -key %s"
  138. (vlo/project-path)
  139. username
  140. job
  141. key)
  142. (format "cd %s; vlocity packDeploy -sfdx.username %s -job %s"
  143. (vlo/project-path)
  144. username
  145. job)
  146. )))
  147. (vlo/exec-process cmd "vlocity:retrieve" t))
  148. (message "ERROR Deploying:: project: %s, user: %s, job: %s, key: %s"
  149. (vlo/project-path)
  150. username
  151. job
  152. key)))
  153. (defun vlo/packSearch (username job)
  154. "Run the packGetAllAvailableExports command with USERNAME and JOB file."
  155. (if (and (vlo/in-vlocity-project)
  156. (file-exists-p (concat (vlo/project-path) job)))
  157. (vlo/exec-process
  158. (format "cd %s; vlocity packGetAllAvailableExports -sfdx.username %s -job %s -type VlocityUITemplate"
  159. (vlo/project-path)
  160. username
  161. job) "vlocity:exports" t)
  162. (message "ERROR Retrieving List:: project: %s, user: %s, job: %s"
  163. (vlo/project-path)
  164. username
  165. job)))
  166. (defun vlo/createDatapack ()
  167. "Description."
  168. (interactive)
  169. (message "TODO :: Creating..."))
  170. (defun vlo/search ()
  171. "Description."
  172. (interactive)
  173. (message "TODO :: Searching..."))
  174. ;; DONE
  175. (defun vlo/exportThisAction ()
  176. "Destructively retrieve this component."
  177. (interactive)
  178. (let ((key (vlo/get-deployment-key)))
  179. (if (yes-or-no-p "Retrieve \"%s\"? (THIS WILL OVERWRITE LOCAL CHANGES!) ")
  180. (progn
  181. (message "Retrieving \"%s\"..." key)
  182. (vlo/packExport (vlo/get-project-user) (vlo/get-jobfile-name) key))
  183. (message "Cancelled Retrieve"))))
  184. ;; DONE
  185. (defun vlo/exportAllAction ()
  186. "Description."
  187. (interactive)
  188. (if (yes-or-no-p "Retrieve ALL DataPacks in Manifest? (THIS WILL OVERWRITE LOCAL CHANGES!) ")
  189. (progn
  190. (message "Retrieving ALL DataPacks in Manifest file...")
  191. (vlo/packExport (vlo/get-project-user) (vlo/get-jobfile-name)))
  192. (message "Cancelled Retrieve")))
  193. ;; DONE
  194. (defun vlo/deployThisAction ()
  195. "Description."
  196. (interactive)
  197. (let ((key (vlo/get-deployment-key)))
  198. (if key
  199. (progn
  200. (vlo/packDeploy (vlo/get-project-user) (vlo/get-jobfile-name) key)
  201. (message "Deploying \"%s\"..." key))
  202. (message "No DataPack file found for this component!"))))
  203. ;; DONE
  204. (defun vlo/deployAllAction ()
  205. "Description."
  206. (interactive)
  207. (if (yes-or-no-p "Are you sure you wish to deploy everything? ")
  208. (vlo/packDeploy (vlo/get-project-user) (vlo/get-jobfile-name))
  209. (message "Cancelled")))
  210. (defun vlo/transient-action ()
  211. "Dynamically choose which transient to show based on if currently in a project."
  212. (interactive)
  213. (if (vlo/in-vlocity-project)
  214. (vlo/transient-project-action)
  215. (vlo/transient-init-action)))
  216. (define-transient-command vlo/transient-init-action ()
  217. "Vlocity Build Tool CLI Actions"
  218. ["Vlocity Project file not created"
  219. ("i" "Initialize Vlocity Project" vlo/generate-project-file)])
  220. (define-transient-command vlo/transient-project-action ()
  221. "Vlocity Build Tool CLI Actions"
  222. ["Create"
  223. ("c" "Create a new datapack (dynamically)" vlo/createDatapack)]
  224. ["Retrieve"
  225. ("s" "Search for new datapack" vlo/search)
  226. ("r" "refresh this datapack (destructive)" vlo/exportThisAction)
  227. ("R" "refresh all local datapacks (destructive)" vlo/exportAllAction)
  228. ]
  229. ["Deploy"
  230. ("d" "deploy this datapack" vlo/deployThisAction)
  231. ("D" "deploy all local datapacks" vlo/deployAllAction)
  232. ])
  233. (provide 'vlocitemacs)
  234. ;;; vlocitemacs.el ends here