Skip to content

Commit e4ce372

Browse files
ficristomarijnh
authored andcommitted
[Dockerfile mode] Some improvements
add more instructions highlight instructions as 'keyword' highlight 'from ... as' add tests
1 parent b7711f6 commit e4ce372

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

mode/dockerfile/dockerfile.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"use strict";
1313

1414
// Collect all Dockerfile directives
15-
var instructions = ["from", "maintainer", "run", "cmd", "expose", "env",
15+
var instructions = ["arg", "from", "maintainer", "run", "cmd", "label", "expose", "env",
1616
"add", "copy", "entrypoint", "volume", "user",
17-
"workdir", "onbuild"],
17+
"workdir", "onbuild", "stopsignal", "healthcheck", "shell"],
1818
instructionRegex = "(" + instructions.join('|') + ")",
1919
instructionOnlyLine = new RegExp(instructionRegex + "\\s*$", "i"),
2020
instructionWithArguments = new RegExp(instructionRegex + "(\\s+)", "i");
@@ -26,22 +26,62 @@
2626
regex: /#.*$/,
2727
token: "comment"
2828
},
29+
{
30+
regex: /^(\s*)\b(from)\b/i,
31+
token: [null, "keyword"],
32+
next: "from"
33+
},
2934
// Highlight an instruction without any arguments (for convenience)
3035
{
3136
regex: instructionOnlyLine,
32-
token: "variable-2"
37+
token: "keyword"
3338
},
3439
// Highlight an instruction followed by arguments
3540
{
3641
regex: instructionWithArguments,
37-
token: ["variable-2", null],
42+
token: ["keyword", null],
3843
next: "arguments"
3944
},
4045
{
4146
regex: /./,
4247
token: null
4348
}
4449
],
50+
from: [
51+
{
52+
regex: /\s*$/,
53+
token: null,
54+
next: "start"
55+
},
56+
{
57+
// Line comment without instruction arguments is an error
58+
regex: /(\s*)(#.*)$/,
59+
token: [null, "error"],
60+
next: "start"
61+
},
62+
{
63+
// ex: FROM golang:1.9.2-alpine3.6 AS build
64+
regex: /(\s*\S+\s+)(as)(\s+)\S+/i,
65+
token: [null, "keyword", null],
66+
next: "start"
67+
},
68+
{
69+
// ex: FROM node:carbon
70+
regex: /\s*[^#]+$/,
71+
token: null,
72+
next: "start"
73+
},
74+
{
75+
// ex: FROM node:carbon # comment
76+
regex: /(\s*[^#]+)\s*(#.*)$/,
77+
token: [null, "comment"],
78+
next: "start"
79+
},
80+
{
81+
token: null,
82+
next: "start"
83+
}
84+
],
4585
arguments: [
4686
{
4787
// Line comment without instruction arguments is an error

mode/dockerfile/test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: http://codemirror.net/LICENSE
3+
4+
(function() {
5+
var mode = CodeMirror.getMode({indentUnit: 2}, "text/x-dockerfile");
6+
function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)); }
7+
8+
MT("simple_nodejs_dockerfile",
9+
"[keyword FROM] node:carbon",
10+
"[comment # Create app directory]",
11+
"[keyword WORKDIR] /usr/src/app",
12+
"[comment # Install app dependencies]",
13+
"[comment # A wildcard is used to ensure both package.json AND package-lock.json are copied]",
14+
"[comment # where available (npm@5+)]",
15+
"[keyword COPY] package*.json ./",
16+
"[keyword RUN] npm install",
17+
"[keyword COPY] . .",
18+
"[keyword EXPOSE] 8080 3000",
19+
"[keyword ENV] NODE_ENV development",
20+
"[keyword CMD] [[\"npm\", \"start\"]]");
21+
22+
// Ideally the last space should not be highlighted.
23+
MT("instruction_without_args_1",
24+
"[keyword CMD ]");
25+
26+
MT("instruction_without_args_2",
27+
"[comment # An instruction without args...]",
28+
"[keyword ARG] [error #...is an error]");
29+
30+
MT("multiline",
31+
"[keyword RUN] apt-get update && apt-get install -y \\",
32+
" mercurial \\",
33+
" subversion \\",
34+
" && apt-get clean \\",
35+
" && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*");
36+
37+
MT("from_comment",
38+
" [keyword FROM] debian:stretch [comment # I tend to use stable as that is more stable]",
39+
" [keyword FROM] debian:stretch [keyword AS] stable [comment # I am even more stable]",
40+
" [keyword FROM] [error # this is an error]");
41+
42+
MT("from_as",
43+
"[keyword FROM] golang:1.9.2-alpine3.6 [keyword AS] build",
44+
"[keyword COPY] --from=build /bin/project /bin/project",
45+
"[keyword ENTRYPOINT] [[\"/bin/project\"]]",
46+
"[keyword CMD] [[\"--help\"]]");
47+
48+
MT("arg",
49+
"[keyword ARG] VERSION=latest",
50+
"[keyword FROM] busybox:$VERSION",
51+
"[keyword ARG] VERSION",
52+
"[keyword RUN] echo $VERSION > image_version");
53+
54+
MT("label",
55+
"[keyword LABEL] com.example.label-with-value=\"foo\"",
56+
"[keyword LABEL] description=\"This text illustrates \"",
57+
" that label-values can span multiple lines.\"");
58+
59+
MT("maintainer",
60+
"[keyword MAINTAINER] Foo Bar \"foo@bar.com\"",
61+
"[keyword MAINTAINER] Bar Baz <bar@baz.com>");
62+
63+
MT("verify_keyword",
64+
"[keyword RUN] add-apt-repository ppa:chris-lea/node.js");
65+
66+
MT("scripts",
67+
"[comment # Set an entrypoint, to automatically install node modules]",
68+
"[keyword ENTRYPOINT] [[\"/bin/bash\", \"-c\", \"if [[ ! -d node_modules ]]; then npm install; fi; exec \\\"${@:0}\\\";\"]]",
69+
"[keyword CMD] npm start",
70+
"[keyword RUN] npm run build && npm run test");
71+
})();

test/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<!-- clike must be after css or vim and sublime tests will fail -->
2323
<script src="../mode/cypher/cypher.js"></script>
2424
<script src="../mode/d/d.js"></script>
25+
<script src="../mode/dockerfile/dockerfile.js"></script>
2526
<script src="../mode/gfm/gfm.js"></script>
2627
<script src="../mode/haml/haml.js"></script>
2728
<script src="../mode/htmlmixed/htmlmixed.js"></script>
@@ -117,6 +118,7 @@ <h2>Test Suite</h2>
117118
<script src="../mode/css/less_test.js"></script>
118119
<script src="../mode/cypher/test.js"></script>
119120
<script src="../mode/d/test.js"></script>
121+
<script src="../mode/dockerfile/test.js"></script>
120122
<script src="../mode/gfm/test.js"></script>
121123
<script src="../mode/haml/test.js"></script>
122124
<script src="../mode/javascript/test.js"></script>

0 commit comments

Comments
 (0)