-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdotnet.go
More file actions
144 lines (131 loc) · 4.9 KB
/
dotnet.go
File metadata and controls
144 lines (131 loc) · 4.9 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
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
package sdkbuild
import (
"context"
"fmt"
"html"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// BuildDotNetProgramOptions are options for BuildDotNetProgram.
type BuildDotNetProgramOptions struct {
// Directory that will have a temporary directory created underneath.
BaseDir string
// Required version. If it contains a slash, it is assumed to be a path to the
// base of the repo (and will have a src/Temporalio/Temporalio.csproj child).
// Otherwise it is a NuGet version.
Version string
// If present, this directory is expected to exist beneath base dir. Otherwise
// a temporary dir is created.
DirName string
// Required Program.cs content. If not set, no Program.cs is created (so it)
ProgramContents string
// Required csproj content. This should not contain a dependency on Temporalio
// because this adds a package/project reference near the end.
CsprojContents string
}
// DotNetProgram is a .NET-specific implementation of Program.
type DotNetProgram struct {
dir string
}
var _ Program = (*DotNetProgram)(nil)
func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions) (*DotNetProgram, error) {
if options.BaseDir == "" {
return nil, fmt.Errorf("base dir required")
} else if options.Version == "" {
return nil, fmt.Errorf("version required")
} else if options.ProgramContents == "" {
return nil, fmt.Errorf("program contents required")
} else if options.CsprojContents == "" {
return nil, fmt.Errorf("csproj contents required")
}
// Create temp dir if needed that we will remove if creating is unsuccessful
success := false
var dir string
if options.DirName != "" {
dir = filepath.Join(options.BaseDir, options.DirName)
} else {
var err error
dir, err = os.MkdirTemp(options.BaseDir, "program-")
if err != nil {
return nil, fmt.Errorf("failed making temp dir: %w", err)
}
defer func() {
if !success {
// Intentionally swallow error
_ = os.RemoveAll(dir)
}
}()
}
// Create program.csproj
var depLine string
// Slash means it is a path
if strings.ContainsAny(options.Version, `/\`) {
// Get absolute path of csproj file
absCsproj, err := filepath.Abs(filepath.Join(options.Version, "src/Temporalio/Temporalio.csproj"))
if err != nil {
return nil, fmt.Errorf("cannot make absolute path from version: %w", err)
} else if _, err := os.Stat(absCsproj); err != nil {
return nil, fmt.Errorf("cannot find version path of %v: %w", absCsproj, err)
}
depLine = `<ProjectReference Include="` + html.EscapeString(absCsproj) + `" />`
// Need to build this csproj first
cmd := exec.CommandContext(ctx, "dotnet", "build", absCsproj)
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed dotnet build of csproj in version: %w", err)
}
} else {
depLine = `<PackageReference Include="Temporalio" Version="` +
html.EscapeString(strings.TrimPrefix(options.Version, "v")) + `" />`
}
// Add the item group for the Temporalio dep just before the ending project tag
endProjectTag := strings.LastIndex(options.CsprojContents, "</Project>")
if endProjectTag == -1 {
return nil, fmt.Errorf("no ending project tag found in csproj contents")
}
csproj := options.CsprojContents[:endProjectTag] + "\n <ItemGroup>\n " + depLine +
"\n </ItemGroup>\n" + options.CsprojContents[endProjectTag:]
if err := os.WriteFile(filepath.Join(dir, "program.csproj"), []byte(csproj), 0644); err != nil {
return nil, fmt.Errorf("failed writing program.csproj: %w", err)
}
// Create Program.cs
if err := os.WriteFile(filepath.Join(dir, "Program.cs"), []byte(options.ProgramContents), 0644); err != nil {
return nil, fmt.Errorf("failed writing Program.cs: %w", err)
}
// Build it into build folder
cmd := exec.CommandContext(ctx, "dotnet", "build", "--output", "build")
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed dotnet build: %w", err)
}
// All good
success = true
return &DotNetProgram{dir}, nil
}
// DotNetProgramFromDir recreates the Go program from a Dir() result of a
// BuildDotNetProgram().
func DotNetProgramFromDir(dir string) (*DotNetProgram, error) {
// Quick sanity check on the presence of program.csproj
if _, err := os.Stat(filepath.Join(dir, "program.csproj")); err != nil {
return nil, fmt.Errorf("failed finding program.csproj in dir: %w", err)
}
return &DotNetProgram{dir}, nil
}
// Dir is the directory to run in.
func (d *DotNetProgram) Dir() string { return d.dir }
// NewCommand makes a new command for the given args.
func (d *DotNetProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd, error) {
exe := "./build/program"
if runtime.GOOS == "windows" {
exe += ".exe"
}
cmd := exec.CommandContext(ctx, exe, args...)
cmd.Dir = d.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return cmd, nil
}