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.

193 lines
6.9 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/org-usernames '()
  14. "Auto-generated list of usernames from SFDX that are both authenticated and connected.")
  15. ;; (defvar vlo/default-environment-template "{
  16. ;; \"environments\": [
  17. ;; {
  18. ;; \"env\": \"default\",
  19. ;; \"username\": \"\",
  20. ;; \"job\": \"job.yaml\"
  21. ;; }
  22. ;; ]
  23. ;; }" "Default template for creating the initial .vemacs file.")
  24. (defvar vlo/project-file-name ".vemacs.txt"
  25. "The name of the VlocitEmacs configuration file.
  26. This file exists for the purpose of locating the project folder as there is no
  27. good way of knowing you are in the root vlocity project. It also contains the
  28. list of environments to be used for deployments/exports.")
  29. (defun vlo/generate-project-file ()
  30. "Create the project file and append it to .gitignore if file exists."
  31. (interactive)
  32. (let* ((dir (read-directory-name "Choose Project Root: "))
  33. (project-file (concat dir vlo/project-file-name))
  34. (gitignore (concat dir ".gitignore")))
  35. (with-temp-file project-file (insert ""))
  36. (let ((current-user (vlo/prompt-org-list)))
  37. (with-temp-file project-file (insert current-user))
  38. (when (file-exists-p gitignore)
  39. (append-to-file vlo/project-file-name nil gitignore)))))
  40. (defun vlo/prompt-org-list ()
  41. "Get a list of authenticated orgs via SFDX cli and store the selected user in the project file."
  42. (interactive)
  43. (let* ((project-file (concat (vlo/project-path) vlo/project-file-name))
  44. (temp-json-file
  45. (make-temp-file "sfdx-org-list" nil ".json"
  46. (shell-command-to-string "sfdx force:org:list --json")))
  47. (json-object-type 'hash-table)
  48. (json-array-type 'list)
  49. (json-key-type 'string)
  50. (json (json-read-file temp-json-file))
  51. (result (gethash "result" json))
  52. (orgs (gethash "nonScratchOrgs" result))
  53. )
  54. (setq vlo/org-usernames '())
  55. (dolist (org orgs)
  56. (add-to-list 'vlo/org-usernames (gethash "username" org)))
  57. (let ((current-user (completing-read "SFDX user: " vlo/org-usernames)))
  58. (with-temp-file project-file current-user)
  59. current-user)))
  60. (defun vlo/get-project-user ()
  61. "Return the user stored in the project file."
  62. (interactive)
  63. (if (vlo/in-project)
  64. (let* ((project-dir (concat (vlo/project-path) vlo/project-file-name)))
  65. (with-temp-buffer (insert-file-contents project-dir)
  66. (buffer-string)))
  67. (prog1
  68. nil
  69. (message "Must be in vlocity project for that command"))))
  70. (defun vlo/get-jobfile-name ()
  71. "The name of the job.yaml file."
  72. "job.yaml")
  73. (defun vlo/get-deployment-key ()
  74. "Return the \"key\" used by vlocity to specify what to deploy/export."
  75. (let ((path-list (delete "" (split-string (file-name-directory (buffer-file-name)) "/"))))
  76. (format "%s/%s"
  77. (nth (- (length path-list) 2) path-list)
  78. (nth (- (length path-list) 1) path-list))))
  79. (defun vlo/project-path ()
  80. "Return path to the project file, or nil.
  81. If project file exists in the current working directory, or a
  82. parent directory recursively, return its path. Otherwise, return
  83. nil."
  84. (let ((dir (locate-dominating-file default-directory vlo/project-file-name)))
  85. (unless dir
  86. (error (concat "Error: cannot find " vlo/project-file-name)))
  87. dir))
  88. (defun vlo/in-project ()
  89. "Check if you are currently inside a vlocity project."
  90. (if (locate-dominating-file default-directory vlo/project-file-name)
  91. t
  92. nil))
  93. (defun leo/exec-process (cmd name &optional comint)
  94. "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'."
  95. (let ((compilation-buffer-name-function
  96. (lambda (mode)
  97. (format "*%s*" name))))
  98. (message (concat "Running " cmd))
  99. (compile cmd comint)))
  100. (defun vlo/packExport ()
  101. "Run the packExport command."
  102. (interactive)
  103. (if (and (vlo/in-project)
  104. (file-exists-p (concat (vlo/project-path) (vlo/get-jobfile-name))))
  105. (leo/exec-process
  106. (format "cd %s; vlocity packExport -sfdx.username %s -job %s -key %s"
  107. (vlo/project-path)
  108. (vlo/get-project-user)
  109. (vlo/get-jobfile-name)
  110. (vlo/get-deployment-key)) "vlocity:retrieve" t)
  111. (message "ERROR Exporting:: project: %s, user: %s, job: %s, key: %s"
  112. (vlo/project-path)
  113. (vlo/get-project-user)
  114. (vlo/get-jobfile-name)
  115. (vlo/get-deployment-key))))
  116. (defun vlo/packDeploy ()
  117. "Run the packDeploy command."
  118. (interactive)
  119. (if (and (vlo/in-project)
  120. (file-exists-p (concat (vlo/project-path) (vlo/get-jobfile-name))))
  121. (leo/exec-process
  122. (format "cd %s; vlocity packDeploy -sfdx.username %s -job %s -key %s"
  123. (vlo/project-path)
  124. (vlo/get-project-user)
  125. (vlo/get-jobfile-name)
  126. (vlo/get-deployment-key)) "vlocity:deploy" t)
  127. (message "ERROR Deploying:: project: %s, user: %s, job: %s, key: %s"
  128. (vlo/project-path)
  129. (vlo/get-project-user)
  130. (vlo/get-jobfile-name)
  131. (vlo/get-deployment-key))))
  132. (defun vlo/packList ()
  133. "Run the packGetAllAvailableExports command."
  134. (interactive)
  135. (if (and (vlo/in-project)
  136. (file-exists-p (concat (vlo/project-path) (vlo/get-jobfile-name))))
  137. (leo/exec-process
  138. (format "cd %s; vlocity packGetAllAvailableExports -sfdx.username %s -job %s -type VlocityUITemplate"
  139. (vlo/project-path)
  140. (vlo/get-project-user)
  141. (vlo/get-jobfile-name)) "vlocity:exports" t)
  142. (message "ERROR Retrieving List:: project: %s, user: %s, job: %s"
  143. (vlo/project-path)
  144. (vlo/get-project-user)
  145. (vlo/get-jobfile-name))))
  146. (defun vlo/transient-action ()
  147. "Dynamically choose which transient to show based on if currently in a project."
  148. (interactive)
  149. (if (vlo/in-project)
  150. (vlo/transient-project-action)
  151. (vlo/transient-init-action)))
  152. (define-transient-command vlo/transient-init-action ()
  153. "Vlocity Build Tool CLI Actions"
  154. ["Initialize"
  155. ("c" "Create project file" vlo/generate-project-file)])
  156. (define-transient-command vlo/transient-project-action ()
  157. "Vlocity Build Tool CLI Actions"
  158. ;; ["Environments"
  159. ;; ("a" "Add new org environment" vlo/transient-action)]
  160. ["List"
  161. ("l" "get available list" vlo/packList)]
  162. ["Actions"
  163. ("r" "retrieve" vlo/packExport)
  164. ("d" "deploy" vlo/packDeploy)])
  165. (provide 'vlocitemacs)
  166. ;;; vlocitemacs.el ends here