Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker: Improvements #2720

Merged
merged 7 commits into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 96 additions & 12 deletions components/prism-docker.js
@@ -1,14 +1,98 @@
Prism.languages.docker = {
'keyword': {
pattern: /(^\s*)(?:ADD|ARG|CMD|COPY|ENTRYPOINT|ENV|EXPOSE|FROM|HEALTHCHECK|LABEL|MAINTAINER|ONBUILD|RUN|SHELL|STOPSIGNAL|USER|VOLUME|WORKDIR)(?=\s)/mi,
lookbehind: true
},
'string': /("|')(?:(?!\1)[^\\\r\n]|\\(?:\r\n|[\s\S]))*\1/,
'comment': {
pattern: /#.*/,
(function (Prism) {

// Many of the following regexes will contain negated lookaheads like `[ \t]+(?![ \t])`. This is a trick to ensure
// that quantifiers behave *atomically*. Atomic quantifiers are necessary to prevent exponential backtracking.

var spaceAfterBackSlash = /\\[\r\n](?:\s|\\[\r\n]|#.*(?!.))*(?![\s#]|\\[\r\n])/.source;
// At least one space, comment, or line break
var space = /(?:[ \t]+(?![ \t])(?:<SP_BS>)?|<SP_BS>)/.source
.replace(/<SP_BS>/g, function () { return spaceAfterBackSlash; });

var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/.source;
var option = /--[\w-]+=(?:<STR>|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(/<STR>/g, function () { return string; });

var stringRule = {
pattern: RegExp(string),
greedy: true
};
var commentRule = {
pattern: /(^[ \t]*)#.*/m,
lookbehind: true,
greedy: true
},
'punctuation': /---|\.\.\.|[:[\]{}\-,|>?]/
};
};

/**
* @param {string} source
* @param {string} flags
* @returns {RegExp}
*/
function re(source, flags) {
source = source
.replace(/<OPT>/g, function () { return option; })
.replace(/<SP>/g, function () { return space; });

return RegExp(source, flags);
}

Prism.languages.docker = {
'instruction': {
pattern: /(^[ \t]*)(?:ADD|ARG|CMD|COPY|ENTRYPOINT|ENV|EXPOSE|FROM|HEALTHCHECK|LABEL|MAINTAINER|ONBUILD|RUN|SHELL|STOPSIGNAL|USER|VOLUME|WORKDIR)(?=\s)(?:\\.|[^\r\n\\])*(?:\\$(?:\s|#.*$)*(?![\s#])(?:\\.|[^\r\n\\])*)*/mi,
lookbehind: true,
greedy: true,
inside: {
'options': {
pattern: re(/(^(?:ONBUILD<SP>)?\w+<SP>)<OPT>(?:<SP><OPT>)*/.source, 'i'),
lookbehind: true,
greedy: true,
inside: {
'property': {
pattern: /(^|\s)--[\w-]+/,
lookbehind: true
},
'string': [
stringRule,
{
pattern: /(=)(?!["'])(?:[^\s\\]|\\.)+/,
lookbehind: true
}
],
'operator': /\\$/m,
'punctuation': /=/
}
},
'keyword': [
{
// https://docs.docker.com/engine/reference/builder/#healthcheck
pattern: re(/(^(?:ONBUILD<SP>)?HEALTHCHECK<SP>(?:<OPT><SP>)*)(?:CMD|NONE)\b/.source, 'i'),
lookbehind: true,
greedy: true
},
{
// https://docs.docker.com/engine/reference/builder/#from
pattern: re(/(^(?:ONBUILD<SP>)?FROM<SP>(?:<OPT><SP>)*(?!--)[^ \t\\]+<SP>)AS/.source, 'i'),
lookbehind: true,
greedy: true
},
{
// https://docs.docker.com/engine/reference/builder/#onbuild
pattern: re(/(^ONBUILD<SP>)\w+/.source, 'i'),
lookbehind: true,
greedy: true
},
{
pattern: /^\w+/,
greedy: true
}
],
'comment': commentRule,
'string': stringRule,
'variable': /\$(?:\w+|\{[^{}"'\\]*\})/,
'operator': /\\$/m
}
},
'comment': commentRule
};

Prism.languages.dockerfile = Prism.languages.docker;

Prism.languages.dockerfile = Prism.languages.docker;
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-docker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 186 additions & 0 deletions tests/languages/docker/instruction_feature.test
@@ -0,0 +1,186 @@
RUN apt-get \
update && apt-get \
#comment
#
\
\


install git -y #not-a-comment \
something

RUN echo hello \
# comment
world

# this is a comment-line
RUN echo hello
RUN echo world

RUN echo "\
hello\
world"

LABEL multi.label1="value1" \
multi.label2="value2" \
other="value3"

EXPOSE 80/udp

ENV MY_NAME="John Doe"
ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
MY_CAT=fluffy

ADD hom?.txt /mydir/

ENTRYPOINT ["executable", "param1", "param2"]

FROM debian:stable
RUN apt-get update && apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

ENTRYPOINT [ "/path/myprocess", \
"arg1", \
"arg2" \
]

----------------------------------------------------

[
["instruction", [
["keyword", "RUN"], " apt-get ", ["operator", "\\"],
"\r\nupdate && apt-get ", ["operator", "\\"],
["comment", "#comment"],
["comment", "#"],
["operator", "\\"],
["operator", "\\"],

"\r\n\r\n\r\ninstall git -y #not-a-comment ", ["operator", "\\"],
"\r\nsomething"
]],

["instruction", [
["keyword", "RUN"], " echo hello ", ["operator", "\\"],
["comment", "# comment"],
"\r\nworld"
]],

["comment", "# this is a comment-line"],
["instruction", [
["keyword", "RUN"],
" echo hello"
]],
["instruction", [
["keyword", "RUN"],
" echo world"
]],

["instruction", [
["keyword", "RUN"],
" echo ",
["string", "\"\\\r\n hello\\\r\n world\""]
]],

["instruction", [
["keyword", "LABEL"],
" multi.label1=",
["string", "\"value1\""],
["operator", "\\"],

"\r\n multi.label2=",
["string", "\"value2\""],
["operator", "\\"],

"\r\n other=",
["string", "\"value3\""]
]],

["instruction", [
["keyword", "EXPOSE"],
" 80/udp"
]],

["instruction", [
["keyword", "ENV"],
" MY_NAME=",
["string", "\"John Doe\""]
]],

["instruction", [
["keyword", "ENV"],
" MY_NAME=",
["string", "\"John Doe\""],
" MY_DOG=Rex\\ The\\ Dog ",
["operator", "\\"],

"\r\n MY_CAT=fluffy"
]],

["instruction", [
["keyword", "ADD"],
" hom?.txt /mydir/"
]],

["instruction", [
["keyword", "ENTRYPOINT"],
" [",
["string", "\"executable\""],
", ",
["string", "\"param1\""],
", ",
["string", "\"param2\""],
"]"
]],

["instruction", [
["keyword", "FROM"],
" debian:stable"
]],
["instruction", [
["keyword", "RUN"],
" apt-get update && apt-get install -y --force-yes apache2"
]],
["instruction", [
["keyword", "EXPOSE"],
" 80 443"
]],
["instruction", [
["keyword", "VOLUME"],
" [",
["string", "\"/var/www\""],
", ",
["string", "\"/var/log/apache2\""],
", ",
["string", "\"/etc/apache2\""],
"]"
]],
["instruction", [
["keyword", "ENTRYPOINT"],
" [",
["string", "\"/usr/sbin/apache2ctl\""],
", ",
["string", "\"-D\""],
", ",
["string", "\"FOREGROUND\""],
"]"
]],

["instruction", [
["keyword", "ENTRYPOINT"],
" [ ",
["string", "\"/path/myprocess\""],
", ",
["operator", "\\"],

["string", "\"arg1\""],
", ",
["operator", "\\"],

["string", "\"arg2\""],
["operator", "\\"],

"\r\n]"
]]
]