Skip to content

Commit d52bcfc

Browse files
committed
Build with custom logger
1 parent c9d352c commit d52bcfc

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

sdkbuild/dotnet.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"runtime"
1111
"strings"
12+
13+
"io"
1214
)
1315

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

3237
// DotNetProgram is a .NET-specific implementation of Program.
@@ -77,7 +82,7 @@ func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions)
7782
// Need to build this csproj first
7883
cmd := exec.CommandContext(ctx, "dotnet", "build", absCsproj)
7984
cmd.Dir = dir
80-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
85+
setupCommandIO(cmd, options.Stdout, options.Stderr)
8186
if err := cmd.Run(); err != nil {
8287
return nil, fmt.Errorf("failed dotnet build of csproj in version: %w", err)
8388
}
@@ -103,7 +108,7 @@ func BuildDotNetProgram(ctx context.Context, options BuildDotNetProgramOptions)
103108
}
104109
cmd := exec.CommandContext(ctx, "dotnet", cmdArgs...)
105110
cmd.Dir = dir
106-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
111+
setupCommandIO(cmd, options.Stdout, options.Stderr)
107112
if err := cmd.Run(); err != nil {
108113
return nil, fmt.Errorf("failed dotnet build: %w", err)
109114
}
@@ -134,6 +139,6 @@ func (d *DotNetProgram) NewCommand(ctx context.Context, args ...string) (*exec.C
134139
}
135140
cmd := exec.CommandContext(ctx, exe, args...)
136141
cmd.Dir = d.dir
137-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
142+
setupCommandIO(cmd, nil, nil)
138143
return cmd, nil
139144
}

sdkbuild/go.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -37,6 +38,9 @@ type BuildGoProgramOptions struct {
3738
// If present, applied to build commands before run. May be called multiple
3839
// times for a single build.
3940
ApplyToCommand func(context.Context, *exec.Cmd) error
41+
// If present, custom writers that will capture stdout/stderr.
42+
Stdout io.Writer
43+
Stderr io.Writer
4044
}
4145

