Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions sdkbuild/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"runtime"
"strings"

"io"
)

// BuildDotNetProgramOptions are options for BuildDotNetProgram.
Expand All @@ -27,6 +29,9 @@ type BuildDotNetProgramOptions struct {
// Required csproj content. This should not contain a dependency on Temporalio
// because this adds a package/project reference near the end.
CsprojContents string
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// DotNetProgram is a .NET-specific implementation of Program.
Expand Down Expand Up @@ -77,7 +82,7 @@ func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions)
// 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
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed dotnet build of csproj in version: %w", err)
}
Expand All @@ -103,7 +108,7 @@ func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions)
}
cmd := exec.CommandContext(ctx, "dotnet", cmdArgs...)
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed dotnet build: %w", err)
}
Expand Down Expand Up @@ -134,6 +139,6 @@ func (d *DotNetProgram) NewCommand(ctx context.Context, args ...string) (*exec.C
}
cmd := exec.CommandContext(ctx, exe, args...)
cmd.Dir = d.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, nil, nil)
return cmd, nil
}
10 changes: 7 additions & 3 deletions sdkbuild/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -37,6 +38,9 @@ type BuildGoProgramOptions struct {
// If present, applied to build commands before run. May be called multiple
// times for a single build.
ApplyToCommand func(context.Context, *exec.Cmd) error
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// GoProgram is a Go-specific implementation of Program.
Expand Down Expand Up @@ -112,7 +116,7 @@ func BuildGoProgram(ctx context.Context, options BuildGoProgramOptions) (*GoProg
// Tidy it
cmd := exec.CommandContext(ctx, "go", "mod", "tidy")
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if options.ApplyToCommand != nil {
if err := options.ApplyToCommand(ctx, cmd); err != nil {
return nil, err
Expand All @@ -133,7 +137,7 @@ func BuildGoProgram(ctx context.Context, options BuildGoProgramOptions) (*GoProg
}
cmd = exec.CommandContext(ctx, "go", cmdArgs...)
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if options.ApplyToCommand != nil {
if err := options.ApplyToCommand(ctx, cmd); err != nil {
return nil, err
Expand Down Expand Up @@ -169,6 +173,6 @@ func (g *GoProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd,
}
cmd := exec.CommandContext(ctx, exe, args...)
cmd.Dir = g.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, nil, nil)
return cmd, nil
}
10 changes: 8 additions & 2 deletions sdkbuild/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sdkbuild
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -35,6 +36,9 @@ type BuildJavaProgramOptions struct {
// If present, applied to build commands before run. May be called multiple
// times for a single build.
ApplyToCommand func(context.Context, *exec.Cmd) error
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// JavaProgram is a Java-specific implementation of Program.
Expand Down Expand Up @@ -139,6 +143,7 @@ includeBuild('` + asAbsPath + `') {
// used by run.
cmd := j.buildGradleCommand(ctx, dir, true,
options.ApplyToCommand, "--no-daemon", "--include-build", "../", "build")
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -177,7 +182,9 @@ func (j *JavaProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd
}
argsStr += "'" + arg + "'"
}
return j.buildGradleCommand(ctx, j.dir, true, nil, "--include-build", "../", "run", "--args", argsStr), nil
cmd := j.buildGradleCommand(ctx, j.dir, true, nil, "--include-build", "../", "run", "--args", argsStr)
setupCommandIO(cmd, nil, nil)
return cmd, nil
}

func (j *JavaProgram) buildGradleCommand(
Expand Down Expand Up @@ -207,6 +214,5 @@ func (j *JavaProgram) buildGradleCommand(

cmd := exec.CommandContext(ctx, exe, args...)
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return cmd
}
15 changes: 10 additions & 5 deletions sdkbuild/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os/exec"
"path/filepath"
"runtime"

"io"
)

// BuildPhpProgramOptions are options for BuildPhpProgram.
Expand All @@ -17,6 +19,9 @@ type BuildPhpProgramOptions struct {
// a temporary dir is created.
DirName string
RootDir string
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// PhpProgram is a PHP-specific implementation of Program.
Expand Down Expand Up @@ -47,7 +52,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP

// Skip if installed
if st, err := os.Stat(filepath.Join(dir, "vendor")); err == nil && st.IsDir() {
return &PhpProgram{dir, sourceDir}, nil
return &PhpProgram{dir: dir, source: sourceDir}, nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer.

}

// Copy composer.json from sourceDir into dir
Expand Down Expand Up @@ -75,7 +80,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
if options.Version != "" {
cmd = exec.CommandContext(ctx, "composer", "req", "temporal/sdk", options.Version, "-W", "--no-install", "--ignore-platform-reqs")
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed installing SDK deps: %w", err)
}
Expand All @@ -84,7 +89,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
// Install dependencies via composer
cmd = exec.CommandContext(ctx, "composer", "i", "-n", "-o", "-q", "--no-scripts", "--ignore-platform-reqs")
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed installing SDK deps: %w", err)
}
Expand All @@ -98,7 +103,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
if os.IsNotExist(err) {
cmd = exec.CommandContext(ctx, "composer", "run", "rr-get")
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed downloading RoadRunner: %w", err)
}
Expand Down Expand Up @@ -130,6 +135,6 @@ func (p *PhpProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd,
args = append([]string{filepath.Join(p.source, "runner.php")}, args...)
cmd := exec.CommandContext(ctx, "php", args...)
cmd.Dir = p.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, nil, nil)
return cmd, nil
}
17 changes: 11 additions & 6 deletions sdkbuild/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"
"strings"

"io"
)

// BuildPythonProgramOptions are options for BuildPythonProgram.
Expand All @@ -28,6 +30,9 @@ type BuildPythonProgramOptions struct {
// If present, applied to build commands before run. May be called multiple
// times for a single build.
ApplyToCommand func(context.Context, *exec.Cmd) error
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// PythonProgram is a Python-specific implementation of Program.
Expand Down Expand Up @@ -71,7 +76,7 @@ func BuildPythonProgram(ctx context.Context, options BuildPythonProgramOptions)
executeCommand := func(name string, args ...string) error {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Dir = dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, options.Stdout, options.Stderr)
if options.ApplyToCommand != nil {
if err := options.ApplyToCommand(ctx, cmd); err != nil {
return err
Expand All @@ -96,7 +101,7 @@ requires-python = "~=3.9"
executeCommand("uv", "add", options.VersionFromPyProj)
} else if strings.ContainsAny(options.Version, `/\`) {
// It's a path; install from wheel
wheel, err := getWheel(ctx, options.Version)
wheel, err := getWheel(ctx, options.Version, options.Stdout, options.Stderr)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +120,7 @@ requires-python = "~=3.9"
return &PythonProgram{dir}, nil
}

func getWheel(ctx context.Context, version string) (string, error) {
func getWheel(ctx context.Context, version string, stdout, stderr io.Writer) (string, error) {
// We expect a dist/ directory with a single whl file present
sdkPath, err := filepath.Abs(version)
if err != nil {
Expand All @@ -132,13 +137,13 @@ getWheels:
// Try to build the project
cmd := exec.CommandContext(ctx, "uv", "sync")
cmd.Dir = sdkPath
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, stdout, stderr)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("problem installing deps when building sdk by path: %w", err)
}
cmd = exec.CommandContext(ctx, "uv", "build")
cmd.Dir = sdkPath
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, stdout, stderr)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("problem building sdk by path: %w", err)
}
Expand Down Expand Up @@ -181,6 +186,6 @@ func (p *PythonProgram) NewCommand(ctx context.Context, args ...string) (*exec.C
args = append([]string{"run", "python", "-m"}, args...)
cmd := exec.CommandContext(ctx, "uv", args...)
cmd.Dir = p.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, nil, nil)
return cmd, nil
}
18 changes: 18 additions & 0 deletions sdkbuild/sdkbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package sdkbuild

import (
"context"
"io"
"os"
"os/exec"
)

Expand All @@ -17,3 +19,19 @@ type Program interface {
// stdio set as the current stdio.
NewCommand(ctx context.Context, args ...string) (*exec.Cmd, error)
}

// setupCommandIO sets up the command's I/O. If stdout or stderr are nil,
// defaults to os.Stdout and os.Stderr respectively. os.Stdin is always set to os.Stdin.
func setupCommandIO(cmd *exec.Cmd, stdout, stderr io.Writer) {
cmd.Stdin = os.Stdin
if stdout != nil {
cmd.Stdout = stdout
} else {
cmd.Stdout = os.Stdout
}
if stderr != nil {
cmd.Stderr = stderr
} else {
cmd.Stderr = os.Stderr
}
}
11 changes: 8 additions & 3 deletions sdkbuild/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"strconv"
"strings"

"io"
)

// BuildTypeScriptProgramOptions are options for BuildTypeScriptProgram.
Expand All @@ -33,6 +35,9 @@ type BuildTypeScriptProgramOptions struct {
Excludes []string
// If present, add additional dependencies -> version string to package.json.
MoreDependencies map[string]string
// If present, custom writers that will capture stdout/stderr.
Stdout io.Writer
Stderr io.Writer
}

// TypeScriptProgram is a TypeScript-specific implementation of Program.
Expand Down Expand Up @@ -255,12 +260,12 @@ func TypeScriptProgramFromDir(dir string) (*TypeScriptProgram, error) {
// Dir is the directory to run in.
func (t *TypeScriptProgram) Dir() string { return t.dir }

// NewCommand makes a new Node command. The first argument needs to be the name
// of the script.
// NewCommand makes a new Node command.
// The first argument needs to be the name of the script.
func (t *TypeScriptProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd, error) {
args = append([]string{"-r", "tsconfig-paths/register"}, args...)
cmd := exec.CommandContext(ctx, "node", args...)
cmd.Dir = t.dir
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
setupCommandIO(cmd, nil, nil)
return cmd, nil
}
Loading