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