huge cleanup - wip

This commit is contained in:
ignuki
2018-09-20 21:19:32 +02:00
parent a83556a9d4
commit 120ea05ea1
34 changed files with 318 additions and 774 deletions

View File

@@ -55,13 +55,27 @@ Each element of the list will be passed as a separate
:type '(repeat string)
:group 'dockerfile)
(defface dockerfile-image-name
'((t (:inherit (font-lock-type-face bold))))
"Face to highlight the base image name after FROM instruction.")
(defface dockerfile-image-alias
'((t (:inherit (font-lock-constant-face bold))))
"Face to highlight the base image alias inf FROM ... AS <alias> construct.")
(defconst dockerfile--from-regex
(rx "from " (group (+? nonl)) (or " " eol) (? "as " (group (1+ nonl)))))
(defvar dockerfile-font-lock-keywords
`(,(cons (rx (or line-start "onbuild ")
(group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
"add" "copy" "entrypoint" "volume" "user" "workdir" "onbuild"
"label" "stopsignal" "shell" "healthcheck"))
word-boundary)
font-lock-keyword-face)
(group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
"add" "copy" "entrypoint" "volume" "user" "workdir" "onbuild"
"label" "stopsignal" "shell" "healthcheck"))
word-boundary)
font-lock-keyword-face)
(,dockerfile--from-regex
(1 'dockerfile-image-name)
(2 'dockerfile-image-alias nil t))
,@(sh-font-lock-keywords)
,@(sh-font-lock-keywords-2)
,@(sh-font-lock-keywords-1))
@@ -69,7 +83,7 @@ Each element of the list will be passed as a separate
(defvar dockerfile-mode-map
(let ((map (make-sparse-keymap))
(menu-map (make-sparse-keymap)))
(menu-map (make-sparse-keymap)))
(define-key map "\C-c\C-b" 'dockerfile-build-buffer)
(define-key map "\C-c\M-b" 'dockerfile-build-no-cache-buffer)
(define-key map "\C-c\C-z" 'dockerfile-test-function)
@@ -77,13 +91,13 @@ Each element of the list will be passed as a separate
(define-key map [menu-bar dockerfile-mode] (cons "Dockerfile" menu-map))
(define-key menu-map [dfc]
'(menu-item "Comment Region" comment-region
:help "Comment Region"))
:help "Comment Region"))
(define-key menu-map [dfb]
'(menu-item "Build" dockerfile-build-buffer
:help "Send the Dockerfile to docker build"))
:help "Send the Dockerfile to docker build"))
(define-key menu-map [dfb]
'(menu-item "Build without cache" dockerfile-build-no-cache-buffer
:help "Send the Dockerfile to docker build without cache"))
:help "Send the Dockerfile to docker build without cache"))
map))
(defvar dockerfile-mode-syntax-table
@@ -101,10 +115,25 @@ Each element of the list will be passed as a separate
(unless dockerfile-mode-abbrev-table
(define-abbrev-table 'dockerfile-mode-abbrev-table ()))
(defun dockerfile-indent-line-function ()
"Indent lines in a Dockerfile.
Lines beginning with a keyword are ignored, and any others are
indented by one `tab-width'."
(unless (eq (get-text-property (point-at-bol) 'face)
'font-lock-keyword-face)
(save-excursion
(beginning-of-line)
(skip-chars-forward "[ \t]" (point-at-eol))
(unless (equal (point) (point-at-eol)) ; Ignore empty lines.
;; Delete existing whitespace.
(delete-char (- (point-at-bol) (point)))
(indent-to tab-width)))))
(defun dockerfile-build-arg-string ()
"Create a --build-arg string for each element in `dockerfile-build-args'."
(mapconcat (lambda (arg) (concat "--build-arg " (shell-quote-argument arg)))
dockerfile-build-args " "))
dockerfile-build-args " "))
(defun dockerfile-standard-filename (file)
"Convert the FILE name to OS standard.
@@ -136,13 +165,13 @@ If prefix arg NO-CACHE is set, don't cache the image."
(if (stringp image-name)
(compilation-start
(format
"%sdocker build %s -t %s %s -f %s %s"
(if dockerfile-use-sudo "sudo " "")
(if no-cache "--no-cache" "")
(shell-quote-argument image-name)
(dockerfile-build-arg-string)
(shell-quote-argument (dockerfile-standard-filename (buffer-file-name)))
(shell-quote-argument (dockerfile-standard-filename default-directory)))
"%sdocker build %s -t %s %s -f %s %s"
(if dockerfile-use-sudo "sudo " "")
(if no-cache "--no-cache" "")
(shell-quote-argument image-name)
(dockerfile-build-arg-string)
(shell-quote-argument (dockerfile-standard-filename (buffer-file-name)))
(shell-quote-argument (dockerfile-standard-filename default-directory)))
nil
(lambda (_) (format "*docker-build-output: %s *" image-name)))
(print "dockerfile-image-name must be a string, consider surrounding it with double quotes")))
@@ -153,12 +182,28 @@ If prefix arg NO-CACHE is set, don't cache the image."
(interactive (list (dockerfile-read-image-name)))
(dockerfile-build-buffer image-name t))
(defun dockerfile--imenu-function ()
"Find the previous headline from point.
Search for a FROM instruction. If an alias is used this is
returned, otherwise the base image name is used."
(when (re-search-backward dockerfile--from-regex nil t)
(let ((data (match-data)))
(when (match-string 2)
;; we drop the first match group because
;; imenu-generic-expression can only use one offset, so we
;; normalize to `1'.
(set-match-data (list (nth 0 data) (nth 1 data) (nth 4 data) (nth 5 data))))
t)))
;;;###autoload
(define-derived-mode dockerfile-mode prog-mode "Dockerfile"
"A major mode to edit Dockerfiles.
\\{dockerfile-mode-map}
"
(set-syntax-table dockerfile-mode-syntax-table)
(set (make-local-variable 'imenu-generic-expression)
`(("Stage" dockerfile--imenu-function 1)))
(set (make-local-variable 'require-final-newline) mode-require-final-newline)
(set (make-local-variable 'comment-start) "#")
(set (make-local-variable 'comment-end) "")
@@ -166,7 +211,8 @@ If prefix arg NO-CACHE is set, don't cache the image."
(set (make-local-variable 'parse-sexp-ignore-comments) t)
(set (make-local-variable 'font-lock-defaults)
'(dockerfile-font-lock-keywords nil t))
(setq local-abbrev-table dockerfile-mode-abbrev-table))
(setq local-abbrev-table dockerfile-mode-abbrev-table)
(setq indent-line-function #'dockerfile-indent-line-function))
;;;###autoload
(add-to-list 'auto-mode-alist '("Dockerfile\\(?:\\..*\\)?\\'" . dockerfile-mode))