Skip to content

Commit b5482f1

Browse files
borrow tapioca doc harness
1 parent 5cfa7d9 commit b5482f1

10 files changed

+180
-10
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
.ruby-lsp
1+
.ruby-lsp/
22
*.gem
3+
.yardoc/

Gemfile

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ ruby "3.3.5"
55
source "https://rubygems.org"
66
gemspec
77

8-
gem "rubocop-rspec"
9-
gem "rubocop-shopify"
10-
gem "rubocop-sorbet"
11-
128
group :development do
139
gem "money-rails"
10+
gem "rubocop-rspec"
11+
gem "rubocop-shopify"
12+
gem "rubocop-sorbet"
1413
end

Gemfile.lock

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ GEM
5252
rdoc (>= 4.0.0)
5353
reline (>= 0.4.2)
5454
json (2.7.2)
55+
kramdown (2.4.0)
56+
rexml
5557
language_server-protocol (3.17.0.3)
5658
logger (1.6.1)
5759
loofah (2.22.0)
@@ -174,6 +176,7 @@ PLATFORMS
174176

175177
DEPENDENCIES
176178
boba!
179+
kramdown
177180
money-rails
178181
rubocop-rspec
179182
rubocop-shopify

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ clean:
1313
.PHONY: release
1414
release: clean build
1515
gem push boba-*.gem
16+
17+
18+
.PHONY: docs
19+
docs:
20+
bundle exec rake generate_dsl_documentation

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ Boba is a collection of compilers for Sorbet & Tapioca.
66

