Skip to content

Commit

Permalink
Merge pull request #6 from Uaitt/fix/constant-loading-issues
Browse files Browse the repository at this point in the history
Feat:constant loading improvements
  • Loading branch information
Uaitt authored Aug 3, 2023
2 parents 21e581d + 844e324 commit 631ceef
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/type_checking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bin/steep check
- run: bin/rbs collection install && bin/steep check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.rubocop-*
/.gem_rbs_collection
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ gemspec

group :development, :test do
gem 'activerecord'
gem 'rake'
gem 'rbs'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-performance'
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
aasm_rbs (0.1.0)
aasm_rbs (0.2.0)
aasm (~> 5)

GEM
Expand Down Expand Up @@ -40,6 +40,7 @@ GEM
racc
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -114,6 +115,8 @@ PLATFORMS
DEPENDENCIES
aasm_rbs!
activerecord
rake
rbs
rspec
rubocop
rubocop-performance
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# AASM RBS Generator

[![Gem Version](https://badge.fury.io/rb/aasm_rbs.svg)](https://badge.fury.io/rb/aasm_rbs) [![Gem Downloads](https://badgen.net/rubygems/dt/aasm_rbs)](https://rubygems.org/gems/aasm_rbs) [![Linters](/~https://github.com/Uaitt/aasm_rbs/actions/workflows/linters.yml/badge.svg)](/~https://github.com/Uaitt/aasm_rbs/actions/workflows/linters.yml) [![Specs](/~https://github.com/Uaitt/aasm_rbs/actions/workflows/specs.yml/badge.svg)](/~https://github.com/Uaitt/aasm_rbs/actions/workflows/specs.yml)

Easily generate RBS signatures for all the AASM automatically generated methods and constants of your ruby classes.

## Description
Expand Down Expand Up @@ -26,11 +29,30 @@ gem 'aasm_rbs'
Then, execute `bundle install` in order to load the gem's code.

## Usage
At the moment AASM RBS only supports pure-ruby projects or Rails applications.

This gem assumes that your project is arranged with a traditional structure:
- If dealing with a Rails app, your classes should be in any folder nested inside of `app/` or `lib/`
- If dealing with a Ruby project, your actual classes should go inside of `lib/` and arranged as:
```
lib/
├── foo/
│ ├── bar.rb # contains Foo::Bar
│ ├── baz.rb # contains Foo::Baz
│── foo.rb # contains Foo
```

For more information about how to structure your projects, take a look at the following articles:
- [Autoloading and reloading constants](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) from the Rails guides
- [Exploring the structure of a Ruby gem](https://www.cloudbees.com/blog/exploring-structure-ruby-gems) fantastic article from cloudbees (a little bit old but still relevant)

Generating the RBS signatures is as easy as launching the following command from the command-line:
```
bundle exec aasm_rbs ClassName
bundle exec aasm_rbs Namespace::ClassName
```

If your class is namespaced inside of other modules/classes, please pass the whole name as you see in the previous command to AASM RBS or it won't be able to infer the path.

The generated signatures will appear in `stdout`.


Expand Down
27 changes: 27 additions & 0 deletions bin/rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rbs' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rbs", "rbs")
6 changes: 6 additions & 0 deletions exe/aasm_rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/usr/bin/env ruby

unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
abort 'Please run aasm_rbs from the project root.'
end

require_relative '../lib/aasm_rbs'

AasmRbs.load_constants(ARGV[0] || '')

$stdout.puts ''
$stdout.puts AasmRbs.run(ARGV[0] || '')
$stdout.puts ''
18 changes: 18 additions & 0 deletions lib/aasm_rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
require_relative 'aasm_rbs/output'

module AasmRbs
def self.load_constants(klass_name)
load './Rakefile' if File.exist?('./Rakefile')
begin
Rake::Task[:environment].invoke
rescue StandardError
nil
end
return if defined?(Rails)

# not in a Rails project, load the file manually
file = Dir.pwd + "/lib/#{klass_name.split('::').join('/').downcase}"
begin
require file
rescue LoadError
abort 'There was a problem loading the class file'
end
end

def self.run(klass_name)
klass = Object.const_get(klass_name)
states = klass.aasm.states.map(&:name)
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm_rbs/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AasmRbs
VERSION = '0.1.0'
VERSION = '0.2.0'
end
110 changes: 110 additions & 0 deletions rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
sources:
- type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
path: ".gem_rbs_collection"
gems:
- name: activemodel
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activerecord
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activesupport
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: ast
version: '2.4'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: concurrent-ruby
version: '1.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: date
version: '0'
source:
type: stdlib
- name: i18n
version: '1.10'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: json
version: '0'
source:
type: stdlib
- name: logger
version: '0'
source:
type: stdlib
- name: minitest
version: '0'
source:
type: stdlib
- name: monitor
version: '0'
source:
type: stdlib
- name: mutex_m
version: '0'
source:
type: stdlib
- name: parallel
version: '1.20'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rainbow
version: '3.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: d114ffa80f35b138aceb7660a224d78afa1b2a63
remote: /~https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: securerandom
version: '0'
source:
type: stdlib
- name: singleton
version: '0'
source:
type: stdlib
- name: time
version: '0'
source:
type: stdlib
gemfile_lock_path: Gemfile.lock
15 changes: 15 additions & 0 deletions rbs_collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Download sources
sources:
- type: git
name: ruby/gem_rbs_collection
remote: /~https://github.com/ruby/gem_rbs_collection.git
revision: main
repo_dir: gems

path: .gem_rbs_collection

gems:
- name: rbs
ignore: true
- name: steep
ignore: true
3 changes: 3 additions & 0 deletions sig/aasm_rbs.rbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module AasmRbs
Rake: untyped # there is yet no Rake official RBS

def self.load_constants: (String) -> void
def self.run: (String) -> String?
end

Expand Down

0 comments on commit 631ceef

Please sign in to comment.