Skip to content

Commit 32bc6b7

Browse files
authored
Merge pull request #46 from teknologi-umum/feat/dockerfile
2 parents ba19f7f + 262adc8 commit 32bc6b7

File tree

7 files changed

+186
-2
lines changed

7 files changed

+186
-2
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Clojure } from './languages/clojure';
44
import { CPP } from './languages/cpp';
55
import { CS } from './languages/cs';
66
import { CSS } from './languages/css';
7+
import { Dockerfile } from './languages/dockerfile';
78
import { Go } from './languages/go';
89
import { HTML } from './languages/html';
910
import { Java } from './languages/java';
@@ -27,6 +28,7 @@ const languages: Record<string, LanguagePattern[]> = {
2728
'C++': CPP,
2829
'C#': CS,
2930
CSS,
31+
Dockerfile,
3032
Go,
3133
HTML,
3234
Java,

src/languages/dockerfile.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { LanguagePattern } from '../types';
2+
3+
const KEYWORDS = [
4+
'ADD',
5+
'ARG',
6+
'AS',
7+
'CMD',
8+
'COPY',
9+
'CROSS_BUILD',
10+
'ENTRYPOINT',
11+
'ENV',
12+
'EXPOSE',
13+
'FROM',
14+
'HEALTHCHECK',
15+
'LABEL',
16+
'MAINTAINER',
17+
'ONBUILD',
18+
'RUN',
19+
'SHELL',
20+
'STOPSIGNAL',
21+
'USER',
22+
'VOLUME',
23+
'WORKDIR',
24+
];
25+
26+
export const Dockerfile: LanguagePattern[] = [
27+
// Keywords
28+
// This should be enough to identify dockerfile since they always exist
29+
{ pattern: new RegExp(`^(${KEYWORDS.join('|')})`), type: 'keyword' },
30+
];

src/languages/sql.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const SQL: LanguagePattern[] = [
77
{ pattern: /INSERT INTO/, type: 'keyword' },
88
{ pattern: /(SELECT|SELECT DISTINCT)\s/, type: 'keyword' },
99
{ pattern: /INNER JOIN/, type: 'keyword' },
10-
{ pattern: /GROUP BY/, type: 'keyword' },
10+
{ pattern: /(GROUP|ORDER) BY/, type: 'keyword' },
1111
{ pattern: /(END;|COMMIT;)/, type: 'keyword' },
1212

1313
{ pattern: /UPDATE\s+\w+\sSET/, type: 'keyword' },
@@ -19,6 +19,8 @@ export const SQL: LanguagePattern[] = [
1919
{ pattern: /(BIT|TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT|DOUBLE)\([0-9]+\)/, type: 'constant.type' },
2020
{ pattern: /(TINYBLOB|TINYTEXT|MEDIUMTEXT|MEDIUMBLOB|LONGTEXT|LONGBLOB)/, type: 'constant.type' },
2121
{ pattern: /(BOOLEAN|BOOL|DATE|YEAR)/, type: 'constant.type' },
22+
// Math
23+
{ pattern: /(EXP|SUM|SQRT|MIN|MAX)/, type: 'keyword.operator' },
2224
// Avoiding Lua
2325
{ pattern: /local\s(function|\w+)?\s=\s/, type: 'not' },
2426
{ pattern: /(require|dofile)\((.*)\)/, type: 'not' },

tests/cpp.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test('hello world', () => {
1616
'C++': 5,
1717
CSS: 0,
1818
'C#': 0,
19+
Dockerfile: 0,
1920
Go: 0,
2021
HTML: 0,
2122
Java: 0,

tests/cs.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test('hello world', () => {
1616
'C++': -40,
1717
'C#': 10,
1818
CSS: 0,
19+
Dockerfile: 0,
1920
Go: -39,
2021
HTML: 0,
2122
Java: -40,

tests/dockerfile.test.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import detectLang from '../src/index';
4+
5+
test('teknum bot', () => {
6+
const code = detectLang(`
7+
FROM node:16.6-buster
8+
9+
WORKDIR /app
10+
11+
COPY . .
12+
13+
RUN npm install --production
14+
15+
EXPOSE 8080
16+
17+
CMD ["npm", "start"]`);
18+
19+
assert.equal(code, 'Dockerfile');
20+
});
21+
22+
test('botnet', () => {
23+
const code = detectLang(`
24+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
25+
26+
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0.0-rc.1-bullseye-slim-amd64 AS base
27+
RUN apt-get update && apt-get install -y libc6-dev libgdiplus
28+
WORKDIR /app
29+
EXPOSE 80
30+
EXPOSE 443
31+
32+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
33+
WORKDIR /src
34+
COPY ["BotNet/BotNet.csproj", "BotNet/"]
35+
RUN dotnet restore "BotNet/BotNet.csproj"
36+
COPY . .
37+
WORKDIR "/src/BotNet"
38+
RUN dotnet build "BotNet.csproj" -c Release -o /app/build
39+
40+
FROM build AS publish
41+
RUN dotnet publish "BotNet.csproj" -c Release -o /app/publish
42+
43+
FROM base AS final
44+
WORKDIR /app
45+
COPY --from=publish /app/publish .
46+
ENTRYPOINT ["dotnet", "BotNet.dll"]`);
47+
48+
assert.equal(code, 'Dockerfile');
49+
});
50+
51+
test('casperjs dockerfile', () => {
52+
const code = detectLang(`
53+
#
54+
# Dockerfile for casperjs
55+
#
56+
57+
FROM debian:jessie
58+
MAINTAINER kev <[email protected]>
59+
60+
ENV PHANTOM_VER 2.1.1
61+
ENV PHANTOM_URL https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-\${PHANTOM_VER}-linux-x86_64.tar.bz2
62+
ENV PHANTOM_DIR /usr/local/bin
63+
64+
ENV CASPER_VER 1.1.4-1
65+
ENV CASPER_URL https://github.com/casperjs/casperjs/archive/\${CASPER_VER}.tar.gz
66+
ENV CASPER_DIR /usr/local/casperjs
67+
68+
RUN set -xe \
69+
&& apt-get update \
70+
&& apt-get install -y bzip2 \
71+
curl \
72+
libfontconfig \
73+
libicu52 \
74+
libsqlite3-0 \
75+
python \
76+
&& curl -sSL $PHANTOM_URL | tar xj -C $PHANTOM_DIR --strip 2 --wildcards '*/bin/phantomjs' \
77+
&& chmod +x /usr/local/bin/phantomjs \
78+
&& mkdir -p $CASPER_DIR \
79+
&& curl -sSL $CASPER_URL | tar xz --strip 1 -C $CASPER_DIR \
80+
&& ln -sf $CASPER_DIR/bin/casperjs /usr/local/bin/ \
81+
&& apt-get remove -y bzip2 \
82+
curl \
83+
&& rm -rf /var/lib/apt/lists/*
84+
85+
COPY ./sample.js /app/
86+
87+
VOLUME /app/
88+
WORKDIR /app/
89+
90+
ENTRYPOINT ["casperjs"]
91+
CMD ["--help"]
92+
`);
93+
94+
assert.equal(code, 'Dockerfile');
95+
});
96+
97+
test('kafka dockerfile', () => {
98+
const code = detectLang(`
99+
FROM azul/zulu-openjdk-alpine:8u292-8.54.0.21
100+
101+
ARG kafka_version=2.7.1
102+
ARG scala_version=2.13
103+
ARG glibc_version=2.31-r0
104+
ARG vcs_ref=unspecified
105+
ARG build_date=unspecified
106+
107+
LABEL org.label-schema.name="kafka" \
108+
org.label-schema.description="Apache Kafka" \
109+
org.label-schema.build-date="\${build_date}" \
110+
org.label-schema.vcs-url="https://github.com/wurstmeister/kafka-docker" \
111+
org.label-schema.vcs-ref="\${vcs_ref}" \
112+
org.label-schema.version="\${scala_version}_\${kafka_version}" \
113+
org.label-schema.schema-version="1.0" \
114+
maintainer="wurstmeister"
115+
116+
ENV KAFKA_VERSION=$kafka_version \
117+
SCALA_VERSION=$scala_version \
118+
KAFKA_HOME=/opt/kafka \
119+
GLIBC_VERSION=$glibc_version
120+
121+
ENV PATH=\${PATH}:\${KAFKA_HOME}/bin
122+
123+
COPY download-kafka.sh start-kafka.sh broker-list.sh create-topics.sh versions.sh /tmp/
124+
125+
RUN apk add --no-cache bash curl jq docker \
126+
&& chmod a+x /tmp/*.sh \
127+
&& mv /tmp/start-kafka.sh /tmp/broker-list.sh /tmp/create-topics.sh /tmp/versions.sh /usr/bin \
128+
&& sync && /tmp/download-kafka.sh \
129+
&& tar xfz /tmp/kafka_\${SCALA_VERSION}-\${KAFKA_VERSION}.tgz -C /opt \
130+
&& rm /tmp/kafka_\${SCALA_VERSION}-\${KAFKA_VERSION}.tgz \
131+
&& ln -s /opt/kafka_\${SCALA_VERSION}-\${KAFKA_VERSION} \${KAFKA_HOME} \
132+
&& rm /tmp/* \
133+
&& wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/\${GLIBC_VERSION}/glibc-\${GLIBC_VERSION}.apk \
134+
&& apk add --no-cache --allow-untrusted glibc-\${GLIBC_VERSION}.apk \
135+
&& rm glibc-\${GLIBC_VERSION}.apk
136+
137+
COPY overrides /opt/overrides
138+
139+
VOLUME ["/kafka"]
140+
141+
# Use "exec" form so that it runs as PID 1 (useful for graceful shutdown)
142+
CMD ["start-kafka.sh"]`);
143+
144+
assert.equal(code, 'Dockerfile');
145+
});
146+
147+
test.run();

tests/large.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ test('large input', () => {
663663
'C++': 160,
664664
'C#': 19,
665665
CSS: 0,
666+
Dockerfile: 0,
666667
Go: 0,
667668
HTML: 0,
668669
Java: -832,
@@ -675,7 +676,7 @@ test('large input', () => {
675676
Python: -140,
676677
Ruby: 0,
677678
Rust: 4,
678-
SQL: 0,
679+
SQL: 24,
679680
Unknown: 1,
680681
YAML: 4,
681682
});

0 commit comments

Comments
 (0)