|
| 1 | +package sdkbuild |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "html" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// BuildDotNetProgramOptions are options for BuildDotNetProgram. |
| 15 | +type BuildDotNetProgramOptions struct { |
| 16 | + // Directory that will have a temporary directory created underneath. |
| 17 | + BaseDir string |
| 18 | + // Required version. If it contains a slash, it is assumed to be a path to the |
| 19 | + // base of the repo (and will have a src/Temporalio/Temporalio.csproj child). |
| 20 | + // Otherwise it is a NuGet version. |
| 21 | + Version string |
| 22 | + // If present, this directory is expected to exist beneath base dir. Otherwise |
| 23 | + // a temporary dir is created. |
| 24 | + DirName string |
| 25 | + // Required Program.cs content. If not set, no Program.cs is created (so it) |
| 26 | + ProgramContents string |
| 27 | + // Required csproj content. This should not contain a dependency on Temporalio |
| 28 | + // because this adds a package/project reference near the end. |
| 29 | + CsprojContents string |
| 30 | +} |
| 31 | + |
| 32 | +// DotNetProgram is a .NET-specific implementation of Program. |
| 33 | +type DotNetProgram struct { |
| 34 | + dir string |
| 35 | +} |
| 36 | + |
| 37 | +var _ Program = (*DotNetProgram)(nil) |
| 38 | + |
| 39 | +func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions) (*DotNetProgram, error) { |
| 40 | + if options.BaseDir == "" { |
| 41 | + return nil, fmt.Errorf("base dir required") |
| 42 | + } else if options.Version == "" { |
| 43 | + return nil, fmt.Errorf("version required") |
| 44 | + } else if options.ProgramContents == "" { |
| 45 | + return nil, fmt.Errorf("program contents required") |
| 46 | + } else if options.CsprojContents == "" { |
| 47 | + return nil, fmt.Errorf("csproj contents required") |
| 48 | + } |
| 49 | + |
| 50 | + // Create temp dir if needed that we will remove if creating is unsuccessful |
| 51 | + success := false |
| 52 | + var dir string |
| 53 | + if options.DirName != "" { |
| 54 | + dir = filepath.Join(options.BaseDir, options.DirName) |
| 55 | + } else { |
| 56 | + var err error |
| 57 | + dir, err = os.MkdirTemp(options.BaseDir, "program-") |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed making temp dir: %w", err) |
| 60 | + } |
| 61 | + defer func() { |
| 62 | + if !success { |
| 63 | + // Intentionally swallow error |
| 64 | + _ = os.RemoveAll(dir) |
| 65 | + } |
| 66 | + }() |
| 67 | + } |
| 68 | + |
| 69 | + // Create program.csproj |
| 70 | + var depLine string |
| 71 | + // Slash means it is a path |
| 72 | + if strings.ContainsAny(options.Version, `/\`) { |
| 73 | + // Get absolute path of csproj file |
| 74 | + absCsproj, err := filepath.Abs(filepath.Join(options.Version, "src/Temporalio/Temporalio.csproj")) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("cannot make absolute path from version: %w", err) |
| 77 | + } else if _, err := os.Stat(absCsproj); err != nil { |
| 78 | + return nil, fmt.Errorf("cannot find version path of %v: %w", absCsproj, err) |
| 79 | + } |
| 80 | + depLine = `<ProjectReference Include="` + html.EscapeString(absCsproj) + `" />` |
| 81 | + // Need to build this csproj first |
| 82 | + cmd := exec.CommandContext(ctx, "dotnet", "build", absCsproj) |
| 83 | + cmd.Dir = dir |
| 84 | + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr |
| 85 | + if err := cmd.Run(); err != nil { |
| 86 | + return nil, fmt.Errorf("failed dotnet build of csproj in version: %w", err) |
| 87 | + } |
| 88 | + } else { |
| 89 | + depLine = `<PackageReference Include="Temporalio" Version="` + |
| 90 | + html.EscapeString(strings.TrimPrefix(options.Version, "v")) + `" />` |
| 91 | + } |
| 92 | + // Add the item group for the Temporalio dep just before the ending project tag |
| 93 | + endProjectTag := strings.LastIndex(options.CsprojContents, "</Project>") |
| 94 | + if endProjectTag == -1 { |
| 95 | + return nil, fmt.Errorf("no ending project tag found in csproj contents") |
| 96 | + } |
| 97 | + csproj := options.CsprojContents[:endProjectTag] + "\n <ItemGroup>\n " + depLine + |
| 98 | + "\n </ItemGroup>\n" + options.CsprojContents[endProjectTag:] |
| 99 | + if err := os.WriteFile(filepath.Join(dir, "program.csproj"), []byte(csproj), 0644); err != nil { |
| 100 | + return nil, fmt.Errorf("failed writing program.csproj: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Create Program.cs |
| 104 | + if err := os.WriteFile(filepath.Join(dir, "Program.cs"), []byte(options.ProgramContents), 0644); err != nil { |
| 105 | + return nil, fmt.Errorf("failed writing Program.cs: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Build it into build folder |
| 109 | + cmd := exec.CommandContext(ctx, "dotnet", "build", "--output", "build") |
| 110 | + cmd.Dir = dir |
| 111 | + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr |
| 112 | + if err := cmd.Run(); err != nil { |
| 113 | + return nil, fmt.Errorf("failed dotnet build: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // All good |
| 117 | + success = true |
| 118 | + return &DotNetProgram{dir}, nil |
| 119 | +} |
| 120 | + |
| 121 | +// DotNetProgramFromDir recreates the Go program from a Dir() result of a |
| 122 | +// BuildDotNetProgram(). |
| 123 | +func DotNetProgramFromDir(dir string) (*DotNetProgram, error) { |
| 124 | + // Quick sanity check on the presence of program.csproj |
| 125 | + if _, err := os.Stat(filepath.Join(dir, "program.csproj")); err != nil { |
| 126 | + return nil, fmt.Errorf("failed finding program.csproj in dir: %w", err) |
| 127 | + } |
| 128 | + return &DotNetProgram{dir}, nil |
| 129 | +} |
| 130 | + |
| 131 | +// Dir is the directory to run in. |
| 132 | +func (d *DotNetProgram) Dir() string { return d.dir } |
| 133 | + |
| 134 | +// NewCommand makes a new command for the given args. |
| 135 | +func (d *DotNetProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd, error) { |
| 136 | + exe := "./build/program" |
| 137 | + if runtime.GOOS == "windows" { |
| 138 | + exe += ".exe" |
| 139 | + } |
| 140 | + cmd := exec.CommandContext(ctx, exe, args...) |
| 141 | + cmd.Dir = d.dir |
| 142 | + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr |
| 143 | + return cmd, nil |
| 144 | +} |
0 commit comments