lap 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.lap.yml +1 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +46 -0
- data/LICENSE.txt +21 -0
- data/README.md +200 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/lap +10 -0
- data/lap.gemspec +25 -0
- data/lib/core_ext/string.rb +7 -0
- data/lib/lap.rb +44 -0
- data/lib/lap/class.rb +61 -0
- data/lib/lap/helpers.rb +45 -0
- data/lib/lap/method.rb +88 -0
- data/lib/lap/module.rb +45 -0
- data/lib/lap/version.rb +3 -0
- data/sample/a.rbs +20 -0
- data/sample/bank.rbs +40 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 67d48053cc0e3fe1a6f28c32b764c69497c2a3416e7c21ee7eeedd36b7768e33
|
4
|
+
data.tar.gz: 86b2d87a47f3e77964f14f6b876553a1365207a0bb1522df8f48cae43cbefd21
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1dfe0739b7d757c9d67474b02c2f4c832f217b8a49129c97e068fa048d661dc066883e162462feb5a5b3fd7b8bd60b4f965f4921898a79c374a0db38f299fad4
|
7
|
+
data.tar.gz: 8b3f3a348b71222475155bfe7271757dbaf6ab505c0e6166e3c31140794f9a3cc25ba9a06d3ac6886af8f2098130cb8a2b503a148c015933afc143e0c43be939
|
data/.gitignore
ADDED
data/.lap.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
indent: 2
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
lap (0.1)
|
5
|
+
rbs
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
byebug (11.1.3)
|
11
|
+
coderay (1.1.3)
|
12
|
+
diff-lcs (1.4.4)
|
13
|
+
method_source (1.0.0)
|
14
|
+
pry (0.13.1)
|
15
|
+
coderay (~> 1.1)
|
16
|
+
method_source (~> 1.0)
|
17
|
+
pry-byebug (3.9.0)
|
18
|
+
byebug (~> 11.0)
|
19
|
+
pry (~> 0.13.0)
|
20
|
+
rake (12.3.3)
|
21
|
+
rbs (0.17.0)
|
22
|
+
rspec (3.10.0)
|
23
|
+
rspec-core (~> 3.10.0)
|
24
|
+
rspec-expectations (~> 3.10.0)
|
25
|
+
rspec-mocks (~> 3.10.0)
|
26
|
+
rspec-core (3.10.0)
|
27
|
+
rspec-support (~> 3.10.0)
|
28
|
+
rspec-expectations (3.10.0)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.10.0)
|
31
|
+
rspec-mocks (3.10.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.10.0)
|
34
|
+
rspec-support (3.10.0)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
lap!
|
41
|
+
pry-byebug
|
42
|
+
rake (~> 12.0)
|
43
|
+
rspec (~> 3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Joseph Johansen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
# Lap
|
2
|
+
|
3
|
+
Don't write your code, *and* rbs types! Write the rbs types first, then generate a template to
|
4
|
+
fill in with business logic.
|
5
|
+
|
6
|
+
A `lap`idary (or `lap`idist) is someone who cuts, polishes, or engraves precious stones.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
```sh
|
11
|
+
$ lap sig/file.rbs # outputs to stdout
|
12
|
+
$ lap sig/file.rbs > lib/file.rb # output into a file
|
13
|
+
```
|
14
|
+
|
15
|
+
## Example
|
16
|
+
|
17
|
+
0. Plan your project/module/class by writing some blueprints:
|
18
|
+
```ruby
|
19
|
+
# sig/lib/bank/account.rbs
|
20
|
+
module Bank
|
21
|
+
class Account
|
22
|
+
attr_reader interest_rate: Float
|
23
|
+
attr_reader owner: Bank::Customer
|
24
|
+
attr_reader balance: Float
|
25
|
+
|
26
|
+
type date_or_dt = Date | DateTime
|
27
|
+
|
28
|
+
# can optionally specify when the transaction should take place
|
29
|
+
def deposit: (Float amount, ?when: date_or_dt) -> Float
|
30
|
+
|
31
|
+
# can optionally specify when the transaction should take place
|
32
|
+
def withdraw: (Float amount, ?when: date_or_dt) -> Float
|
33
|
+
|
34
|
+
# must filter results by specifying `to` and `from` params
|
35
|
+
def transactions: (from: date_or_dt, to: date_or_dt) -> Array[Bank::Transaction]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
1. Generate your ruby templates
|
41
|
+
|
42
|
+
```sh
|
43
|
+
$ lap sig/lib/bank/account.rbs > lib/bank/account.rb
|
44
|
+
```
|
45
|
+
|
46
|
+
(Generates lib/bank/account.rb)
|
47
|
+
```ruby
|
48
|
+
module Bank
|
49
|
+
class Account
|
50
|
+
attr_reader :interest_rate
|
51
|
+
attr_reader :owner
|
52
|
+
attr_reader :balance
|
53
|
+
|
54
|
+
def initialize(owner, interest_rate, balance)
|
55
|
+
@owner = owner
|
56
|
+
@interest_rate = interest_rate
|
57
|
+
@balance = balance
|
58
|
+
end
|
59
|
+
|
60
|
+
# can optionally specify when the transaction should take place
|
61
|
+
def deposit(amount, when: nil)
|
62
|
+
# TODO: return Float
|
63
|
+
end
|
64
|
+
|
65
|
+
# can optionally specify when the transaction should take place
|
66
|
+
def withdraw(amount, when: nil)
|
67
|
+
# TODO: return Float
|
68
|
+
end
|
69
|
+
|
70
|
+
# must filter results by specifying `to` and `from` params
|
71
|
+
def transactions(to:, from:)
|
72
|
+
# TODO: return Array
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
2. Fill in your business logic!
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
module Bank
|
82
|
+
class Account
|
83
|
+
attr_reader :interest_rate
|
84
|
+
attr_reader :owner
|
85
|
+
attr_reader :balance
|
86
|
+
|
87
|
+
def initialize(owner, interest_rate, balance)
|
88
|
+
@owner = owner
|
89
|
+
@interest_rate = interest_rate
|
90
|
+
@balance = balance
|
91
|
+
end
|
92
|
+
|
93
|
+
# can optionally specify when the transaction should take place
|
94
|
+
def deposit(amount, when: nil)
|
95
|
+
@balance += amount
|
96
|
+
end
|
97
|
+
|
98
|
+
# can optionally specify when the transaction should take place
|
99
|
+
def withdraw(amount, when: nil)
|
100
|
+
@balance -= amount
|
101
|
+
end
|
102
|
+
|
103
|
+
# must filter results by specifying `to` and `from` params
|
104
|
+
def transactions(to:, from:)
|
105
|
+
Transaction.where("created_at BETWEEN ? AND ?", from, to)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
From here, you now have some ruby code which you can [type check](https://github.com/soutaro/steep)!
|
112
|
+
|
113
|
+
## Installation
|
114
|
+
|
115
|
+
Add this line to your application's Gemfile:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
gem 'cutter'
|
119
|
+
```
|
120
|
+
|
121
|
+
And then execute:
|
122
|
+
|
123
|
+
$ bundle install
|
124
|
+
|
125
|
+
Or install it yourself as:
|
126
|
+
|
127
|
+
$ gem install cutter
|
128
|
+
|
129
|
+
## Configuration
|
130
|
+
|
131
|
+
You can specify preferences in a `.lap.yml` file in your project directory. Example
|
132
|
+
|
133
|
+
```yml
|
134
|
+
indent: 4 # default is 2
|
135
|
+
# this is it for now, more to come
|
136
|
+
```
|
137
|
+
|
138
|
+
## Coverage
|
139
|
+
|
140
|
+
Currently not every feature of RBS is supported - yet! (contributions
|
141
|
+
welcome!)
|
142
|
+
|
143
|
+
Feature|Coverage
|
144
|
+
---|---
|
145
|
+
classes (includes nested)|✅
|
146
|
+
modules (includes nested)|✅
|
147
|
+
class methods|✅
|
148
|
+
instance methods|✅
|
149
|
+
required positional arguments|✅
|
150
|
+
optional positional arguments|✅
|
151
|
+
required keyword arguments|✅
|
152
|
+
optional keyword arguments|✅
|
153
|
+
method comments|✅
|
154
|
+
access modifiers|✅
|
155
|
+
attr_reader|✅
|
156
|
+
attr_writer|✅
|
157
|
+
attr_accessor|✅
|
158
|
+
include|✅
|
159
|
+
extend|✅
|
160
|
+
methods with blocks|✅
|
161
|
+
method overloading|⚠️
|
162
|
+
procs|⚠️
|
163
|
+
constants|⚠️
|
164
|
+
interfaces|⚠️
|
165
|
+
|
166
|
+
### Experimental:
|
167
|
+
|
168
|
+
There is an experimental feature which allows you to specify some ruby logic *within* your rbs files
|
169
|
+
for your methods. You specify it between `@!begin` and `@!end`, eg.:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
# take some moeny out of the customers account
|
173
|
+
# @!begin
|
174
|
+
# @balance -= amount
|
175
|
+
# @!end
|
176
|
+
def withdraw(Float amount) -> Float
|
177
|
+
```
|
178
|
+
This would produce:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
# take some moeny out of the customers account
|
182
|
+
def withdraw(amount)
|
183
|
+
@balance -= amount
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
## Development
|
188
|
+
|
189
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
190
|
+
|
191
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
192
|
+
|
193
|
+
## Contributing
|
194
|
+
|
195
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/lap.
|
196
|
+
|
197
|
+
|
198
|
+
## License
|
199
|
+
|
200
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cutter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/lap
ADDED
data/lap.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'lib/lap/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "lap"
|
5
|
+
spec.version = Lap::VERSION
|
6
|
+
spec.authors = ["Joseph Johansen"]
|
7
|
+
spec.email = ["joe@stotles.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Generate ruby from rbs blueprints}
|
10
|
+
spec.description = "Don't write your code, and rbs types! Write the rbs types first, then generate a template to fill in with business logic."
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
13
|
+
spec.homepage = "https://github.com/johansenja/lap"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.add_runtime_dependency "rbs"
|
24
|
+
spec.add_development_dependency "pry-byebug"
|
25
|
+
end
|
data/lib/lap.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rbs"
|
4
|
+
require "pathname"
|
5
|
+
require "lap/version"
|
6
|
+
require "lap/helpers"
|
7
|
+
require "lap/class"
|
8
|
+
require "lap/method"
|
9
|
+
require "lap/module"
|
10
|
+
require "pry-byebug"
|
11
|
+
require "core_ext/string"
|
12
|
+
|
13
|
+
module Lap
|
14
|
+
CONFIG_FILENAME = ".lap.yml"
|
15
|
+
DEFAULT_CONFIG = {
|
16
|
+
indent: 2
|
17
|
+
}
|
18
|
+
fp = File.join(File.dirname(__FILE__), "..", CONFIG_FILENAME)
|
19
|
+
yml_config = Pathname(fp).exist? ? YAML.safe_load(File.read(fp)) : {}
|
20
|
+
Config = DEFAULT_CONFIG.merge(yml_config.transform_keys(&:to_sym))
|
21
|
+
|
22
|
+
class Output
|
23
|
+
def initialize(pathname)
|
24
|
+
loader = RBS::EnvironmentLoader.new(core_root: nil) # don't pollute the env with ruby stdlib
|
25
|
+
loader.add(path: Pathname(pathname))
|
26
|
+
@env = RBS::Environment.from_loader(loader).resolve_type_names
|
27
|
+
end
|
28
|
+
|
29
|
+
def render
|
30
|
+
output = @env.declarations.map do |decl|
|
31
|
+
case decl
|
32
|
+
when RBS::AST::Declarations::Class
|
33
|
+
Lap::Class.new(decl).render
|
34
|
+
when RBS::AST::Declarations::Module
|
35
|
+
Lap::Module.new(decl).render
|
36
|
+
else
|
37
|
+
"TODO: #{decl}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
puts output.join("\n")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/lap/class.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lap
|
4
|
+
class Class
|
5
|
+
include Helpers
|
6
|
+
|
7
|
+
def initialize(node, indent_level = 0)
|
8
|
+
@node = node
|
9
|
+
@indent_level = indent_level
|
10
|
+
@has_contents = !@node.members.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def render
|
14
|
+
superclass = @node.super_class ? " < #{@node.super_class.name.name}" : ""
|
15
|
+
self_indent = " " * (Lap::Config[:indent] * @indent_level)
|
16
|
+
comment = get_comment(@node)
|
17
|
+
"#{comment}#{self_indent}class #{@node.name.name}#{superclass}#{contents}#{self_indent if @has_contents}end\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def contents
|
23
|
+
@contents ||= begin
|
24
|
+
if @has_contents
|
25
|
+
member_indent = Lap::Config[:indent] * (@indent_level + 1)
|
26
|
+
members = @node.members.map do |m|
|
27
|
+
case m
|
28
|
+
when RBS::AST::Members::MethodDefinition
|
29
|
+
Lap::Method.new(m, @indent_level + 1).render
|
30
|
+
when RBS::AST::Declarations::Class
|
31
|
+
self.class.new(m, @indent_level + 1).render
|
32
|
+
when RBS::AST::Declarations::Module
|
33
|
+
Lap::Module.new(m, @indent_level + 1).render
|
34
|
+
when RBS::AST::Members::AttrReader
|
35
|
+
with_comment(m, "attr_reader :#{m.name}").indent(member_indent)
|
36
|
+
when RBS::AST::Members::AttrWriter
|
37
|
+
with_comment(m, "attr_writer :#{m.name}").indent(member_indent)
|
38
|
+
when RBS::AST::Members::AttrAccessor
|
39
|
+
with_comment(m, "attr_accessor :#{m.name}").indent(member_indent)
|
40
|
+
when RBS::AST::Members::Public
|
41
|
+
"public\n".indent(member_indent)
|
42
|
+
when RBS::AST::Members::Private
|
43
|
+
"private\n".indent(member_indent)
|
44
|
+
when RBS::AST::Members::Include
|
45
|
+
with_comment(m, "include #{m.name}").indent(member_indent)
|
46
|
+
when RBS::AST::Members::Extend
|
47
|
+
with_comment(m, "extend #{m.name}").indent(member_indent)
|
48
|
+
when RBS::AST::Declarations::Alias
|
49
|
+
# no-op: not present in ruby
|
50
|
+
else
|
51
|
+
warn "Unsupported member for classes: #{m}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
"\n#{members.compact.join("\n")}"
|
55
|
+
else
|
56
|
+
"; "
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/lap/helpers.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Lap
|
2
|
+
module Helpers
|
3
|
+
CLASS_TO_LITERAL = {
|
4
|
+
String: '""',
|
5
|
+
Array: '[]',
|
6
|
+
Hash: "{}",
|
7
|
+
}
|
8
|
+
|
9
|
+
def args(rp, rkw, op, okw)
|
10
|
+
if [rp, rkw, op, okw].any? { |arg| arg.length.positive? }
|
11
|
+
contents = [
|
12
|
+
rp.map { |pos| pos.name || pos.type.name.name.downcase },
|
13
|
+
op.map { |pos| "#{pos.name || pos.type.name.name.downcase} = #{CLASS_TO_LITERAL[pos.type.name.name]}" },
|
14
|
+
rkw.map { |name, _| "#{name}:" },
|
15
|
+
okw.map { |name, t| "#{name}: #{CLASS_TO_LITERAL[t.type.name.name]}" },
|
16
|
+
].reject(&:empty?).flatten.join(", ")
|
17
|
+
"(#{contents})"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_comment(node, output)
|
22
|
+
if (comment = node.comment&.string)
|
23
|
+
"#{comment.lines.map { |l| "# #{l}" }.join}#{output}"
|
24
|
+
else
|
25
|
+
output
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_comment(node)
|
30
|
+
return nil unless node.comment
|
31
|
+
|
32
|
+
lines = node.comment
|
33
|
+
.string
|
34
|
+
.lines
|
35
|
+
comment = []
|
36
|
+
lines.each do |l|
|
37
|
+
break if l.start_with? "@!begin"
|
38
|
+
|
39
|
+
comment << "# #{l}"
|
40
|
+
end
|
41
|
+
|
42
|
+
comment.join
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/lap/method.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lap
|
4
|
+
class Method
|
5
|
+
include Helpers
|
6
|
+
|
7
|
+
def initialize(node, indent_level = 0)
|
8
|
+
@node = node
|
9
|
+
@indent_level = indent_level
|
10
|
+
end
|
11
|
+
|
12
|
+
def render
|
13
|
+
comment = get_comment(@node)
|
14
|
+
result = <<~METHOD
|
15
|
+
#{comment}def #{"self." unless @node.kind == :instance}#{@node.name}#{arguments}
|
16
|
+
#{body}
|
17
|
+
end
|
18
|
+
METHOD
|
19
|
+
result.indent(Lap::Config[:indent] * @indent_level)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def return_type(t)
|
25
|
+
case t
|
26
|
+
when RBS::Types::Literal
|
27
|
+
t
|
28
|
+
when ->(t) { t.class.to_s.start_with? "RBS::Types::Bases" }
|
29
|
+
"# returns #{t}"
|
30
|
+
else
|
31
|
+
"# TODO: return #{t.name.name}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def arguments
|
36
|
+
@arguments ||= begin
|
37
|
+
type = @node.types.first.type
|
38
|
+
rp = type.required_positionals
|
39
|
+
rkw = type.required_keywords
|
40
|
+
op = type.optional_positionals
|
41
|
+
okw = type.optional_keywords
|
42
|
+
args(rp, rkw, op, okw)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def body
|
47
|
+
@body ||= begin
|
48
|
+
comment = @node.comment
|
49
|
+
logic = ""
|
50
|
+
if comment
|
51
|
+
real_comment = []
|
52
|
+
logic = []
|
53
|
+
has_logic = false
|
54
|
+
comment.string.lines.each do |line|
|
55
|
+
if has_logic
|
56
|
+
break if line.lstrip.start_with? "@!end"
|
57
|
+
|
58
|
+
logic << line
|
59
|
+
elsif line.lstrip.start_with? "@!begin"
|
60
|
+
has_logic = true
|
61
|
+
else
|
62
|
+
real_comment << line
|
63
|
+
end
|
64
|
+
end
|
65
|
+
logic = logic.join
|
66
|
+
comment = real_comment.join
|
67
|
+
end
|
68
|
+
if logic.length.positive?
|
69
|
+
logic
|
70
|
+
else
|
71
|
+
block = @node.types.first.block
|
72
|
+
return_type = return_type(@node.types.first.type.return_type)
|
73
|
+
yld = if block
|
74
|
+
bt = block.type
|
75
|
+
a = args(
|
76
|
+
bt.required_positionals,
|
77
|
+
bt.required_keywords,
|
78
|
+
bt.optional_positionals,
|
79
|
+
bt.optional_keywords
|
80
|
+
)
|
81
|
+
"yield#{a}\n"
|
82
|
+
end
|
83
|
+
"#{yld}#{return_type}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/lap/module.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lap
|
4
|
+
class Module
|
5
|
+
include Helpers
|
6
|
+
|
7
|
+
def initialize(node, indent_level = 0)
|
8
|
+
@node = node
|
9
|
+
@indent_level = indent_level
|
10
|
+
@has_contents = !@node.members.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def render
|
14
|
+
self_indent = " " * (Lap::Config[:indent] * @indent_level)
|
15
|
+
comment = get_comment(@node)
|
16
|
+
"#{comment}#{self_indent}module #{@node.name.name}#{contents}#{self_indent if @has_contents}end\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def contents
|
22
|
+
@contents ||= begin
|
23
|
+
if @has_contents
|
24
|
+
members = @node.members.map do |m|
|
25
|
+
case m
|
26
|
+
when RBS::AST::Members::MethodDefinition
|
27
|
+
Lap::Method.new(m, @indent_level + 1).render
|
28
|
+
when RBS::AST::Declarations::Class
|
29
|
+
Lap::Class.new(m, @indent_level + 1).render
|
30
|
+
when RBS::AST::Declarations::Module
|
31
|
+
self.class.new(m, @indent_level + 1).render
|
32
|
+
when RBS::AST::Declarations::Alias
|
33
|
+
# no-op: not present in ruby
|
34
|
+
else
|
35
|
+
warn "Unsupported member for modules: #{m}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
"\n#{members.join("\n")}"
|
39
|
+
else
|
40
|
+
"; "
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/lap/version.rb
ADDED
data/sample/a.rbs
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class A < String
|
2
|
+
def self.a: () -> untyped
|
3
|
+
|
4
|
+
def self.b: (String arg1) -> Integer
|
5
|
+
|
6
|
+
attr_reader foo: String
|
7
|
+
|
8
|
+
include Kernel
|
9
|
+
|
10
|
+
def length: () -> Integer
|
11
|
+
|
12
|
+
private def c: (?arg: Array) -> void
|
13
|
+
|
14
|
+
class D
|
15
|
+
end
|
16
|
+
|
17
|
+
module E
|
18
|
+
def f: (?Hash options) -> bool
|
19
|
+
end
|
20
|
+
end
|
data/sample/bank.rbs
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# The top level module for my feature/app
|
2
|
+
module Bank
|
3
|
+
# This class is responsible for representing Accounts within the Bank
|
4
|
+
class Account
|
5
|
+
# to 2 decimal places
|
6
|
+
attr_reader interest_rate: Float
|
7
|
+
# each account has one customer
|
8
|
+
attr_reader owner: Bank::Customer
|
9
|
+
# how much money in the account
|
10
|
+
attr_reader balance: Float
|
11
|
+
|
12
|
+
# each account needs logic to handle different currencies
|
13
|
+
include Currency
|
14
|
+
# Query methods to be able to look up different accounts
|
15
|
+
extend Query
|
16
|
+
|
17
|
+
type date_or_dt = Date | DateTime
|
18
|
+
|
19
|
+
# can optionally specify when the transaction should take place
|
20
|
+
def deposit: (Float amount, ?when: date_or_dt) -> Float
|
21
|
+
|
22
|
+
# can optionally specify when the transaction should take place
|
23
|
+
# @!begin
|
24
|
+
# @balance += amount
|
25
|
+
# @!end
|
26
|
+
def withdraw: (Float amount, ?when: date_or_dt) -> Float
|
27
|
+
|
28
|
+
# must filter results by specifying `to` and `from` params,
|
29
|
+
# to prevent the whole transaction history from being loaded!
|
30
|
+
def transactions: (from: date_or_dt, to: date_or_dt) -> Array[Bank::Transaction]
|
31
|
+
|
32
|
+
def with_block: () {(String arg1) -> String } -> void
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
# TODO:
|
37
|
+
# this can probably live elsewhere in due course
|
38
|
+
def send_confirmation_email: (String subject) -> void
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseph Johansen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rbs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Don't write your code, and rbs types! Write the rbs types first, then
|
42
|
+
generate a template to fill in with business logic.
|
43
|
+
email:
|
44
|
+
- joe@stotles.com
|
45
|
+
executables:
|
46
|
+
- lap
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".lap.yml"
|
52
|
+
- ".rspec"
|
53
|
+
- ".travis.yml"
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/console
|
60
|
+
- bin/setup
|
61
|
+
- exe/lap
|
62
|
+
- lap.gemspec
|
63
|
+
- lib/core_ext/string.rb
|
64
|
+
- lib/lap.rb
|
65
|
+
- lib/lap/class.rb
|
66
|
+
- lib/lap/helpers.rb
|
67
|
+
- lib/lap/method.rb
|
68
|
+
- lib/lap/module.rb
|
69
|
+
- lib/lap/version.rb
|
70
|
+
- sample/a.rbs
|
71
|
+
- sample/bank.rbs
|
72
|
+
homepage: https://github.com/johansenja/lap
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 2.7.0
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.1.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Generate ruby from rbs blueprints
|
95
|
+
test_files: []
|