4246
// GoProgram is a Go-specific implementation of Program.
@@ -112,7 +116,7 @@ func BuildGoProgram(ctx context.Context, options BuildGoProgramOptions) (*GoProg
112116
// Tidy it
113117
cmd := exec.CommandContext(ctx, "go", "mod", "tidy")
114118
cmd.Dir = dir
115-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
119+
setupCommandIO(cmd, options.Stdout, options.Stderr)
116120
if options.ApplyToCommand != nil {
117121
if err := options.ApplyToCommand(ctx, cmd); err != nil {
118122
return nil, err
@@ -133,7 +137,7 @@ func BuildGoProgram(ctx context.Context, options BuildGoProgramOptions) (*GoProg
133137
}
134138
cmd = exec.CommandContext(ctx, "go", cmdArgs...)
135139
cmd.Dir = dir
136-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
140+
setupCommandIO(cmd, options.Stdout, options.Stderr)
137141
if options.ApplyToCommand != nil {
138142
if err := options.ApplyToCommand(ctx, cmd); err != nil {
139143
return nil, err
@@ -169,6 +173,6 @@ func (g *GoProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd,
169173
}
170174
cmd := exec.CommandContext(ctx, exe, args...)
171175
cmd.Dir = g.dir
172-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
176+
setupCommandIO(cmd, nil, nil)
173177
return cmd, nil
174178
}

sdkbuild/java.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sdkbuild
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -35,6 +36,9 @@ type BuildJavaProgramOptions struct {
3536
// If present, applied to build commands before run. May be called multiple
3637
// times for a single build.
3738
ApplyToCommand func(context.Context, *exec.Cmd) error
39+
// If present, custom writers that will capture stdout/stderr.
40+
Stdout io.Writer
41+
Stderr io.Writer
3842
}
3943

4044
// JavaProgram is a Java-specific implementation of Program.
@@ -139,6 +143,7 @@ includeBuild('` + asAbsPath + `') {
139143
// used by run.
140144
cmd := j.buildGradleCommand(ctx, dir, true,
141145
options.ApplyToCommand, "--no-daemon", "--include-build", "../", "build")
146+
setupCommandIO(cmd, options.Stdout, options.Stderr)
142147
if err := cmd.Run(); err != nil {
143148
return nil, err
144149
}
@@ -177,7 +182,9 @@ func (j *JavaProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd
177182
}
178183
argsStr += "'" + arg + "'"
179184
}
180-
return j.buildGradleCommand(ctx, j.dir, true, nil, "--include-build", "../", "run", "--args", argsStr), nil
185+
cmd := j.buildGradleCommand(ctx, j.dir, true, nil, "--include-build", "../", "run", "--args", argsStr)
186+
setupCommandIO(cmd, nil, nil)
187+
return cmd, nil
181188
}
182189

183190
func (j *JavaProgram) buildGradleCommand(
@@ -207,6 +214,5 @@ func (j *JavaProgram) buildGradleCommand(
207214

208215
cmd := exec.CommandContext(ctx, exe, args...)
209216
cmd.Dir = dir
210-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
211217
return cmd
212218
}

sdkbuild/php.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os/exec"
88
"path/filepath"
99
"runtime"
10+
11+
"io"
1012
)
1113

1214
// BuildPhpProgramOptions are options for BuildPhpProgram.
@@ -17,6 +19,9 @@ type BuildPhpProgramOptions struct {
1719
// a temporary dir is created.
1820
DirName string
1921
RootDir string
22+
// If present, custom writers that will capture stdout/stderr.
23+
Stdout io.Writer
24+
Stderr io.Writer
2025
}
2126

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

4853
// Skip if installed
4954
if st, err := os.Stat(filepath.Join(dir, "vendor")); err == nil && st.IsDir() {
50-
return &PhpProgram{dir, sourceDir}, nil
55+
return &PhpProgram{dir: dir, source: sourceDir}, nil
5156
}
5257

5358
// Copy composer.json from sourceDir into dir
@@ -75,7 +80,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
7580
if options.Version != "" {
7681
cmd = exec.CommandContext(ctx, "composer", "req", "temporal/sdk", options.Version, "-W", "--no-install", "--ignore-platform-reqs")
7782
cmd.Dir = dir
78-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
83+
setupCommandIO(cmd, options.Stdout, options.Stderr)
7984
if err := cmd.Run(); err != nil {
8085
return nil, fmt.Errorf("failed installing SDK deps: %w", err)
8186
}
@@ -84,7 +89,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
8489
// Install dependencies via composer
8590
cmd = exec.CommandContext(ctx, "composer", "i", "-n", "-o", "-q", "--no-scripts", "--ignore-platform-reqs")
8691
cmd.Dir = dir
87-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
92+
setupCommandIO(cmd, options.Stdout, options.Stderr)
8893
if err := cmd.Run(); err != nil {
8994
return nil, fmt.Errorf("failed installing SDK deps: %w", err)
9095
}
@@ -98,7 +103,7 @@ func BuildPhpProgram(ctx context.Context, options BuildPhpProgramOptions) (*PhpP
98103
if os.IsNotExist(err) {
99104
cmd = exec.CommandContext(ctx, "composer", "run", "rr-get")
100105
cmd.Dir = dir
101-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
106+
setupCommandIO(cmd, options.Stdout, options.Stderr)
102107
if err := cmd.Run(); err != nil {
103108
return nil, fmt.Errorf("failed downloading RoadRunner: %w", err)
104109
}
@@ -130,6 +135,6 @@ func (p *PhpProgram) NewCommand(ctx context.Context, args ...string) (*exec.Cmd,
130135
args = append([]string{filepath.Join(p.source, "runner.php")}, args...)
131136
cmd := exec.CommandContext(ctx, "php", args...)
132137
cmd.Dir = p.dir
133-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
138+
setupCommandIO(cmd, nil, nil)
134139
return cmd, nil
135140
}

sdkbuild/python.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"path/filepath"
99
"runtime"
1010
"strings"
11+
12+
"io"
1113
)
1214

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

3338
// PythonProgram is a Python-specific implementation of Program.
@@ -71,7 +76,7 @@ func BuildPythonProgram(ctx context.Context, options BuildPythonProgramOptions)
7176
executeCommand := func(name string, args ...string) error {
7277
cmd := exec.CommandContext(ctx, name, args...)
7378
cmd.Dir = dir
74-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
79+
setupCommandIO(cmd, options.Stdout, options.Stderr)
7580
if options.ApplyToCommand != nil {
7681
if err := options.ApplyToCommand(ctx, cmd); err != nil {
7782
return err
@@ -96,7 +101,7 @@ requires-python = "~=3.9"
96101
executeCommand("uv", "add", options.VersionFromPyProj)
97102
} else if strings.ContainsAny(options.Version, `/\`) {
98103
// It's a path; install from wheel
99-
wheel, err := getWheel(ctx, options.Version)
104+
wheel, err := getWheel(ctx, options.Version, options.Stdout, options.Stderr)
100105
if err != nil {
101106
return nil, err
102107
}
@@ -115,7 +120,7 @@ requires-python = "~=3.9"
115120
return &PythonProgram{dir}, nil
116121
}
117122

118-
func getWheel(ctx context.Context, version string) (string, error) {
123+
func getWheel(ctx context.Context, version string, stdout, stderr io.Writer) (string, error) {
119124
// We expect a dist/ directory with a single whl file present
120125
sdkPath, err := filepath.Abs(version)
121126
if err != nil {
@@ -132,13 +137,13 @@ getWheels:
132137
// Try to build the project
133138
cmd := exec.CommandContext(ctx, "uv", "sync")
134139
cmd.Dir = sdkPath
135-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
140+
setupCommandIO(cmd, stdout, stderr)
136141
if err := cmd.Run(); err != nil {
137142
return "", fmt.Errorf("problem installing deps when building sdk by path: %w", err)
138143
}
139144
cmd = exec.CommandContext(ctx, "uv", "build")
140145
cmd.Dir = sdkPath
141-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
146+
setupCommandIO(cmd, stdout, stderr)
142147
if err := cmd.Run(); err != nil {
143148
return "", fmt.Errorf("problem building sdk by path: %w", err)
144149
}
@@ -181,6 +186,6 @@ func (p *PythonProgram) NewCommand(ctx context.Context, args ...string) (*exec.C
181186
args = append([]string{"run", "python", "-m"}, args...)
182187
cmd := exec.CommandContext(ctx, "uv", args...)
183188
cmd.Dir = p.dir
184-
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
189+
setupCommandIO(cmd, nil, nil)
185190
return cmd, nil
186191
}

sdkbuild/sdkbuild.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
package sdkbuild
44

55
import (
6+
"cmp"
67
"context"
8+
"io"
9+
"os"
710
"os/exec"
811
)
912

@@ -17,3 +20,11 @@ type Program interface {
1720
// stdio set as the current stdio.
1821
NewCommand(ctx context.Context, args ...string) (*exec.Cmd, error)
1922
}
23+
24+
// setupCommandIO sets up the command's I/O. If stdout or stderr are nil,
25+
// defaults to os.Stdout and os.Stderr respectively. os.Stdin is always set to os.Stdin.
26+
func setupCommandIO(cmd *exec.Cmd, stdout, stderr io.Writer) {
27+
cmd.Stdin = os.Stdin
28+
cmd.Stdout = cmp.Or(stdout, os.Stdout)
29+
cmd.Stderr = cmp.Or(stderr, os.Stderr)
30+
}

sdkbuild/typescript.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"path/filepath"
99
"strconv"
1010
"strings"
11+
12+
"io"
1113
)
1214

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

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

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

0 commit comments

Comments
 (0)