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.

179 lines
6.0 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.json"
  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 vlo/default-environment-template))
  36. (when (file-exists-p gitignore)
  37. (append-to-file vlo/project-file-name nil gitignore))))
  38. (defun vlo/get-org-list ()
  39. "Get a list of authenticated orgs via SFDX cli and populate the `vlo/org-usernames' variable."
  40. (interactive)
  41. (let* ((temp-json-file
  42. (make-temp-file "sfdx-org-list" nil ".json"
  43. (shell-command-to-string "sfdx force:org:list --json")))
  44. (json-object-type 'hash-table)
  45. (json-array-type 'list)
  46. (json-key-type 'string)
  47. (json (json-read-file temp-json-file))
  48. (result (gethash "result" json))
  49. (orgs (gethash "nonScratchOrgs" result))
  50. )
  51. ;; (message "%s" orgs)
  52. (setq vlo/org-usernames '())
  53. (dolist (org orgs)
  54. (add-to-list 'vlo/org-usernames (gethash "username" org)))
  55. )
  56. )
  57. ;; (defun vlo/get-environment-list (&optional key)
  58. ;; "Get the list of org environment vars from within vlo/project-file-name JSON file with optional KEY to get a list of keys."
  59. ;; (let* ())
  60. ;; )
  61. (defun vlo/getuser ()
  62. "Prompt for sfdx username or use stored."
  63. (interactive)
  64. (let ((username (completing-read "SFDX User: " vlo/org-usernames nil t nil nil)))
  65. (if (yes-or-no-p (format "Set \"%s\" as default for this project?" username))
  66. ;;insert username into project file
  67. (progn
  68. (with-temp-file (concat (vlo/project-path) vlo/project-file-name) (insert username))
  69. (message "Default set to: %s" username)
  70. )
  71. (message "No default set"))))
  72. (defun vlo/get-deployment-key ()
  73. "Return the \"key\" used by vlocity to specify what to deploy/export."
  74. (vlo/ensure-init-file)
  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. "Asserts that you're currently inside a vlocity project."
  90. (let ((dir (locate-dominating-file default-directory vlo/project-file-name)))
  91. (if dir
  92. t
  93. nil)))
  94. (defun leo/exec-process (cmd &optional comint)
  95. "Execute a process running CMD and optionally pass COMINT as t to put buffer in `comint-mode'."
  96. (let ((compilation-buffer-name-function
  97. (lambda (mode)
  98. (format "*exec-process*"))))
  99. (message (concat "Running " cmd))
  100. (compile cmd comint)))
  101. (defun vlo/packExport ()
  102. "Run the packExport command."
  103. (interactive)
  104. (if (and (vlo/in-project)
  105. (file-exists-p (vlo/get-propertyfile-name))
  106. (file-exists-p (vlo/get-jobfile-name)))
  107. (leo/exec-process
  108. (format "cd %s; vlocity packExport -sfdx.username %s -job %s -key %s"
  109. (vlo/project-path)
  110. (vlo/get-user)
  111. (vlo/get-jobfile-name)
  112. (vlo/get-deployment-key)))
  113. (error (format "Error: cannot find either %s or %s" (vlo/get-propertyfile-name) (vlo/get-jobfile-name)))))
  114. (defun vlo/packDeploy ()
  115. "Run the packDeploy command."
  116. (interactive)
  117. (if (and (vlo/in-project)
  118. (file-exists-p (vlo/get-propertyfile-name))
  119. (file-exists-p (vlo/get-jobfile-name)))
  120. (leo/exec-process
  121. (format "cd %s; vlocity packDeploy -sfdx.username %s -job %s -key %s"
  122. (vlo/project-path)
  123. (vlo/get-user)
  124. (vlo/get-jobfile-name)
  125. (vlo/get-deployment-key)))
  126. (error (format "Error: cannot find either %s or %s" (vlo/get-propertyfile-name) (vlo/get-jobfile-name)))))
  127. (defun vlo/transient-action ()
  128. "Dynamically choose which transient to show based on if currently in a project."
  129. (interactive)
  130. (if (vlo/in-project)
  131. (vlo/transient-project-action)
  132. (vlo/transient-init-action)))
  133. (define-transient-command vlo/transient-init-action ()
  134. "Vlocity Build Tool CLI Actions"
  135. ["Initialize"
  136. ("i" "Create .vemacs in project root" vlo/generate-project-file)]
  137. )
  138. (define-transient-command vlo/transient-project-action ()
  139. "Vlocity Build Tool CLI Actions"
  140. ["Environments"
  141. ("a" "Add new org environment" vlo/transient-action)]
  142. ["Actions"
  143. ("e" "export" vlo/packExport)
  144. ("d" "deploy" vlo/packDeploy)])
  145. (provide 'vlocitemacs)
  146. ;;; vlocitemacs.el ends here