Skip to content

Commit 98da223

Browse files
RUBY-3209 Generate api docs (#2768)
1 parent 9c90702 commit 98da223

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ data/*
2525
gemfiles/*.gemfile.lock
2626
.env.private*
2727
.env
28+
build

Diff for: upload-api-docs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'bundler/inline'
5+
6+
gemfile true do
7+
source 'https://rubygems.org'
8+
gem 'nokogiri'
9+
gem 'aws-sdk-s3'
10+
gem 'yard'
11+
end
12+
13+
require 'aws-sdk-s3'
14+
require 'optparse'
15+
require 'yard'
16+
17+
# This class contains logic for uploading API docs to S3.
18+
class FileUploader
19+
def initialize(options)
20+
Aws.config.update({
21+
region: options[:region],
22+
credentials: Aws::Credentials.new(options[:access_key], options[:secret_key])
23+
})
24+
Aws.use_bundled_cert!
25+
@s3 = Aws::S3::Client.new
26+
@bucket = options[:bucket]
27+
@prefix = options[:prefix]
28+
@docs_path = options[:docs_path]
29+
end
30+
31+
def upload_docs
32+
Dir.glob("#{@docs_path}/**/*").each do |file|
33+
next if File.directory?(file)
34+
35+
upload_file(file, key(file))
36+
print '.'
37+
$stdout.flush
38+
end
39+
puts "\nDone!"
40+
end
41+
42+
private
43+
44+
def key(file)
45+
File.join(@prefix, file.gsub("#{@docs_path}/", ''))
46+
end
47+
48+
def upload_file(file, key)
49+
mime_type = mime_type(file)
50+
@s3.put_object(bucket: @bucket, key: key, body: File.read(file), content_type: mime_type)
51+
end
52+
53+
def mime_type(file)
54+
{
55+
'.html' => 'text/html',
56+
'.css' => 'text/css',
57+
'.js' => 'application/javascript',
58+
}.fetch(File.extname(file))
59+
end
60+
end
61+
62+
# This class contains logic for parsing CLI and ENV options.
63+
class Options
64+
def initialize
65+
@options = {}
66+
parse_cli_options!
67+
parse_env_options!
68+
@options[:prefix] = 'docs/ruby-driver/current/api'
69+
@options[:docs_path] = 'build/public/current/api'
70+
end
71+
72+
def [](key)
73+
@options[key]
74+
end
75+
76+
private
77+
78+
def parse_cli_options!
79+
OptionParser.new do |opts|
80+
opts.banner = 'Usage: upload-api-docs [options]'
81+
82+
opts.on('-b BUCKET', '--bucket=BUCKET', 'S3 Bucket to upload') do |b|
83+
@options[:bucket] = b
84+
end
85+
opts.on('-r REGION', '--region=REGION', 'AWS region') do |r|
86+
@options[:region] = r
87+
end
88+
end.parse!
89+
%i[bucket region].each do |opt|
90+
raise OptionParser::MissingArgument, "Option --#{opt} is required" unless @options[opt]
91+
end
92+
end
93+
94+
def parse_env_options!
95+
@options[:access_key] = ENV.fetch('DOCS_AWS_ACCESS_KEY_ID') do
96+
raise ArgumentError, 'Please provide aws access key via DOCS_AWS_ACCESS_KEY_ID env variable'
97+
end
98+
@options[:secret_key] = ENV.fetch('DOCS_AWS_SECRET_ACCESS_KEY') do
99+
raise ArgumentError, 'Please provide aws secret key via DOCS_AWS_SECRET_ACCESS_KEY env variable'
100+
end
101+
end
102+
end
103+
104+
def generate_docs(options)
105+
YARD::CLI::Yardoc.run(
106+
'.',
107+
'--exclude', './.evergreen',
108+
'--exclude', './.mod',
109+
'--exclude', './examples',
110+
'--exclude', './profile',
111+
'--exclude', './release',
112+
'--exclude', './spec',
113+
'--readme', './README.md',
114+
'-o', options[:docs_path]
115+
)
116+
end
117+
118+
options = Options.new
119+
generate_docs(options)
120+
FileUploader.new(options).upload_docs
121+
return

0 commit comments

Comments
 (0)