forked from brendandburns/dotnet-dockerfile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.cs
More file actions
67 lines (62 loc) · 2.35 KB
/
Dockerfile.cs
File metadata and controls
67 lines (62 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace dockerfile {
public class Dockerfile {
public Comment[] Comments { set; get; }
public Instruction[] Instructions { set; get; }
public Directive[] Directives { set; get; }
public Dockerfile(Instruction[] instructions, Comment[] comments, Directive[] directives=null) {
Instructions = instructions;
Comments = comments;
Directives = directives;
}
public string Contents() {
var b = new StringBuilder();
if (Directives != null) {
foreach(var dir in Directives) {
b.Append(dir.Generate());
b.Append('\n');
}
}
if (Instructions != null) {
foreach(var inst in Instructions) {
b.Append(inst.ToString());
b.Append('\n');
}
}
return b.ToString();
}
public static Dockerfile Parse(TextReader reader) {
var t = ParseAsync(reader);
return t.GetAwaiter().GetResult();
}
public static async Task<Dockerfile> ParseAsync(TextReader reader) {
var instructions = new List<Instruction>();
var comments = new List<Comment>();
Directive escape = null;
int number = 0;
for (string line = await reader.ReadLineAsync();
line != null;
line = await reader.ReadLineAsync()) {
line.Trim();
number++;
if (line.Length == 0) {
continue;
}
if (line[0] == '#') {
comments.Add(new Comment(line, number));
continue;
}
if (line.StartsWith("# escape=")) {
var parts = line.Substring(2).Trim().Split(new []{'='}, 2);
escape = Directive.Parse(line.Substring(2).Trim(), number);
continue;
}
instructions.Add(Instruction.Parse(line, number));
}
return new Dockerfile(instructions.ToArray(), comments.ToArray(), escape != null ? new []{escape} : null);
}
}
}