Skip to content

Commit 86e4c0d

Browse files
authored
Specify additional dependencies with Ruby build (#809)
* specify additional dependencies with Ruby build * cleanup
1 parent e96b79a commit 86e4c0d

1 file changed

Lines changed: 42 additions & 16 deletions

File tree

sdkbuild/ruby.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ type BuildRubyProgramOptions struct {
2424
// If present, this directory is expected to exist beneath base dir. Otherwise
2525
// a temporary dir is created.
2626
DirName string
27+
// If present, additional gems to add to the generated Gemfile.
28+
MoreDependencies []RubyDependency
2729
// If present, custom writers that will capture stdout/stderr.
2830
Stdout io.Writer
2931
Stderr io.Writer
3032
}
3133

34+
// RubyDependency is an additional gem dependency for the generated Gemfile.
35+
type RubyDependency struct {
36+
Name string
37+
Version string
38+
Path string
39+
}
40+
3241
// RubyProgram is a Ruby-specific implementation of Program.
3342
type RubyProgram struct {
3443
dir string
@@ -79,7 +88,7 @@ func BuildRubyProgram(ctx context.Context, options BuildRubyProgramOptions) (*Ru
7988
// Build the Gemfile content. We use Bundler's `gemspec` directive to
8089
// auto-discover the gemspec in the source directory (via path: option).
8190
// This works for any gem name (harness, omes, etc.).
82-
var gemfileContent string
91+
gemfileLines := []string{`source "https://rubygems.org"`, ""}
8392
if strings.ContainsAny(options.Version, `/\`) {
8493
// It's a path to a local SDK repo
8594
sdkPath, err := filepath.Abs(options.Version)
@@ -95,25 +104,22 @@ func BuildRubyProgram(ctx context.Context, options BuildRubyProgramOptions) (*Ru
95104
return nil, fmt.Errorf("failed finding temporalio.gemspec in version dir: %w", err)
96105
}
97106
}
98-
gemfileContent = fmt.Sprintf(`source "https://rubygems.org"
99-
100-
gem "temporalio", path: %q
101-
gemspec path: %q
102-
`, gemPath, sourceDir)
107+
gemfileLines = append(gemfileLines, fmt.Sprintf(`gem "temporalio", path: %q`, gemPath))
103108
} else if options.Version != "" {
104109
version := strings.TrimPrefix(options.Version, "v")
105-
gemfileContent = fmt.Sprintf(`source "https://rubygems.org"
106-
107-
gem "temporalio", "%s"
108-
gemspec path: %q
109-
`, version, sourceDir)
110-
} else {
111-
// No version constraint — Bundler resolves to latest from RubyGems
112-
gemfileContent = fmt.Sprintf(`source "https://rubygems.org"
110+
gemfileLines = append(gemfileLines, fmt.Sprintf(`gem "temporalio", %q`, version))
111+
}
113112

114-
gemspec path: %q
115-
`, sourceDir)
113+
moreDependencyLines, err := renderRubyDependencies(options.MoreDependencies)
114+
if err != nil {
115+
return nil, err
116116
}
117+
if len(moreDependencyLines) > 0 {
118+
gemfileLines = append(gemfileLines, moreDependencyLines...)
119+
gemfileLines = append(gemfileLines, "")
120+
}
121+
gemfileLines = append(gemfileLines, fmt.Sprintf(`gemspec path: %q`, sourceDir), "")
122+
gemfileContent := strings.Join(gemfileLines, "\n")
117123

118124
if err := os.WriteFile(filepath.Join(dir, "Gemfile"), []byte(gemfileContent), 0644); err != nil {
119125
return nil, fmt.Errorf("failed writing Gemfile: %w", err)
@@ -157,6 +163,26 @@ gemspec path: %q
157163
return &RubyProgram{dir: dir, source: sourceDir}, nil
158164
}
159165

166+
func renderRubyDependencies(dependencies []RubyDependency) ([]string, error) {
167+
lines := make([]string, 0, len(dependencies))
168+
for _, dependency := range dependencies {
169+
if dependency.Name == "" {
170+
return nil, fmt.Errorf("ruby dependency name required")
171+
}
172+
if dependency.Path != "" && dependency.Version != "" {
173+
return nil, fmt.Errorf("ruby dependency %q cannot have both path and version", dependency.Name)
174+
}
175+
if dependency.Path != "" {
176+
lines = append(lines, fmt.Sprintf(`gem %q, path: %q`, dependency.Name, dependency.Path))
177+
} else if dependency.Version != "" {
178+
lines = append(lines, fmt.Sprintf(`gem %q, %q`, dependency.Name, dependency.Version))
179+
} else {
180+
lines = append(lines, fmt.Sprintf(`gem %q`, dependency.Name))
181+
}
182+
}
183+
return lines, nil
184+
}
185+
160186
// RubyProgramFromDir recreates the Ruby program from a Dir() result of a
161187
// BuildRubyProgram(). The sourceDir should point to the directory containing
162188
// the gemspec and runner.rb.

0 commit comments

Comments
 (0)