-
Notifications
You must be signed in to change notification settings - Fork 173
/
dotnet.go
218 lines (185 loc) · 7.02 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package dotnet
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/blang/semver/v4"
)
type DotNetCli interface {
tools.ExternalTool
Restore(ctx context.Context, project string) error
Build(ctx context.Context, project string, configuration string, output string) error
Publish(ctx context.Context, project string, configuration string, output string) error
PublishContainer(ctx context.Context, project string, configuration string, imageName string, server string) error
InitializeSecret(ctx context.Context, project string) error
PublishAppHostManifest(ctx context.Context, hostProject string, manifestPath string) error
SetSecrets(ctx context.Context, secrets map[string]string, project string) error
GetMsBuildProperty(ctx context.Context, project string, propertyName string) (string, error)
}
type dotNetCli struct {
commandRunner exec.CommandRunner
}
func (cli *dotNetCli) Name() string {
return ".NET CLI"
}
func (cli *dotNetCli) InstallUrl() string {
return "https://dotnet.microsoft.com/download"
}
func (cli *dotNetCli) versionInfo() tools.VersionInfo {
return tools.VersionInfo{
MinimumVersion: semver.Version{
Major: 6,
Minor: 0,
Patch: 3},
UpdateCommand: "Visit https://docs.microsoft.com/en-us/dotnet/core/releases-and-support to upgrade",
}
}
func (cli *dotNetCli) CheckInstalled(ctx context.Context) error {
err := tools.ToolInPath("dotnet")
if err != nil {
return err
}
dotnetRes, err := tools.ExecuteCommand(ctx, cli.commandRunner, "dotnet", "--version")
if err != nil {
return fmt.Errorf("checking %s version: %w", cli.Name(), err)
}
log.Printf("dotnet version: %s", dotnetRes)
dotnetSemver, err := tools.ExtractVersion(dotnetRes)
if err != nil {
return fmt.Errorf("converting to semver version fails: %w", err)
}
updateDetail := cli.versionInfo()
if dotnetSemver.LT(updateDetail.MinimumVersion) {
return &tools.ErrSemver{ToolName: cli.Name(), VersionInfo: updateDetail}
}
return nil
}
func (cli *dotNetCli) Restore(ctx context.Context, project string) error {
runArgs := exec.NewRunArgs("dotnet", "restore", project)
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("dotnet restore on project '%s' failed: %w", project, err)
}
return nil
}
func (cli *dotNetCli) Build(ctx context.Context, project string, configuration string, output string) error {
runArgs := exec.NewRunArgs("dotnet", "build", project)
if configuration != "" {
runArgs = runArgs.AppendParams("-c", configuration)
}
if output != "" {
runArgs = runArgs.AppendParams("--output", output)
}
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("dotnet build on project '%s' failed: %w", project, err)
}
return nil
}
func (cli *dotNetCli) Publish(ctx context.Context, project string, configuration string, output string) error {
runArgs := exec.NewRunArgs("dotnet", "publish", project)
if configuration != "" {
runArgs = runArgs.AppendParams("-c", configuration)
}
if output != "" {
runArgs = runArgs.AppendParams("--output", output)
}
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("dotnet publish on project '%s' failed: %w", project, err)
}
return nil
}
func (cli *dotNetCli) PublishAppHostManifest(
ctx context.Context, hostProject string, manifestPath string,
) error {
// TODO(ellismg): Before we GA manifest support, we should remove this debug tool, but being able to control what
// manifest is used is helpful, while the manifest/generator is still being built. So if
// `AZD_DEBUG_DOTNET_APPHOST_USE_FIXED_MANIFEST` is set, then we will expect to find apphost-manifest.json SxS with the host
// project, and we just use that instead.
if enabled, err := strconv.ParseBool(os.Getenv("AZD_DEBUG_DOTNET_APPHOST_USE_FIXED_MANIFEST")); err == nil && enabled {
m, err := os.ReadFile(filepath.Join(filepath.Dir(hostProject), "apphost-manifest.json"))
if err != nil {
return fmt.Errorf(
"reading apphost-manifest.json (did you mean to have AZD_DEBUG_DOTNET_APPHOST_USE_FIXED_MANIFEST set?): %w", err)
}
return os.WriteFile(manifestPath, m, osutil.PermissionFile)
}
runArgs := exec.NewRunArgs(
"dotnet", "run", "--project", filepath.Base(hostProject), "--publisher", "manifest", "--output-path", manifestPath)
runArgs = runArgs.WithCwd(filepath.Dir(hostProject))
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("dotnet run --publisher manifest on project '%s' failed: %w", hostProject, err)
}
return nil
}
// PublishContainer runs a `dotnet publish“ with `PublishProfile=DefaultContainer` to build and publish the container.
func (cli *dotNetCli) PublishContainer(
ctx context.Context, project string, configuration string, imageName string, server string,
) error {
runArgs := exec.NewRunArgs("dotnet", "publish", project)
if configuration != "" {
runArgs = runArgs.AppendParams("-c", configuration)
}
if imageName != "" {
runArgs = runArgs.AppendParams(fmt.Sprintf("-p:ContainerImageName=%s", imageName))
}
runArgs = runArgs.AppendParams(
"-r", "linux-x64",
"-p:PublishProfile=DefaultContainer",
fmt.Sprintf("-p:ContainerRegistry=%s", server),
)
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("dotnet publish on project '%s' failed: %w", project, err)
}
return nil
}
func (cli *dotNetCli) InitializeSecret(ctx context.Context, project string) error {
runArgs := exec.NewRunArgs("dotnet", "user-secrets", "init", "--project", project)
_, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("failed to initialize secrets at project '%s': %w", project, err)
}
return nil
}
func (cli *dotNetCli) SetSecrets(ctx context.Context, secrets map[string]string, project string) error {
secretsJson, err := json.Marshal(secrets)
if err != nil {
return fmt.Errorf("failed to marshal secrets: %w", err)
}
// dotnet user-secrets now support setting multiple values at once
//https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows#set-multiple-secrets
runArgs := exec.
NewRunArgs("dotnet", "user-secrets", "set", "--project", project).
WithStdIn(strings.NewReader(string(secretsJson)))
_, err = cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return fmt.Errorf("failed running %s secret set: %w", cli.Name(), err)
}
return nil
}
func (cli *dotNetCli) GetMsBuildProperty(ctx context.Context, project string, propertyName string) (string, error) {
runArgs := exec.NewRunArgs("dotnet", "msbuild", project, fmt.Sprintf("--getProperty:%s", propertyName))
res, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return "", err
}
return res.Stdout, nil
}
func NewDotNetCli(commandRunner exec.CommandRunner) DotNetCli {
return &dotNetCli{
commandRunner: commandRunner,
}
}