auto-load-project-el/auto-load-project.el

72 lines
2.6 KiB
EmacsLisp
Raw Permalink Normal View History

2019-08-19 21:59:22 +00:00
;;; auto-load-project.el --- Auto load elisp file on project open
;; Copyright © 2019 Yann Esposito <yann@esposito.host>
;;; Commentary:
;;
;;; Code:
(require 'projectile)
2019-08-20 12:53:12 +00:00
(defvar auto-load-project/trusted-gpg-key-fingerprints
2019-08-19 21:59:22 +00:00
'()
"The list of GPG fingerprint you trust when decrypting a gpg file.
You can retrieve the fingerprints of your own private keys
with: `gpg --list-secret-keys' (take care of removing the
2019-08-20 12:53:12 +00:00
spaces when copy/pasting here)")
2019-08-19 21:59:22 +00:00
2019-08-20 12:53:12 +00:00
(defun auto-load-project/get-sign-key (file)
2019-08-19 21:59:22 +00:00
"Return the fingerprint of they key that signed FILE.
To sign a file you should used
`gpg --local-user my@email --output project.el.sig --detach-sign project.el`"
(string-trim-right
(shell-command-to-string
(concat
2019-08-20 13:15:16 +00:00
(format "gpg --status-fd 1 --verify %s.sig %s 2>/dev/null " file file)
2019-08-19 21:59:22 +00:00
"|grep VALIDSIG"
"|awk '{print $3}'"))))
2019-08-20 12:53:12 +00:00
(defun auto-load-project/trusted-gpg-origin-p (file)
2019-08-19 21:59:22 +00:00
"Return non-nil if the FILE is encrypted with a trusted key."
2019-08-20 12:53:12 +00:00
(member (auto-load-project/get-sign-key file)
auto-load-project/trusted-gpg-key-fingerprints))
2019-08-19 21:59:22 +00:00
2019-08-20 12:53:12 +00:00
(defconst auto-load-project/project-file "project.el"
2019-08-19 21:59:22 +00:00
"Project configuration file name.")
2019-08-20 12:53:12 +00:00
(defvar auto-load-project/loaded-projects (list)
"Projects that have been loaded by `auto-load-project/load'.")
(defun auto-load-project/load ()
"Loads the `auto-load-project/project-file' for a project.
This is run once the project is loaded signifying project setup."
(interactive)
(when (projectile-project-p)
(lexical-let* ((current-project-root (projectile-project-root))
2019-08-20 13:15:16 +00:00
(project-init-file (expand-file-name auto-load-project/project-file current-project-root))
(project-sign-file (concat project-init-file ".sig")))
2019-08-20 12:53:12 +00:00
(when (and (not (member current-project-root auto-load-project/loaded-projects))
(file-exists-p project-init-file)
2019-08-20 13:15:16 +00:00
(file-exists-p project-sign-file)
2019-08-20 12:53:12 +00:00
(auto-load-project/trusted-gpg-origin-p project-init-file))
(message "Loading project init file for %s" (projectile-project-name))
(condition-case ex
(progn (load project-init-file)
(add-to-list 'auto-load-project/loaded-projects current-project-root)
(message "%s loaded successfully" project-init-file))
('error
(message
"There was an error loading %s: %s"
project-init-file
(error-message-string ex))))))))
(add-hook 'find-file-hook #'auto-load-project/load t)
(add-hook 'dired-mode-hook #'auto-load-project/load t)
2019-08-19 21:59:22 +00:00
(provide 'auto-load-project)
;;; auto-load-project.el ends here