-
Notifications
You must be signed in to change notification settings - Fork 4
/
dotnet_publish_process.go
90 lines (73 loc) · 2.27 KB
/
dotnet_publish_process.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
package dotnetpublish
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) error
}
type DotnetPublishProcess struct {
executable Executable
logger scribe.Emitter
clock chronos.Clock
}
func NewDotnetPublishProcess(executable Executable, logger scribe.Emitter, clock chronos.Clock) DotnetPublishProcess {
return DotnetPublishProcess{
executable: executable,
logger: logger,
clock: clock,
}
}
func (p DotnetPublishProcess) Execute(workingDir, nugetCachePath, projectPath, outputPath string, debug bool, flags []string) error {
args := []string{
"publish", filepath.Join(workingDir, projectPath), // change to workingDir plus project path
}
if !containsFlag(flags, "--configuration") && !containsFlag(flags, "-c") {
if debug {
args = append(args, "--configuration", "Debug")
} else {
args = append(args, "--configuration", "Release")
}
}
if !containsFlag(flags, "--runtime") && !containsFlag(flags, "-r") {
args = append(args, "--runtime", "linux-x64")
}
if !containsFlag(flags, "--self-contained") && !containsFlag(flags, "--no-self-contained") {
args = append(args, "--self-contained", "false")
}
if !containsFlag(flags, "--output") && !containsFlag(flags, "-o") {
args = append(args, "--output", outputPath)
}
args = append(args, flags...)
p.logger.Subprocess("Running 'dotnet %s'", strings.Join(args, " "))
duration, err := p.clock.Measure(func() error {
return p.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Env: append(os.Environ(), fmt.Sprintf("NUGET_PACKAGES=%s", nugetCachePath)),
Stdout: p.logger.ActionWriter,
Stderr: p.logger.ActionWriter,
})
})
if err != nil {
p.logger.Action("Failed after %s", duration)
return fmt.Errorf("failed to execute 'dotnet publish': %w", err)
}
p.logger.Action("Completed in %s", duration)
p.logger.Break()
return nil
}
func containsFlag(flags []string, match string) bool {
for _, flag := range flags {
if strings.HasPrefix(flag, match) {
return true
}
}
return false
}