Personal emacs config
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.

235 lines
8.8 KiB

  1. ;;; dockerfile-mode.el --- Major mode for editing Docker's Dockerfiles -*- lexical-binding: t -*-
  2. ;; Copyright (c) 2013 Spotify AB
  3. ;; Package-Requires: ((emacs "24") (s "1.12"))
  4. ;; Package-Version: 1.3
  5. ;; Package-Commit: d31f7685ebc5832d957e25070a930aa42984327d
  6. ;; Homepage: https://github.com/spotify/dockerfile-mode
  7. ;;
  8. ;; Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. ;; use this file except in compliance with the License. You may obtain a copy of
  10. ;; the License at
  11. ;;
  12. ;; http://www.apache.org/licenses/LICENSE-2.0
  13. ;;
  14. ;; Unless required by applicable law or agreed to in writing, software
  15. ;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. ;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. ;; License for the specific language governing permissions and limitations under
  18. ;; the License.
  19. ;;; Commentary:
  20. ;; Provides a major mode `dockerfile-mode' for use with the standard
  21. ;; `Dockerfile' file format. Additional convenience functions allow
  22. ;; images to be built easily.
  23. ;;; Code:
  24. (require 'sh-script)
  25. (require 'rx)
  26. (require 's)
  27. (declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
  28. (defgroup dockerfile nil
  29. "dockerfile code editing commands for Emacs."
  30. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  31. :prefix "dockerfile-"
  32. :group 'languages)
  33. (defcustom dockerfile-mode-hook nil
  34. "*Hook called by `dockerfile-mode'."
  35. :type 'hook
  36. :group 'dockerfile)
  37. (defcustom dockerfile-mode-command "docker"
  38. "Which binary to use to build images"
  39. :group 'dockerfile
  40. :type 'string)
  41. (defcustom dockerfile-use-sudo nil
  42. "Runs docker builder command with sudo."
  43. :type 'boolean
  44. :group 'dockerfile)
  45. (defcustom dockerfile-build-args nil
  46. "List of --build-arg to pass to docker build.
  47. Each element of the list will be passed as a separate
  48. --build-arg to the docker build command."
  49. :type '(repeat string)
  50. :group 'dockerfile)
  51. (defface dockerfile-image-name
  52. '((t (:inherit (font-lock-type-face bold))))
  53. "Face to highlight the base image name after FROM instruction.")
  54. (defface dockerfile-image-alias
  55. '((t (:inherit (font-lock-constant-face bold))))
  56. "Face to highlight the base image alias inf FROM ... AS <alias> construct.")
  57. (defconst dockerfile--from-regex
  58. (rx "from " (group (+? nonl)) (or " " eol) (? "as " (group (1+ nonl)))))
  59. (defvar dockerfile-font-lock-keywords
  60. `(,(cons (rx (or line-start "onbuild ")
  61. (group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
  62. "add" "copy" "entrypoint" "volume" "user" "workdir" "onbuild"
  63. "label" "stopsignal" "shell" "healthcheck"))
  64. word-boundary)
  65. font-lock-keyword-face)
  66. (,dockerfile--from-regex
  67. (1 'dockerfile-image-name)
  68. (2 'dockerfile-image-alias nil t))
  69. ,@(sh-font-lock-keywords)
  70. ,@(sh-font-lock-keywords-2)
  71. ,@(sh-font-lock-keywords-1))
  72. "Default `font-lock-keywords' for `dockerfile mode'.")
  73. (defvar dockerfile-mode-map
  74. (let ((map (make-sparse-keymap))
  75. (menu-map (make-sparse-keymap)))
  76. (define-key map "\C-c\C-b" #'dockerfile-build-buffer)
  77. (define-key map "\C-c\M-b" #'dockerfile-build-no-cache-buffer)
  78. (define-key map "\C-c\C-c" #'comment-region)
  79. (define-key map [menu-bar dockerfile-mode] (cons "Dockerfile" menu-map))
  80. (define-key menu-map [dfc]
  81. '(menu-item "Comment Region" comment-region
  82. :help "Comment Region"))
  83. (define-key menu-map [dfb]
  84. '(menu-item "Build" dockerfile-build-buffer
  85. :help "Send the Dockerfile to docker build"))
  86. (define-key menu-map [dfb]
  87. '(menu-item "Build without cache" dockerfile-build-no-cache-buffer
  88. :help "Send the Dockerfile to docker build without cache"))
  89. map))
  90. (defvar dockerfile-mode-syntax-table
  91. (let ((table (make-syntax-table)))
  92. (modify-syntax-entry ?# "<" table)
  93. (modify-syntax-entry ?\n ">" table)
  94. (modify-syntax-entry ?' "\"" table)
  95. (modify-syntax-entry ?= "." table)
  96. table)
  97. "Syntax table for `dockerfile-mode'.")
  98. (define-abbrev-table 'dockerfile-mode-abbrev-table nil
  99. "Abbrev table used while in `dockerfile-mode'.")
  100. (unless dockerfile-mode-abbrev-table
  101. (define-abbrev-table 'dockerfile-mode-abbrev-table ()))
  102. (defun dockerfile-indent-line-function ()
  103. "Indent lines in a Dockerfile.
  104. Lines beginning with a keyword are ignored, and any others are
  105. indented by one `tab-width'."
  106. (unless (member (get-text-property (point-at-bol) 'face)
  107. '(font-lock-comment-delimiter-face font-lock-keyword-face))
  108. (save-excursion
  109. (beginning-of-line)
  110. (skip-chars-forward "[ \t]" (point-at-eol))
  111. (unless (equal (point) (point-at-eol)) ; Ignore empty lines.
  112. ;; Delete existing whitespace.
  113. (delete-char (- (point-at-bol) (point)))
  114. (indent-to tab-width)))))
  115. (defun dockerfile-build-arg-string ()
  116. "Create a --build-arg string for each element in `dockerfile-build-args'."
  117. (mapconcat (lambda (arg) (concat "--build-arg " (shell-quote-argument arg)))
  118. dockerfile-build-args " "))
  119. (defun dockerfile-standard-filename (file)
  120. "Convert the FILE name to OS standard.
  121. If in Cygwin environment, uses Cygwin specific function to convert the
  122. file name. Otherwise, uses Emacs' standard conversion function."
  123. (if (fboundp 'cygwin-convert-file-name-to-windows)
  124. (s-replace "\\" "\\\\" (cygwin-convert-file-name-to-windows file))
  125. (convert-standard-filename file)))
  126. (defun dockerfile-tag-string (image-name)
  127. "Return a --tag shell-quoted IMAGE-NAME string or an empty string if image-name is blank."
  128. (if (string= image-name "") "" (format "--tag %s " (shell-quote-argument image-name))))
  129. (defvar dockerfile-image-name nil
  130. "Name of the dockerfile currently being used.
  131. This can be set in file or directory-local variables.")
  132. (define-obsolete-variable-alias 'docker-image-name 'dockerfile-image-name)
  133. (defvar dockerfile-image-name-history nil
  134. "History of image names read by `dockerfile-read-image-name'.")
  135. (defun dockerfile-read-image-name ()
  136. "Read a docker image name."
  137. (read-string "Image name: " dockerfile-image-name 'dockerfile-image-name-history))
  138. ;;;###autoload
  139. (defun dockerfile-build-buffer (image-name &optional no-cache)
  140. "Build an image called IMAGE-NAME based upon the buffer.
  141. If prefix arg NO-CACHE is set, don't cache the image.
  142. The build string will be of the format:
  143. `sudo docker build --no-cache --tag IMAGE-NAME --build-args arg1.. -f filename directory`"
  144. (interactive (list (dockerfile-read-image-name) prefix-arg))
  145. (save-buffer)
  146. (compilation-start
  147. (format
  148. "%s%s build %s %s %s -f %s %s"
  149. (if dockerfile-use-sudo "sudo " "")
  150. dockerfile-mode-command
  151. (if no-cache "--no-cache" "")
  152. (dockerfile-tag-string image-name)
  153. (dockerfile-build-arg-string)
  154. (shell-quote-argument (dockerfile-standard-filename (buffer-file-name)))
  155. (shell-quote-argument (dockerfile-standard-filename default-directory)))
  156. nil
  157. (lambda (_) (format "*docker-build-output: %s *" image-name))))
  158. ;;;###autoload
  159. (defun dockerfile-build-no-cache-buffer (image-name)
  160. "Build an image called IMAGE-NAME based upon the buffer without cache."
  161. (interactive (list (dockerfile-read-image-name)))
  162. (dockerfile-build-buffer image-name t))
  163. (defun dockerfile--imenu-function ()
  164. "Find the previous headline from point.
  165. Search for a FROM instruction. If an alias is used this is
  166. returned, otherwise the base image name is used."
  167. (when (re-search-backward dockerfile--from-regex nil t)
  168. (let ((data (match-data)))
  169. (when (match-string 2)
  170. ;; we drop the first match group because
  171. ;; imenu-generic-expression can only use one offset, so we
  172. ;; normalize to `1'.
  173. (set-match-data (list (nth 0 data) (nth 1 data) (nth 4 data) (nth 5 data))))
  174. t)))
  175. ;;;###autoload
  176. (define-derived-mode dockerfile-mode prog-mode "Dockerfile"
  177. "A major mode to edit Dockerfiles.
  178. \\{dockerfile-mode-map}
  179. "
  180. (set-syntax-table dockerfile-mode-syntax-table)
  181. (set (make-local-variable 'imenu-generic-expression)
  182. `(("Stage" dockerfile--imenu-function 1)))
  183. (set (make-local-variable 'require-final-newline) mode-require-final-newline)
  184. (set (make-local-variable 'comment-start) "#")
  185. (set (make-local-variable 'comment-end) "")
  186. (set (make-local-variable 'comment-start-skip) "#+ *")
  187. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  188. (set (make-local-variable 'font-lock-defaults)
  189. '(dockerfile-font-lock-keywords nil t))
  190. (setq local-abbrev-table dockerfile-mode-abbrev-table)
  191. (set (make-local-variable 'indent-line-function) #'dockerfile-indent-line-function))
  192. ;;;###autoload
  193. (add-to-list 'auto-mode-alist '("Dockerfile\\(?:\\..*\\)?\\'" . dockerfile-mode))
  194. (provide 'dockerfile-mode)
  195. ;;; dockerfile-mode.el ends here