-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotnet.go
226 lines (184 loc) · 5.58 KB
/
dotnet.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package builder
import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strings"
"github.com/groenlid/docker-builder/cmd/structs"
)
type DotnetBuilderConfig struct {
Type string `json:"type"`
DotnetRuntime string `json:"dotnetruntime"`
}
func findProjectFileInPath(path string) (fs.FileInfo, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range files {
if strings.Contains(file.Name(), ".csproj") {
return file, nil
}
}
return nil, fmt.Errorf("could not find project file in path %s", path)
}
func unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func findPackageDependenciesInProjectFile(projectFilesPaths []string) ([]string, error) {
dependencies := []string{}
for _, projectFilePath := range projectFilesPaths {
content, err := os.ReadFile(projectFilePath)
if err != nil {
return nil, err
}
re := regexp.MustCompile(`PackageReference Include="(.*)"`)
matches := re.FindAllStringSubmatch(string(content), -1)
for _, match := range matches {
dependencies = append(dependencies, match[1])
}
}
return dependencies, nil
}
func findProjectDependenciesInProjectFile(projectfolder string, projectfile string) ([]string, error) {
projectFilePath := path.Join(projectfolder, projectfile)
projectFileContent, err := os.ReadFile(projectFilePath)
if err != nil {
return nil, err
}
re := regexp.MustCompile(`ProjectReference Include="(.*)"`)
matches := re.FindAllStringSubmatch(string(projectFileContent), -1)
dependencies := []string{
projectFilePath,
}
for _, dependency := range matches {
paths := strings.Split(dependency[1], `\`)
dependencyPath := projectfolder
for _, pathFragment := range paths {
dependencyPath = path.Join(dependencyPath, pathFragment)
}
dependencyDir := path.Dir(dependencyPath)
dependencies = append(dependencies, dependencyPath)
subdependencies, err := findProjectDependenciesInProjectFile(dependencyDir, strings.Replace(dependencyPath, dependencyDir, "", 1))
if err != nil {
return nil, err
}
dependencies = append(dependencies, subdependencies...)
}
return unique(dependencies), nil
}
const DockerSrc = "/src/"
func getDockerCopyCommand(from string, to string) string {
return fmt.Sprintf(`COPY %s %s`, from, path.Join(DockerSrc, to))
}
func getDockerCopyCommandForDependency(from []string) string {
copyCommands := []string{}
for _, item := range from {
copyCommands = append(copyCommands, getDockerCopyCommand(item, item))
}
return strings.Join(copyCommands, "\n")
}
func getDirOfPaths(paths []string) []string {
dirs := []string{}
for _, item := range paths {
dirs = append(dirs, path.Dir(item))
}
return dirs
}
func getDotnetRuntime(builderConfig *DotnetBuilderConfig, projectfiles []string) (string, error) {
runtimeImage := map[string]string{
"runtime": "mcr.microsoft.com/dotnet/runtime",
"aspnet": "mcr.microsoft.com/dotnet/aspnet",
}
if builderConfig.DotnetRuntime != "" {
return runtimeImage[builderConfig.DotnetRuntime], nil
}
aspnetDependencies := []string{
"Microsoft.AspNetCore.App",
"Microsoft.AspNetCore.All",
}
dependencies, err := findPackageDependenciesInProjectFile(projectfiles)
if err != nil {
return "", err
}
for _, dependency := range dependencies {
for _, aspnetDedependency := range aspnetDependencies {
if aspnetDedependency == dependency {
return runtimeImage["aspnet"], nil
}
}
}
return runtimeImage["runtime"], nil
}
var DotnetBuilder = &Builder{
BuilderNames: []string{"dotnet"},
GetBuildArguments: func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error) {
builderConfig := &DotnetBuilderConfig{}
err := json.Unmarshal(conf.Builder, builderConfig)
if err != nil {
return nil, err
}
projectFile, err := findProjectFileInPath(conf.ProjectPath)
if err != nil {
return nil, err
}
log.Printf("Found projectfile in %s", projectFile.Name())
projectDependencies, err := findProjectDependenciesInProjectFile(conf.ProjectPath, projectFile.Name())
if err != nil {
return nil, err
}
copyProjectDependenciesProjectFiles := getDockerCopyCommandForDependency(projectDependencies)
projectDir := path.Join(DockerSrc, conf.ProjectPath)
copyProjectDependencies := getDockerCopyCommandForDependency(getDirOfPaths(projectDependencies))
projectName := strings.Replace(projectFile.Name(), ".csproj", "", 1)
dockerRuntimeImage, err := getDotnetRuntime(builderConfig, projectDependencies)
if err != nil {
return nil, err
}
dockercontent := fmt.Sprintf(`
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
# Copy csproj and restore as distinct layers
%s
WORKDIR %s
RUN dotnet restore
# Copy everything else and build
%s
RUN dotnet publish -c Release -o out
# Build runtime image
FROM %s:6.0
WORKDIR /app
COPY --from=build-env %s/out .
ENTRYPOINT ["dotnet", "%s.dll"]
`, copyProjectDependenciesProjectFiles, projectDir, copyProjectDependencies, dockerRuntimeImage, projectDir, projectName)
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, err
}
dockerFilePath := path.Join(tmpDir, "Dockerfile")
bytesToSend := []byte(dockercontent)
err = os.WriteFile(dockerFilePath, bytesToSend, 0755)
if err != nil {
return nil, err
}
arguments := &BuildArguments{
DockerBuildContextPaths: map[string]string{
".": "",
tmpDir: "",
},
}
return arguments, nil
},
}