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.

253 lines
9.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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"))
  4. ;; Package-Version: 1.4
  5. ;; Package-Commit: ed1d04c89cd8b53963f2dcae7cb3a46967e0abbf
  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. (declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
  27. (defgroup dockerfile nil
  28. "dockerfile code editing commands for Emacs."
  29. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  30. :prefix "dockerfile-"
  31. :group 'languages)
  32. (defcustom dockerfile-mode-hook nil
  33. "*Hook called by `dockerfile-mode'."
  34. :type 'hook
  35. :group 'dockerfile)
  36. (defcustom dockerfile-mode-command "docker"
  37. "Which binary to use to build images"
  38. :group 'dockerfile
  39. :type 'string)
  40. (defcustom dockerfile-use-sudo nil
  41. "Runs docker builder command with sudo."
  42. :type 'boolean
  43. :group 'dockerfile)
  44. (defcustom dockerfile-build-args nil
  45. "List of --build-arg to pass to docker build.
  46. Each element of the list will be passed as a separate
  47. --build-arg to the docker build command."
  48. :type '(repeat string)
  49. :group 'dockerfile)
  50. (defcustom dockerfile-use-buildkit nil
  51. "If t use Docker buildkit for building images
  52. This is the new buildsystem for docker, and in time it will replace the old one
  53. but for now it has to be explicitly enabled to work.
  54. It is supported from docker 18.09"
  55. :type 'boolean)
  56. (defcustom dockerfile-indent-offset (or standard-indent 2)
  57. "Dockerfile number of columns for margin-changing functions to indent."
  58. :type 'integer
  59. :safe #'integerp
  60. :group 'dockerfile)
  61. (defface dockerfile-image-name
  62. '((t (:inherit (font-lock-type-face bold))))
  63. "Face to highlight the base image name after FROM instruction.")
  64. (defface dockerfile-image-alias
  65. '((t (:inherit (font-lock-constant-face bold))))
  66. "Face to highlight the base image alias inf FROM ... AS <alias> construct.")
  67. (defconst dockerfile--from-regex
  68. (rx "from " (group (+? nonl)) (or " " eol) (? "as " (group (1+ nonl)))))
  69. (defvar dockerfile-font-lock-keywords
  70. `(,(cons (rx (or line-start "onbuild ")
  71. (group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
  72. "add" "copy" "entrypoint" "volume" "user" "workdir" "onbuild"
  73. "label" "stopsignal" "shell" "healthcheck"))
  74. word-boundary)
  75. font-lock-keyword-face)
  76. (,dockerfile--from-regex
  77. (1 'dockerfile-image-name)
  78. (2 'dockerfile-image-alias nil t))
  79. ,@(sh-font-lock-keywords)
  80. ,@(sh-font-lock-keywords-2)
  81. ,@(sh-font-lock-keywords-1))
  82. "Default `font-lock-keywords' for `dockerfile mode'.")
  83. (defvar dockerfile-mode-map
  84. (let ((map (make-sparse-keymap))
  85. (menu-map (make-sparse-keymap)))
  86. (define-key map "\C-c\C-b" #'dockerfile-build-buffer)
  87. (define-key map "\C-c\M-b" #'dockerfile-build-no-cache-buffer)
  88. (define-key map "\C-c\C-c" #'comment-region)
  89. (define-key map [menu-bar dockerfile-mode] (cons "Dockerfile" menu-map))
  90. (define-key menu-map [dfc]
  91. '(menu-item "Comment Region" comment-region
  92. :help "Comment Region"))
  93. (define-key menu-map [dfb]
  94. '(menu-item "Build" dockerfile-build-buffer
  95. :help "Send the Dockerfile to docker build"))
  96. (define-key menu-map [dfb]
  97. '(menu-item "Build without cache" dockerfile-build-no-cache-buffer
  98. :help "Send the Dockerfile to docker build without cache"))
  99. map))
  100. (defvar dockerfile-mode-syntax-table
  101. (let ((table (make-syntax-table)))
  102. (modify-syntax-entry ?# "<" table)
  103. (modify-syntax-entry ?\n ">" table)
  104. (modify-syntax-entry ?' "\"" table)
  105. (modify-syntax-entry ?= "." table)
  106. table)
  107. "Syntax table for `dockerfile-mode'.")
  108. (define-abbrev-table 'dockerfile-mode-abbrev-table nil
  109. "Abbrev table used while in `dockerfile-mode'.")
  110. (unless dockerfile-mode-abbrev-table
  111. (define-abbrev-table 'dockerfile-mode-abbrev-table ()))
  112. (defun dockerfile-indent-line-function ()
  113. "Indent lines in a Dockerfile.
  114. Lines beginning with a keyword are ignored, and any others are
  115. indented by one `dockerfile-indent-offset'."
  116. (unless (member (get-text-property (point-at-bol) 'face)
  117. '(font-lock-comment-delimiter-face font-lock-keyword-face))
  118. (save-excursion
  119. (beginning-of-line)
  120. (skip-chars-forward "[ \t]" (point-at-eol))
  121. (unless (equal (point) (point-at-eol)) ; Ignore empty lines.
  122. ;; Delete existing whitespace.
  123. (delete-char (- (point-at-bol) (point)))
  124. (indent-to dockerfile-indent-offset)))))
  125. (defun dockerfile-build-arg-string ()
  126. "Create a --build-arg string for each element in `dockerfile-build-args'."
  127. (mapconcat (lambda (arg) (concat "--build-arg " (shell-quote-argument arg)))
  128. dockerfile-build-args " "))
  129. (defun dockerfile-standard-filename (file)
  130. "Convert the FILE name to OS standard.
  131. If in Cygwin environment, uses Cygwin specific function to convert the
  132. file name. Otherwise, uses Emacs' standard conversion function."
  133. (if (fboundp 'cygwin-convert-file-name-to-windows)
  134. (replace-regexp-in-string
  135. (rx "\\") "\\\\" (cygwin-convert-file-name-to-windows file) t t)
  136. (convert-standard-filename file)))
  137. (defun dockerfile-tag-string (image-name)
  138. "Return a --tag shell-quoted IMAGE-NAME string or an empty string if image-name is blank."
  139. (if (string= image-name "") "" (format "--tag %s " (shell-quote-argument image-name))))
  140. (defvar dockerfile-image-name nil
  141. "Name of the dockerfile currently being used.
  142. This can be set in file or directory-local variables.")
  143. (define-obsolete-variable-alias 'docker-image-name 'dockerfile-image-name "2017-10-22")
  144. (defvar dockerfile-image-name-history nil
  145. "History of image names read by `dockerfile-read-image-name'.")
  146. (defun dockerfile-read-image-name ()
  147. "Read a docker image name."
  148. (read-string "Image name: " dockerfile-image-name 'dockerfile-image-name-history))
  149. ;;;###autoload
  150. (defun dockerfile-build-buffer (image-name &optional no-cache)
  151. "Build an image called IMAGE-NAME based upon the buffer.
  152. If prefix arg NO-CACHE is set, don't cache the image.
  153. The build string will be of the format:
  154. `sudo docker build --no-cache --tag IMAGE-NAME --build-args arg1.. -f filename directory`"
  155. (interactive (list (dockerfile-read-image-name) prefix-arg))
  156. (save-buffer)
  157. (compilation-start
  158. (format
  159. "%s%s%s build %s %s %s -f %s %s"
  160. (if dockerfile-use-buildkit "DOCKER_BUILDKIT=1 " "")
  161. (if dockerfile-use-sudo "sudo " "")
  162. dockerfile-mode-command
  163. (if no-cache "--no-cache" "")
  164. (dockerfile-tag-string image-name)
  165. (dockerfile-build-arg-string)
  166. (shell-quote-argument (dockerfile-standard-filename (buffer-file-name)))
  167. (shell-quote-argument (dockerfile-standard-filename default-directory)))
  168. nil
  169. (lambda (_) (format "*docker-build-output: %s *" image-name))))
  170. ;;;###autoload
  171. (defun dockerfile-build-no-cache-buffer (image-name)
  172. "Build an image called IMAGE-NAME based upon the buffer without cache."
  173. (interactive (list (dockerfile-read-image-name)))
  174. (dockerfile-build-buffer image-name t))
  175. (defun dockerfile--imenu-function ()
  176. "Find the previous headline from point.
  177. Search for a FROM instruction. If an alias is used this is
  178. returned, otherwise the base image name is used."
  179. (when (re-search-backward dockerfile--from-regex nil t)
  180. (let ((data (match-data)))
  181. (when (match-string 2)
  182. ;; we drop the first match group because
  183. ;; imenu-generic-expression can only use one offset, so we
  184. ;; normalize to `1'.
  185. (set-match-data (list (nth 0 data) (nth 1 data) (nth 4 data) (nth 5 data))))
  186. t)))
  187. ;;;###autoload
  188. (define-derived-mode dockerfile-mode prog-mode "Dockerfile"
  189. "A major mode to edit Dockerfiles.
  190. \\{dockerfile-mode-map}
  191. "
  192. (set-syntax-table dockerfile-mode-syntax-table)
  193. (set (make-local-variable 'imenu-generic-expression)
  194. `(("Stage" dockerfile--imenu-function 1)))
  195. (set (make-local-variable 'require-final-newline) mode-require-final-newline)
  196. (set (make-local-variable 'comment-start) "#")
  197. (set (make-local-variable 'comment-end) "")
  198. (set (make-local-variable 'comment-start-skip) "#+ *")
  199. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  200. (set (make-local-variable 'font-lock-defaults)
  201. '(dockerfile-font-lock-keywords nil t))
  202. (setq local-abbrev-table dockerfile-mode-abbrev-table)
  203. (set (make-local-variable 'indent-line-function) #'dockerfile-indent-line-function))
  204. ;;;###autoload
  205. (add-to-list 'auto-mode-alist '("/Dockerfile\\(?:\\..*\\)?\\'" . dockerfile-mode))
  206. ;;;###autoload
  207. (add-to-list 'auto-mode-alist '("\\.dockerfile\\'" . dockerfile-mode))
  208. (provide 'dockerfile-mode)
  209. ;;; dockerfile-mode.el ends here