77
Tapioca is very opinionated about what types of compilers or changes are accepted into the repository. See
88
[here](https://github.com/Shopify/tapioca?tab=readme-ov-file#dsl-compilers). Boba come in all
9-
different shapes, sizes, consistencies, etc, and is much less opinionated about what is or is not accepted. Think of
10-
Boba like a collection of compilers that you can pick and choose from.
9+
different shapes, sizes, consistencies, etc, and is much less opinionated about what is or is not accepted. Boba is a collection of optional compilers that you can pick and choose from.
10+
11+
### Available Compilers
12+
13+
See [the compilers manual](https://github.com/angellist/boba/blob/main/manual/compilers.md) for a list of available compilers.
1114

1215
## Usage
1316

@@ -26,6 +29,7 @@ dsl:
2629
Compiler1
2730
Compiler2
2831
```
32+
This makes it easy to selectively enable only the compilers you want to use in your project.
2933
3034
## Contributing
3135
@@ -39,9 +43,10 @@ Since Boba is intended to be used alongside Tapioca and the compilers provided b
3943
we will not accept compilers which overwrite the Tapioca default compilers. See the [Tapioca Manual](https://github.com/Shopify/tapioca/blob/main/manual/compilers.md) for a list of these
4044
compilers. Instead, compilers which extend or overwrite the default Tapioca compilers should be given unique names.
4145
42-
Contributed compilers should be documented and include a link or reference to the Gem, DSL, or other module they implement RBIs for.
46+
Contributed compilers should be well documented, and named after and include a link or reference to the Gem, DSL, or other module they implement RBIs for.
47+
48+
Compilers for Gems, DSLs, or modules that are not publicly available will not be accepted.
4349
4450
## Todo
4551
4652
1. Specs & spec harness
47-
2. Docs & doc harness

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
Dir["tasks/**/*.rake"].each { |t| load t }
5+
6+
require "rubocop/rake_task"
7+
RuboCop::RakeTask.new

lib/tapioca/dsl/compilers/money_rails.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ module Dsl
1010
module Compilers
1111
# `Tapioca::Dsl::Compilers::MoneyRails` decorates RBI files for classes that use the `monetize` method provided
1212
# by the `money-rails` gem.
13+
# https://github.com/RubyMoney/money-rails
1314
#
1415
# For example, with the following ActiveRecord model:
1516
# ~~~rb
1617
# class Product < ActiveRecord::Base
17-
# monetize :price_cents
18+
# monetize :price_cents
1819
# end
1920
# ~~~
2021
#

manual/compiler_moneyrails.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## MoneyRails
2+
3+
`Tapioca::Dsl::Compilers::MoneyRails` decorates RBI files for classes that use the `monetize` method provided
4+
by the `money-rails` gem.
5+
https://github.com/RubyMoney/money-rails
6+
7+
For example, with the following ActiveRecord model:
8+
~~~rb
9+
class Product < ActiveRecord::Base
10+
monetize :price_cents
11+
end
12+
~~~
13+
14+
This compiler will generate the following RBI:
15+
~~~rbi
16+
class Product
17+
include MoneyRailsGeneratedMethods
18+
19+
module MoneyRailsGeneratedMethods
20+
sig { returns(::Money) }
21+
def price; end
22+
23+
sig { params(value: ::Money).returns(::Money) }
24+
def price=(value); end
25+
end
26+
end
27+
~~~

manual/compilers.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Compilers
2+
3+
This list is an evergeen list of currently available compilers.
4+
5+
## Compiler List
6+
7+
<!-- START_COMPILER_LIST -->
8+
* [MoneyRails](compiler_moneyrails.md)
9+
<!-- END_COMPILER_LIST -->

tasks/generator_documentation.rake

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "yard"
5+
require "tapioca"
6+
require "tapioca/runtime/reflection"
7+
8+
YARD::Rake::YardocTask.new(:yard_for_generate_documentation) do |task|
9+
task.files = ["lib/tapioca/dsl/compilers/**/*.rb"]
10+
task.options = ["--no-output"]
11+
end
12+
13+
desc("Generate docs of all DSL compilers")
14+
task generate_dsl_documentation: :yard_for_generate_documentation do
15+
def assert_manual_synchronized
16+
# Do not print diff and yield whether exit code was zero
17+
sh('test -z "$(git status --porcelain manual)"') do |outcome, _|
18+
return if outcome
19+
20+
# Output diff before raising error
21+
sh("git status --porcelain manual")
22+
23+
warn(<<~WARNING)
24+
The manual directory is out of sync.
25+
Run `bin/docs` (or `dev docs`) and commit the results.
26+
WARNING
27+
28+
exit!
29+
end
30+
end
31+
32+
def table_contents(registry)
33+
registry
34+
.filter_map do |entry|
35+
"* #{entry.link}"
36+
end
37+
.join("\n")
38+
end
39+
40+
def print_table_of_contents(registry)
41+
path = "#{Dir.pwd}/manual/compilers.md"
42+
original = File.read(path)
43+
content = +"<!-- START_COMPILER_LIST -->\n"
44+
45+
content << table_contents(registry)
46+
47+
content << "\n<!-- END_COMPILER_LIST -->"
48+
49+
content = if original.empty?
50+
content
51+
else
52+
original.sub(
53+
/<!-- START_COMPILER_LIST -->.+<!-- END_COMPILER_LIST -->/m, content
54+
)
55+
end
56+
File.write(path, content)
57+
end
58+
59+
def compiler_body(registry_entry)
60+
content = +"## #{registry_entry.name}\n"
61+
content << "\n"
62+
content << "#{registry_entry.description}\n"
63+
content
64+
end
65+
66+
def print_compilers(registry)
67+
registry.each do |entry|
68+
File.write(entry.filename, compiler_body(entry))
69+
end
70+
end
71+
72+
def load_registry
73+
YARD::Registry.all(:class).filter_map do |code_object|
74+
next unless code_object.superclass.to_s == "Tapioca::Dsl::Compiler"
75+
76+
RegistryEntry.new(code_object.name.to_s, code_object)
77+
end.sort_by(&:name)
78+
end
79+
80+
RegistryEntry = Struct.new(:name, :code_object) do
81+
def filename
82+
"#{Dir.pwd}/manual/compiler_#{name.downcase}.md"
83+
end
84+
85+
def filebasename
86+
File.basename(filename)
87+
end
88+
89+
def link
90+
"[#{name}](#{filebasename})"
91+
end
92+
93+
def description
94+
if code_object.docstring.blank?
95+
"[No description provided]"
96+
else
97+
code_object.docstring
98+
end
99+
end
100+
end
101+
102+
def main
103+
Dir.glob("lib/tapioca/dsl/{compiler.rb,compilers/**/*.rb}") { |file| YARD.parse(file) }
104+
registry = load_registry
105+
106+
print_table_of_contents(registry)
107+
print_compilers(registry)
108+
109+
assert_manual_synchronized if ENV["CI"] == "true"
110+
end
111+
112+
main
113+
end

0 commit comments

Comments
 (0)