trains 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5413c3e6b18ce996f955e2ad43eaddb7fc13d67ead0eab3ed79fdc09820c7fc9
4
+ data.tar.gz: 1c8f46daedfc9115f35baa7ab70d8c987145e67e2f02c51906cd9a1af012c2be
5
+ SHA512:
6
+ metadata.gz: 0cdd205741e22ff8cacf3f4bf590faac07cba5eb6d3dae8126da6f13cefbb65ce556734e3484f70183777c3b9dea617e3fe0c2cc953bc1b380a975ba78dbf983
7
+ data.tar.gz: 31c57301eb2fa078d067f7685380ca981d2cf16c724d54fc0deed63e52836f1fbc66a3813b11756d243203133bc33c9eb33052c77329732fa5ea00df3f8d797f
data/.deepsource.toml ADDED
@@ -0,0 +1,7 @@
1
+ version = 1
2
+
3
+ [[analyzers]]
4
+ name = "ruby"
5
+
6
+ [[transformers]]
7
+ name = "rubocop"
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1.0
3
+
4
+ Layout/LineLength:
5
+ Max: 80
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-19
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at faraaz98@live.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '~> 7.0'
4
+ gem 'parallel', '~> 1.22'
5
+ gem 'rubocop-ast'
6
+ gem 'zeitwerk', '~> 2.5'
7
+
8
+ group :test, :development do
9
+ gem 'rspec', '~> 3.12'
10
+ gem 'ruby-lsp', '~> 0.3.7', require: false
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (7.0.2.3)
5
+ concurrent-ruby (~> 1.0, >= 1.0.2)
6
+ i18n (>= 1.6, < 2)
7
+ minitest (>= 5.1)
8
+ tzinfo (~> 2.0)
9
+ ast (2.4.2)
10
+ concurrent-ruby (1.1.10)
11
+ diff-lcs (1.5.0)
12
+ i18n (1.10.0)
13
+ concurrent-ruby (~> 1.0)
14
+ language_server-protocol (3.17.0.2)
15
+ minitest (5.15.0)
16
+ parallel (1.22.1)
17
+ parser (3.1.1.0)
18
+ ast (~> 2.4.1)
19
+ prettier_print (1.2.0)
20
+ rspec (3.12.0)
21
+ rspec-core (~> 3.12.0)
22
+ rspec-expectations (~> 3.12.0)
23
+ rspec-mocks (~> 3.12.0)
24
+ rspec-core (3.12.0)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-expectations (3.12.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-mocks (3.12.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-support (3.12.0)
33
+ rubocop-ast (1.16.0)
34
+ parser (>= 3.1.1.0)
35
+ ruby-lsp (0.3.8)
36
+ language_server-protocol (~> 3.17.0)
37
+ sorbet-runtime
38
+ syntax_tree (>= 5.0.0, < 6)
39
+ sorbet-runtime (0.5.10634)
40
+ syntax_tree (5.3.0)
41
+ prettier_print (>= 1.2.0)
42
+ tzinfo (2.0.4)
43
+ concurrent-ruby (~> 1.0)
44
+ zeitwerk (2.5.4)
45
+
46
+ PLATFORMS
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ activesupport (~> 7.0)
51
+ parallel (~> 1.22)
52
+ rspec (~> 3.12)
53
+ rubocop-ast
54
+ ruby-lsp (~> 0.3.7)
55
+ zeitwerk (~> 2.5)
56
+
57
+ BUNDLED WITH
58
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Syed Faraaz Ahmad
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,102 @@
1
+ # Trains
2
+
3
+ A gem that statically analyses your Rails app and extracts information about its structure.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add it to the application's Gemfile by executing:
8
+
9
+ $ bundle add trains
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install trains
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'trains'
19
+
20
+ scanner = Trains::Scanner.new('~/lib/trains_app')
21
+ result = scanner.scan
22
+ ```
23
+
24
+ ## Features
25
+
26
+ Trains currently has the ability to achieve the following:
27
+
28
+ ### Create Model definitions from migrations
29
+
30
+ Given a DB migration in your Rails app like so:
31
+
32
+ ```ruby
33
+ class CreateGroups < ActiveRecord::Migration[7.0]
34
+ def change
35
+ create_table :groups do |t|
36
+ t.string :title
37
+
38
+ t.timestamps
39
+ end
40
+ add_index :groups, :title, unique: true
41
+ end
42
+ end
43
+ ```
44
+
45
+ Trains will generate the following Model definition:
46
+
47
+ ```ruby
48
+ Trains::DTO::Model(
49
+ name: 'Group',
50
+ fields:
51
+ Set[
52
+ Trains::DTO::Field(:datetime, :created_at),
53
+ Trains::DTO::Field(:datetime, :updated_at),
54
+ Trains::DTO::Field(:string, :title)
55
+ ],
56
+ version: 7.0
57
+ )
58
+ ```
59
+
60
+ ### Create Controller definitions from files
61
+
62
+ Given a controller in your Rails app like so:
63
+
64
+ ```ruby
65
+ class BoxController < ApplicationController
66
+ def create; end
67
+
68
+ def edit; end
69
+
70
+ def update; end
71
+
72
+ def destroy; end
73
+ end
74
+ ```
75
+
76
+ Trains will return the following controller definition:
77
+
78
+ ```ruby
79
+ Trains::DTO::Controller(
80
+ name: 'BoxController',
81
+ methods: Set[:create, :edit, :update, :destroy]
82
+ )
83
+ ```
84
+
85
+ ## Development
86
+
87
+ 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.
88
+
89
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/trains. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/trains/blob/master/CODE_OF_CONDUCT.md).
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
98
+
99
+ ## Code of Conduct
100
+
101
+ Everyone interacting in the Trains project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/trains/blob/master/CODE_OF_CONDUCT.md).
102
+
@@ -0,0 +1,13 @@
1
+ module Trains
2
+ module DTO
3
+ App = Struct.new(
4
+ 'App',
5
+ :name,
6
+ :controllers,
7
+ :models,
8
+ :migrations,
9
+ :helpers,
10
+ keyword_init: true
11
+ )
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'set'
2
+
3
+ module Trains
4
+ module DTO
5
+ Controller = Struct.new('Controller', :name, :method_list, keyword_init: true)
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Trains
2
+ module DTO
3
+ Field = Struct.new(:name, :type)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Trains
2
+ module DTO
3
+ Method = Struct.new('Method', :name, :source, :visibility, keyword_init: true)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Trains
2
+ module DTO
3
+ Model = Struct.new(:name, :fields, :version)
4
+ end
5
+ end
@@ -0,0 +1,139 @@
1
+ module Trains
2
+ # Scanner parses all the code and stores metadata about the repo
3
+ class Scanner
4
+ include Utils
5
+
6
+ attr_accessor :models, :controllers, :helpers, :migrations
7
+
8
+ def initialize(folder)
9
+ @root_folder = folder
10
+ @nodes = []
11
+ @models = []
12
+ @controllers = []
13
+ @helpers = []
14
+ @dir = Dir.new(File.expand_path(folder))
15
+ Dir.chdir @dir
16
+ end
17
+
18
+ def scan
19
+ # @nodes = get_tree(@dir)
20
+
21
+ # Check if @folder is a Rails directory before beginning analysis
22
+ rails_dir_result = RailsDir.check @dir
23
+ case rails_dir_result
24
+ in Result[data: true, error: nil]
25
+ # Do nothing
26
+ else
27
+ return(
28
+ Result.new(
29
+ data: nil,
30
+ error: ArgumentError.new('Not a Rails directory')
31
+ )
32
+ )
33
+ end
34
+
35
+ @migrations = Set[*get_migrations]
36
+ @controllers = Set[*get_controllers]
37
+ # @helpers = get_helpers
38
+ # @models = get_models
39
+
40
+ # Create instance of Trains::DTO::App
41
+ DTO::App.new(
42
+ name: nil,
43
+ controllers: @controllers,
44
+ models: @models,
45
+ migrations: @migrations,
46
+ helpers: @helpers
47
+ )
48
+ end
49
+
50
+ # Build central ASTStore
51
+ def build_ast_store
52
+ all_files = Dir.glob(File.join(@root_folder, '**', '*.rb'))
53
+ Parallel.each(all_files) do |file_path|
54
+ ASTStore.instance.set(
55
+ file_path,
56
+ RuboCop::AST::ProcessedSource.from_file(file_path, RUBY_VERSION.to_f)
57
+ )
58
+ end
59
+ end
60
+
61
+ def get_models; end
62
+
63
+ def get_helpers
64
+ app_folder = @nodes[:children].find { |node| node[:path].include? 'app' }
65
+ helpers_folder =
66
+ app_folder[:children].find { |node| node[:path].include? 'app/helpers' }
67
+ helpers =
68
+ helpers_folder[:children].filter do |node|
69
+ node[:path].end_with? '_helper.rb'
70
+ end
71
+
72
+ @helpers = parse_util(helpers, Visitor::Helper)
73
+ end
74
+
75
+ def get_gemfile; end
76
+
77
+ def get_controllers
78
+ controllers =
79
+ Dir.glob(File.join(@dir, 'app', 'controllers', '**', '*_controller.rb'))
80
+
81
+ parse_util(controllers, Visitor::Controller)
82
+ end
83
+
84
+ def get_migrations
85
+ migrations = Dir.glob(File.join(@dir, 'db', 'migrate', '**', '*.rb'))
86
+
87
+ parse_util(migrations, Visitor::Migration)
88
+ end
89
+
90
+ def parse_util(file_nodes, klass)
91
+ unless file_nodes.class.include? Enumerable
92
+ return(
93
+ Result.new(
94
+ data: nil,
95
+ error:
96
+ TypeError.new(
97
+ "Object of type #{file_nodes.class} is not iterable"
98
+ )
99
+ )
100
+ )
101
+ end
102
+
103
+ begin
104
+ Parallel.map(file_nodes) do |node|
105
+ processed_source =
106
+ RuboCop::AST::ProcessedSource.from_file(node, RUBY_VERSION.to_f)
107
+ visitor = klass.new
108
+ visitor.process(processed_source.ast)
109
+ visitor.result
110
+ end
111
+ rescue StandardError => e
112
+ puts e.backtrace
113
+ Result.new(data: nil, error: e)
114
+ end
115
+ end
116
+
117
+ def get_tree(node, prefix = '')
118
+ path = File.join(prefix, node)
119
+ obj = { path: nil }
120
+
121
+ # puts "DEBUG: #{path} #{ FastIgnore.new.allowed? path }"
122
+ if path != @dir.to_path &&
123
+ FastIgnore.new.allowed?(path, directory: false) == false
124
+ return nil
125
+ end
126
+
127
+ if Dir.exist? path
128
+ children = []
129
+ Dir.each_child path do |child|
130
+ child_node = get_tree(child, path)
131
+ children.append(child_node) unless child_node.nil?
132
+ end
133
+ obj[:children] = children
134
+ end
135
+
136
+ obj
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,25 @@
1
+ require 'singleton'
2
+
3
+ module Trains
4
+ module Utils
5
+ class ASTStore
6
+ include Singleton
7
+
8
+ def initialize
9
+ @store = {}
10
+ end
11
+
12
+ def set(file_path, ast_object)
13
+ @store[file_path] = ast_object
14
+ end
15
+
16
+ def get(file_path)
17
+ unless @store.key? file_path
18
+ set(file_path,
19
+ RuboCop::AST::ProcessedSource.from_file(file_path, RUBY_VERSION.to_f))
20
+ end
21
+ @store[file_path]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Logger
2
+ def debug(log)
3
+ puts '[DEBUG]:'.bold.blue
4
+ pp log
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Trains
2
+ module Utils
3
+ class RailsDir
4
+ # checks if supplied dir is in a Rails app dir
5
+ def self.check(root_path)
6
+ rails_bin = File.join(root_path, 'bin', 'rails')
7
+
8
+ Result.new(File.exist?(rails_bin), nil)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Trains
2
+ module Utils
3
+ Result = Struct.new('Result', :data, :error)
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubocop-ast'
2
+ require 'parser'
3
+
4
+ module Trains
5
+ module Visitor
6
+ class Base < Parser::AST::Processor
7
+ include RuboCop::AST::Traversal
8
+ extend RuboCop::AST::NodePattern::Macros
9
+
10
+ def result
11
+ raise NotImplementedError
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../dto/controller'
2
+ require_relative '../dto/method'
3
+
4
+ module Trains
5
+ module Visitor
6
+ # Visitor that parses controllers and returns a DTO::Controller object
7
+ class Controller < Base
8
+ def initialize
9
+ @method_list = []
10
+ @methods = {}
11
+ @class_name = nil
12
+ end
13
+
14
+ def on_class(node)
15
+ class_name = node.identifier.const_name
16
+ parent_class = node.parent_class.const_name.to_sym
17
+
18
+ is_controller =
19
+ if parent_class.nil?
20
+ true if class_name == :ApplicationController
21
+ else
22
+ parent_class == :"ActionController::Base"
23
+ end
24
+ return unless is_controller
25
+
26
+ @class_name = class_name
27
+ end
28
+
29
+ # List out all controller methods
30
+ def on_def(node)
31
+ method_name = node.method_name
32
+ @method_list.append(
33
+ DTO::Method.new(name: method_name.to_s, visibility: nil, source: nil)
34
+ )
35
+ end
36
+
37
+ def result
38
+ DTO::Controller.new(name: @class_name, method_list: @method_list.uniq)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ module Trains
2
+ module Visitor
3
+ class Helper < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,79 @@
1
+ require 'yaml'
2
+
3
+ module Trains
4
+ module Visitor
5
+ # Visitor that parses DB migration and associates them with Rails models
6
+ class Migration < Base
7
+ def_node_matcher :send_node?, '(send nil? ...)'
8
+ attr_reader :is_migration, :model
9
+
10
+ def initialize
11
+ @model = nil
12
+ @table_name = nil
13
+ @is_class = false
14
+ @is_migration = false
15
+ @class_name = nil
16
+ @fields = []
17
+
18
+ @scope = { class: nil, method: nil, send: nil }
19
+ end
20
+
21
+ def on_class(node)
22
+ return unless node.parent_class.source.include? 'ActiveRecord::Migration'
23
+
24
+ @migration_class = node.children.first.source
25
+ @migration_version = extract_version(node.parent_class.source)
26
+
27
+ process_node(node.body)
28
+ end
29
+
30
+ def extract_version(class_const)
31
+ match = class_const.match(/\d+.\d+/)
32
+ return nil if match.nil?
33
+
34
+ match.to_s.to_f
35
+ end
36
+
37
+ def process_node(node)
38
+ process_def_node(node) if node.def_type?
39
+ end
40
+
41
+ def process_def_node(node)
42
+ allowed_method_names = %i[change up down]
43
+ allowed_table_modifiers = %i[create_table update_column]
44
+
45
+ method_name = node.method_name
46
+ return unless allowed_method_names.include? method_name
47
+
48
+ table_modifier = node.body.children[0].method_name
49
+ return unless allowed_table_modifiers.include? table_modifier
50
+
51
+ raw_table_name = node.body.children[0].children[0].children[2].value.to_s
52
+ @table_name = raw_table_name.singularize.camelize
53
+
54
+ node.body.children[0].children[2].each_child_node do |child|
55
+ process_migration_field(child)
56
+ end
57
+ end
58
+
59
+ def process_migration_field(node)
60
+ return unless node.send_type?
61
+
62
+ if node.children.count < 3
63
+ if node.children[1] == :timestamps
64
+ @fields.append(DTO::Field.new(:created_at, :datetime))
65
+ @fields.append(DTO::Field.new(:updated_at, :datetime))
66
+ end
67
+ elsif node.children.count >= 3
68
+ type = node.children[1]
69
+ value = node.children[2].value
70
+ @fields.append(DTO::Field.new(value, type))
71
+ end
72
+ end
73
+
74
+ def result
75
+ DTO::Model.new(@table_name, @fields, @migration_version)
76
+ end
77
+ end
78
+ end
79
+ end
data/lib/trains.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'yaml'
2
+ require 'rubocop-ast'
3
+ require 'fast_ignore'
4
+ require 'zeitwerk'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'parallel'
7
+
8
+ loader = Zeitwerk::Loader.for_gem
9
+ loader.inflector.inflect('dto' => 'DTO')
10
+ loader.setup
11
+
12
+ module Trains
13
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trains
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Syed Faraaz Ahmad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parallel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.22'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.22'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-ast
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
69
+ description:
70
+ email:
71
+ - faraaz98@live.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".deepsource.toml"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-version"
80
+ - CHANGELOG.md
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - lib/trains.rb
87
+ - lib/trains/dto/app.rb
88
+ - lib/trains/dto/controller.rb
89
+ - lib/trains/dto/field.rb
90
+ - lib/trains/dto/method.rb
91
+ - lib/trains/dto/model.rb
92
+ - lib/trains/scanner.rb
93
+ - lib/trains/utils/ast_store.rb
94
+ - lib/trains/utils/logger.rb
95
+ - lib/trains/utils/rails_dir.rb
96
+ - lib/trains/utils/result.rb
97
+ - lib/trains/visitor/base.rb
98
+ - lib/trains/visitor/controller.rb
99
+ - lib/trains/visitor/helper.rb
100
+ - lib/trains/visitor/migration.rb
101
+ homepage: https://github.com/faraazahmad/trains
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ homepage_uri: https://github.com/faraazahmad/trains
106
+ source_code_uri: https://github.com/faraazahmad/trains
107
+ changelog_uri: https://github.com/faraazahmad/trains/blob/master/CHANGELOG.md
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.6.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.2.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Collect metadata about your Rails app by statically analyzing it
127
+ test_files: []