jao-compilation.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; jao-compilation.el --- utilities to lauch compilations -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020, 2021, 2022 jao
  3. ;; Author: jao <mail@jao.io>
  4. ;; Keywords: convenience
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Utilities to launch compilation processes from adequate root directories
  17. ;;; Code:
  18. (defvar jao-compilation-dominating-files nil)
  19. (defvar jao-compilation-dominating-file-rxs '(".+\\.cabal"))
  20. (defvar jao-compilation-environment ())
  21. (defvar jao-compilation-dominating-rx "")
  22. (defun jao-compilation--environment ()
  23. (let (result)
  24. (dolist (v jao-compilation-environment result)
  25. (let ((vv (getenv v)))
  26. (when vv (add-to-list 'result (format "%s=%s" v vv)))))))
  27. ;;;###autoload
  28. (defun jao-compilation-add-dominating (&rest fs)
  29. (dolist (f fs) (add-to-list 'jao-compilation-dominating-files f))
  30. (setq jao-compilation-dominating-rx
  31. (concat "\\("
  32. (regexp-opt jao-compilation-dominating-files)
  33. "\\|"
  34. (mapconcat 'identity
  35. jao-compilation-dominating-file-rxs
  36. "\\|")
  37. "\\)$")))
  38. ;;;###autoload
  39. (defun jao-path-relative-to (path base)
  40. (let* ((path (file-name-directory path))
  41. (base (file-name-directory base))
  42. (blen (length base)))
  43. (if (<= (length path) blen)
  44. path
  45. (if (string-equal base (substring path 0 blen))
  46. (substring path blen)
  47. path))))
  48. ;;;###autoload
  49. (defun jao-compilation-find-root (file doms)
  50. (when file
  51. (locate-dominating-file file `(lambda (d)
  52. (when (file-directory-p d)
  53. (directory-files d nil ,doms))))))
  54. ;;;###autoload
  55. (defun jao-compilation-root (&optional dir)
  56. (when-let ((rfn (jao-compilation-find-root (or dir (buffer-file-name))
  57. jao-compilation-dominating-rx)))
  58. (let* ((default-directory (expand-file-name rfn))
  59. (dir (file-name-directory rfn))
  60. (rel-path (jao-path-relative-to dir default-directory)))
  61. (if (and (file-directory-p "build")
  62. (not (file-exists-p "build.xml"))
  63. (not (file-exists-p "setup.py")))
  64. (expand-file-name rel-path (expand-file-name "build/"))
  65. default-directory))))
  66. ;;;###autoload
  67. (defun jao-compilation-root-file ()
  68. (when-let ((dir (jao-compilation-root)))
  69. (car (directory-files dir nil jao-compilation-dominating-rx))))
  70. ;;;###autoload
  71. (defun jao-find-compilation-root (dir)
  72. (when (and (stringp dir) (file-exists-p dir))
  73. (when-let ((root (jao-compilation-root dir)))
  74. (cons 'transient root))))
  75. ;;;###autoload
  76. (defun jao-compilation-env (v)
  77. "Add new environment variables to the compilation environment
  78. used by `jao-compile'"
  79. (add-to-list 'jao-compilation-environment v))
  80. ;;;###autoload
  81. (defun jao-compile ()
  82. "Find the root of current file's project and issue a
  83. compilation command"
  84. (interactive)
  85. (let ((default-directory (or (jao-compilation-root) default-directory))
  86. (compilation-environment (jao-compilation--environment))
  87. (compilation-read-command 'compilation-read-command))
  88. (call-interactively 'compile)))
  89. ;;;###autoload
  90. (defun jao-compilation-setup ()
  91. (jao-compilation-add-dominating
  92. "Makefile" "makefile" "configure.ac" "bootstrap.sh" "aclocal.m4"
  93. "project.clj" "build.xml" "pom.xml" "setup.py" "stack.yaml")
  94. (with-eval-after-load "project"
  95. (add-to-list 'project-find-functions #'jao-find-compilation-root t)))
  96. (provide 'jao-compilation)
  97. ;;; jao-compilation.el ends here