Skip to content

Commit 3589a34

Browse files
chore(internal): contribute.md and contributor QoL improvements
1 parent d2d98bd commit 3589a34

File tree

10 files changed

+209
-23
lines changed

10 files changed

+209
-23
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
*.gem
22
.idea/
3+
.ignore
34
.prism.log
45
.ruby-lsp/
56
.yardoc/
6-
Brewfile.lock.json
77
bin/tapioca
8+
Brewfile.lock.json
89
doc/
9-
sorbet/*
10-
!/sorbet/config
10+
sorbet/tapioca/*

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1.0

CONTRIBUTING.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
## Setting up the environment
2+
3+
This repository contains a `.ruby-version` file, which should work with either [rbenv](https://github.com/rbenv/rbenv) or [asdf](https://github.com/asdf-vm/asdf) with the [ruby plugin](https://github.com/asdf-vm/asdf-ruby).
4+
5+
Please follow the instructions for your preferred version manager to install the Ruby version specified in the `.ruby-version` file.
6+
7+
To set up the repository, run:
8+
9+
```bash
10+
$ ./scripts/bootstrap
11+
```
12+
13+
This will install all the required dependencies.
14+
15+
## Modifying/Adding code
16+
17+
Most of the SDK is generated code. Modifications to code will be persisted between generations, but may result in merge conflicts between manual patches and changes from the generator. The generator will never modify the contents `examples/` directory.
18+
19+
## Adding and running examples
20+
21+
All files in the `examples/` directory are not modified by the generator and can be freely edited or added to.
22+
23+
```ruby
24+
#!/usr/bin/env ruby
25+
# frozen_string_literal: true
26+
27+
require_relative "../lib/openai"
28+
29+
# ...
30+
```
31+
32+
```bash
33+
$ chmod +x './examples/<your-example>.rb'
34+
35+
# run the example against your api
36+
$ ruby './examples/<your-example>.rb'
37+
```
38+
39+
## Using the repository from source
40+
41+
If you’d like to use the repository from source, you can either install from git or reference a cloned repository:
42+
43+
To install via git in your `Gemfile`:
44+
45+
```ruby
46+
gem "openai", git: "https://www.github.com/openai/openai-ruby"
47+
```
48+
49+
Alternatively, reference local copy of the repo:
50+
51+
```bash
52+
$ git clone -- 'https://www.github.com/openai/openai-ruby' '<path-to-repo>'
53+
```
54+
55+
```ruby
56+
gem "openai", path: "<path-to-repo>"
57+
```
58+
59+
## Running commands
60+
61+
Running `rake` by itself will show all runnable commands.
62+
63+
```bash
64+
$ bundle exec rake
65+
```
66+
67+
## Running tests
68+
69+
Most tests require you to [set up a mock server](https://github.com/stoplightio/prism) against the OpenAPI spec to run the tests.
70+
71+
```bash
72+
$ npx prism mock path/to/your/openapi.yml
73+
```
74+
75+
```bash
76+
$ bundle exec rake test
77+
```
78+
79+
## Linting and formatting
80+
81+
This repository uses [rubocop](https://github.com/rubocop/rubocop) for linting and formatting of `*.rb` and `*.rbi` files. [syntax_tree](https://github.com/ruby-syntax-tree/syntax_tree) is used for formatting `*.rbs` files.
82+
83+
There are two separate type checkers supported by this library: [sorbet](https://github.com/sorbet/sorbet) and [steep](https://github.com/soutaro/steep) are used for verifying `*.rbi` and `*.rbs` files respectively.
84+
85+
To lint and typecheck:
86+
87+
```bash
88+
$ bundle exec rake lint
89+
```
90+
91+
To format and fix all lint issues automatically:
92+
93+
```bash
94+
$ bundle exec rake format
95+
```
96+
97+
## Editor Support
98+
99+
### Solargraph
100+
101+
This library includes [Solargraph](https://solargraph.org) support for both auto-completion and go to definition.
102+
103+
```ruby
104+
gem "solargraph", group: :development
105+
```
106+
107+
Note: if you had installed the gem locally using `git: "..."` or `path: "..."`, you must update your [`.solargraph.yml`](https://solargraph.org/guides/configuration) to include the path to where the gem is located:
108+
109+
```yaml
110+
include:
111+
- '<path-to-repo>/lib/**/*.rb'
112+
```
113+
114+
### Sorbet
115+
116+
[Sorbet](https://sorbet.org) should mostly work out of the box when editing this library directly. However, there are a some caveats due to the colocation of `*.rb` and `*.rbi` files in the same project. These issues should not otherwise manifest when this library is used as a dependency.
117+
118+
1. For go to definition usages, sorbet might get confused and may not always navigate to the correct location.
119+
120+
2. For each generic type in `*.rbi` files, a spurious "Duplicate type member" error is present.
121+
122+
### Ruby LSP
123+
124+
The Ruby LSP has [best effort support](https://shopify.github.io/ruby-lsp/#guessed-types) for inferring type information from Ruby code, and as such it may not always be able to provide accurate type information.
125+
126+
## Documentation Preview
127+
128+
To preview the documentation, run:
129+
130+
```bash
131+
$ bundle exec rake docs:preview [PORT=8808]
132+
```

Rakefile

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
# frozen_string_literal: true
22

3+
require "pathname"
34
require "securerandom"
45
require "shellwords"
56

67
require "minitest/test_task"
78
require "rake/clean"
89
require "rubocop/rake_task"
910

10-
CLEAN.push(*%w[.idea/ .ruby-lsp/ .yardoc/])
11+
tapioca = "sorbet/tapioca"
12+
ignore_file = ".ignore"
1113

12-
multitask(default: [:test])
14+
CLEAN.push(*%w[.idea/ .ruby-lsp/ .yardoc/ doc/], *FileList["*.gem"], ignore_file)
1315

16+
CLOBBER.push(*%w[sorbet/rbi/annotations/ sorbet/rbi/gems/], tapioca)
17+
18+
multitask(:default) do
19+
sh(*%w[rake --tasks])
20+
end
21+
22+
desc("Preview docs; use `PORT=<PORT>` to change the port")
23+
multitask(:"docs:preview") do
24+
sh(*%w[yard server --bind [::] --reload --quiet --port], ENV.fetch("PORT", "8808"))
25+
end
26+
27+
desc("Run test suites; use `TEST=path/to/test.rb` to run a specific test file")
1428
multitask(:test) do
1529
rb =
1630
FileList[ENV.fetch("TEST", "./test/**/*_test.rb")]
@@ -23,17 +37,20 @@ end
2337
rubo_find = %w[find ./lib ./test ./rbi -type f -and ( -name *.rb -or -name *.rbi ) -print0]
2438
xargs = %w[xargs --no-run-if-empty --null --max-procs=0 --max-args=300 --]
2539

26-
multitask(:rubocop) do
40+
desc("Lint `*.rb(i)`")
41+
multitask(:"lint:rubocop") do
2742
lint = xargs + %w[rubocop --fail-level E] + (ENV.key?("CI") ? %w[--format github] : [])
2843
sh("#{rubo_find.shelljoin} | #{lint.shelljoin}")
2944
end
3045

31-
multitask(:ruboformat) do
46+
desc("Format `*.rb(i)`")
47+
multitask(:"format:rubocop") do
3248
fmt = xargs + %w[rubocop --fail-level F --autocorrect --format simple --]
3349
sh("#{rubo_find.shelljoin} | #{fmt.shelljoin}")
3450
end
3551

36-
multitask(:syntax_tree) do
52+
desc("Format `*.rbs`")
53+
multitask(:"format:syntax_tree") do
3754
find = %w[find ./sig -type f -name *.rbs -print0]
3855
inplace = /darwin|bsd/ =~ RUBY_PLATFORM ? %w[-i''] : %w[-i]
3956
uuid = SecureRandom.uuid
@@ -74,27 +91,49 @@ multitask(:syntax_tree) do
7491
fail unless success
7592
end
7693

77-
multitask(format: [:ruboformat, :syntax_tree])
94+
desc("Format everything")
95+
multitask(format: [:"format:rubocop", :"format:syntax_tree"])
7896

79-
multitask(:steep) do
97+
desc("Typecheck `*.rbs`")
98+
multitask(:"typecheck:steep") do
8099
sh(*%w[steep check])
81100
end
82101

83-
multitask(:sorbet) do
102+
desc("Typecheck `*.rbi`")
103+
multitask(:"typecheck:sorbet") do
84104
sh(*%w[srb typecheck])
85105
end
86106

87-
file("sorbet/tapioca") do
107+
file(tapioca) do
88108
sh(*%w[tapioca init])
89109
end
90110

91-
multitask(typecheck: [:steep, :sorbet])
92-
multitask(lint: [:rubocop, :typecheck])
111+
desc("Typecheck everything")
112+
multitask(typecheck: [:"typecheck:steep", :"typecheck:sorbet"])
113+
114+
desc("Lint everything")
115+
multitask(lint: [:"lint:rubocop", :typecheck])
116+
117+
desc("Build yard docs")
118+
multitask(:"build:docs") do
119+
sh(*%w[yard])
120+
end
121+
122+
desc("Build ruby gem")
123+
multitask(:"build:gem") do
124+
# optimizing for grepping through the gem bundle: many tools honour `.ignore` files, including VSCode
125+
#
126+
# both `rbi` and `sig` directories are navigable by their respective tool chains and therefore can be ignored by tools such as `rg`
127+
Pathname(ignore_file).write(<<~GLOB)
128+
rbi/*
129+
sig/*
130+
GLOB
93131

94-
multitask(:build) do
95132
sh(*%w[gem build -- openai.gemspec])
133+
rm_rf(ignore_file)
96134
end
97135

98-
multitask(release: [:build]) do
136+
desc("Release ruby gem")
137+
multitask(release: [:"build:gem"]) do
99138
sh(*%w[gem push], *FileList["openai-*.gem"])
100139
end

openai.gemspec

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ Gem::Specification.new do |s|
88
s.summary = "Ruby library to access the OpenAI API"
99
s.authors = ["OpenAI"]
1010
s.email = "[email protected]"
11-
s.files = Dir["lib/**/*.rb", "rbi/**/*.rbi", "sig/**/*.rbs", "manifest.yaml", "CHANGELOG.md", "SECURITY.md"]
12-
s.extra_rdoc_files = ["README.md"]
13-
s.required_ruby_version = ">= 3.0.0"
14-
s.add_dependency "connection_pool"
1511
s.homepage = "https://gemdocs.org/gems/openai"
1612
s.metadata["homepage_uri"] = s.homepage
1713
s.metadata["source_code_uri"] = "https://github.com/openai/openai-ruby"
1814
s.metadata["rubygems_mfa_required"] = false.to_s
15+
s.required_ruby_version = ">= 3.0.0"
16+
17+
s.files = Dir[
18+
"lib/**/*.rb",
19+
"rbi/**/*.rbi",
20+
"sig/**/*.rbs",
21+
"manifest.yaml",
22+
"SECURITY.md",
23+
"CHANGELOG.md",
24+
".ignore"
25+
]
26+
s.extra_rdoc_files = ["README.md"]
27+
s.add_dependency "connection_pool"
1928
end

scripts/bootstrap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
cd "$(dirname "$0")/.."
5+
cd -- "$(dirname -- "$0")/.."
66

77
if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "$SKIP_BREW" != "1" ]; then
88
brew bundle check >/dev/null 2>&1 || {
@@ -13,4 +13,4 @@ fi
1313

1414
echo "==> Installing Ruby dependencies…"
1515

16-
bundle install
16+
exec -- bundle install "$@"

scripts/format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ set -e
55
cd -- "$(dirname -- "$0")/.."
66

77
echo "==> Running formatters"
8+
89
exec -- bundle exec rake format "$@"

scripts/lint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ set -e
44

55
cd -- "$(dirname -- "$0")/.."
66

7+
echo "==> Running linters"
8+
79
exec -- bundle exec rake lint "$@"

scripts/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
cd "$(dirname "$0")/.."
5+
cd -- "$(dirname -- "$0")/.."
66

77
RED='\033[0;31m'
88
GREEN='\033[0;32m'

sorbet/rbi/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)